PHP 8.6 is out. DressCode rewrites your code for it. Upgrading to a newer PHP →

Configuration

The configuration is a single file in the root of your project: dresscode.neon, or dresscode.php if you prefer PHP. Instead of assembling rules, you describe your code in it, key by key: blankLines.aroundMethods: 1 means one blank line around methods. A standard provides the base, sets add intents on top of it, and you write yourself whatever you want different, for the whole project or just part of it.

The Configuration File

This is the whole configuration of a project that builds on PER Coding Style 3.1, wants to clean up needless code and does a few things its own way:

use:
	- perCs
	- cleanup

paths:
	- src
	- tests

excludePaths:
	- tests/fixtures

braces:
	position:
		class: nextLine
blankLines:
	aroundMethods: 1
	beforeStatement:
		return: 1
spacing:
	concatenation: '$a.$b'

The configuration is written in NEON, which reads much like YAML: indentation expresses nesting and # starts a comment. If you prefer plain PHP, write the same into dresscode.php, as Configuration in PHP shows.

All names in the configuration (keys, values and preset names) are written in camelCase. In NEON, a list with a single item may also be written as a plain value, so paths: src is the same as paths: [src]. This works for use, paths, excludePaths, fileExtensions, fixRisky, reportOnly and for the paths in overrides.

A typo is not silently skipped; it causes an error when the configuration loads, and the message offers what you most likely meant: Key `spacing.cal` is unknown; write `spacing.call`. The same happens with a value the key does not take, and the message lists the values it does take. Without a configuration file, DressCode refuses to run and recommends dresscode init, because it has no style of its own; it runs only when you give it a preset with the --use option: dresscode check src --use perCs.

Decisions: What a Key Names and How It Is Written

A key of the configuration names a thing in the code, not the rule that checks it. The space between a function name and its parenthesis is spacing.call, the brace of a class is braces.position.class, the blank lines around methods are blankLines.aroundMethods. The value then says how the thing is written: that it must or must not be in the code (required, forbidden), or a word (nextLine), a code sample ('$a.$b'), a count (1), a list or a map. We call such a key with its value a decision, and you will see its whole path next to every finding in the output:

src/Cart.php
  error   9:52  Expected a line break before the opening brace.  braces.position.class

You write the path into the configuration when you want a different value, into a dresscode:ignore comment when one place should stay as it is, and after dresscode explain when you want to know what the key is for and what values it takes. There are about twenty sections, from file to upgrading, and each has a page with all its keys; the map of the whole tree is on the Rules page.

A key that neither you nor any preset writes requires nothing. And where even a preset's value should not apply, you write keep: “nothing is enforced here, the code stays as it is”. keep is the one word for “don't touch this”. Every decision that requires something accepts it, and on a whole section it applies to everything under it:

blankLines: keep           # nothing decides about blank lines
expressions:
	comparison:
		yoda: keep         # Yoda conditions may stay as they are

The page Rules describes which values each key takes, from a word through a range to a map.

Standards and Sets

You don't have to write hundreds of keys, because the configuration rests on two questions you can answer right away.

How should the code look? That is the standard, a preset that decides how the code looks, and you choose one:

standard what it is
psr12 PSR-12, section by section
perCs PER Coding Style 3.1, the successor of PSR-12
nette Nette Coding Standard, PER Coding Style with tabs and the layout of the Nette libraries
symfony Symfony Coding Standards, following the @Symfony set of PHP CS Fixer
laravel the style Laravel Pint writes by default, its laravel preset

What else do you want from the code besides its looks? Those are the sets, presets of one intent that decide nothing about the looks, and so they never conflict with the standard:

set what it decides
modernizations constructs of newer PHP where an older form says the same thing
deprecations what a newer PHP or a library has deprecated or removed
cleanup code that serves no purpose
correctness what is most likely a bug
types types written where PHP reads them
compilerOptimizations global names in the form PHP optimizes already at compile time

Both go into the use key, the standard first and the sets after it; a single preset can be written without a list (use: perCs):

use:
	- perCs
	- cleanup
	- modernizations

