Complex classes like Definition often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Definition, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Definition |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var RecordManager|RecordsTrait |
||
| 20 | */ |
||
| 21 | protected $manager; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $name = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $label = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $field; |
||
| 37 | |||
| 38 | protected $items = null; |
||
| 39 | |||
| 40 | protected $itemsDirectory = null; |
||
| 41 | |||
| 42 | protected $defaultValue = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param $name |
||
| 46 | * |
||
| 47 | * @return Property |
||
| 48 | * @throws Exception |
||
| 49 | */ |
||
| 50 | 7 | public function getItem($name) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @return null|Property[] |
||
| 64 | */ |
||
| 65 | 11 | public function getItems() |
|
| 73 | |||
| 74 | 11 | public function initItems() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | 11 | public function getItemsNames() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @return array|boolean |
||
| 98 | */ |
||
| 99 | 11 | protected function getItemsNamesFromManager() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | 15 | public function getName() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param mixed $name |
||
| 123 | */ |
||
| 124 | 15 | public function setName($name) |
|
| 128 | |||
| 129 | 15 | protected function initName() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @return mixed |
||
| 137 | */ |
||
| 138 | 15 | public function getField() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param mixed $field |
||
| 145 | */ |
||
| 146 | 15 | public function setField($field) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @return RecordManager |
||
| 153 | */ |
||
| 154 | 13 | public function getManager() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param RecordManager|RecordsTrait $manager |
||
| 161 | */ |
||
| 162 | 15 | public function setManager($manager) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 11 | protected function getItemsNamesFromFiles() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @return null|string |
||
| 186 | */ |
||
| 187 | 13 | public function getItemsDirectory() |
|
| 195 | |||
| 196 | 13 | public function initItemsDirectory() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | 13 | public function generateItemsDirectory() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 13 | protected function generateManagerDirectory() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 13 | protected function generatePropertyDirectory() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | 13 | public function getLabel() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $label |
||
| 246 | */ |
||
| 247 | 13 | public function setLabel($label) |
|
| 251 | |||
| 252 | 13 | protected function initLabel() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $name |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | 11 | public function isAbstractItemName($name) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $type |
||
| 277 | * |
||
| 278 | * @return Property |
||
| 279 | */ |
||
| 280 | 11 | public function newStatus($type = null) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @param null $type |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 11 | public function getItemClass($type = null) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | 2 | public function getDefaultValue() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param null $defaultValue |
||
| 317 | */ |
||
| 318 | 2 | public function setDefaultValue($defaultValue) |
|
| 322 | |||
| 323 | 2 | protected function initDefaultValue() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @return bool|string |
||
| 337 | */ |
||
| 338 | 2 | protected function getDefaultValueFromManager() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param $name |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | 7 | public function hasItem($name) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | 11 | protected function getPropertyItemsRootNamespace() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @param $name |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | 2 | public function getValues($name) |
|
| 394 | } |
||
| 395 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.