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 |
||
23 | final class AddUseStmts extends NodeVisitorAbstract |
||
24 | { |
||
25 | /** @var array */ |
||
26 | private $useStmts; |
||
27 | |||
28 | /** |
||
29 | * @param array $useStmts |
||
30 | */ |
||
31 | public function __construct(array $useStmts) |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | View Code Duplication | public function leaveNode(Node $node) |
|
51 | |||
52 | /** |
||
53 | * @param Node\Stmt[] $stmts |
||
54 | * @param Node[] $nodes |
||
55 | * @return Node\Stmt\Use_[] |
||
56 | */ |
||
57 | private function removeDuplicates(array $stmts, array $nodes): array |
||
75 | |||
76 | /** |
||
77 | * @param Node\Stmt[] $stmts |
||
78 | * @return string[] |
||
79 | */ |
||
80 | private function getExistingUseParts(array $stmts): array |
||
95 | |||
96 | /** |
||
97 | * @return Node[] |
||
98 | */ |
||
99 | private function buildUseStmts(): array |
||
109 | } |
||
110 |
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.