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 | final class TokenContainer implements Cleanable, ContainsChildren, Removable |
||
9 | { |
||
10 | /** @var Token[] */ |
||
11 | private $children; |
||
12 | |||
13 | /** @var Configuration */ |
||
14 | private $configuration; |
||
15 | |||
16 | /** |
||
17 | * Constructor |
||
18 | */ |
||
19 | 156 | public function __construct(Configuration $configuration) |
|
24 | |||
25 | /** |
||
26 | * Required by ContainsChildren interface. |
||
27 | */ |
||
28 | 156 | public function getChildren() : array |
|
32 | |||
33 | /** |
||
34 | * Required by ContainsChildren interface. |
||
35 | */ |
||
36 | 1 | public function hasChild(Token $token) : bool |
|
40 | |||
41 | /** |
||
42 | * Required by ContainsChildren interface. |
||
43 | */ |
||
44 | 155 | public function appendChild(Token $token) |
|
48 | |||
49 | /** |
||
50 | * Required by ContainsChildren interface. |
||
51 | */ |
||
52 | 1 | public function prependChild(Token $token) |
|
56 | |||
57 | /** |
||
58 | * Required by ContainsChildren interface. |
||
59 | */ |
||
60 | 5 | View Code Duplication | public function removeChild(Token $token) |
67 | |||
68 | /** |
||
69 | * Required by Cleanable interface. |
||
70 | */ |
||
71 | 148 | public function clean(LoggerInterface $logger) : bool |
|
79 | |||
80 | /** |
||
81 | * Required by the Removable interface. |
||
82 | */ |
||
83 | 147 | public function remove(LoggerInterface $logger) |
|
113 | } |
||
114 |
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.