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 |
||
15 | class AssignmentModel extends BaseObject |
||
16 | { |
||
17 | /** |
||
18 | * @var IdentityInterface |
||
19 | */ |
||
20 | public $user; |
||
21 | |||
22 | /** |
||
23 | * @var int User id |
||
24 | */ |
||
25 | public $userId; |
||
26 | |||
27 | /** |
||
28 | * @var \yii\rbac\ManagerInterface |
||
29 | */ |
||
30 | protected $manager; |
||
31 | |||
32 | /** |
||
33 | * AssignmentModel constructor. |
||
34 | * |
||
35 | * @param IdentityInterface $user |
||
36 | * @param array $config |
||
37 | * |
||
38 | * @throws InvalidConfigException |
||
39 | */ |
||
40 | public function __construct(IdentityInterface $user, $config = []) |
||
52 | |||
53 | /** |
||
54 | * Assign a roles and permissions to the user. |
||
55 | * |
||
56 | * @param array $items |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | View Code Duplication | public function assign(array $items): bool |
|
70 | |||
71 | /** |
||
72 | * Revokes a roles and permissions from the user. |
||
73 | * |
||
74 | * @param array $items |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | View Code Duplication | public function revoke(array $items): bool |
|
88 | |||
89 | /** |
||
90 | * Get all available and assigned roles and permissions |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | public function getItems(): array |
||
119 | } |
||
120 |
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.