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 namespace Comodojo\RpcServer\Request; |
||
26 | class XmlProcessor { |
||
27 | |||
28 | /** |
||
29 | * Requested RPC method |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $method; |
||
34 | |||
35 | /** |
||
36 | * A parameters object |
||
37 | * |
||
38 | * @var \Comodojo\RpcServer\Request\Parameters |
||
39 | */ |
||
40 | private $parameters = null; |
||
41 | |||
42 | /** |
||
43 | * Selected method |
||
44 | * |
||
45 | * @var \Comodojo\RpcServer\RpcMethod |
||
46 | */ |
||
47 | private $registered_method = null; |
||
48 | |||
49 | /** |
||
50 | * Selected signature |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | private $selected_signature = null; |
||
55 | |||
56 | /** |
||
57 | * Current logger |
||
58 | * |
||
59 | * @var \Psr\Log\LoggerInterface |
||
60 | */ |
||
61 | private $logger = null; |
||
62 | |||
63 | /** |
||
64 | * Class constructor |
||
65 | * |
||
66 | * @param array $payload |
||
67 | * @param \Comodojo\RpcServer\Request\Parameters $parameters |
||
68 | * @param \Psr\Log\LoggerInterface $logger |
||
69 | */ |
||
70 | 36 | public function __construct($payload, Parameters $parameters, \Psr\Log\LoggerInterface $logger) { |
|
110 | |||
111 | /** |
||
112 | * Run the processor and exec callback(s) |
||
113 | * |
||
114 | * @return mixed |
||
115 | * @throws Exception |
||
116 | */ |
||
117 | 33 | public function run() { |
|
166 | |||
167 | /** |
||
168 | * Static constructor - start processor |
||
169 | * |
||
170 | * @param array $payload |
||
171 | * @param \Comodojo\RpcServer\Request\Parameters $parameters |
||
172 | * @param \Psr\Log\LoggerInterface $logger |
||
173 | * |
||
174 | * @return mixed |
||
175 | * @throws \Comodojo\Exception\RpcException |
||
176 | * @throws Exception |
||
177 | */ |
||
178 | 36 | View Code Duplication | public static function process($payload, Parameters $parameters, \Psr\Log\LoggerInterface $logger) { |
199 | |||
200 | /** |
||
201 | * Check if a request is sustainable (i.e. if method is registered) |
||
202 | * |
||
203 | * @return \Comodojo\RpcServer\RpcMethod |
||
204 | * @throws \Comodojo\Exception\RpcException |
||
205 | */ |
||
206 | 36 | View Code Duplication | private function checkRequestSustainability() { |
215 | |||
216 | /** |
||
217 | * Check if a request is consistent (i.e. if it matches one of method's signatures) |
||
218 | * |
||
219 | * @param array $provided_parameters |
||
220 | * |
||
221 | * @return int |
||
222 | * @throws \Comodojo\Exception\RpcException |
||
223 | */ |
||
224 | 33 | private function checkRequestConsistence($provided_parameters) { |
|
261 | |||
262 | /** |
||
263 | * Create an associative array of $name => $parameter from current signature |
||
264 | * |
||
265 | * @param array $provided |
||
266 | * @param \Comodojo\RpcServer\RpcMethod $method |
||
267 | * @param integer $selected_signature |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | 33 | View Code Duplication | private static function matchParameters($provided, $method, $selected_signature) { |
288 | |||
289 | /** |
||
290 | * Preprocess a single xml request |
||
291 | * |
||
292 | * @param array $payload |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | 36 | private static function preprocessRequest($payload) { |
|
301 | |||
302 | } |
||
303 |
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.