A set names an intent, not a list of keys. Instead of twenty decisions you write modernizations, and when DressCode learns a new modernization in the next version, the set brings it without you changing a single line of your configuration. A decision that writes newer PHP than the project allows quietly waits until the target version allows it.

The nette standard also decides about code hygiene, because it is part of Nette: it uses the cleanup, types and correctness sets itself and sets a few of their decisions back to keep, for example native types of parameters, properties and return values. No standard includes the modernizations and deprecations sets, because upgrading to a newer PHP is a step you take when you decide to.

A preset is a file of the same shape as your configuration. The page Standards and Sets shows exactly what each one decides, with comments explaining why. Built-in presets are written in short form, as in the examples: the full name has the form vendor/name, and the vendor of the built-in ones is dresscode, so perCs is short for dresscode/perCs. dresscode config and dresscode explain print names in the full form. A package from the vendor dresscode is written in short form too, for example upgrade-nette instead of dresscode/upgrade-nette; a preset from a plugin of another vendor is always written in full. And the use key also takes a path to a file, for example use: [perCs, ../shared/house.neon], relative to the file that contains it.

Layers

For each file, the values are composed from layers, from the lowest to the highest:

  1. the presets from the use key in the order they are written, each on top of the presets it builds on,
  2. the decisions of your configuration,
  3. the overrides for a part of the project that the file matches, in the order they are written,
  4. the command line (--use, --set).

A higher layer changes only what it states itself: changing one value from a preset means writing one line. How lists, maps and keep merge is described in Merging Values Across Layers.

Each preset is applied only once, at the place where something first names it. When you list it again, it changes nothing, and the run says so:

Warning: The configuration uses preset `dresscode/perCs`, which preset `dresscode/nette` already brings; the entry does nothing.

The same holds for the sets a standard uses. nette builds on cleanup, types and correctness and turns some of their decisions off, for example types.declaration.parameter. When you write use: [nette, types], the types set is applied only once, under nette, so you don't get the turned-off decisions back, and the run prints the same warning. If you want them, write them yourself:

use: nette

types:
	declaration:
		parameter: required
		property: required
		return: required

The order of keys in the file has no effect on the run; DressCode itself decides the order in which rules run. dresscode config prints where each value came from, in the shape of the file, with the layer in a comment next to each key.

Paths

paths says what is checked: files and directories relative to the root of the project. Paths given on the command line take precedence. But when you give a directory that contains some of the paths from paths, only what paths names is checked in it (see Command Line).

excludePaths leaves paths out, and it only adds: it adds yours to the default list (vendor, node_modules, temp, tmp, log, and all directories and files starting with a dot), and every plugin does the same. Nobody can bring back a path that someone else excluded.

paths:
	- src
	- tests

excludePaths:
	- tests/fixtures
	- '*.generated.php'

A pattern with a slash is anchored to the root, so tests/fixtures is exactly that one directory. A pattern without a slash matches the name of a file or directory at any depth. An asterisk stands for anything except a slash, and two asterisks match slashes too, so tests/**.phpt matches all tests at any depth. Two asterisks with a slash mean zero or more directories, the same as in .gitignore, so src/**/Test.php also finds src/Test.php. A question mark stands for any single character except a slash, [abc] for one of the listed characters and [!abc] for any other one. DressCode refuses an invalid pattern, for example one with an unclosed square bracket, as soon as it reads the configuration.

fileExtensions says which extensions are treated as PHP (the default is only php); a project with Nette Tester tests adds phpt. To skip a file by its content, for example generated code by its header, use skipWhen and pass it a callback as an entity; it receives the content of the file and its path, and returns true when the file should be skipped:

skipWhen: App\CodeStyle\GeneratedFilter::isGenerated(...)

PHP and Package Versions

The PHP version the code is written for is a property of the project, and it is read from composer.json, from the constraint in require.php. A decision that writes syntax of a newer PHP applies only when every version the constraint allows supports that syntax, so with ^8.1 PHP 8.1 decides, even if the project runs on a newer one. The lower bound is raised by config.platform.php in composer.json, a value Composer never changes by itself: with ^8.1 and the platform 8.3, PHP 8.3 decides. A constraint without a lower bound, such as <8.4 or *, does not give a lowest version, so the default PHP 8.0 applies. DressCode does not support versions older than PHP 8.0: it checks the code as PHP 8.0 and warns that a fix may write syntax the older PHP does not support. The Target line in the header of every command's output shows where the target version came from.

