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 TokenContainer implements Cleanable, ContainsChildren |
||
9 | { |
||
10 | /** @var array[Token] */ |
||
11 | private $children; |
||
12 | |||
13 | /** @var Configuration */ |
||
14 | private $configuration; |
||
15 | |||
16 | /** |
||
17 | * Constructor |
||
18 | */ |
||
19 | 21 | public function __construct(Configuration $configuration) |
|
24 | |||
25 | /** |
||
26 | * Required by ContainsChildren interface. |
||
27 | */ |
||
28 | 21 | public function getChildren() |
|
32 | |||
33 | /** |
||
34 | * Required by ContainsChildren interface. |
||
35 | */ |
||
36 | public function hasChild(Token $token) |
||
40 | |||
41 | /** |
||
42 | * Required by ContainsChildren interface. |
||
43 | */ |
||
44 | 21 | public function addChild(Token $token) |
|
50 | |||
51 | /** |
||
52 | * Required by ContainsChildren interface. |
||
53 | */ |
||
54 | View Code Duplication | public function removeChild(Token $token) |
|
65 | |||
66 | /** |
||
67 | * Required by Cleanable interface. |
||
68 | */ |
||
69 | 15 | public function clean(LoggerInterface $logger = null) |
|
81 | } |
||
82 |
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.