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 | abstract class Category |
||
26 | { |
||
27 | /** |
||
28 | * @return int|string |
||
29 | */ |
||
30 | abstract public function getId(); |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | * @ORM\Column(name="name", type="string", length=255) |
||
35 | */ |
||
36 | protected $name; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | * @ORM\Column(name="slug", type="string", length=255, unique=true) |
||
41 | */ |
||
42 | protected $slug; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | * @ORM\Column(name="description", type="text", nullable=true) |
||
47 | */ |
||
48 | protected $description; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | * @ORM\Column(name="enabled", type="boolean") |
||
53 | */ |
||
54 | protected $enabled = false; |
||
55 | |||
56 | /** |
||
57 | * @var \DateTime |
||
58 | * |
||
59 | * @ORM\Column(name="created_at", type="datetime") |
||
60 | */ |
||
61 | protected $createdAt; |
||
62 | |||
63 | /** |
||
64 | * @var Category |
||
65 | */ |
||
66 | protected $parent; |
||
67 | |||
68 | /** |
||
69 | * @var Category[]|ArrayCollection |
||
70 | */ |
||
71 | protected $children; |
||
72 | |||
73 | /** |
||
74 | * @var Page[]|ArrayCollection |
||
75 | */ |
||
76 | protected $pages; |
||
77 | |||
78 | public function __toString() |
||
79 | { |
||
80 | return $this->name; |
||
81 | } |
||
82 | |||
83 | public function __construct() |
||
84 | { |
||
85 | $this->createdAt = new \DateTime(); |
||
86 | $this->children = new ArrayCollection(); |
||
87 | $this->pages = new ArrayCollection(); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getName() |
||
94 | { |
||
95 | return $this->name; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param string $name |
||
100 | * |
||
101 | * @return Category |
||
102 | */ |
||
103 | public function setName(string $name): Category |
||
104 | { |
||
105 | $this->name = $name; |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getDescription() |
||
114 | { |
||
115 | return $this->description; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $description |
||
|
|||
120 | * |
||
121 | * @return Category |
||
122 | */ |
||
123 | public function setDescription(string $description = null): Category |
||
124 | { |
||
125 | $this->description = $description; |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | public function getSlug() |
||
134 | { |
||
135 | return $this->slug; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param string $slug |
||
140 | * |
||
141 | * @return Category |
||
142 | */ |
||
143 | public function setSlug(string $slug = null): Category |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function isEnabled() |
||
154 | { |
||
155 | return $this->enabled; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param bool $enabled |
||
160 | * |
||
161 | * @return Category |
||
162 | */ |
||
163 | public function setEnabled(bool $enabled = null): Category |
||
169 | |||
170 | /** |
||
171 | * @return Category |
||
172 | */ |
||
173 | public function getParent() |
||
177 | |||
178 | /** |
||
179 | * @param Category|null $parent |
||
180 | * |
||
181 | * @return Category |
||
182 | */ |
||
183 | View Code Duplication | public function setParent(Category $parent = null): Category |
|
201 | |||
202 | /** |
||
203 | * @return \DateTime |
||
204 | */ |
||
205 | public function getCreatedAt(): \DateTime |
||
209 | |||
210 | /** |
||
211 | * @return Category[]|ArrayCollection |
||
212 | */ |
||
213 | public function getChildren() |
||
217 | |||
218 | /** |
||
219 | * @param Category $category |
||
220 | * |
||
221 | * @return Category |
||
222 | */ |
||
223 | public function addChild(Category $category): Category |
||
233 | |||
234 | /** |
||
235 | * @param Category $child |
||
236 | * |
||
237 | * @return Category |
||
238 | */ |
||
239 | public function removeChild(Category $child): Category |
||
245 | |||
246 | /** |
||
247 | * @param string $separator |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | View Code Duplication | public function getTree(string $separator = '/'): string |
|
263 | |||
264 | /** |
||
265 | * @ORM\PrePersist() |
||
266 | * @ORM\PreUpdate() |
||
267 | */ |
||
268 | public function updateSlug() |
||
274 | |||
275 | /** |
||
276 | * @ORM\PreRemove() |
||
277 | * |
||
278 | * @param LifecycleEventArgs $event |
||
279 | */ |
||
280 | View Code Duplication | public function onRemove(LifecycleEventArgs $event) |
|
294 | } |
||
295 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.