You write a different version into the targets key when it differs from composer.json or when DressCode cannot read it from there, and into an override when a part of the project runs on a different PHP:

targets:
	php: '8.2'

Just like a PHP version, a decision may also require a certain version of a package, when it checks the API of that package. The version of the package is read from composer.json and from the installed packages next to it, and what counts is the version the project builds on: for a package the project requires itself, that is the lowest version its constraint allows, not the installed one. A package that the project requires without a lower bound (*), or does not require at all and gets only as a dependency of another package, may be in any version: a decision that wants a certain version of such a package therefore does not apply, and its upgrade data is only reported; nothing is rewritten according to it. You can also give the version the code should be written for in the targets key, for example when upgrading libraries you convert the code to a new version before you install it. The version is written in quotes, because NEON would read 3.10 as the number 3.1:

targets:
	php: '8.2'
	nette/forms: '3.3'

Warnings and Risky Fixes

Every violation is an error and fails the build, unless the configuration softens it. The severity key does that: a map of names to warning or error. A name is a decision, a group, a section or a preset, and it applies to everything under it; within one layer, the most specific name wins. A warning enforces the same thing as an error, and fix fixes it the same way, but the output counts it separately and the exit code stays 0; the --max-warnings <n> option sets a threshold. This is useful for findings that fix cannot fix, mainly long lines:

severity:
	file.longLines: warning
	phpdoc: warning

The severity is not part of the value of a decision, so a new value does not carry it along: a severity applies to the value written in the same layer or in a layer below it. The psr12 and perCs standards soften long lines this way. When a project writes file.longLines: forbidden itself and gives no severity with it, long lines are an error again, because now the project has decided about them.

A risky fix is one that can change what the code does, and whether a fix is risky always depends on the particular place. Turning in_array($role, $roles) into in_array($role, $roles, true) is safe where all the values searched are of a single type; elsewhere the code may stop finding a value it found before. So DressCode fixes what it can fix safely and reports each risky occurrence together with what would decide it:

  • The type of a value. The types DressCode takes from your project's PHPStan would decide it.
  • The name of a function. An unqualified is_null() in a namespace may be your own function App\is_null(). A single line, nameResolution: certain, decides that.
  • Your judgment. file.strictTypes.declaration turns on strict types, which by its nature changes how the code behaves; functions.staticClosure cannot see whether you bind the closure to an object somewhere else. No extra information helps here; the choice is yours.

In the output, a risky place has the status risky, and under the message there is a sentence about what can go wrong at that place:

src/Users.php
  risky  17:16  The `in_array()` call must pass `$strict = true`.  mistakes.strictArgument
                Risky because a strict search no longer finds a value of another type.

Writing a decision into the configuration, yourself or through a preset, does not mean agreeing to its risky fixes. The project gives its consent with the fixRisky key, a list of decisions whose risky fixes it accepts; the list may also name a section or a preset, which covers everything under it. The only section with the same name as a set is types: you write the section as types.* and the set by its full name dresscode/types, because the bare name types could mean either, and DressCode refuses it. A preset cannot give this consent. That is why a Git hook or an editor needs no option: it fixes what the project has accepted and reports the rest. The --fix-risky option allows the risky fixes of all decisions for a single run, --fix-risky=mistakes.strictArgument only those of that one decision:

fixRisky:
	- mistakes.strictArgument
	- hygiene.classNameNotation

The fixRisky list adds up across layers, so an override can accept, for its part of the project only, risky fixes you don't want to accept elsewhere. Without consent, the risky place is reported, and its severity follows what would decide it. Where types or nameResolution would be enough, it is a warning, because failing the build over such a place would push you to give consent blindly. Where the fix changes behavior by its nature, it is an error like any other violation. The summary counts risky fixes by their cause and says what to do about each:

