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 |
||
19 | class Array2ObjectContext |
||
20 | { |
||
21 | /** |
||
22 | * @var array|ValueParserInterface[] |
||
23 | */ |
||
24 | private $parsers = []; |
||
25 | |||
26 | /** |
||
27 | * @var PropertyMatcherInterface |
||
28 | */ |
||
29 | private $matcher; |
||
30 | |||
31 | /** |
||
32 | * @var PropertyWriterInterface |
||
33 | */ |
||
34 | private $writer; |
||
35 | |||
36 | /** |
||
37 | * @return array|Parser\ValueParserInterface[] |
||
38 | */ |
||
39 | public function getParsers() |
||
43 | |||
44 | /** |
||
45 | * @param array|Parser\ValueParserInterface[] $parsers |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | View Code Duplication | public function setParsers($parsers) |
|
60 | |||
61 | /** |
||
62 | * Append parser to the list of parsers, LOW priority |
||
63 | * |
||
64 | * @param ValueParserInterface $parser |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function appendParser(ValueParserInterface $parser) |
||
74 | |||
75 | /** |
||
76 | * Prepend parser to list of parsers, HIGH priority |
||
77 | * |
||
78 | * @param ValueParserInterface $parser |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function prependParser(ValueParserInterface $parser) |
||
93 | |||
94 | /** |
||
95 | * @return PropertyMatcherInterface |
||
96 | */ |
||
97 | public function getMatcher() |
||
101 | |||
102 | /** |
||
103 | * @param PropertyMatcherInterface $matcher |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function setMatcher($matcher) |
||
113 | |||
114 | /** |
||
115 | * @return PropertyWriterInterface |
||
116 | */ |
||
117 | public function getWriter() |
||
121 | |||
122 | /** |
||
123 | * @param PropertyWriterInterface $writer |
||
124 | * |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setWriter($writer) |
||
133 | } |
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.