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 |
||
43 | 1 | class Phone extends Forms\Controls\TextInput |
|
44 | { |
||
45 | /** |
||
46 | * Define filed attributes |
||
47 | */ |
||
48 | const FIELD_COUNTRY = 'country'; |
||
49 | const FIELD_NUMBER = 'number'; |
||
50 | |||
51 | /** |
||
52 | * @var IPub\Phone\Phone |
||
53 | */ |
||
54 | private $phoneUtils; |
||
55 | |||
56 | /** |
||
57 | * List of allowed countries |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | private $allowedCountries = []; |
||
62 | |||
63 | /** |
||
64 | * List of allowed phone types |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | private $allowedTypes = []; |
||
69 | |||
70 | /** |
||
71 | * @var string|NULL |
||
72 | */ |
||
73 | private $number = NULL; |
||
74 | |||
75 | /** |
||
76 | * @var string|NULL |
||
77 | */ |
||
78 | private $country = NULL; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | private $defaultCountry; |
||
84 | |||
85 | /** |
||
86 | * @var bool |
||
87 | */ |
||
88 | private static $registered = FALSE; |
||
89 | |||
90 | /** |
||
91 | * @param PhoneUtils $phoneUtils |
||
92 | * @param string|NULL $label |
||
93 | * @param int|NULL $maxLength |
||
94 | */ |
||
95 | public function __construct(PhoneUtils $phoneUtils, string $label = NULL, int $maxLength = NULL) |
||
101 | |||
102 | /** |
||
103 | * @param array $countries |
||
104 | * |
||
105 | * @return $this |
||
106 | * |
||
107 | * @throws Exceptions\NoValidCountryException |
||
108 | */ |
||
109 | public function setAllowedCountries(array $countries = []) |
||
128 | |||
129 | /** |
||
130 | * @param string $country |
||
131 | * |
||
132 | * @return $this |
||
133 | * |
||
134 | * @throws Exceptions\NoValidCountryException |
||
135 | */ |
||
136 | public function addAllowedCountry(string $country) |
||
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getAllowedCountries() : array |
||
165 | |||
166 | /** |
||
167 | * @param string|NULL $country |
||
168 | * |
||
169 | * @return $this |
||
170 | * |
||
171 | * @throws Exceptions\NoValidCountryException |
||
172 | */ |
||
173 | public function setDefaultCountry(string $country = NULL) |
||
186 | |||
187 | /** |
||
188 | * @param array $types |
||
189 | * |
||
190 | * @return $this |
||
191 | * |
||
192 | * @throws Exceptions\NoValidTypeException |
||
193 | */ |
||
194 | public function setAllowedPhoneTypes(array $types = []) |
||
208 | |||
209 | /** |
||
210 | * @param string $type |
||
211 | * |
||
212 | * @return $this |
||
213 | * |
||
214 | * @throws Exceptions\NoValidTypeException |
||
215 | */ |
||
216 | public function addAllowedPhoneType(string $type) |
||
226 | |||
227 | /** |
||
228 | * @return array |
||
229 | */ |
||
230 | public function getAllowedPhoneTypes() : array |
||
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) |
||
276 | |||
277 | /** |
||
278 | * @param string $key |
||
279 | * |
||
280 | * @return string |
||
281 | * |
||
282 | * @throws Exceptions\InvalidArgumentException |
||
283 | */ |
||
284 | public function getValuePart(string $key) : string |
||
295 | |||
296 | /** |
||
297 | * @return IPub\Phone\Entities\Phone|NULL|FALSE |
||
298 | */ |
||
299 | public function getValue() |
||
316 | |||
317 | /** |
||
318 | * Loads HTTP data |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | public function loadHttpData() |
||
330 | |||
331 | /** |
||
332 | * @return Utils\Html |
||
333 | */ |
||
334 | public function getControl() |
||
341 | |||
342 | /** |
||
343 | * @return Utils\Html |
||
344 | * |
||
345 | * @throws Exceptions\InvalidArgumentException |
||
346 | */ |
||
347 | public function getControlPart() |
||
441 | |||
442 | /** |
||
443 | * {@inheritdoc} |
||
444 | */ |
||
445 | public function getLabel($caption = NULL) |
||
452 | |||
453 | /** |
||
454 | * {@inheritdoc} |
||
455 | */ |
||
456 | public function getLabelPart() |
||
460 | |||
461 | /** |
||
462 | * @param string $country |
||
463 | * |
||
464 | * @return string |
||
465 | * |
||
466 | * @throws Exceptions\NoValidCountryException |
||
467 | */ |
||
468 | private function validateCountry(string $country) : string |
||
480 | |||
481 | /** |
||
482 | * @param string $type |
||
483 | * |
||
484 | * @return string |
||
485 | * |
||
486 | * @throws Exceptions\NoValidTypeException |
||
487 | */ |
||
488 | private function validateType(string $type) : string |
||
500 | |||
501 | /** |
||
502 | * @param PhoneUtils $phoneUtils |
||
503 | * @param string $method |
||
504 | */ |
||
505 | public static function register(PhoneUtils $phoneUtils, string $method = 'addPhone') |
||
524 | } |
||
525 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.