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 |
||
13 | class DoormanQueuedJobTask implements Task, Expires, Process, Cancellable |
||
14 | { |
||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $id; |
||
19 | |||
20 | /** |
||
21 | * @var QueuedJobDescriptor |
||
22 | */ |
||
23 | protected $descriptor; |
||
24 | |||
25 | /** |
||
26 | * Reload descriptor from DB |
||
27 | */ |
||
28 | protected function refreshDescriptor() |
||
34 | |||
35 | /** |
||
36 | * @inheritdoc |
||
37 | * |
||
38 | * @return null|int |
||
39 | */ |
||
40 | public function getId() |
||
44 | |||
45 | /** |
||
46 | * @inheritdoc |
||
47 | * |
||
48 | * @param int $id |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function setId($id) |
||
58 | |||
59 | /** |
||
60 | * @return QueuedJobDescriptor |
||
61 | */ |
||
62 | public function getDescriptor() |
||
66 | |||
67 | /** |
||
68 | * @param QueuedJobDescriptor $descriptor |
||
69 | */ |
||
70 | public function __construct(QueuedJobDescriptor $descriptor) |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | public function serialize() |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | * |
||
90 | * @throws InvalidArgumentException |
||
91 | * @param string |
||
92 | */ |
||
93 | public function unserialize($serialized) |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getHandler() |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getData() |
||
129 | |||
130 | /** |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function ignoresRules() |
||
141 | |||
142 | /** |
||
143 | * @return bool |
||
144 | */ |
||
145 | public function stopsSiblings() |
||
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | * |
||
157 | * @return int |
||
158 | */ |
||
159 | public function getExpiresIn() |
||
167 | |||
168 | /** |
||
169 | * @inheritdoc |
||
170 | * |
||
171 | * @param int $startedAt |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function shouldExpire($startedAt) |
||
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | View Code Duplication | public function canRunTask() |
|
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | View Code Duplication | public function isCancelled() |
|
218 | } |
||
219 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.