Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Countlinescode Bundle Laravel Package

c975l/countlinescode-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require-dev c975l/countlinescode-bundle
    

    Ensure this is added to require-dev in composer.json for CI/CD or local dev environments.

  2. Configuration: Create /config/packages/dev/count_lines_codes.yaml (or merge into existing dev config):

    c975LCountLinesCode:
        folders: ['src', 'templates']  # Start with core directories
        extensions: ['php', 'twig']    # Focus on primary languages
    
  3. First Run:

    php bin/console count:loc
    

    Outputs a summary of lines of code (LOC) per file type and directory.

First Use Case

  • Onboarding: Quickly assess project size for new developers.
  • CI/CD: Add to a dev pipeline to track LOC growth over time (e.g., via php bin/console count:loc --format=json).
  • Documentation: Generate a "Codebase Overview" section in your README.md with LOC stats.

Implementation Patterns

Workflows

  1. Custom Reports: Extend the command to output structured data (e.g., CSV/JSON) for dashboards:

    // In a custom command or service
    $locService = $container->get('c975l_count_lines_code.loc_service');
    $results = $locService->countLinesOfCode();
    file_put_contents('loc_report.json', json_encode($results));
    
  2. Excluding Directories: Use folders config to exclude vendor/, node_modules/, or tests/:

    folders: ['src', 'templates']
    # Explicitly omit tests/ to focus on production code
    
  3. Integration with Tests: Add a post-test hook in phpunit.xml to log LOC changes:

    <phpunit>
        <listeners>
            <listener class="App\Listener\LocChangeListener" file="tests/Listener/LocChangeListener.php"/>
        </listeners>
    </phpunit>
    
    // LocChangeListener.php
    public function postTestSuite(PostTestSuiteEvent $event) {
        $loc = shell_exec('php bin/console count:loc --format=json');
        file_put_contents('tests/loc_after_tests.json', $loc);
    }
    
  4. Symfony Flex Compatibility: If using Symfony 4.3+, ensure the bundle is loaded in config/bundles.php:

    return [
        // ...
        C975L\CountLinesCodeBundle\C975LCountLinesCodeBundle::class => ['dev' => true],
    ];
    

Integration Tips

  • Git Hooks: Add to pre-commit to alert on LOC spikes:
    # .git/hooks/pre-commit
    php bin/console count:loc --format=json | jq '.total' > /tmp/loc_total
    git diff --numstat | awk '{add+=$1} END {print add}' > /tmp/loc_diff
    if [ $(cat /tmp/loc_diff) -gt 500 ]; then
        echo "LOC change exceeds threshold!" >&2
        exit 1
    fi
    
  • Docker: Run in a dev container to avoid host filesystem issues:
    # Dockerfile.dev
    RUN composer require-dev c975l/countlinescode-bundle
    CMD ["php", "bin/console", "count:loc"]
    

Gotchas and Tips

Pitfalls

  1. Performance:

    • Avoid running on large monorepos (e.g., folders: ['.']). Stick to src/ and templates/.
    • Cache results in var/cache/dev/loc_cache.json to avoid repeated scans:
      # config/packages/dev/count_lines_codes.yaml
      c975LCountLinesCode:
          cache: true
      
  2. False Positives:

    • Twig: Exclude {% extends %}, {% block %}, and comments from counts by overriding the parser:
      extensions:
          twig: { exclude_patterns: ['{%.*%}', '{#.*#}'] }
      
    • PHP: Ignore generated files (e.g., var/cache/*) by adding to folders:
      folders: ['src', 'templates']
      exclude_folders: ['var/', 'node_modules/']
      
  3. Symfony 5+ Deprecations:

    • The bundle may not support PHP 8.1+ features. Test with:
      php bin/console debug:container | grep count_lines_code
      
  4. Command Output:

    • The default output is human-readable but not machine-parsable. Use --format=json for automation:
      php bin/console count:loc --format=json | jq '.files[] | {path, lines}'
      

Debugging

  • Verbose Mode:

    php bin/console count:loc -vvv
    

    Reveals skipped files and parsing issues.

  • Log Parsing Errors: Enable monolog in config/packages/dev/monolog.yaml to catch edge cases:

    handlers:
        count_lines:
            type: stream
            path: var/log/count_lines.log
            level: debug
    

Extension Points

  1. Custom File Types: Extend the FileType class to support new languages (e.g., YAML for Symfony config):

    // src/Service/CountLinesCode/CustomFileType.php
    namespace App\Service\CountLinesCode;
    
    use C975L\CountLinesCodeBundle\Service\CountLinesCode\FileType;
    
    class CustomFileType extends FileType
    {
        public function __construct()
        {
            $this->extension = 'yaml';
            $this->commentPattern = '#^#.*$#m';
        }
    }
    

    Register in services.yaml:

    services:
        App\Service\CountLinesCode\CustomFileType:
            tags: { name: c975l_count_lines_code.file_type }
    
  2. Pre/Post-Processing: Hook into the CountLinesCodeEvent (if available) to modify results:

    // src/EventListener/LocListener.php
    namespace App\EventListener;
    
    use C975L\CountLinesCodeBundle\Event\CountLinesCodeEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    class LocListener implements EventSubscriberInterface
    {
        public static function getSubscribedEvents()
        {
            return [
                CountLinesCodeEvent::NAME => 'onCountLinesCode',
            ];
        }
    
        public function onCountLinesCode(CountLinesCodeEvent $event)
        {
            $results = $event->getResults();
            $results['metadata']['processed_at'] = new \DateTime();
            $event->setResults($results);
        }
    }
    
  3. Parallel Processing: For large codebases, override the LocService to use parallel workers (e.g., ReactPHP):

    // src/Service/ParallelLocService.php
    use C975L\CountLinesCodeBundle\Service\LocService as BaseLocService;
    
    class ParallelLocService extends BaseLocService
    {
        protected function countLinesInFile(string $file): int
        {
            // Implement parallel logic here
        }
    }
    

    Bind in services.yaml:

    services:
        C975L\CountLinesCodeBundle\Service\LocService:
            alias: App\Service\ParallelLocService
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope