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 |
||
27 | class Array2ObjectBuilder |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * @var Array2ObjectContext |
||
32 | */ |
||
33 | private $context; |
||
34 | |||
35 | /** |
||
36 | * @var array|ValueParserInterface[] |
||
37 | */ |
||
38 | private $parsers = []; |
||
39 | |||
40 | /** |
||
41 | * @var PropertyMatcherInterface |
||
42 | */ |
||
43 | private $propertyMatcher; |
||
44 | |||
45 | /** |
||
46 | * @var PropertyWriterInterface |
||
47 | */ |
||
48 | private $propertyWriter; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private $disabledParsers = []; |
||
54 | |||
55 | /** |
||
56 | * create builder instance |
||
57 | * |
||
58 | * @return static |
||
59 | */ |
||
60 | public static function create() |
||
64 | |||
65 | /** |
||
66 | * @return Array2ObjectContext |
||
67 | */ |
||
68 | public function getContext() |
||
72 | |||
73 | /** |
||
74 | * @param Array2ObjectContext $context |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function setContext($context) |
||
84 | |||
85 | /** |
||
86 | * @return array|Parser\ValueParserInterface[] |
||
87 | */ |
||
88 | public function getParsers() |
||
92 | |||
93 | /** |
||
94 | * @param ValueParserInterface $parser |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function addParser(ValueParserInterface $parser) |
||
104 | |||
105 | /** |
||
106 | * @param string|ValueParserInterface $parser |
||
107 | * |
||
108 | * @return boolean |
||
109 | */ |
||
110 | public function hasParser($parser) |
||
118 | |||
119 | /** |
||
120 | * disableParser |
||
121 | * |
||
122 | * @param string|ValueParserInterface $parser |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function disableParser($parser) |
||
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getDisabledParsers() |
||
146 | |||
147 | /** |
||
148 | * @param string|ValueParserInterface $parser |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function removeParser($parser) |
||
164 | |||
165 | /** |
||
166 | * @param array|Parser\ValueParserInterface[] $parsers |
||
167 | * |
||
168 | * @return $this |
||
169 | */ |
||
170 | View Code Duplication | public function setParsers($parsers) |
|
181 | |||
182 | /** |
||
183 | * @return PropertyMatcherInterface |
||
184 | */ |
||
185 | public function getPropertyMatcher() |
||
189 | |||
190 | /** |
||
191 | * @param PropertyMatcherInterface $propertyMatcher |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setPropertyMatcher(PropertyMatcherInterface $propertyMatcher) |
||
201 | |||
202 | /** |
||
203 | * @return PropertyWriterInterface |
||
204 | */ |
||
205 | public function getPropertyWriter() |
||
209 | |||
210 | /** |
||
211 | * @param PropertyWriterInterface $propertyWriter |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function setPropertyWriter($propertyWriter) |
||
221 | |||
222 | /** |
||
223 | * Build custom Array2Object instance |
||
224 | */ |
||
225 | public function build() |
||
265 | } |
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.