2 risky fixes wait, the type is unknown: set `typeAnalysis: phpstan`. See https://dresscode.run/types#enable
1 risky fix waits, a name may reach a function of the namespace: run `dresscode init`, which lists what the namespaces declare, or set `nameResolution: certain` if they declare nothing. See https://dresscode.run/namespaces#name-resolution

The opposite case is handled by the reportOnly key: decisions, sections or presets whose violations are reported but never fixed. This is useful when you introduce the tool and want to see first what would change, or for a decision the project prefers to fix by hand. The names are read the same way as in fixRisky, the list adds up across layers, and an override may have one for its part of the project; a preset cannot write it. A decision in reportOnly does not wait for consent to a risky fix either, because it offers no fix:

reportOnly:
	- multiline
	- mistakes.strictArgument

Where your judgment decides, run dresscode fix --ask-risky: it makes the safe fixes as usual, then asks about each remaining risky fix separately, shows its diff and makes only those you accept. Put into fixRisky the decisions whose risky fixes are fine everywhere, and mark a place that should stay as it is with a dresscode:ignore comment.

Types and Namespaces

There are two things about a project that DressCode cannot learn from a single file, so you tell it through keys.

typeAnalysis: phpstan says that the types of the code are taken from the PHPStan installed in the project, with its phpstan.neon and extensions. Only with types do the decisions that make no sense without them apply, for example rewriting the deprecated API of libraries. It is a decision of the whole project; neither a preset nor an override can set it. The page Types from PHPStan describes what types make possible and what they cost.

namespaces lists the functions and constants that the namespaces of the project declare, and nameResolution: certain says that there are no others. DressCode then knows whether an unqualified strlen() in a namespace means the global function, and the fixes that depend on it stop being risky. The page Functions and Constants in Namespaces explains why this matters so much.

typeAnalysis: phpstan
nameResolution: certain
namespaces:
	constants:
		- 'App\{VERSION, DEBUG}'

Different Values for Part of the Project

A project rarely looks the same everywhere. You want library code as fast as possible, tests have their own helper functions, and generator templates use a different indentation. An override in the overrides key applies to the files that match its paths, and it may contain decisions, presets in use, the PHP version in targets, namespaces, nameResolution, fixRisky, reportOnly, severity and suppressionComments:

use: nette

nameResolution: certain

overrides:
	# the library writes global names the way PHP can optimize them
	-
		paths: [src]
		use: [compilerOptimizations]

	# tests declare helper functions in their own namespace
	-
		paths: [tests]
		nameResolution: uncertain
		blankLines:
			aroundMethods: 1
		severity:
			phpdoc.property.plainComment: warning

	# generator templates are indented with two spaces
	-
		paths: [resources/stubs]
		indentation:
			unit: '2 spaces'

The result:

  • Files in src get the compilerOptimizations set on top of the nette standard. The set writes global functions and constants so that PHP can optimize them.
  • In tests, nameResolution: uncertain says that the lists of functions from the configuration are not complete for these files, and fixes that depend on a function being global are only reported as risky; the details are on the Functions and Constants in Namespaces page. Methods are separated by one blank line instead of two, and a plain comment above a property, where a doc comment belongs, is only a warning.
  • Templates in resources/stubs are indented with two spaces, and everything else is the same as in the rest of the project.

Paths have the same syntax as excludePaths and are relative to the root of the project. A file that matches several overrides gets their layers in the order they are written. A decision that should not apply to a part of the project gets keep in the override, and a later override can turn it on again for the files that match both. DressCode goes through every override right at the start of the run, even when no file matches it, so it reports a typo right away, together with the override it is in:

Error: The override for `tests`: Key `expresions` is unknown; write `expressions`.

dresscode config --file tests/CartTest.php shows what applies to a particular file in the end.

Merging Values Across Layers

How a higher layer changes a value from a lower one depends on the kind of value:

  • a word, a code sample, a count and yes or no replace the value from below;
  • a list replaces the whole list from below, even the default one: hygiene.aliasFunctions: [core, mysqli] means only these two groups of aliases, whatever the preset said. If you want to add to it, write ... into the list, which stands for the list from below: hygiene.aliasFunctions: [..., mysqli];
  • a map of names merges item by item, and an item with the value keep removes that name from all layers below it, even from package data; an exact item takes precedence over a pattern with an asterisk, whichever layer wrote it, and an exclamation mark after the map's key (replacedClasses!:) replaces the maps from below completely;
  • a section and a key with subkeys merge key by key, so changing one number from a preset means writing one number;
  • keep written on a section or on a key with subkeys cancels everything under it, and a later layer brings back only the keys it names itself. A section or a key with nothing left under it, after commenting it out for instance, says nothing; only keep removes what the preset says.

