Configuration
custos reads custos.json from the project root. Every key is optional.
{
"php": "8.3",
"comparisonStyle": "yoda",
"shortOpenTag": false,
"flow": { "assertions": false },
"paths": ["src", "tests"],
"exclude": ["generated", "public/build"],
"baseline": "custos-baseline.json",
"rules": {
"StaticClosureCanBeUsed": { "enabled": false },
"OneTimeUseVariables": {
"severity": "info",
"options": { "ALLOW_LONG_STATEMENTS": true }
},
"ForgottenDebugOutput": {
"options": { "configuration": ["\\App\\Debug::dump"] }
}
}
}| Key | Meaning |
|---|---|
php | Target PHP version: 5.3–5.6, 7.0–7.4, or 8.0–8.5. See Target PHP version. |
comparisonStyle | "regular" ($x === null) or "yoda" (null === $x): the operand order that comparison rules expect and that quick-fixes write. Without it, the project's PHP-CS-Fixer or Mago configuration decides (see Comparison style); otherwise "regular". |
typeCheck | Choose which type-checking rules run together: "off", "proven" (default: mismatches the code alone proves) or "documented" (also mismatches against PHPDoc, PHPStan, Psalm and PhpStorm declarations). See Type checking. |
shortOpenTag | Treat <? as an opening tag, as with PHP's short_open_tag setting. Default false. |
flow.assertions | Trust assert() calls as guards. Default false. See Assertions. |
parseOnce | Keep each analyzed file's syntax tree between phases, as --parse-once does: faster, more memory. custos fix and the language server ignore it. Default false. |
flow.order | Summarize cross-file flow in the order the last run saved, as --flow-order does; the language server honors it too. Default false. |
paths | What custos analyse and custos fix check when no path is given. Relative to the root; must stay inside it. Default: the root. |
exclude | Extra directories to skip. See Excluded directories. |
baseline | Baseline file used by analyse, relative to the root (see Baseline). |
rules | Per-rule settings, keyed by rule ID or PhpStorm name. |
customRules | The project's own pattern rules. See Custom rules. |
customRuleFiles | JSON files, relative to the root, each holding a list of custom rules. |
format | How custos format lays out code. See Format. |
customRuleTests | Directory of the rules' fixtures for custos test-rules, relative to the root. Default custos-tests, which analyse and fix skip; add a directory you name to exclude. |
Each rules entry accepts:
enabled:trueorfalse. It overrides the rule's default; the rule reference shows which rules are on by default.severity:"error","warning"or"info", replacing the rule's default severity.options: the rule's options. Each rule page lists them with their defaults, as doescustos explain <rule>.
Project root
The project root is the first directory, going up from the start directory, that holds custos.json or composer.json; if none does, it is the start directory itself. The start directory is the --config directory if given, else the first path on the command line (its directory, for a file), else the current directory.
The PHP version, the vendor/ declarations and the PHPUnit version are read from that root. When composer.json sits in a subdirectory (src/), run custos from there, or pass that directory as --config. Otherwise custos uses the default PHP version, does not see the types declared in vendor/, and offers no quick-fix for the PHPUnit assertions renamed in 9.1.
Validation
custos stops with a configuration error (exit code 2) when custos.json is not valid JSON, or when it has:
- an unknown rule ID;
- a
severityother thanerror,warningorinfo; - a
comparisonStyleother thanregularoryoda; - a
typeCheckother thanoff,provenordocumented; - an unsupported
phpversion; - a
pathsentry orbaselinethat is absolute or leaves the root; - in
format: an unknown preset or setting, a value a setting does not accept, or ariskyentry that is not a risky setting.
Unknown top-level keys and unknown rule options are ignored.
Format
The format section configures custos format, the language server's formatting and the Formatting rule:
{
"format": {
"preset": "per",
"settings": {
"line_length": 100,
"blank_line_before": ["return", "throw"]
},
"risky": ["remove_unused_imports"],
"exclude": ["resources/views/**", "src/Generated"]
}
}| Key | Meaning |
|---|---|
preset | auto (default), psr1, psr12, per, symfony, laravel or drupal. auto takes the style of the project's Laravel Pint, PHP-CS-Fixer or Mago configuration, else per; see Presets. |
settings | Setting IDs and values, over the preset and .editorconfig. Format settings lists them. |
risky | Risky settings to turn on: true for an on/off setting, else the preset's risky value. See Risky settings. |
allowRisky | true applies the preset's own risky values. Default false. |
exclude | Files not to format: globs relative to the root, in EditorConfig syntax (*, **, {a,b}), matching a file or one of its directories. analyse and fix still check them. |
editorconfig | false ignores .editorconfig files. Default true. |
Values resolve in this order, later ones winning: the preset, the other tool's configuration (with auto only), .editorconfig, this section, and --set on the command line; custos format --show-config shows the source of each value. See How settings resolve.
Type checking
typeCheck enables the type-checking rules as a group; a rule's own enabled setting still wins. Type checking lists the levels, their rules and the declaration formats custos reads.
Excluded directories
Directories named vendor and node_modules, hidden directories (whose name starts with a dot, such as .git, .idea, .custos or .phpunit.cache), and var/cache directories (a nested application's too), are always skipped; a scanned path may still be a hidden directory itself. exclude (or --exclude a,b on the command line) adds more. Each entry is a directory name or a path of several names, skipped at any depth below each scanned path (not the root): with "paths": ["src"], write "gen" or "sub/gen" to skip src/sub/gen (and src/app/sub/gen), not "src/sub/gen".
Declaration files that never run are not analyzed either: php-src's extension stubs (*.stub.php) and Drupal's hook documentation (*.api.php). Their classes and functions are still known to the other files, and a file named on the command line is analyzed.
Target PHP version
The first of these that is set decides:
--phpon the command line;phpincustos.json;config.platform.phpincomposer.json;- the lowest version that
require.phpincomposer.jsonallows (a bare major such as5.*or^7stands for its lowest version, 5.3 or 7.0); - the version pinned by
.php-versionat the project root (as read by phpenv or the Symfony CLI): its first line, such as8.2or8.2.12; - PHP 8.4.
custos analyse --stats shows which version was used and where it came from.
custos supports PHP 5.3 to 8.5. The target is the oldest version the code must run on: rules only suggest what that version has, and quick-fixes and custos format never make a file need a newer version. A fix that would (such as ?? in a PHP 5.6 project) is not applied, and the formatter leaves the file as written; a file that already uses newer syntax is not held back by its own syntax. The UnavailableFunction rule reports calls to functions PHP only has from a later version, such as str_contains() in a PHP 5.6 project, or removed before the target, such as each() in a PHP 8 project, unless a polyfill declares them or the call is guarded.
Comparison style
When custos.json sets no comparisonStyle (and --comparison-style is not given), custos reads .php-cs-fixer.php, or else .php-cs-fixer.dist.php, at the project root:
- a
yoda_stylerule decides:false, or a list setting bothequalandidenticaltofalse, meansregular; any other value meansyoda; - without one, enabling the
@Symfonyor@PhpCsFixerrule set (or their:riskyvariants), which includeyoda_style, meansyoda; - without a PHP-CS-Fixer file, or when it says nothing about the order, custos reads
mago.toml, or elsemago.dist.toml: when Mago'syoda-conditionslint rule is enabled ([linter.rules]entryyoda-conditions = { enabled = true, mode = "require" }, or a[linter.rules.yoda-conditions]table), modedenymeansregularandrequire, its default, meansyoda; - otherwise the style is
regular.
PHPUnit version
The PHPUnit rules adapt their advice to the PHPUnit version the project uses: the installed phpunit/phpunit when the project's vendor is available, else the lowest version composer.json allows (require-dev, then require), else the version of the schema that phpunit.xml or phpunit.xml.dist names. See the PHP_UNIT_VERSION option of PhpUnitTests and PhpUnitDeprecations.
Assertions
By default, custos does not treat assert() as a guard, because PHP can be configured to skip assertions. Set flow.assertions to true only when your runtime always runs assertions and a failed one stops execution.
The assertion methods of PHPUnit ($this->assertNotFalse($handle), self::assertIsResource($stream)) and of webmozart/assert always run and throw when they fail, so they are guards whatever this setting: after them, rules that look for unchecked failures know the value is not the failure value. Other functions and methods documented with @phpstan-assert or @psalm-assert refine inferred types, but those rules do not take their documentation as proof.
Cache
analyse and fix keep, between runs, the declarations of the files they index without checking them and the cross-file summaries of the checked files, and analyse the findings of each checked file, under custos in the user cache directory or in $CUSTOS_CACHE_DIR. A change to the configuration that changes what a run checks or how (php, shortOpenTag, flow.assertions, paths, exclude) sets the affected parts aside; findings are the same with or without the cache. See Paths for what invalidates it, and --no-cache to bypass it.
In the language server
custos lsp reads the same custos.json, found from the first workspace folder. Editors can override part of it with LSP initializationOptions; see Editors for what they can change.
