Extending DressCode
A custom rule for your team, a preset for all your projects, a plugin that shares them as a package, an analysis shared by several rules, or upgrade data that moves the users of your library to its new version: when to choose each of the five ways to extend DressCode, and how to run DressCode from your own tool.
Five Places You Can Extend
A rule is the answer to “I also want to check this”. It declares the decisions it enforces (how to name them is in
A Rule in Detail), finds their violations
and usually offers a fix. Users then set its decisions just like the decisions of the engine: those of a project's rules in the
project section, those of a plugin's rules in a section named after the plugin. You can write one in an afternoon:
How to Write Your Own Rule.
A preset is the answer to “I want this style on all my projects”. It is a NEON file shaped like a configuration, and it carries decisions. It can build on another preset, and you can use it for a whole project or just for a part of it. See Your Own Preset and Plugin.
A plugin is the answer to “I want to share this as a package”. It is a class implementing
DressCode\Plugin, through which a Composer package brings its rules, presets and analyses to a project. It loads only
when the project names it in the key use, because it brings code that runs on every check. A plugin does not decide
how the code looks: the project or a preset writes the decisions. See Your
Own Preset and Plugin.
An analysis is the answer to “several rules need this information about a file”. It is a plain class, built on top
of the tree, that rules ask for, and the engine computes it for them only once. The built-in ones are NameResolver and Scope, PhpDoc, PhpSymbols with
PhpSignatures (what PHP itself declares) and Types (the types of the code from the PHPStan of the
project). See A Rule in Detail.
Upgrade data is the answer to “the new version of my library renamed its API, and users should fix their code”.
These are not rules but NEON files in a directory that a package announces in extra.dresscode.upgrading-dir. They
fill the maps of the engine, such as upgrading.api.replacedClasses, or the maps of plugin rules. See Replacement Maps.
What you cannot extend is the parser. It is a separate library, PhpSyntax; its
grammar is PHP and nothing else, and the tree it produces is the same for all rules. That is exactly why rules can be combined
without knowing about each other. You also cannot add your own output format: the output is a contract of its own, and if you want
to process the results somewhere else, read the JSON from dresscode check --format json.
The terms the pages about extending work with, such as node, slot, pass or claim, are explained in the glossary. The classes of DressCode an extension can rely on between versions are listed on DressCode Backward Compatibility.
DressCode Inside Your Own Tool
If you bundle DressCode into your own tool with its own binary, run the whole command-line application, the
DressCode\Console\Application class, inside your process, and give it the configuration that applies when the project
has none of its own:
use DressCode\Config;
use DressCode\Console\Application;
$application = new Application(script: __FILE__, defaultConfig: new Config(use: ['nette'], paths: ['src', 'tests']));
exit($application->run($argv));
script is your binary: DressCode uses it to start the parallel worker processes and an upgrade run that continues
in a new process, so that they get the same default configuration. Everything else (the commands, options, formats and parallel
runs) stays the same as with dresscode, and run() returns the same exit codes. run() itself prints the errors that belong to the
run to standard error: an invalid call and a bad configuration with exit code 3, a rule failure, rules in a loop, and
environment errors of the RuntimeException type, for example a file that cannot be written or a crashed worker
process, with exit code 2. Other exceptions, for example LogicException or Error, indicate
a bug in the program, and run() lets them propagate.