tomasvotruba/cognitive-complexity
Installation Add the package via Composer:
composer require --dev tomasvotruba/cognitive-complexity
Enable the rules in your phpstan.neon:
includes:
- vendor/tomasvotruba/cognitive-complexity/phpstan-rules.neon
First Run Execute PHPStan with the new rules:
vendor/bin/phpstan analyse src
Look for errors like:
CognitiveComplexity\TooHighCognitiveComplexityMethod
First Use Case Identify a method with high cognitive complexity (e.g., > 20) and refactor it:
// Before (complex)
public function calculate($input): float {
if ($input > 10) {
if ($input % 2 === 0) {
return $input * 0.1;
} else {
return $input * 0.2;
}
} else {
return $input * 0.05;
}
}
// After (simplified)
public function calculate($input): float {
$factor = $this->getFactor($input);
return $input * $factor;
}
private function getFactor(float $input): float {
if ($input <= 10) return 0.05;
return $input % 2 === 0 ? 0.1 : 0.2;
}
Integrate with CI/CD Add PHPStan to your pipeline (e.g., GitHub Actions):
- name: PHPStan
run: vendor/bin/phpstan analyse --level=5 src
Fail builds if complexity exceeds thresholds.
Custom Thresholds
Override defaults in phpstan.neon:
parameters:
cognitiveComplexity:
maxMethodComplexity: 15
maxClassComplexity: 50
Focused Analysis Run on specific files/directories:
vendor/bin/phpstan analyse src/Service/
Pair with Other Tools
Combine with phpstan/extension-installer for seamless updates:
composer require --dev phpstan/extension-installer
excludeFiles:
- vendor/**
- bootstrap/**
@complexity PHPDoc tags to justify high-complexity methods:
/**
* @complexity 25 (legacy, but well-tested)
*/
public function legacyMethod() { ... }
False Positives
phpstan.neon:
excludeFiles:
- app/Models/*.php
Performance
vendor/bin/phpstan analyse --parallel
Configuration Overrides
phpstan.local.neon) can conflict with team settings.
Fix: Document override rules in CONTRIBUTING.md.--error-format=json to analyze raw complexity data:
vendor/bin/phpstan analyse --error-format=json > complexity.json
Custom Rules Extend the package by creating your own PHPStan rules:
use PHPStan\Rules\Rule;
use CognitiveComplexity\Rules\ComplexityRule;
class CustomComplexityRule extends ComplexityRule {
protected function getMaxAllowedComplexity(): int { return 10; }
}
Register in phpstan.neon:
services:
- CognitiveComplexity\Rules\CustomComplexityRule
Integration with Linters
Sync with ESLint (via laravel-shift/laravel-phpstan) for frontend-backend parity.
Dynamic Thresholds Use environment variables for dynamic thresholds:
parameters:
cognitiveComplexity:
maxMethodComplexity: %env.int(MAX_METHOD_COMPLEXITY, 20)%
How can I help you explore Laravel packages today?