Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | 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() |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function getLabel($caption = NULL) |
||
450 | |||
451 | /** |
||
452 | * {@inheritdoc} |
||
453 | */ |
||
454 | public function getLabelPart() |
||
458 | |||
459 | /** |
||
460 | * @param string $country |
||
461 | * |
||
462 | * @return string |
||
463 | * |
||
464 | * @throws Exceptions\NoValidCountryException |
||
465 | */ |
||
466 | private function validateCountry(string $country) : string |
||
478 | |||
479 | /** |
||
480 | * @param string $type |
||
481 | * |
||
482 | * @return string |
||
483 | * |
||
484 | * @throws Exceptions\NoValidTypeException |
||
485 | */ |
||
486 | private function validateType(string $type) : string |
||
498 | |||
499 | /** |
||
500 | * @param PhoneUtils $phoneUtils |
||
501 | * @param string $method |
||
502 | */ |
||
503 | View Code Duplication | public static function register(PhoneUtils $phoneUtils, string $method = 'addPhone') |
|
522 | } |
||
523 |
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.