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 |
||
8 | class CallbackViewFactory implements ViewFactory { |
||
9 | /** @var callable */ |
||
10 | private $callback; |
||
11 | /** @var null */ |
||
12 | private $baseDir; |
||
13 | /** @var array */ |
||
14 | private $vars; |
||
15 | |||
16 | /** |
||
17 | * @param callable $callback |
||
18 | * @param string $baseDir |
||
19 | * @param array $vars |
||
20 | */ |
||
21 | public function __construct($callback, $baseDir = null, array $vars = []) { |
||
26 | |||
27 | /** |
||
28 | * @param string $baseDir |
||
29 | * @param array $vars |
||
30 | * @return Renderer |
||
31 | */ |
||
32 | View Code Duplication | public function create($baseDir = null, array $vars = []) { |
|
40 | |||
41 | /** |
||
42 | * @param string $baseDir |
||
43 | * @param array $vars |
||
44 | * @return $this |
||
45 | */ |
||
46 | View Code Duplication | public function deriveFactory($baseDir = null, array $vars = []) { |
|
54 | } |
||
55 |
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.