sebastian/lines-of-code
sebastian/lines-of-code is a lightweight PHP library for counting lines of code in PHP source files. Useful for reporting, metrics, and tooling, it can be installed via Composer as a runtime or dev dependency.
Install the package via Composer as a dev dependency: composer require --dev sebastian/lines-of-code. It requires PHP 8.4+ and nikic/php-parser ^5.0. Use it in a script or test to analyze PHP source code by instantiating SebastianBergmann\LinesOfCode\Counter and calling count() on a directory or file path. The result is an object with properties like linesOfCode, commentLinesOfCode, and nonCommentLinesOfCode. Note: In version 5.0.1, a bug was fixed where lines containing multiple comments were incorrectly double-counted, which could previously result in negative values for $nonCommentLinesOfCode.
Use it in CI pipelines (e.g., GitHub Actions) to enforce LOC thresholds by writing a simple PHP script that calls Counter::count() on your src/ and tests/ directories and asserts against configured limits. In unit tests, wrap the counter to validate that new code doesn’t exceed project-specific complexity budgets. Combine with nikic/php-parser AST analysis for deeper insights (e.g., counting only class/method definitions). For integration, pair with tools like PHP_CodeSniffer or PHPMD to correlate LOC with style/maintainability rules.
The package counts logical lines of code (excluding blank lines and comments), not raw file lines—this may differ from what IDEs report. Version 5.0.1 fixes a critical bug where lines with multiple comments were double-counted, potentially causing negative $nonCommentLinesOfCode values. Be aware it still excludes declare() statements and other compiler directives only when they appear on their own line; mixed usage may cause minor variations. Always specify paths relative to your project root; absolute paths may behave unexpectedly in CLI vs CI environments. The library is immutable—each call creates a new Result object, so avoid repeated counting of the same directory in loops without caching. Since it depends on nikic/php-parser, ensure parser version compatibility if you’re also using php-parser directly in your tooling. Test thoroughly after upgrading to 5.0.1, especially if your codebase contains files with mixed comments or edge-case formatting.
How can I help you explore Laravel packages today?