sebastian/complexity
Compute code complexity metrics for PHP units (functions, methods, classes). sebastian/complexity provides programmatic analysis useful for static analysis tools, quality gates, and CI reporting; install via Composer for project or dev-only use.
Install sebastian/complexity as a dev dependency via Composer (composer require --dev sebastian/complexity). It requires PHP ≥8.4 and nikic/php-parser ^5.0. Begin by analyzing a single class or method using its core calculator classes—e.g., Complexity\Calculator and Complexity\AstVisitor. The simplest first use case is to run complexity calculations over unit tests to identify overly complex methods that may need refactoring. Check the examples/ directory in the repository for minimal working examples.
Calculator::calculateForFunctionLike() to analyze methods/functions.Complexity\AstVisitor to collect additional context (e.g., per-file, per-class complexity averages) and emit JSON or TSV for dashboarding.php complexity:check) that walks your app’s app/ directory, calculates complexity for controllers, services, and listeners, and outputs violations.^5.0 if still on PHP 8.3.if, for, &&, ?:, catch)—not logical complexity like internal state dependencies. Combine with static analysis (PHPStan) for broader insights.calculateForFunctionLike() (for methods/functions) or calculateForClass() (for aggregating method complexities). Avoid computing class-level cyclomatic complexity—it’s rarely meaningful.php-parser is fast, but scanning large codebases recursively in CI may benefit from parallelization or caching (e.g., store results per-file in storage/).How can I help you explore Laravel packages today?