One Decision for One Run: –set and –only

The command line is the highest layer and lies above the configuration and above all overrides. --use adds a preset or a plugin, --set gives a decision a value as if it stood in the configuration, and --only narrows the run to the decisions, sections or presets it names, without changing values. These options are not keys of the configuration file: they decide one run, not the project.

The options can be repeated, and together they cover whatever you want to do with a decision just once, without editing the configuration. The value after = is written in NEON, so it is exactly what you would write into the file; put a sample with spaces in quotes, so that the shell does not split it:

dresscode check --set hygiene.is_null=forbidden
dresscode check --set blankLines.aroundMethods=1
dresscode check --set 'spacing.arrowFunction="fn ($x) => $x"'

--only changes no value. The whole configuration tree is resolved, and --only merely selects from it what is reported and fixed. A decision outside the selection stays silent, but its value still applies to everything the selected decisions write: when --only runs the rewrite to match in a project indented with four spaces, the new block gets four spaces.

Trying a Decision You Don't Have in Your Configuration

Do you want to know what a decision would do to your code when the configuration does not mention it or your standard leaves it at keep? Give it a value and run just that decision:

dresscode check --set controlFlow.elseAfterReturn=forbidden --only controlFlow.elseAfterReturn --diff
src/Discount.php
  error  12:5  Useless else, because the branches before it always leave.  controlFlow.elseAfterReturn
--- src/Discount.php
+++ src/Discount.php
@@ -9,8 +9,7 @@
 	{
 		if ($count > 10) {
 			return 0.1;
-		} else {
-			return 0.0;
 		}
+			return 0.0;
 	}
 }

FOUND  1 violation, a fix leaves none in 1 file

The other decisions do not run, and that is why the return in the diff stayed indented one level too deep: indentation is the job of indentation.unit. --only alone, without --set, does not run a decision the configuration does not make, because there is no value to run it with; the message says so and suggests --set:

Error: Option `--only` names decision `controlFlow.elseAfterReturn`, which has no rule that runs here; `--set controlFlow.elseAfterReturn=<value>` decides it for the run.

Introducing a New Decision in One Commit

Once you decide to adopt a decision, you want a commit that holds exactly its changes, already formatted by your standard. In a project that is clean according to its configuration, --set alone is enough:

dresscode fix --set controlFlow.elseAfterReturn=forbidden

The whole configuration runs, plus that one decision, so the other decisions immediately correct whatever its fix moves: the return from the example above ends up indented correctly. Everything else was already in order, so the diff contains only what the new decision brought. Then write the decision into the configuration and commit both at once.

Cleaning Up Old Code Step by Step

A big cleanup is hard to read as a single diff. Split it by intent, one commit for each set the configuration uses, or for each section:

dresscode fix --only cleanup
dresscode fix --only correctness
dresscode fix --only blankLines
dresscode fix

Only the types set has the same name as a section, so here it is written by its full name, dresscode/types, and the section as types.*; the bare name could mean either, and DressCode refuses it. The name of a set or a preset in --only runs only those of its decisions that the configuration really makes, never more. The name itself decides nothing about any of them, so it cannot overrule what the project left at keep on purpose. And nothing is hidden: when you have turned off some decisions of the set in the configuration, the run tells you which ones:

Warning: Option `--only` keeps 29 of the 30 rules of preset `dresscode/cleanup` that may run here, without `controlFlow.elseAfterReturn`, `controlFlow.elseifAfterReturn`, which the configuration turns off.

To run a set the configuration does not use, add it with --use. dresscode fix --use modernizations rewrites the code to a newer PHP and formats the result by your standard right away; --use modernizations --only modernizations runs the set alone, and that is also what the message suggests when --only names a preset that the configuration does not have in full. A name that would run nothing is an error, not a silently clean run.

