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 |
||
22 | abstract class AbstractBuilder |
||
23 | { |
||
24 | /** |
||
25 | * @var AbstractContext |
||
26 | */ |
||
27 | private $context; |
||
28 | |||
29 | /** |
||
30 | * @var array|ValueParserInterface[] |
||
31 | */ |
||
32 | private $parsers = []; |
||
33 | |||
34 | /** |
||
35 | * @var PropertyMatcherInterface |
||
36 | */ |
||
37 | private $matcher; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $disabledParsers = []; |
||
43 | |||
44 | /** |
||
45 | * create builder instance |
||
46 | * |
||
47 | * @return static |
||
48 | */ |
||
49 | public static function create() |
||
53 | |||
54 | /** |
||
55 | * @return AbstractContext |
||
56 | */ |
||
57 | public function getContext() |
||
61 | |||
62 | /** |
||
63 | * @param AbstractContext $context |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function setContext($context) |
||
73 | |||
74 | /** |
||
75 | * @return array|Parser\ValueParserInterface[] |
||
76 | */ |
||
77 | public function getParsers() |
||
81 | |||
82 | /** |
||
83 | * @param ValueParserInterface $parser |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function addParser(ValueParserInterface $parser) |
||
93 | |||
94 | /** |
||
95 | * @param string|ValueParserInterface $parser |
||
96 | * |
||
97 | * @return boolean |
||
98 | */ |
||
99 | public function hasParser($parser) |
||
107 | |||
108 | /** |
||
109 | * disableParser |
||
110 | * |
||
111 | * @param string|ValueParserInterface $parser |
||
112 | * |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function disableParser($parser) |
||
127 | |||
128 | /** |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getDisabledParsers() |
||
135 | |||
136 | /** |
||
137 | * @param string|ValueParserInterface $parser |
||
138 | * |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function removeParser($parser) |
||
153 | |||
154 | /** |
||
155 | * @param array|Parser\ValueParserInterface[] $parsers |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | View Code Duplication | public function setParsers($parsers) |
|
170 | |||
171 | /** |
||
172 | * @return PropertyMatcherInterface |
||
173 | */ |
||
174 | public function getMatcher() |
||
178 | |||
179 | /** |
||
180 | * @param PropertyMatcherInterface $matcher |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function setMatcher(PropertyMatcherInterface $matcher) |
||
190 | |||
191 | /** |
||
192 | * @param AbstractContext $context |
||
193 | */ |
||
194 | protected function prepareContext(AbstractContext $context) |
||
225 | |||
226 | abstract public function build(); |
||
227 | } |
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.