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 |
||
24 | abstract class BackgroundTaskBase |
||
25 | { |
||
26 | /** @var JobQueue */ |
||
27 | private $job; |
||
28 | /** @var PdoDatabase */ |
||
29 | private $database; |
||
30 | /** @var IOAuthProtocolHelper */ |
||
31 | private $oauthProtocolHelper; |
||
32 | /** @var SiteConfiguration */ |
||
33 | private $siteConfiguration; |
||
34 | /** @var IEmailHelper */ |
||
35 | private $emailHelper; |
||
36 | /** @var HttpHelper */ |
||
37 | private $httpHelper; |
||
38 | /** @var IrcNotificationHelper */ |
||
39 | private $notificationHelper; |
||
40 | /** @var User */ |
||
41 | private $triggerUser; |
||
42 | /** @var Request */ |
||
43 | private $request; |
||
44 | /** @var EmailTemplate */ |
||
45 | private $emailTemplate = null; |
||
46 | /** @var mixed */ |
||
47 | private $parameters; |
||
48 | |||
49 | /** |
||
50 | * @return JobQueue |
||
51 | */ |
||
52 | public function getJob() |
||
56 | |||
57 | /** |
||
58 | * @param JobQueue $job |
||
59 | */ |
||
60 | public function setJob(JobQueue $job) |
||
64 | |||
65 | /** |
||
66 | * @return PdoDatabase |
||
67 | */ |
||
68 | public function getDatabase() |
||
72 | |||
73 | /** |
||
74 | * @param PdoDatabase $database |
||
75 | */ |
||
76 | public function setDatabase(PdoDatabase $database) |
||
80 | |||
81 | /** |
||
82 | * @return IOAuthProtocolHelper |
||
83 | */ |
||
84 | public function getOauthProtocolHelper() |
||
88 | |||
89 | /** |
||
90 | * @param IOAuthProtocolHelper $oauthProtocolHelper |
||
91 | */ |
||
92 | public function setOauthProtocolHelper(IOAuthProtocolHelper $oauthProtocolHelper) |
||
96 | |||
97 | /** |
||
98 | * @return SiteConfiguration |
||
99 | */ |
||
100 | public function getSiteConfiguration() |
||
104 | |||
105 | /** |
||
106 | * @param SiteConfiguration $siteConfiguration |
||
107 | */ |
||
108 | public function setSiteConfiguration(SiteConfiguration $siteConfiguration) |
||
112 | |||
113 | /** |
||
114 | * @return HttpHelper |
||
115 | */ |
||
116 | public function getHttpHelper() |
||
120 | |||
121 | /** |
||
122 | * @param HttpHelper $httpHelper |
||
123 | */ |
||
124 | public function setHttpHelper(HttpHelper $httpHelper) |
||
128 | |||
129 | /** |
||
130 | * @return IEmailHelper |
||
131 | */ |
||
132 | public function getEmailHelper() |
||
136 | |||
137 | /** |
||
138 | * @param IEmailHelper $emailHelper |
||
139 | */ |
||
140 | public function setEmailHelper(IEmailHelper $emailHelper) |
||
144 | |||
145 | /** |
||
146 | * @return IrcNotificationHelper |
||
147 | */ |
||
148 | public function getNotificationHelper() |
||
152 | |||
153 | /** |
||
154 | * @param IrcNotificationHelper $notificationHelper |
||
155 | */ |
||
156 | public function setNotificationHelper($notificationHelper) |
||
160 | |||
161 | /** |
||
162 | * @return void |
||
163 | */ |
||
164 | protected abstract function execute(); |
||
165 | |||
166 | public function run() |
||
225 | |||
226 | View Code Duplication | protected function markComplete() |
|
235 | |||
236 | View Code Duplication | protected function markCancelled($reason = null) |
|
245 | |||
246 | View Code Duplication | protected function markFailed($reason = null) |
|
255 | |||
256 | /** |
||
257 | * @return User |
||
258 | */ |
||
259 | public function getTriggerUser() |
||
263 | |||
264 | /** |
||
265 | * @return Request |
||
266 | */ |
||
267 | public function getRequest() |
||
271 | |||
272 | /** |
||
273 | * @return EmailTemplate |
||
274 | */ |
||
275 | public function getEmailTemplate() |
||
279 | |||
280 | /** |
||
281 | * @return mixed |
||
282 | */ |
||
283 | public function getParameters() |
||
287 | } |
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.