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 | 1 | ||
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 | 1 | ||
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) |
||
96 | 1 | { |
|
97 | parent::__construct($label, $maxLength); |
||
98 | 1 | ||
99 | 1 | $this->phoneUtils = $phoneUtils; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param array $countries |
||
104 | * |
||
105 | * @return $this |
||
106 | * |
||
107 | * @throws Exceptions\NoValidCountryException |
||
108 | */ |
||
109 | public function setAllowedCountries(array $countries = []) |
||
110 | 1 | { |
|
111 | $this->allowedCountries = []; |
||
112 | 1 | ||
113 | 1 | foreach ($countries as $country) { |
|
114 | 1 | $country = $this->validateCountry($country); |
|
115 | 1 | $this->allowedCountries[] = strtoupper($country); |
|
116 | } |
||
117 | |||
118 | 1 | // Check for auto country detection |
|
119 | if (in_array('AUTO', $this->allowedCountries)) { |
||
120 | $this->allowedCountries = ['AUTO']; |
||
121 | } |
||
122 | |||
123 | 1 | // Remove duplicities |
|
124 | array_unique($this->allowedCountries); |
||
125 | 1 | ||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string $country |
||
131 | * |
||
132 | * @return $this |
||
133 | 1 | * |
|
134 | * @throws Exceptions\NoValidCountryException |
||
135 | */ |
||
136 | public function addAllowedCountry(string $country) |
||
137 | 1 | { |
|
138 | 1 | $country = $this->validateCountry($country); |
|
139 | $this->allowedCountries[] = strtoupper($country); |
||
140 | |||
141 | 1 | // Remove duplicities |
|
142 | array_unique($this->allowedCountries); |
||
143 | 1 | ||
144 | if (strtoupper($country) === 'AUTO') { |
||
145 | $this->allowedCountries = ['AUTO']; |
||
146 | 1 | ||
147 | } elseif (($key = array_search('AUTO', $this->allowedCountries)) && $key !== FALSE) { |
||
148 | unset($this->allowedCountries[$key]); |
||
149 | } |
||
150 | 1 | ||
151 | return $this; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getAllowedCountries() : array |
||
158 | 1 | { |
|
159 | 1 | if (in_array('AUTO', $this->allowedCountries, TRUE) || $this->allowedCountries === []) { |
|
160 | return $this->phoneUtils->getSupportedCountries(); |
||
161 | } |
||
162 | 1 | ||
163 | return $this->allowedCountries; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param string|NULL $country |
||
168 | * |
||
169 | * @return $this |
||
170 | * |
||
171 | * @throws Exceptions\NoValidCountryException |
||
172 | */ |
||
173 | public function setDefaultCountry(string $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(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 | 1 | * @param string $key |
|
279 | * |
||
280 | 1 | * @return string |
|
281 | * |
||
282 | 1 | * @throws Exceptions\InvalidArgumentException |
|
283 | */ |
||
284 | public function getValuePart(string $key) : string |
||
295 | |||
296 | /** |
||
297 | 1 | * @return IPub\Phone\Entities\Phone|NULL|FALSE |
|
298 | 1 | */ |
|
299 | public function getValue() |
||
316 | |||
317 | /** |
||
318 | * Loads HTTP data |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | 1 | public function loadHttpData() |
|
330 | |||
331 | /** |
||
332 | * @return Utils\Html |
||
333 | */ |
||
334 | public function getControl() |
||
341 | 1 | ||
342 | 1 | /** |
|
343 | 1 | * @return Utils\Html |
|
344 | * |
||
345 | 1 | * @throws Exceptions\InvalidArgumentException |
|
346 | */ |
||
347 | 1 | public function getControlPart() |
|
439 | |||
440 | 1 | /** |
|
441 | * @return NULL |
||
442 | 1 | */ |
|
443 | 1 | public function getLabelPart() |
|
447 | |||
448 | /** |
||
449 | * @param string $country |
||
450 | * |
||
451 | * @return string |
||
452 | * |
||
453 | * @throws Exceptions\NoValidCountryException |
||
454 | */ |
||
455 | private function validateCountry(string $country) : string |
||
467 | 1 | ||
468 | /** |
||
469 | 1 | * @param string $type |
|
470 | * |
||
471 | 1 | * @return string |
|
472 | 1 | * |
|
473 | * @throws Exceptions\NoValidTypeException |
||
474 | 1 | */ |
|
475 | private function validateType(string $type) : string |
||
487 | |||
488 | /** |
||
489 | * @param PhoneUtils $phoneUtils |
||
490 | * @param string $method |
||
491 | */ |
||
492 | public static function register(PhoneUtils $phoneUtils, string $method = 'addPhone') |
||
511 | } |
||
512 |
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.