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 |
||
16 | class FeedReader implements ReaderInterface |
||
17 | { |
||
18 | /** @var \Symfony\Component\PropertyAccess\PropertyAccessor */ |
||
19 | private $accessor; |
||
20 | |||
21 | /** @var bool */ |
||
22 | private $isErrorEnabled; |
||
23 | |||
24 | /** |
||
25 | * Create reader instance |
||
26 | * |
||
27 | * @param bool $isErrorEnabled |
||
28 | * @return FeedReader |
||
29 | */ |
||
30 | public static function create($isErrorEnabled = true) |
||
34 | |||
35 | /** |
||
36 | * Constructor |
||
37 | */ |
||
38 | private function __construct($isErrorEnabled) |
||
43 | |||
44 | /** |
||
45 | * Define if errors are enable on parsing feed data |
||
46 | * |
||
47 | * @param bool $enable |
||
48 | * @return FeedReader |
||
49 | */ |
||
50 | public function enableErrorOnParsing($enable) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function readFromJson($json) |
||
69 | |||
70 | /** |
||
71 | * Browse feed node |
||
72 | * |
||
73 | * @param array $content |
||
74 | * @return Feed |
||
75 | */ |
||
76 | private function readFeedNode(array $content) |
||
111 | |||
112 | /** |
||
113 | * Browse item node |
||
114 | * |
||
115 | * @param array $content |
||
116 | * @return Item |
||
117 | */ |
||
118 | private function readItemNode(array $content) |
||
155 | |||
156 | /** |
||
157 | * Browse author node |
||
158 | * |
||
159 | * @param array $content |
||
160 | * @return Author |
||
161 | */ |
||
162 | private function readAuthorNode(array $content) |
||
183 | |||
184 | /** |
||
185 | * Browse hub node |
||
186 | * |
||
187 | * @param array $content |
||
188 | * @return Hub |
||
189 | */ |
||
190 | private function readHubNode(array $content) |
||
197 | |||
198 | /** |
||
199 | * Browse attachment node |
||
200 | * |
||
201 | * @param array $content |
||
202 | * @return Attachment |
||
203 | */ |
||
204 | private function readAttachmentNode(array $content) |
||
224 | } |
||
225 |
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.