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 |
||
21 | abstract class AbstractTariffManager extends BaseObject |
||
22 | { |
||
23 | /** |
||
24 | * @var int Parent tariff ID |
||
25 | */ |
||
26 | public $parent_id; |
||
27 | |||
28 | /** |
||
29 | * @var AbstractTariffForm |
||
30 | */ |
||
31 | public $form; |
||
32 | |||
33 | /** |
||
34 | * @var array options used to build [[form]] |
||
35 | * @see buildForm() |
||
36 | */ |
||
37 | public $formOptions = []; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | public $scenario; |
||
43 | |||
44 | /** |
||
45 | * @var Tariff The actual tariff |
||
46 | */ |
||
47 | protected $tariff; |
||
48 | |||
49 | /** |
||
50 | * @var Tariff Parent tariff |
||
51 | */ |
||
52 | protected $parentTariff; |
||
53 | |||
54 | /** |
||
55 | * @var string The type used to find parent tariffs |
||
56 | */ |
||
57 | protected $type; |
||
58 | |||
59 | public function init() |
||
68 | |||
69 | /** |
||
70 | * Fills [[form]] property with a proper [[AbstractTariffForm]] object. |
||
71 | */ |
||
72 | protected function buildForm() |
||
81 | |||
82 | protected function getFormOptions() |
||
86 | |||
87 | protected function initParentTariff() |
||
106 | |||
107 | /** |
||
108 | * Finds parent tariff ID. |
||
109 | * |
||
110 | * @return int |
||
111 | */ |
||
112 | protected function determineParentTariff() |
||
128 | |||
129 | public function getType() |
||
133 | |||
134 | /** |
||
135 | * @param Tariff $tariff |
||
136 | */ |
||
137 | public function setTariff($tariff) |
||
141 | |||
142 | View Code Duplication | public function insert(): bool |
|
156 | |||
157 | View Code Duplication | public function update(): bool |
|
169 | } |
||
170 |
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.