

Easy and Quick way to Measure lines of Code in PHP | Tomas Votruba
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 withcomposer 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:
— Tomas Votruba (@VotrubaT) August 2, 2023
What is a quick size of the vendor?
* Too long? Make it short 😉
* Too verbose? Make it json 😉 pic.twitter.com/FRsqsNXUJE
3. Steps to Measure Lines of your PHP project
- Install the lines package
composer require tomasvotruba/lines --dev
- Run bin with paths to measure
vendor/bin/lines measure src
- 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
— Francisco Madeira (@xiCO2k) August 8, 2023
Let me know what do you think! 👊https://t.co/fFRVYbpzj5 pic.twitter.com/Og9LrDUHSP
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!
Recommend
-
95
Rector is a PHP tool that handles 2 things: instant upgrades and architecture refactorings.What exactly Rector does and how does it work?
-
45
The standard is still behind the door, but feedback, before it gets accepted, is very important. After accepting it will be written down and it will be difficult to change anything.Try PSR-12 today and see, how it works for your code.
-
67
What's New in PHP 7.3 in 30 Seconds in Diffs 2 min Thu, Aug 16, 2018...
-
61
You already have a monorepo, you have at least 2 packages, autoloaded with composer and splitting works.Now you're about to set up testing and code quality tools.How to make testing so tight no bug can escape?
-
37
PHP Framework Trends Updated at 1. 12. What are yearly downloads of popular PHP frameworks from Packagist? Is it marketing fake news or is it true?
-
3
Personal Insights
-
11
-
11
-
9
-
7
How to Remove Transitional Dependencies You don't Need 2023-08-16 In the last post
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK