Skip to content

Code Quality

Arthu Santiago edited this page Nov 30, 2026 · 1 revision

Code Quality

Besides the tests, KeyAnyWhere measures its code with static analysis, mess detection, architecture rules and mutation testing, and combines them into a quality index from 0 to 100. CI fails a pull request that makes any of them worse. This page explains each tool, how to run it, and how the "ratchet" against the reference values works.

Before you start

A development environment with the development dependencies. Mutation testing also needs a coverage driver (PCOV) and the test database.

The checks

Command Tool What it checks
composer stan PHPStan, level 8 Types and likely bugs.
composer md PHPMD (phpmd.xml.dist) Size, complexity and coupling (single responsibility).
composer arquitetura Deptrac (deptrac.yaml) Layer rules; see Architecture.
composer analise the three above Only new problems fail; old debt lives in the baselines.
composer mutacao coverage + Infection Changes the code on purpose and checks that some test fails (about 2 minutes).
composer mutacao:alteracoes Infection Same, only on the lines changed since origin/main, listing the mutants that survived.
composer qualidade bin/qualidade --comparar The quality index and the ratchet (below).

Run the static checks:

composer analise

Run mutation testing only on what you changed:

composer mutacao:alteracoes

The quality index

bin/qualidade, configured by qualidade.json, scores six metrics and weighs them into an index from 0 to 100:

Metric Weight Measures
Coverage 20 Lines covered by tests.
Mutation (MSI) 20 Mutants killed by the tests.
Types 20 Files without PHPStan debt.
Complexity 15 Methods within cyclomatic complexity 10 and NPath 200 (PDepend).
Cohesion 10 PHPMD.
Architecture 15 Deptrac.
composer qualidade

The ratchet

The CI job Qualidade compares each metric with qualidade-referencia.json and fails when:

  • any metric drops more than 0.5 point below the reference; or
  • the debt grows: PHPStan baseline plus @phpstan-ignore, PHPMD baseline, Deptrac baseline, @SuppressWarnings.

Fix new problems instead of adding them to a baseline. When you fix old debt, regenerate the baseline and commit it with the fix:

composer stan:baseline
composer md:baseline
composer arquitetura:baseline

Important

When a metric improves, update qualidade-referencia.json with the values from the CI run (the job summary of your pull request), not from your computer. A few mutants depend on the local environment (config/app_local.php, config/.env): they are killed locally and escape in CI, so a reference made locally can make CI fail. composer qualidade:referencia writes the file from the current measurements.

Next step

Architecture

Clone this wiki locally