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 |
||
| 9 | class IniSection |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $name; |
||
| 15 | /** |
||
| 16 | * @var IniSection|null |
||
| 17 | */ |
||
| 18 | protected $parent; |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $contents = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Section constructor. |
||
| 26 | * |
||
| 27 | * @param string $name |
||
| 28 | * @param IniSection $parent |
||
| 29 | */ |
||
| 30 | 29 | public function __construct($name, IniSection $parent = null) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @param IniSection $parent |
||
| 38 | * |
||
| 39 | * @return $this |
||
| 40 | */ |
||
| 41 | 10 | public function setParent(IniSection $parent) |
|
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * @param array $data |
||
| 51 | * |
||
| 52 | * @return $this |
||
| 53 | * @throws InvalidDataException |
||
| 54 | */ |
||
| 55 | 23 | public function setContents($data) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | 18 | protected function getContents() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | 19 | public function hasParent() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @return IniSection|null |
||
| 85 | */ |
||
| 86 | 10 | public function getParent() |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 2 | public function getName() |
|
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * @return IniSection[] |
||
| 102 | */ |
||
| 103 | 18 | protected function getParents() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | 18 | protected function composeContents() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get normalized item value |
||
| 136 | * |
||
| 137 | * @param string $itemName |
||
| 138 | * @param mixed $defaultValue |
||
| 139 | * |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | 16 | public function get($itemName, $defaultValue = null) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $itemName |
||
| 152 | * @param string|array|bool|null $itemValue |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | * @throws InvalidDataException |
||
| 156 | */ |
||
| 157 | 6 | View Code Duplication | public function set($itemName, $itemValue) |
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $itemName |
||
| 171 | * |
||
| 172 | * @return $this |
||
| 173 | * @throws InvalidDataException |
||
| 174 | */ |
||
| 175 | 4 | View Code Duplication | public function delete($itemName) |
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $itemName |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | 7 | public function hasItem($itemName) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | 2 | public function toArray() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | 1 | public function toString() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return string[] |
||
| 223 | */ |
||
| 224 | 1 | protected function renderName() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $name |
||
| 241 | * @param string|array $value |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | 1 | protected function renderItem($name, $value) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $name |
||
| 261 | * @param string $value |
||
| 262 | * |
||
| 263 | * @return string[] |
||
| 264 | */ |
||
| 265 | 1 | protected function renderStringItem($name, $value) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $name |
||
| 272 | * @param array $values |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | protected function renderArrayItem($name, array $values) |
||
| 288 | |||
| 289 | } |
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.