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 |
||
12 | class PaymentSetting implements CreatableFromArray |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $bankgiro; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $plusgiro; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $domesticBankAccount; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $internationalBankAccount; |
||
33 | |||
34 | /** |
||
35 | * @return mixed |
||
36 | */ |
||
37 | public function getBankgiro() |
||
41 | |||
42 | /** |
||
43 | * @param $bankgiro |
||
44 | * |
||
45 | * @return PaymentSetting |
||
46 | */ |
||
47 | public function withBankgiro($bankgiro) |
||
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | public function getPlusgiro(): string |
||
62 | |||
63 | /** |
||
64 | * @param string $plusgiro |
||
65 | * |
||
66 | * @return PaymentSetting |
||
67 | */ |
||
68 | public function withPlusgiro(string $plusgiro) |
||
75 | |||
76 | /** |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function getDomesticBankAccount() |
||
83 | |||
84 | /** |
||
85 | * @param $domesticBankAccount |
||
86 | * |
||
87 | * @return PaymentSetting |
||
88 | */ |
||
89 | public function withDomesticBankAccount($domesticBankAccount) |
||
96 | |||
97 | /** |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function getInternationalBankAccount() |
||
104 | |||
105 | /** |
||
106 | * @param $internationalBankAccount |
||
107 | * |
||
108 | * @return PaymentSetting |
||
109 | */ |
||
110 | public function withInternationalBankAccount($internationalBankAccount) |
||
117 | |||
118 | /** |
||
119 | * Create an API response object from the HTTP response from the API server. |
||
120 | * |
||
121 | * @param array $data |
||
122 | * |
||
123 | * @return self |
||
124 | */ |
||
125 | 2 | public static function createFromArray(array $data) |
|
135 | |||
136 | 1 | public function toArray() |
|
165 | } |
||
166 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.