fix --only fixes only what you name and does not touch the rest, so the final full fix still changes something, for example the indentation that the selected decisions shifted. This is on purpose, not unfinished work: each commit carries one kind of change, and the last one is pure formatting. The baseline command does not have the --only option, because the baseline would forget what the other decisions found.

Checking Someone Else's Project Without a Configuration

Without a configuration file, DressCode does not start, because it has no style of its own. --use gives it a standard for one run, and nothing is written into the project:

dresscode check src --use psr12

The header of the output then says Config none, using psr12. You can add a set too: dresscode check src --use perCs --use modernizations shows, besides the style, what could be written with a newer PHP. When you want a permanent configuration, dresscode init measures it from the code.

What Finally Applies: dresscode config

The configuration is a few lines, but they result in hundreds of decisions from several layers. dresscode config prints them as the run sees them: in the shape of the configuration file, with the layer that set each value in a comment. It reads the same resolved configuration that the rules and the cache follow, so it cannot claim anything other than what really happens.

DRESS|CODE 1.3.0
Config     /var/www/shop/dresscode.neon
Target     PHP 8.2 from `composer.json`
Use        dresscode/psr12, dresscode/perCs, dresscode/cleanup, dresscode/types, dresscode/correctness, dresscode/nette, dresscode/modernizations
Targets    php 8.2
Style      a tab, the line ending each file mostly has, lines of up to 140 characters
Names      certain, the namespaces declare no function and no constant
Comments   silence decisions on their line
      ~\bintentionally\s+[!=]=~                               expressions.comparison.loose, mistakes.strictArgument
      ~^//\s*@\s+\S~                                          mistakes.errorSuppression
Decisions  339 of 423 set by a layer, the others asking for nothing

file:
	lineLength: 140                             # dresscode/nette
	strictTypes:
		declaration: required                   # dresscode/nette
		position: sameLine                      # dresscode/nette
...
spacing:
	arrowFunction: 'fn ($x) => $x'              # the configuration
...
classes:
	overridingMethod:
		parentCallOnly: forbidden               # dresscode/cleanup, no effect: types
...
expressions:
	comparison:
		loose: forbidden                        # dresscode/nette
...
mistakes:
	strictArgument: required                    # dresscode/correctness, risky fixes accepted
...
upgrading:
	functions:
		arrayFirstLast: required                # dresscode/modernizations, no effect: php

The Use line names the plugins first and then the presets in the order they were applied, including the presets they build on: nette builds on perCs and the sets cleanup, types and correctness, perCs on psr12. The Targets line says which version of PHP and of libraries the code is written for, and the Names line how unqualified names of functions and constants in namespaces are resolved. Each value shows the layer that set it: a preset, the configuration, or an override. A decision that does not apply says why: no effect: php when it needs a newer PHP than the project allows, no effect: types when it needs types and the configuration has none. A decision whose risky fixes you have accepted carries risky fixes accepted, and a decision that the severity key softened to a warning carries only warns. When the project uses upgrade data for libraries or PHP's own data, a Packages line is added. For each package, php included, it shows the version the code must work with, the file the data comes from, and the versions the data would lead to once the lower bound is raised. --file path shows the same for one file, that is, including the overrides that apply to it, and --preset nette one preset alone, with what it builds on. --format json gives the output as data for tools: the keys of the environment as they are written in the configuration (use, targets, nameResolution…), in the decisions key each decision by its path with its value (value), its layer (layer), the reason why it does not apply (inactive), and whether --only selected it (selected), and in the rules key the classes of the rules and whether each one runs.

Explaining a Decision: dresscode explain

A decision path in the output is a reference, not an explanation. dresscode explain <path> adds its description, the values the decision takes with their meanings, its value in this project with the layer that set it, and what the five standards say:

`spacing.arrowFunction`

The space between `fn` and its parenthesis.

By default `keep`.

Takes: `'fn($x) => $x'` (no space before the parenthesis); `'fn ($x) => $x'` (a single space); `keep`

Here `'fn ($x) => $x'`, set by the configuration.

