Complex classes like Unsc 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 Unsc, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Unsc implements ListInterface |
||
| 28 | { |
||
| 29 | protected $path_source; |
||
| 30 | protected $sanction_file_content; |
||
| 31 | protected $entities; |
||
| 32 | protected $date; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Process the list (read, process, set the date and verify entities). |
||
| 36 | * |
||
| 37 | * Example |
||
| 38 | * |
||
| 39 | * if ($this->readEntities()) { |
||
| 40 | * $this->processEntities(); |
||
| 41 | * $this->setDate(); |
||
| 42 | * return $this->verifyEntities(); |
||
| 43 | * } |
||
| 44 | */ |
||
| 45 | 6 | public function run() |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Set source path, the file path of the list. |
||
| 57 | * this method will be called by the Processor. |
||
| 58 | * |
||
| 59 | * Suggestion: add a property to the class |
||
| 60 | * |
||
| 61 | * @param string $path |
||
| 62 | */ |
||
| 63 | 6 | public function setSourcePath($path) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Read the content from the file list. |
||
| 70 | * |
||
| 71 | * Suggestion: add a property to the class to save the content |
||
| 72 | * |
||
| 73 | * @TODO change the name to setFileContent; |
||
| 74 | */ |
||
| 75 | 6 | public function readEntities() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Process entities. It analyses the entities and it parses to classes. |
||
| 84 | */ |
||
| 85 | 6 | public function processEntities() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Parse the individuals. |
||
| 102 | * |
||
| 103 | * @param \SimpleXMLElement $node |
||
| 104 | * |
||
| 105 | * @return Entity |
||
| 106 | */ |
||
| 107 | 6 | protected function parseIndividuals($node) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Parse the entities. |
||
| 129 | * |
||
| 130 | * @param \SimpleXMLElement $node |
||
| 131 | * |
||
| 132 | * @return Entity Entity |
||
| 133 | */ |
||
| 134 | 6 | protected function parseEntitiesAndOtherGroups($node) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Parse the alias of a node. |
||
| 158 | * |
||
| 159 | * @param array $alias |
||
| 160 | * |
||
| 161 | * @return array list of akas |
||
| 162 | */ |
||
| 163 | 6 | protected function parseAkas($alias) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Parse the programs. |
||
| 186 | * |
||
| 187 | * @param $listTypes |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | 6 | protected function parsePrograms($listTypes) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Parses the addresses of a node. |
||
| 207 | * |
||
| 208 | * @param \SimpleXMLElement $addresses array |
||
| 209 | * |
||
| 210 | * @return array list of addresses |
||
| 211 | */ |
||
| 212 | 6 | protected function parseAddresses($addresses) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Parse the date of births of a node. |
||
| 236 | * |
||
| 237 | * @param \SimpleXMLElement $dobs |
||
| 238 | * |
||
| 239 | * @return array list of date of birth |
||
| 240 | */ |
||
| 241 | 6 | protected function parseDateOfBirth($dobs) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Parse place of birth. |
||
| 278 | * |
||
| 279 | * @param \SimpleXMLElement $places_of_birth |
||
| 280 | * |
||
| 281 | * @return array list of place of birth |
||
| 282 | */ |
||
| 283 | 6 | protected function parsePlaceOfBirth($places_of_birth) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Parse individual documents. |
||
| 303 | * |
||
| 304 | * @param \SimpleXMLElement $documents |
||
| 305 | * |
||
| 306 | * @return array list of the individual documents |
||
| 307 | */ |
||
| 308 | 6 | protected function parseIds($documents) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Parse nationalities. |
||
| 331 | * |
||
| 332 | * @param \SimpleXMLElement $node |
||
| 333 | * |
||
| 334 | * @return array list of nationalities |
||
| 335 | */ |
||
| 336 | protected function parseNationalities($node) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Parse designations. |
||
| 351 | * |
||
| 352 | * @param $subNodes array |
||
| 353 | * @param string $delimeter |
||
| 354 | * |
||
| 355 | * @return string with all designations |
||
| 356 | */ |
||
| 357 | 6 | protected function parseDesignations($subNodes, $delimeter = ';') |
|
| 366 | |||
| 367 | /** |
||
| 368 | * just a simple function to add to a string. |
||
| 369 | * |
||
| 370 | * @param string|null $str |
||
| 371 | * @param string $label |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | 6 | protected function addToRemarks($str, $label) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * @param \SimpleXMLElement $array |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | * @TODO improve it |
||
| 385 | */ |
||
| 386 | 6 | protected function areElements($array) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Combine an array in a string seperate by an space |
||
| 393 | * At the end it replaces multiple spaces with a single space. |
||
| 394 | * |
||
| 395 | * @param $array array |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 6 | protected function combine($array) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * It is just a helper to extract the first element from |
||
| 406 | * an array and transform it to string. |
||
| 407 | * |
||
| 408 | * @param $array |
||
| 409 | * |
||
| 410 | * @return null|string |
||
| 411 | */ |
||
| 412 | 6 | protected function g($array) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Verify the entities, for example the content or |
||
| 419 | * if the amount of entities match with amount from the file if it is provided. |
||
| 420 | */ |
||
| 421 | 6 | public function verifyEntities() |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Get entities |
||
| 428 | * return the entities created. |
||
| 429 | */ |
||
| 430 | 6 | public function getEntities() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Set the date when the list was updated, it takes the info from the content of the file if exists. |
||
| 437 | * |
||
| 438 | * @TODO add an attribute to send the format of the date |
||
| 439 | */ |
||
| 440 | 6 | public function setDate() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Get the date of the list. When it was updated. |
||
| 451 | * It must be given in Y-m-d format. |
||
| 452 | */ |
||
| 453 | public function getDate() |
||
| 457 | } |
||
| 458 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: