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 |
||
22 | final class AddProxiedMethods extends NodeVisitorAbstract |
||
23 | { |
||
24 | /** @var string */ |
||
25 | private $resolverProperty; |
||
26 | |||
27 | /** @var Node\Stmt\ClassMethod[] */ |
||
28 | private $methods; |
||
29 | |||
30 | /** @var string */ |
||
31 | private $resolveMethod; |
||
32 | |||
33 | /** |
||
34 | * @param string $property |
||
35 | * @param array $methods |
||
36 | * @param string $resolveMethod |
||
37 | */ |
||
38 | public function __construct(string $property, array $methods, string $resolveMethod) |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function leaveNode(Node $node) |
||
70 | |||
71 | /** |
||
72 | * @return Node\Stmt\Expression |
||
73 | */ |
||
74 | View Code Duplication | private function buildCloneExpression(): Node\Stmt\Expression |
|
83 | |||
84 | /** |
||
85 | * @param Node\Stmt\ClassMethod $method |
||
86 | * @return bool |
||
87 | */ |
||
88 | private function hasReturnStmt(Node\Stmt\ClassMethod $method): bool |
||
104 | |||
105 | /** |
||
106 | * @param Node\Stmt|Node\Stmt\ClassMethod $node |
||
107 | * @return bool |
||
108 | */ |
||
109 | private function findReturnStmt(Node\Stmt $node): bool |
||
127 | |||
128 | /** |
||
129 | * @param Node\Stmt\ClassMethod $method |
||
130 | * @return Node\Stmt\ClassMethod |
||
131 | */ |
||
132 | View Code Duplication | private function modifyReturnMethod(Node\Stmt\ClassMethod $method): Node\Stmt\ClassMethod |
|
147 | |||
148 | /** |
||
149 | * @param Node\Stmt\ClassMethod $method |
||
150 | * @return Node\Stmt\ClassMethod |
||
151 | */ |
||
152 | View Code Duplication | private function modifyExprMethod(Node\Stmt\ClassMethod $method): Node\Stmt\ClassMethod |
|
165 | |||
166 | /** |
||
167 | * @param Node\Stmt\ClassMethod $method |
||
168 | * @return array |
||
169 | */ |
||
170 | private function packMethodArgs(Node\Stmt\ClassMethod $method): array |
||
180 | } |
||
181 |
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.