The standards: perCs `'fn($x) => $x'`, psr12 `keep`, nette `'fn($x) => $x'`, symfony `'fn ($x) => $x'`, laravel `'fn ($x) => $x'`.

Rule `DressCode\Rules\Whitespace\ConstructSpacingRule`, stage Formatting.

See https://dresscode.run/decisions/spacing.arrowFunction

Given the name of a section, as in dresscode explain blankLines, it explains all its decisions. Without an argument, it explains the whole configuration of the project, section by section, and only what some layer decides. With --format markdown, it prints the same as a Markdown document to standard output; redirect the output to save it to a file:

dresscode explain --format markdown > docs/coding-style.md

The document comes from the same configuration that checks the code. Habits from code review, and everything no decision covers, are not in it.

All Decisions the Installation Knows: dresscode catalogue

dresscode catalogue lists all decisions the installation knows, the built-in ones and those from plugins and the project's own rules, each with its description and the names of the rules of other tools that it covers. It marks the ones your configuration makes with an asterisk:

* file.finalLineEndings                              Exactly one line ending after the last line; a file ending in HTML is content  (covers single_blank_line_at_eof, PSR2.Files.EndFileNewline)
* file.openingTag                                    The opening tag of PHP  (covers full_opening_tag, Generic.PHP.DisallowShortOpenTag)
* file.lineEnding                                    The line ending of every line, which the code written new takes too; under `keep` that follows the file  (covers line_ending, Generic.Files.LineEndings)
* file.lineLength                                    The widest line, by which what spreads over lines is split  (covers Generic.Files.LineLength, SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature, SlevomatCodingStandard.ControlStructures.RequireMultiLineCondition, SlevomatCodingStandard.Files.LineLength)
  file.lineLengthExcept                              What a line too wide leaves as it is: a construct named is not spread over lines for its width, and a line it stands on, one a regular expression matches, or with `strings` one wide only for its string literals, is not reported  (covers Generic.Files.LineLength, SlevomatCodingStandard.Files.LineLength)
...

It is useful when you are looking for the name of something, or for where a rule of another tool has gone. With --format json, it prints the catalogue as data: for each decision its kind, its range of values, its description, notes, the values of the five standards and the rules of other tools it covers. The Rules page on this website is built from the same output.

Plugins

A plugin is a class implementing the DressCode\Plugin interface. Through it, a package brings a project its own rules and presets, analyses, excluded paths and a file filter. A package announces its plugin in composer.json under the key extra.dresscode.plugin, and after installation DressCode finds it in vendor/composer/installed.json.

But DressCode loads it only when the project lets it in: by writing the name of the package or the class of the plugin into the use key. A plugin brings code that runs on every check, and the project lets it in the same way Composer lets in its own plugins. The plugin of an installed package that use does not name is not loaded, and the run warns about it. Only two kinds of plugins do not need to be named: a plugin announced by the composer.json of the project itself, and the plugins of upgrade packages, which dresscode upgrade today loads by itself. If a package's plugin had the same name as a preset, use could not tell which one you meant, so DressCode refuses it with an error. A plugin that lives right in the project is listed in the use key next to the presets, by its class, or by an entity with arguments:

use:
	- acme/coding-rules
	- Acme\CodeStyle\Plugin
	- perCs
	- acme/house

The first line lets in the plugin of the package acme/coding-rules; the package name itself is not a preset. A plugin does not decide how the code is written. Its rules have their decisions in their own section, named after the plugin, and its presets only become available; the project itself writes what it wants from them into use and into the plugin's section. That is why the style of a project can always be read in its own file. A plugin belongs to the whole project, so only the configuration lists it. DressCode refuses a plugin in the use of an override or a preset with the error Plugin `Acme\CodeStyle\Plugin` is used by the configuration of the project, never by a preset or an override. How to write a plugin is described in Your Own Preset and Plugin.

A package may also carry upgrade data: files describing what versions of libraries renamed or removed. The package announces their directory under extra.dresscode.upgrading-dir. The data turns on no decision by itself: it applies only when the project says upgrading.packages: yes, which both the deprecations and modernizations sets do. Only then does it also turn on the rewrites of the package's plugin that its files name; the plugin itself is loaded during check and fix only where use names it. DressCode itself carries data of the same form about what PHP has deprecated, and the requirement upgrading.php.deprecatedApi: forbidden applies it. More on this in How Upgrading Libraries Works.

