// Maintain and improve code quality using PHPInsights without decreasing quality thresholds. Use when PHPInsights fails, cyclomatic complexity is too high, code quality drops, or when refactoring for better maintainability. Always maintains 93% complexity for src/ and 95% for tests/, plus 100% quality/architecture/style scores.
| name | complexity-management |
| description | Maintain and improve code quality using PHPInsights without decreasing quality thresholds. Use when PHPInsights fails, cyclomatic complexity is too high, code quality drops, or when refactoring for better maintainability. Always maintains 93% complexity for src/ and 95% for tests/, plus 100% quality/architecture/style scores. |
Maintain exceptional code quality standards using PHPInsights in this Symfony-based microservice. This skill ensures complexity stays manageable while preserving hexagonal architecture, DDD patterns, and CQRS design.
Use this skill when:
make phpinsights returns errors)New to complexity refactoring? Start with the Quick Start Guide for a fast-track workflow with immediate action steps.
CRITICAL: These thresholds are defined in phpinsights.php (for src/) and phpinsights-tests.php (for tests/) and must NEVER be lowered:
// phpinsights.php (source code in src/)
'requirements' => [
'min-quality' => 100, // Code quality
'min-complexity' => 93, // Cyclomatic complexity for src/
'min-architecture' => 100, // Architecture compliance
'min-style' => 100, // Coding style
],
// phpinsights-tests.php (test code in tests/)
'requirements' => [
'min-quality' => 95, // Code quality for tests
'min-complexity' => 95, // Cyclomatic complexity for tests/
'min-architecture' => 90, // Architecture compliance for tests
'min-style' => 95, // Coding style for tests
],
Policy: If PHPInsights fails, fix the code - NEVER lower these thresholds.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ When PHPInsights fails, you MUST FIX THE CODE. โ
โ NEVER lower thresholds in phpinsights.php. โ
โ โ
โ โ FORBIDDEN: Changing config to pass checks โ
โ โ
REQUIRED: Refactoring code to meet standards โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Protected thresholds are non-negotiable:
For source code (src/):
For tests (tests/):
If you're tempted to lower a threshold, it means the code needs refactoring, not the config.
First, find which classes need refactoring:
# Find top 20 most complex classes (default)
make analyze-complexity
# Find top 10 classes
make analyze-complexity N=10
# Export as JSON for tracking
make analyze-complexity-json N=20 > complexity-report.json
# Export as CSV for spreadsheets
make analyze-complexity-csv N=15 > complexity.csv
Output shows for each class:
See: analysis-tools.md for complete tools documentation
# Using project make command (recommended)
make phpinsights
# Direct execution with verbosity
vendor/bin/phpinsights -v
# Analyze specific directory
vendor/bin/phpinsights analyse src/Customer
# Generate JSON report for parsing
vendor/bin/phpinsights --format=json > insights-report.json
PHPInsights provides four scores:
[CODE] 100.0 pts โ
Target: 100%
[COMPLEXITY] 93.5 pts โ
Target: 93%+ (src), 95%+ (tests)
[ARCHITECTURE] 100 pts โ
Target: 100%
[STYLE] 100.0 pts โ
Target: 100%
If any score is below threshold, identify issues:
# Look for issues by severity
โ [Complexity] Method `CustomerCommandHandler::handle` has cyclomatic complexity of 12
โ [Architecture] Class `CustomerRepository` violates layer dependency rules
โ [Style] Line exceeds 100 characters in CustomerTransformer.php:45
Priority Order:
Based on issue type:
For Complexity Issues โ See refactoring-strategies.md For Metric Details โ See reference/complexity-metrics.md For Common Errors โ See reference/troubleshooting.md
# Re-run PHPInsights
make phpinsights
# Verify all thresholds pass
โ
Code quality: 100%
โ
Complexity: 93%+ (src), 95%+ (tests)
โ
Architecture: 100%
โ
Style: 100%
Problem: Method has too many decision points (if/else/switch/loops)
First, identify which classes need refactoring:
# Find top 10 most complex classes
make analyze-complexity N=10
Solutions:
See refactoring-strategies.md for DDD/CQRS-specific patterns.
Problem: Layer dependencies violated (e.g., Domain depending on Infrastructure)
Solutions:
Project Architecture:
src/{BoundedContext}/
โโโ Domain/ # No external dependencies
โ โโโ Entity/
โ โโโ ValueObject/
โ โโโ Repository/ # Interfaces only
โโโ Application/ # Depends on Domain
โ โโโ Command/
โ โโโ CommandHandler/
โ โโโ Event/
โโโ Infrastructure/ # Depends on Domain + Application
โโโ Repository/ # Implementations
Problem: Code doesn't meet PSR-12 or Symfony coding standards
Solutions:
# Auto-fix most style issues
make phpcsfixer
# Re-run PHPInsights to verify
make phpinsights
Problem: Lines exceed configured limit (100 chars)
Config:
LineLengthSniff::class => [
'lineLimit' => 100,
'ignoreComments' => true,
],
Solutions:
These sniffs are intentionally disabled in phpinsights.php:
'remove' => [
UnusedParameterSniff::class, // Allow unused params in interfaces
SuperfluousInterfaceNamingSniff::class, // Allow "Interface" suffix
SuperfluousExceptionNamingSniff::class, // Allow "Exception" suffix
SpaceAfterNotSniff::class, // Symfony style preference
ForbiddenSetterSniff::class, // Allow setters where needed
UseSpacingSniff::class, // Symfony style preference
],
Do NOT create issues for these patterns - they're explicitly allowed.
Some files are excluded from specific checks:
ForbiddenNormalClasses::class => [
'exclude' => [
'src/Shared/Infrastructure/Bus/Command/InMemorySymfonyCommandBus',
'src/Shared/Infrastructure/Bus/Event/InMemorySymfonyEventBus',
'src/Core/Customer/Domain/Entity/Customer',
],
],
These are intentionally not marked final - architectural decision.
Command handlers should have low complexity:
// โ
GOOD: Low complexity (2-3)
final readonly class CreateCustomerCommandHandler implements CommandHandlerInterface
{
public function __construct(
private CustomerRepository $repository,
private DomainEventPublisher $publisher
) {}
public function __invoke(CreateCustomerCommand $command): void
{
$customer = Customer::create(
$command->id,
$command->name,
$command->email
);
$this->repository->save($customer);
$this->publisher->publish(...$customer->pullDomainEvents());
}
}
If complexity is high: Business logic likely in wrong layer - move to Domain.
Domain entities can have higher complexity for business rules:
// โ
Acceptable: Complexity in domain logic
class Customer extends AggregateRoot
{
public function updateSubscription(SubscriptionPlan $plan): void
{
// Complex validation logic is appropriate here
$this->validateSubscriptionChange($plan);
$this->applyDiscount($plan);
$this->record(new SubscriptionChanged($this->id, $plan));
}
}
If too complex: Extract to Domain Service or Value Object.
After running this skill, verify:
โ
make phpinsights passes without errors
โ
Code quality: 100%
โ
Complexity: โฅ 93% (src), โฅ 95% (tests)
โ
Architecture: 100%
โ
Style: 100%
โ
No layer boundary violations (checked by Deptrac)
โ
All tests still pass (make unit-tests, make integration-tests)
โ
Code remains aligned with hexagonal architecture