Complex classes like MoipAbstract 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 MoipAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace SOSTheBlack\Moip; |
||
| 14 | abstract class MoipAbstract |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Api of MoIP |
||
| 18 | * |
||
| 19 | * @var \SOSTheBlack\Moip\Api |
||
| 20 | **/ |
||
| 21 | protected $api; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * data of configuration of the MoIP |
||
| 25 | * |
||
| 26 | * @var array table moip |
||
| 27 | **/ |
||
| 28 | protected $moip; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * undocumented class variable |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | **/ |
||
| 35 | protected $data; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * initialize() |
||
| 39 | * |
||
| 40 | * @return \SOSTheBlack\Moip\Api |
||
| 41 | */ |
||
| 42 | protected function initialize() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * setData() |
||
| 56 | * |
||
| 57 | * @param string[] $data |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | protected function setData($data) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * getValidate() |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | protected function getValidate() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * getUniqueId() |
||
| 77 | * |
||
| 78 | * adds unique id in the order |
||
| 79 | * |
||
| 80 | * @return string|false |
||
| 81 | */ |
||
| 82 | protected function getUniqueId() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * getReason() |
||
| 89 | * |
||
| 90 | * adds payment reason in the order |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | protected function getReason() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * getParams |
||
| 101 | * |
||
| 102 | * Validation of the params sent |
||
| 103 | * |
||
| 104 | * @param string $oneParam |
||
| 105 | * @param string $twoParam |
||
| 106 | * @param boolean $db |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | protected function getParams($oneParam, $twoParam = null, $db = false) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * billetConf |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | protected function billetConf() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * getBilletInstructions |
||
| 138 | * |
||
| 139 | * @return string[] |
||
| 140 | */ |
||
| 141 | private function getBilletInstructions() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * getPaymentWay |
||
| 163 | * |
||
| 164 | * Adds payment way in the order |
||
| 165 | * |
||
| 166 | * @return false|null |
||
| 167 | */ |
||
| 168 | protected function getPaymentWay() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * getMessage |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | protected function getMessage() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * getComission |
||
| 210 | * |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | protected function getComission() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * getParcel |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | protected function getParcel() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * responseValidation |
||
| 251 | * |
||
| 252 | * @param array $send |
||
| 253 | * @param array|string $answer |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | protected function responseValidation($send, $answer) |
||
| 266 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: