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 Link extends AbstractElement implements DisplayRuleInterface, HasAttributesInterface, HasActiveAttributesInterface |
||
28 | { |
||
29 | use DisplayRule, HasAttributes, HasActiveAttributes; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $title; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $url; |
||
40 | |||
41 | /** |
||
42 | * @var Attributes |
||
43 | */ |
||
44 | protected $linkAttributes; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $currentUrl; |
||
50 | |||
51 | /** |
||
52 | * Link constructor. |
||
53 | * @param string $title |
||
54 | * @param string $url |
||
55 | * @param Attributes $attributes |
||
56 | * @param Attributes $activeAttributes |
||
57 | * @param Attributes $linkAttributes |
||
58 | * @param string $view |
||
59 | * @param string $currentUrl |
||
60 | * @param MenuRender $render |
||
61 | */ |
||
62 | 18 | public function __construct($title, $url, Attributes $attributes, Attributes $activeAttributes, |
|
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | 8 | public function render($view = null) |
|
84 | |||
85 | /** |
||
86 | * @inheritDoc |
||
87 | */ |
||
88 | 8 | public function buildAttributes($attributes = []) |
|
95 | |||
96 | /** |
||
97 | * @return array |
||
98 | */ |
||
99 | 8 | protected function renderWith() |
|
110 | |||
111 | /** |
||
112 | * Check is two url equal |
||
113 | * |
||
114 | * @param string $first |
||
115 | * @param string $second |
||
116 | * @return bool |
||
117 | */ |
||
118 | 8 | protected function isUrlEqual($first, $second) |
|
133 | |||
134 | 1 | protected function wakeupCurrentUrl() |
|
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | 2 | View Code Duplication | public function toArray() |
154 | |||
155 | /** |
||
156 | * @inheritDoc |
||
157 | */ |
||
158 | 1 | public function unserialize($serialized) |
|
164 | |||
165 | 2 | View Code Duplication | protected function propertiesForSerialization() |
176 | } |
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.