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 |
||
15 | abstract class AbstractContext |
||
16 | { |
||
17 | /** |
||
18 | * @var array|ValueParserInterface[] |
||
19 | */ |
||
20 | private $parsers = []; |
||
21 | |||
22 | /** |
||
23 | * @var PropertyMatcherInterface |
||
24 | */ |
||
25 | private $matcher; |
||
26 | |||
27 | /** |
||
28 | * @return array|Parser\ValueParserInterface[] |
||
29 | */ |
||
30 | public function getParsers() |
||
34 | |||
35 | /** |
||
36 | * @param array|Parser\ValueParserInterface[] $parsers |
||
37 | * |
||
38 | * @return $this |
||
39 | */ |
||
40 | View Code Duplication | public function setParsers($parsers) |
|
51 | |||
52 | /** |
||
53 | * Append parser to the list of parsers, LOW priority |
||
54 | * |
||
55 | * @param ValueParserInterface $parser |
||
56 | * |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function appendParser(ValueParserInterface $parser) |
||
65 | |||
66 | /** |
||
67 | * Prepend parser to list of parsers, HIGH priority |
||
68 | * |
||
69 | * @param ValueParserInterface $parser |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function prependParser(ValueParserInterface $parser) |
||
84 | |||
85 | /** |
||
86 | * @return PropertyMatcherInterface |
||
87 | */ |
||
88 | public function getMatcher() |
||
92 | |||
93 | /** |
||
94 | * @param PropertyMatcherInterface $matcher |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setMatcher($matcher) |
||
104 | } |
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.