centaur-vova / ird-meter
If/Row Density (IRD) calculator for PHP projects — how linear is your code?
Requires
- php: >=8.1
- nikic/php-parser: ^5.0
Requires (Dev)
- phpunit/phpunit: ^10.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
How linear is your code? Number of
ifstatements per 100 lines of code.
What is IRD?
IRD = number of if statements per 100 lines of code.
It shows how linear your code is. Fewer ifs — more predictable flow.
| IRD | Meaning |
|---|---|
| < 2% | 🟢 clean |
| 2–5% | 🟡 good |
| 5–10% | 🟠 needs attention |
| > 10% | 🔴 for ponies |
Install
composer require --dev centaur-vova/ird-meter
Usage
vendor/bin/ird-meter app
Example
📊 PHP IRD (If/Row Density)
If count: 111
Total lines: 5807
Files: 96
IRD: 1.91%
🟢 clean — this code is linear like a horse's path
With --ignore-comments:
📊 PHP IRD (If/Row Density)
If count: 111
Total lines: 3826
Files: 96
IRD: 2.90%
🟡 good — some branches, but manageable
The difference: 1981 lines (34%) are comments and PHPDoc.
Self-check (Dogfooding)
$ ird-meter src
📊 PHP IRD (If/Row Density)
If count: 12
Total lines: 216
Files: 2
IRD: 5.56%
🟠 needs attention — too many ifs
Yes, our own parser has many ifs. It's a parser — it does nothing but check conditions. Irony appreciated.
See TODO.md — we're working on reducing it. PRs welcome.
Flags
| Flag | Description |
|---|---|
--raw |
Output only the IRD number |
--json |
Output JSON |
--threshold=N |
Exit 1 if IRD > N (for CI) |
--exclude=A,B |
Comma-separated directories to exclude (default: vendor,.git,node_modules,var/cache) |
--ignore-comments |
Count only non-comment, non-empty lines |
--include-elseif |
Count elseif branches too |
--include-ternary |
Count ternary operators |
--include-match |
Count match expressions |
--all |
Enable all optional counters |
--help, -h |
Show this help |
CI Integration
- name: Check IRD run: vendor/bin/ird-meter app --threshold=5
If IRD exceeds threshold — exit code 1, CI fails.
Why IRD?
Because if is the source of all evil:
- Each
ifis a branch - Each branch is a potential bug
- Fewer branches — fewer bugs
Based on nikic/php-parser — accurate AST analysis, no regex, no false positives from comments or strings.
Requirements
- PHP 8.1+
nikic/php-parser5.x
License
MIT. Or KBL v4.0 — horses don't abandon horses, and ponies 🦄