Porting a Rule from Another Tool
You can port a sniff from PHP_CodeSniffer or a fixer from PHP CS Fixer to DressCode in six steps. You take the specification from the tests, not from the source, translate questions about tokens into questions about the tree, and drop the defensive code, because on a tree there is nothing left for it to guard against.
Who This Is For
You have your own sniff (a PHP_CodeSniffer rule) or fixer (a PHP CS Fixer rule). For years it has checked something no standard covers, and you want to have it here too. This procedure is written so that you can work through it step by step, and an AI agent can follow it just as well when you give it the original rule. In fact, agents ported most of the built-in rules of DressCode this way.
But first check whether you need to port it at all: the tables of PHP CS
Fixer rules, PHP_CodeSniffer sniffs and Slevomat sniffs lead you from the name of a rule to the decision that covers
it, and dresscode catalogue prints the same for every decision. Port only what is not in them.
1. Take the Specification from the Tests, Not from the Source
The source code of the other tool's rule describes how to work around the problem on a flat array of tokens: where it steps back, what it counts, when the rule gives up. That is exactly the information that is useless here. The valuable part is the test cases: pairs of code before and after the fix, and the edge cases the author collected over the years. Move them almost word for word into fixtures. A fixture is your specification and at the same time proof that you lost nothing along the way.
2. Translate the Questions from Tokens to the Tree
Every loop over tokens in the original is in fact a question about the structure of the code. The same translations come up again and again:
| in the original | in DressCode |
|---|---|
getPrevMeaningfulToken() in a loop until the start of the expression is found |
a slot of a node: $node->condition, $node->arguments, or $node->parent |
| counting parentheses by hand to find the end of a block | the slots openParen and closeParen, openBrace and closeBrace |
T_STRING and guessing from the context what it actually is |
a specific node class: NameNode, IdentifierNode, FunctionCallNode |
| your own helper for “is this a global function” and “which namespace are we in” | GlobalCalls::findFunction(), findUncertainty(), NameResolver::getNamespace(),
resolveClass() |
your own helper for “are we inside a method” and “is $this available” |
Scope::findFunction(), findClass(), hasThis() |
| comparing two ranges of tokens | Node::matches() |
checking that a range has no ++, no call and the like |
ExpressionNode::isRepeatableRead() |
| searching for a comment between tokens | Token::hasCommentUpTo(), Node::hasInnerComment() |
$phpcsFile->addFixableError() and then $fixer->replaceToken() |
report() in the condition, and only after it a write to a slot or replaceWith() |
Tokens::insertAt() with tokens built by hand |
Parser::parseExpression() or parseStatement() and insert() into a list |
Where the original worked with token indexes, work with nodes; where it read text, ask the tree. The node reference lists which nodes and slots exist.
3. Drop the Defensive Code
A large part of the other tool's rule exists only so that it does not get confused: so that it does not mistake the
[ of an array for an element access, so that a parenthesis inside a string is not counted, so that a comment in the
middle of a call does not break the search. On a tree, these mix-ups cannot happen, so there is no reason to port that code. The
temptation to translate the original line by line is strong, especially for an agent, and a simple test protects you from it:
every line of the new rule must answer a question about the code, not about tokens. Remove any line that deals with
everything that can stand between two tokens.
The result is usually several times shorter. A rule that has more than a hundred lines after porting is suspicious: either it does several things at once and should be split, or it is still a line-by-line translation instead of a real port.
4. Handle Safety Differently from the Original
The original often guarded against side effects with a list: T_INC, T_DEC, maybe calls. Here you use
isRepeatableRead() instead, which is both stricter and more precise. A fix that was risky in PHP CS Fixer may stop
being risky after porting, if the rule asks the tree. Where the code does not show whether a case is safe, report the fix as risky
at every such occurrence, with report(..., risk: Risk::TypeUnknown) and what would decide it (see Risky Fixes); the fix is then made only with the permission of
the project. A rule that by its nature changes the meaning of the program reports every occurrence with
Risk::BehaviorChanges, so its fixes stay subject to the user's permission.
5. Decisions Instead of Options
The options of the other tool's rule are not carried over one to one. First ask yourself what thing in the code the rule
checks, and name it with a decision path: in the project section or in your plugin's section, and as a state, not a
step. What was a choice between two shapes in the original becomes the value of that decision (sameLine,
nextLine); what turned something on and off becomes forbidden or required versus
keep; and a list of exceptions becomes a parameter. Drop an option that only worked around a weakness of tokens, such
as “ignore nested parentheses”, together with the defensive code. A Rule in Detail describes how to name and
declare a decision.
Your users, however, will still have the name of the original sniff or fixer in their phpcs:ignore comments.
DressCode translates the names of other tools only into decisions of the engine, so for your own rule, replace them in the code
with the path of the new decision: migrate suppressions prints the names it does not know, and that is exactly the
list you need.
6. Verify on Their Fixtures and on Your Own
Did the original test cases pass? Then add what they were missing, because the tree sees more: a comment inside a construct, a
construct across several lines, alternative syntax, code mixed with pieces of HTML. Finally, run fix on a larger body
of someone else's code and look at the diff.
The License of the Original
You are carrying over behavior and test cases, not code. PHP_CodeSniffer is under the BSD-3-Clause license, PHP CS Fixer and Slevomat under MIT; both allow derivative works with attribution. Mark fixtures taken from another project with their origin and license in the header of the file. A rule written by this procedure contains no one else's code, because in the end there was nothing left to carry over.