Skip to content

Configuration ​

custos reads custos.json from the project root. Every key is optional.

json
{
  "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"] }
    }
  }
}
KeyMeaning
phpTarget 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".
typeCheckChoose 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.
shortOpenTagTreat <? as an opening tag, as with PHP's short_open_tag setting. Default false.
flow.assertionsTrust assert() calls as guards. Default false. See Assertions.
parseOnceKeep 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.orderSummarize cross-file flow in the order the last run saved, as --flow-order does; the language server honors it too. Default false.
pathsWhat custos analyse and custos fix check when no path is given. Relative to the root; must stay inside it. Default: the root.
excludeExtra directories to skip. See Excluded directories.
baselineBaseline file used by analyse, relative to the root (see Baseline).
rulesPer-rule settings, keyed by rule ID or PhpStorm name.
customRulesThe project's own pattern rules. See Custom rules.
customRuleFilesJSON files, relative to the root, each holding a list of custom rules.
formatHow custos format lays out code. See Format.
customRuleTestsDirectory 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: true or false. 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 does custos 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 severity other than error, warning or info;
  • a comparisonStyle other than regular or yoda;
  • a typeCheck other than off, proven or documented;
  • an unsupported php version;
  • a paths entry or baseline that is absolute or leaves the root;
  • in format: an unknown preset or setting, a value a setting does not accept, or a risky entry 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:

json
{
  "format": {
    "preset": "per",
    "settings": {
      "line_length": 100,
      "blank_line_before": ["return", "throw"]
    },
    "risky": ["remove_unused_imports"],
    "exclude": ["resources/views/**", "src/Generated"]
  }
}
KeyMeaning
presetauto (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.
settingsSetting IDs and values, over the preset and .editorconfig. Format settings lists them.
riskyRisky settings to turn on: true for an on/off setting, else the preset's risky value. See Risky settings.
allowRiskytrue applies the preset's own risky values. Default false.
excludeFiles 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.
editorconfigfalse 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:

  1. --php on the command line;
  2. php in custos.json;
  3. config.platform.php in composer.json;
  4. the lowest version that require.php in composer.json allows (a bare major such as 5.* or ^7 stands for its lowest version, 5.3 or 7.0);
  5. the version pinned by .php-version at the project root (as read by phpenv or the Symfony CLI): its first line, such as 8.2 or 8.2.12;
  6. 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_style rule decides: false, or a list setting both equal and identical to false, means regular; any other value means yoda;
  • without one, enabling the @Symfony or @PhpCsFixer rule set (or their :risky variants), which include yoda_style, means yoda;
  • without a PHP-CS-Fixer file, or when it says nothing about the order, custos reads mago.toml, or else mago.dist.toml: when Mago's yoda-conditions lint rule is enabled ([linter.rules] entry yoda-conditions = { enabled = true, mode = "require" }, or a [linter.rules.yoda-conditions] table), mode deny means regular and require, its default, means yoda;
  • 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.

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.