Easy and Quick way to Measure lines of Code in PHP

The famous phploc package to measure project size was archived by Sebastian on Jan 10, 2023. I used this package to get feedback on CLI apps vendor shrink and for fast estimation of project size in Rector upgrades.

That's why I needed a replacement. Fast!

There are a few forks kind of working, but they don't provide enum support, rely on PHP tokens, and conflict with installation.

You might also suggest generic Linux tools like cloc, but it requires specific operation system, thus different installation etc. Also it doesn't provide PHP-specific metrics that will give you better idea about project code quality :


I told myself, "Maybe we can use more reliable tooling to handle this, like php-parser," and I didn't stop there. I shared my idea on Twitter, and the feedback gave me the energy to think more deeply about this.

What do we Need?

When we look at "why" such a package is used and what people need from it, we come to a few key points:


A few days later, the prototype package was born:


3. Steps to Measure Lines of your PHP project

  1. Install the lines package
composer require tomasvotruba/lines --dev

  1. Run bin with paths to measure
vendor/bin/lines measure src

  1. Adjust the output to fit your needs
vendor/bin/lines measure src --json --short

To get ↓

{
    "filesystem": {
        "directories": 174,
        "files": 753
    },
    "lines_of_code": {
        "code": 42627,
        "code_relative": 65.4,
        "comments": 22545,
        "comments_relative": 34.6,
        "total": 65172
    }
}

This command is perfect for blog posts, as it gives you a idea about the size without the clutter.


That's it!

Termwind on Board

Before I even managed to launch the package, Francisco jumped in and gave the CLI output a fresh and sexy look ↓



Give the lines a try, and if you want to improve the package, just go for it!


Happy coding!




Do you learn from my contents or use open-souce packages like Rector every day?
Consider supporting it on GitHub Sponsors. I'd really appreciate it!