sebastian/lines-of-code
Count lines of code in PHP source with sebastian/lines-of-code. A lightweight library for analyzing PHP files and reporting LOC metrics, useful for tooling, CI checks, and development workflows. Install easily via Composer as a runtime or dev dependency.
nikic/php-parser may introduce conflicts with other parser-based tools (e.g., PHPStan, PHP-CS-Fixer) if not version-pinned.--dev recommended for non-production use).$counter = new \SebastianBergmann\LinesOfCode\Counter();
$result = $counter->count(base_path('src'));
booted, registered), forcing integration via Artisan commands, observers, or CI scripts.nikic/php-parser) must be version-aligned to avoid conflicts with existing tooling.| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| PHP version mismatch | High | Isolate usage in CI/Docker with PHP 8.4+. |
| Parser conflicts | Medium | Pin nikic/php-parser to a compatible version. |
| Performance overhead | Low | Cache results (e.g., Redis) for repeated scans. |
| False positives/negatives | Low | Validate against manual counts for edge cases. |
| No persistence layer | Medium | Implement custom storage (e.g., database, file). |
Execution Environment:
Data Usage:
Edge Cases:
Scaling:
Tooling Conflicts:
nikic/php-parser?Installation:
composer require --dev sebastian/lines-of-code nikic/php-parser:^5.0
--dev to avoid production bloat.nikic/php-parser to avoid version conflicts.Integration Options:
| Approach | Implementation Example | Use Case |
|---|---|---|
| CI Script | GitHub Actions workflow calling a PHP script. | Enforce LOC thresholds. |
| Artisan Command | php artisan loc:count --dir=src |
Local dev inspection. |
| PHPUnit Test | assertLessThan(1000, $result->linesOfCode) |
Guard against code bloat. |
| Observer/Event | Listen to eloquent.creating to log LOC. |
Track model complexity. |
| CI Job | Dedicated PHP 8.4+ container for scans. | Isolated execution. |
Example Artisan Command:
// app/Console/Commands/CountLoc.php
namespace App\Console\Commands;
use SebastianBergmann\LinesOfCode\Counter;
class CountLoc extends Command {
protected $signature = 'loc:count {--dir=src : Directory to scan}';
public function handle() {
$counter = new Counter();
$result = $counter->count(base_path($this->option('dir')));
$this->line("LOC: {$result->linesOfCode}");
$this->line("Comments: {$result->commentLinesOfCode}");
$this->line("Non-comment: {$result->nonCommentLinesOfCode}");
}
}
Example CI Workflow (GitHub Actions):
# .github/workflows/loc-check.yml
name: LOC Check
on: [push, pull_request]
jobs:
loc:
runs-on: ubuntu-latest
container: php:8.4-cli
steps:
- uses: actions/checkout@v4
- run: composer require sebastian/lines-of-code nikic/php-parser:^5.0
- run: php -r "
$counter = new \SebastianBergmann\LinesOfCode\Counter();
$result = $counter->count(__DIR__.'/src');
if ($result->linesOfCode > 500) exit(1);
"
nikic/php-parser v5.0+ aligns with other tools (e.g., PHPStan).Phase 1: Proof of Concept
src/).Phase 2: Integration
Phase 3: Scaling
src/App, src/Modules).nikic/php-parser may require updates; pin versions to avoid conflicts.nikic/php-parser troubleshooting or version adjustments.CREATE TABLE loc_metrics (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
directory
How can I help you explore Laravel packages today?