PhpMetrics

See which code will hurt you next.

PhpMetrics draws your PHP project. Every class becomes a circle: bigger when it is complex, redder when it is hard to maintain. Run one command and the code that needs you is the first thing you see.

$ composer require --dev phpmetrics/phpmetrics
$ vendor/bin/phpmetrics --report-html=report src
  Open report/index.html. That's all.

Your whole project on a single page.

Size, complexity, coupling, the classes everything else depends on, and the rules your code already breaks. Hand it to a new hire, to your CTO, or to yourself after six months away.

The PhpMetrics HTML report: a summary of violations, lines of code and classes, and a chart where each class is a circle sized by complexity and coloured by maintainability.

Start with the big red circles.

A class that is both complex and hard to maintain is where bugs hide and estimates slip. The report shows you which ones, and tells you why: cyclomatic complexity, lack of cohesion, coupling, the probable number of bugs.

Large and red. Complex, hard to change. Look here first.
Amber. Worth watching.
Small and green. Leave it alone.
searches:
  Repository calling a service:
    type: class
    nameMatches: ".*Repository.*"
    usesClasses: [".*Service"]
    failIfFound: true

  Class too complex:
    type: class
    ccn: ">=10"
    failIfFound: true

Then make sure it stays fixed.

Write down the rules your architecture should follow. When a pull request breaks one, the build fails and says which class did it.

Violations are also written in the PMD format, which your CI already knows how to read.

Your code stays home.

No account, no upload, no server to trust. PhpMetrics reads your code on your machine and writes a folder of plain HTML that opens without a connection.

The one request it makes asks Packagist whether your Composer dependencies are up to date. A single flag turns it off.

# nothing leaves this machine
vendor/bin/phpmetrics \
  --composer=false \
  --report-html=report src

Free, since 2013.

PhpMetrics is open source under the MIT license and maintained on spare time. If it saves you some of yours, a star helps other developers find it, and sponsorship pays for the next release.