Objects and Callbacks

Where the PHP configuration passes an object or a callback, in NEON you write an entity. It looks like a call in PHP, and you may know it from the configuration of Nette DI:

  • App\CodeStyle\Filter(8.1, strict: true) creates an object; the arguments may be named,
  • App\CodeStyle\Filter::create(8.1) calls a static method and uses what it returns,
  • App\CodeStyle\Filter::isGenerated(...) passes the method itself as a callback,
  • App\CodeStyle\Filter(8.1)::withLimit(100) calls a method on what the previous link of the chain returned.

Only three places take an entity: a plugin in the use key, the skipWhen key and an analysis factory in the analyses key, written as a pair of a class and an entity:

use:
	- App\CodeStyle\Plugin(strict: true)
	- perCs

skipWhen: App\CodeStyle\GeneratedFilter::isGenerated(...)

rules:
	- App\CodeStyle\ExceptionMessagePeriodRule

analyses:
	- App\CodeStyle\Analyses\Imports
	App\CodeStyle\Analyses\Calls: App\CodeStyle\Analyses\Calls::create(...)

skipWhen and the factory of an analysis are evaluated as soon as the configuration loads, while a plugin from use is created only once DressCode has verified that its class really is a plugin. So the configuration of a repository that you only download and check cannot run any other code through a class name. In NEON, a project rule is named only by its class. A rule with its own dependencies is built by a factory every time DressCode needs it, for example separately for the files of each override, and such a factory is a plain closure in dresscode.php:

return new Config(
	use: ['perCs'],
	rules: [
		App\CodeStyle\ExceptionMessagePeriodRule::class,
		App\CodeStyle\CacheKeyRule::class => fn() => new App\CodeStyle\CacheKeyRule(App\CodeStyle\Keys::load()),
	],
);

Configuration in PHP

The example from the top of this page can be written in PHP as well, into the file dresscode.php. Each NEON key is a named argument of the DressCode\Config constructor, and the sections of decisions go together into the decisions argument:

<?php declare(strict_types=1);

use DressCode\Config;

return new Config(
	use: ['perCs', 'cleanup'],
	paths: ['src', 'tests'],
	excludePaths: ['tests/fixtures'],
	decisions: [
		'braces' => ['position' => ['class' => 'nextLine']],
		'blankLines' => ['aroundMethods' => 1, 'beforeStatement' => ['return' => 1]],
		'spacing' => ['concatenation' => '$a.$b'],
	],
);

A PHP file pays off where the configuration needs code, such as the factory of a rule with its own dependencies (see Objects and Callbacks).

Where DressCode Looks for the Configuration

DressCode looks for the file starting in the current directory and moving upward, and the directory where it finds it becomes the root of the project; all paths in the configuration are relative to it. When there is no configuration and the run gets a standard with the --use option, the root is the directory of the nearest composer.json, or the current directory if there is none, so a run from any subdirectory of the project has the same relative paths and the same cache.

Next to the configuration there may be a template dresscode.neon.dist (or .php.dist) that you commit; a file without .dist then replaces it completely, for example for local changes. Having both NEON and PHP side by side is an error; neither of them takes precedence. You can choose a different file with the --config option.

Other Keys

  • baseline: a file listing violations that cannot be fixed yet and should not be reported; the command dresscode baseline writes it, see Suppressions and the Baseline.
  • suppressionComments: the project's own comments that silence a decision on their line, see Suppressions and the Baseline.
  • cacheDir: where DressCode stores which files passed clean, and with typeAnalysis: phpstan also PHPStan's cache; the default is the directory dresscode in the system temporary directory.
  • rules and ruleUrl: the project's own rules, named by class, and the URL of their pages with the placeholder {slug}, to which each of their findings links in the terminal.
  • analyses: registers your own analysis for your own rules; an analysis of the same class from the configuration takes precedence over the one from a plugin.

Show on GitHub · Report a problem with this page on GitHub · Nov 9, 2026