Complex classes like Phone 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 Phone, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Phone extends Forms\Controls\TextInput |
||
| 43 | { |
||
| 44 | 1 | /** |
|
| 45 | * Define filed attributes |
||
| 46 | */ |
||
| 47 | const FIELD_COUNTRY = 'country'; |
||
| 48 | const FIELD_NUMBER = 'number'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var IPub\Phone\Phone |
||
| 52 | */ |
||
| 53 | private $phoneUtils; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * List of allowed countries |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $allowedCountries = []; |
||
| 61 | |||
| 62 | 1 | /** |
|
| 63 | * List of allowed phone types |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $allowedTypes = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string|NULL |
||
| 71 | */ |
||
| 72 | private $number = NULL; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string|NULL |
||
| 76 | */ |
||
| 77 | private $country = NULL; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $defaultCountry; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private static $registered = FALSE; |
||
| 88 | |||
| 89 | 1 | /** |
|
| 90 | * @param PhoneUtils $phoneUtils |
||
| 91 | * @param string|NULL $label |
||
| 92 | * @param int|NULL $maxLength |
||
| 93 | */ |
||
| 94 | public function __construct(PhoneUtils $phoneUtils, $label = NULL, $maxLength = NULL) |
||
| 100 | 1 | ||
| 101 | /** |
||
| 102 | * @param array $countries |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | * |
||
| 106 | * @throws Exceptions\NoValidCountryException |
||
| 107 | */ |
||
| 108 | public function setAllowedCountries(array $countries = []) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $country |
||
| 131 | * |
||
| 132 | * @return $this |
||
| 133 | 1 | * |
|
| 134 | * @throws Exceptions\NoValidCountryException |
||
| 135 | */ |
||
| 136 | public function addAllowedCountry($country) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getAllowedCountries() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string|NULL $country |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | * |
||
| 172 | * @throws Exceptions\NoValidCountryException |
||
| 173 | */ |
||
| 174 | public function setDefaultCountry($country = NULL) |
||
| 187 | |||
| 188 | /** |
||
| 189 | 1 | * @param array $types |
|
| 190 | 1 | * |
|
| 191 | * @return $this |
||
| 192 | * |
||
| 193 | * @throws Exceptions\NoValidTypeException |
||
| 194 | */ |
||
| 195 | public function setAllowedPhoneTypes(array $types = []) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $type |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | * |
||
| 216 | * @throws Exceptions\NoValidTypeException |
||
| 217 | */ |
||
| 218 | public function addAllowedPhoneType($type) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function getAllowedPhoneTypes() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | * |
||
| 242 | * @throws Exceptions\InvalidArgumentException |
||
| 243 | */ |
||
| 244 | public function setValue($value) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return IPub\Phone\Entities\Phone|NULL |
||
| 269 | */ |
||
| 270 | public function getValue() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Loads HTTP data |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function loadHttpData() |
||
| 303 | |||
| 304 | /** |
||
| 305 | 1 | * @return Utils\Html |
|
| 306 | 1 | */ |
|
| 307 | 1 | public function getControl() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $key |
||
| 315 | * |
||
| 316 | * @return Utils\Html |
||
| 317 | 1 | * |
|
| 318 | * @throws Exceptions\InvalidArgumentException |
||
| 319 | 1 | */ |
|
| 320 | public function getControlPart($key) |
||
| 399 | |||
| 400 | /** |
||
| 401 | 1 | * @return NULL |
|
| 402 | */ |
||
| 403 | 1 | public function getLabelPart() |
|
| 407 | 1 | ||
| 408 | /** |
||
| 409 | * @param string $country |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | * |
||
| 413 | * @throws Exceptions\NoValidCountryException |
||
| 414 | */ |
||
| 415 | protected function validateCountry($country) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $type |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | * |
||
| 433 | * @throws Exceptions\NoValidTypeException |
||
| 434 | */ |
||
| 435 | protected function validateType($type) |
||
| 447 | 1 | ||
| 448 | 1 | /** |
|
| 449 | * @param PhoneUtils $phoneUtils |
||
| 450 | 1 | * @param string $method |
|
| 451 | */ |
||
| 452 | 1 | public static function register(PhoneUtils $phoneUtils, $method = 'addPhone') |
|
| 471 | } |
||
| 472 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.