3

Easy and Quick way to Measure lines of Code in PHP | Tomas Votruba

 8 months ago
source link: https://tomasvotruba.com/blog/easy-and-quick-way-to-measure-lines-of-code-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Easy and Quick way to Measure lines of Code in PHP

2023-08-20

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 :

  • number of classes
  • number of global functions
  • number of public methods
  • number of static methods
  • interface and trait count etc.

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:

  • there must be a JSON output format, for easy piping to next tool
  • it must be easy to install on PHP 7.2+
  • it must use standard dependencies like symfony/console, to make contributions effortless
  • it must have scoped /vendor, so anyone can install it on any project with composer require
  • it should have a short and pretty output so that we can use it in posts

A few days later, the prototype package was born:

Using lines the first time for writing a post with real data:

What is a quick size of the vendor?

* Too long? Make it short 😉

* Too verbose? Make it json 😉 pic.twitter.com/FRsqsNXUJE

— Tomas Votruba (@VotrubaT) August 2, 2023

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 ↓

Just did a PR to add 🍃 Termwind styling to the lines package made by @VotrubaT

Let me know what do you think! 👊https://t.co/fFRVYbpzj5 pic.twitter.com/Og9LrDUHSP

— Francisco Madeira (@xiCO2k) August 8, 2023

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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK