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 | 1 | { |
|
| 44 | /** |
||
| 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) |
||
| 95 | { |
||
| 96 | 1 | parent::__construct($label, $maxLength); |
|
| 97 | |||
| 98 | 1 | $this->phoneUtils = $phoneUtils; |
|
| 99 | 1 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $countries |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | * |
||
| 106 | * @throws Exceptions\NoValidCountryException |
||
| 107 | */ |
||
| 108 | public function setAllowedCountries(array $countries = []) |
||
| 109 | { |
||
| 110 | 1 | $this->allowedCountries = []; |
|
| 111 | |||
| 112 | 1 | foreach ($countries as $country) { |
|
| 113 | 1 | $country = $this->validateCountry($country); |
|
| 114 | 1 | $this->allowedCountries[] = strtoupper($country); |
|
| 115 | 1 | } |
|
| 116 | |||
| 117 | // Check for auto country detection |
||
| 118 | 1 | if (in_array('AUTO', $this->allowedCountries)) { |
|
| 119 | $this->allowedCountries = ['AUTO']; |
||
| 120 | } |
||
| 121 | |||
| 122 | // Remove duplicities |
||
| 123 | 1 | array_unique($this->allowedCountries); |
|
| 124 | |||
| 125 | 1 | return $this; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $country |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | * |
||
| 133 | 1 | * @throws Exceptions\NoValidCountryException |
|
| 134 | */ |
||
| 135 | public function addAllowedCountry($country) |
||
| 136 | { |
||
| 137 | 1 | $country = $this->validateCountry($country); |
|
| 138 | 1 | $this->allowedCountries[] = strtoupper($country); |
|
| 139 | |||
| 140 | // Remove duplicities |
||
| 141 | 1 | array_unique($this->allowedCountries); |
|
| 142 | |||
| 143 | 1 | if (strtoupper($country) === 'AUTO') { |
|
| 144 | $this->allowedCountries = ['AUTO']; |
||
| 145 | |||
| 146 | 1 | } else if (($key = array_search('AUTO', $this->allowedCountries)) && $key !== FALSE) { |
|
| 147 | unset($this->allowedCountries[$key]); |
||
| 148 | } |
||
| 149 | |||
| 150 | 1 | return $this; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getAllowedCountries() |
||
| 157 | { |
||
| 158 | 1 | if (in_array('AUTO', $this->allowedCountries, TRUE) || $this->allowedCountries === []) { |
|
| 159 | 1 | return $this->phoneUtils->getSupportedCountries(); |
|
| 160 | |||
| 161 | } else { |
||
| 162 | 1 | return $this->allowedCountries; |
|
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string|NULL $country |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | * |
||
| 171 | * @throws Exceptions\NoValidCountryException |
||
| 172 | */ |
||
| 173 | public function setDefaultCountry($country = NULL) |
||
| 174 | { |
||
| 175 | 1 | if ($country === NULL) { |
|
| 176 | 1 | $this->defaultCountry = NULL; |
|
| 177 | |||
| 178 | 1 | } else { |
|
| 179 | 1 | $country = $this->validateCountry($country); |
|
| 180 | |||
| 181 | 1 | $this->defaultCountry = strtoupper($country); |
|
| 182 | } |
||
| 183 | |||
| 184 | 1 | return $this; |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param array $types |
||
| 189 | 1 | * |
|
| 190 | 1 | * @return $this |
|
| 191 | * |
||
| 192 | * @throws Exceptions\NoValidTypeException |
||
| 193 | */ |
||
| 194 | public function setAllowedPhoneTypes(array $types = []) |
||
| 195 | { |
||
| 196 | $this->allowedTypes = []; |
||
| 197 | |||
| 198 | foreach ($types as $type) { |
||
| 199 | $type = $this->validateType($type); |
||
| 200 | $this->allowedTypes[] = strtoupper($type); |
||
| 201 | } |
||
| 202 | |||
| 203 | // Remove duplicities |
||
| 204 | array_unique($this->allowedTypes); |
||
| 205 | |||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $type |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | * |
||
| 214 | * @throws Exceptions\NoValidTypeException |
||
| 215 | */ |
||
| 216 | public function addAllowedPhoneType($type) |
||
| 217 | { |
||
| 218 | 1 | $type = $this->validateType($type); |
|
| 219 | 1 | $this->allowedTypes[] = strtoupper($type); |
|
| 220 | |||
| 221 | // Remove duplicities |
||
| 222 | 1 | array_unique($this->allowedTypes); |
|
| 223 | |||
| 224 | 1 | return $this; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getAllowedPhoneTypes() |
||
| 231 | { |
||
| 232 | 1 | return $this->allowedTypes; |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string |
||
| 237 | * |
||
| 238 | * @return $this |
||
| 239 | * |
||
| 240 | * @throws Exceptions\InvalidArgumentException |
||
| 241 | * @throws IPub\Phone\Exceptions\NoValidCountryException |
||
| 242 | * @throws IPub\Phone\Exceptions\NoValidPhoneException |
||
| 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 | * @return Utils\Html |
||
| 306 | 1 | */ |
|
| 307 | 1 | public function getControl() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $key |
||
| 315 | * |
||
| 316 | * @return Utils\Html |
||
| 317 | * |
||
| 318 | * @throws Exceptions\InvalidArgumentException |
||
| 319 | */ |
||
| 320 | public function getControlPart($key) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return NULL |
||
| 404 | */ |
||
| 405 | public function getLabelPart() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param string $country |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | * |
||
| 415 | * @throws Exceptions\NoValidCountryException |
||
| 416 | */ |
||
| 417 | protected function validateCountry($country) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $type |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | * |
||
| 435 | * @throws Exceptions\NoValidTypeException |
||
| 436 | */ |
||
| 437 | protected function validateType($type) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param PhoneUtils $phoneUtils |
||
| 452 | * @param string $method |
||
| 453 | */ |
||
| 454 | public static function register(PhoneUtils $phoneUtils, $method = 'addPhone') |
||
| 473 | } |
||
| 474 |
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.