Monitoring PHP Applications with PHPMetrics

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is PHPMetrics?

PHPMetrics is a powerful library for static analysis and monitoring of PHP applications. It allows you to collect code metrics such as complexity, test coverage, number of lines, dependencies, and potential issues. The library integrates with CI/CD pipelines and helps developers maintain high code quality.

Why monitor PHP applications?

Monitoring PHP applications is not only about tracking runtime performance but also about controlling code quality. PHPMetrics helps identify code smells, excessive complexity, and violations of PSR standards. This is especially important for large projects where maintaining clean code is critical for scalability and reducing technical debt.

Installation

PHPMetrics is installed via Composer:

composer require --dev phpmetrics/phpmetrics

After installation, you can run the analysis from the command line:

vendor/bin/phpmetrics --report-html=./report /path/to/your/project

This will create an HTML report with metric visualizations.

Main features

  • Static analysis: assessment of cyclomatic complexity, inheritance depth, class cohesion.
  • Report generation: HTML, JSON, XML — for integration with Jenkins, GitLab CI, or Grafana.
  • Coverage monitoring: analysis of test code coverage (requires PHPUnit).
  • Dependency visualization: class and package graphs.
  • CI/CD integration: ability to fail the build when complexity thresholds are exceeded.

PHP code example

Below is an example of using PHPMetrics in a script for programmatic analysis:

<?phprequire_once 'vendor/autoload.php';

use PHPMetrics\\Metrics\\Project;use PHPMetrics\\Report\\HtmlReport;use PHPMetrics\\Report\\JsonReport;

// Create a project object$project = new Project('/path/to/your/php/code');$project->run(); // Run the analysis

// Get metrics$metrics = $project->getMetrics();echo 'Average complexity: ' . $metrics->getAverageComplexity() . PHP_EOL;echo 'Number of classes: ' . $metrics->getNumberOfClasses() . PHP_EOL;

// Generate a JSON report$jsonReport = new JsonReport($project);file_put_contents('metrics.json', $jsonReport->generate());

// Or HTML$htmlReport = new HtmlReport($project);$htmlReport->generateToFile('report.html');

This script can be run in cron or as part of a deployment.

When to use

PHPMetrics is ideal for:

  • Regular code audits within CI/CD (e.g., before every merge to main).
  • Assessing technical debt during refactoring.
  • Monitoring code quality in long-term projects.
  • Team training — visual reports help identify which code areas need improvement.

Do not use PHPMetrics for real-time performance monitoring (use Xdebug or Blackfire for that). It is a static analysis tool, not a runtime profiler.

Integrating PHPMetrics into the development process reduces the number of bugs and simplifies code maintenance, especially in distributed teams.

Recommendations