Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
8 | class Shake |
||
9 | { |
||
10 | /** |
||
11 | * Shake node collection by a callback so only the filtered nodes are left as children with respect to their ancestor relations. |
||
12 | * Shaking a collection retains the ancestors for each filtered node |
||
13 | * Pruning a collection only keeps the filtered nodes and collapses the ancestor tree. |
||
14 | * |
||
15 | * @param Node $node |
||
16 | * @param callable $callback |
||
17 | * @param bool $prune - |
||
18 | * @return Node |
||
19 | * @internal param Node[] $nodes |
||
20 | */ |
||
21 | 9 | View Code Duplication | public function __invoke(Node $node, Callable $callback, $prune = false): Node |
30 | |||
31 | /** |
||
32 | * Blacklist of allowed nodes - we reverse the callback so we get the nodes that we do not want included |
||
33 | * @param $copiedNode |
||
34 | * @param callable $callback |
||
35 | * @return array |
||
36 | */ |
||
37 | 9 | private function getBlacklistedNodes(Node $copiedNode, Callable $callback): array |
|
62 | } |
||
63 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.