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 |
||
25 | View Code Duplication | class Route extends BaseRoute implements |
|
1 ignored issue
–
show
|
|||
26 | PersistableInterface, |
||
27 | RouteInterface, |
||
28 | TenantAwareInterface |
||
29 | { |
||
30 | use TenantAwareTrait; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $id; |
||
36 | |||
37 | /** |
||
38 | * @var Collection |
||
39 | */ |
||
40 | protected $articles; |
||
41 | |||
42 | /** |
||
43 | * @var RouteInterface |
||
44 | */ |
||
45 | protected $root; |
||
46 | |||
47 | /** |
||
48 | * @var RouteInterface |
||
49 | */ |
||
50 | protected $parent; |
||
51 | |||
52 | /** |
||
53 | * @var Collection|RouteInterface[] |
||
54 | */ |
||
55 | protected $children; |
||
56 | |||
57 | /** |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $lft; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | protected $rgt; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $level; |
||
71 | |||
72 | 68 | public function __construct() |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function getArticles() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function setArticles($articles) |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 21 | public function getId() |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 2 | public function getRouteName() |
|
108 | { |
||
109 | 2 | return $this->getName(); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function isRoot(): bool |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function getRoot(): RouteInterface |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 21 | public function getParent() |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function setParent(RouteInterface $parent = null) |
||
143 | |||
144 | /** |
||
145 | * @return Collection|RouteInterface[] |
||
146 | */ |
||
147 | public function getChildren() |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function hasChild(RouteInterface $route): bool |
||
159 | |||
160 | /** |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function addChild(RouteInterface $route) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function removeChild(RouteInterface $route) |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function getLeft(): int |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function setLeft(int $left) |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function getRight(): int |
||
205 | |||
206 | /** |
||
207 | * @param int $right |
||
208 | */ |
||
209 | public function setRight(int $right) |
||
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | public function getLevel(): int |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function setLevel(int $level) |
||
229 | } |
||
230 |
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.