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 | 1 | { |
|
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, $label = NULL, $maxLength = NULL) |
||
101 | |||
102 | /** |
||
103 | * @param array $countries |
||
104 | * |
||
105 | * @return $this |
||
106 | * |
||
107 | * @throws Exceptions\NoValidCountryException |
||
108 | */ |
||
109 | public function setAllowedCountries(array $countries = []) |
||
110 | { |
||
111 | 1 | $this->allowedCountries = []; |
|
112 | |||
113 | 1 | foreach($countries as $country) |
|
114 | { |
||
115 | 1 | $country = $this->validateCountry($country); |
|
116 | 1 | $this->allowedCountries[] = strtoupper($country); |
|
117 | 1 | } |
|
118 | |||
119 | // Check for auto country detection |
||
120 | 1 | if (in_array('AUTO', $this->allowedCountries)) { |
|
121 | $this->allowedCountries = ['AUTO']; |
||
122 | } |
||
123 | |||
124 | // Remove duplicities |
||
125 | 1 | array_unique($this->allowedCountries); |
|
126 | |||
127 | 1 | return $this; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $country |
||
132 | * |
||
133 | 1 | * @return $this |
|
134 | * |
||
135 | * @throws Exceptions\NoValidCountryException |
||
136 | */ |
||
137 | public function addAllowedCountry($country) |
||
138 | { |
||
139 | 1 | $country = $this->validateCountry($country); |
|
140 | 1 | $this->allowedCountries[] = strtoupper($country); |
|
141 | |||
142 | // Remove duplicities |
||
143 | 1 | array_unique($this->allowedCountries); |
|
144 | |||
145 | 1 | if (strtoupper($country) === 'AUTO') { |
|
146 | $this->allowedCountries = ['AUTO']; |
||
147 | |||
148 | 1 | } else if (($key = array_search('AUTO', $this->allowedCountries)) && $key !== FALSE) { |
|
149 | unset($this->allowedCountries[$key]); |
||
150 | } |
||
151 | |||
152 | 1 | return $this; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * @return array |
||
157 | */ |
||
158 | public function getAllowedCountries() |
||
159 | { |
||
160 | 1 | if (in_array('AUTO', $this->allowedCountries, TRUE) || $this->allowedCountries === []) { |
|
161 | 1 | return $this->phoneUtils->getSupportedCountries(); |
|
162 | |||
163 | } else { |
||
164 | 1 | return $this->allowedCountries; |
|
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string|NULL $country |
||
170 | * |
||
171 | * @return $this |
||
172 | * |
||
173 | * @throws Exceptions\NoValidCountryException |
||
174 | */ |
||
175 | public function setDefaultCountry($country = NULL) |
||
188 | |||
189 | 1 | /** |
|
190 | 1 | * @param array $types |
|
191 | * |
||
192 | * @return $this |
||
193 | * |
||
194 | * @throws Exceptions\NoValidTypeException |
||
195 | */ |
||
196 | public function setAllowedPhoneTypes(array $types = []) |
||
197 | { |
||
198 | $this->allowedTypes = []; |
||
199 | |||
200 | foreach($types as $type) |
||
201 | { |
||
202 | $type = $this->validateType($type); |
||
203 | $this->allowedTypes[] = strtoupper($type); |
||
204 | } |
||
205 | |||
206 | // Remove duplicities |
||
207 | array_unique($this->allowedTypes); |
||
208 | |||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param string $type |
||
214 | * |
||
215 | * @return $this |
||
216 | * |
||
217 | * @throws Exceptions\NoValidTypeException |
||
218 | */ |
||
219 | public function addAllowedPhoneType($type) |
||
220 | { |
||
221 | 1 | $type = $this->validateType($type); |
|
222 | 1 | $this->allowedTypes[] = strtoupper($type); |
|
223 | |||
224 | // Remove duplicities |
||
225 | 1 | array_unique($this->allowedTypes); |
|
226 | |||
227 | 1 | return $this; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return array |
||
232 | */ |
||
233 | public function getAllowedPhoneTypes() |
||
234 | { |
||
235 | 1 | return $this->allowedTypes; |
|
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param string |
||
240 | * |
||
241 | * @return $this |
||
242 | * |
||
243 | * @throws Exceptions\InvalidArgumentException |
||
244 | */ |
||
245 | public function setValue($value) |
||
267 | |||
268 | /** |
||
269 | * @return IPub\Phone\Entities\Phone|NULL |
||
270 | */ |
||
271 | public function getValue() |
||
290 | |||
291 | /** |
||
292 | * Loads HTTP data |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | 1 | public function loadHttpData() |
|
304 | |||
305 | 1 | /** |
|
306 | 1 | * @return Utils\Html |
|
307 | 1 | */ |
|
308 | public function getControl() |
||
313 | |||
314 | /** |
||
315 | * @param string |
||
316 | * |
||
317 | 1 | * @return Utils\Html |
|
318 | * |
||
319 | 1 | * @throws Exceptions\InvalidArgumentException |
|
320 | */ |
||
321 | public function getControlPart($key) |
||
391 | |||
392 | /** |
||
393 | * @return NULL |
||
394 | */ |
||
395 | public function getLabelPart() |
||
399 | |||
400 | /** |
||
401 | 1 | * @param string $country |
|
402 | * |
||
403 | 1 | * @return string |
|
404 | 1 | * |
|
405 | * @throws Exceptions\NoValidCountryException |
||
406 | */ |
||
407 | 1 | protected function validateCountry($country) |
|
419 | |||
420 | /** |
||
421 | 1 | * @param string $type |
|
422 | * |
||
423 | 1 | * @return string |
|
424 | 1 | * |
|
425 | * @throws Exceptions\NoValidTypeException |
||
426 | */ |
||
427 | protected function validateType($type) |
||
439 | 1 | ||
440 | /** |
||
441 | * @param PhoneUtils $phoneUtils |
||
442 | 1 | * @param string $method |
|
443 | */ |
||
444 | 1 | public static function register(PhoneUtils $phoneUtils, $method = 'addPhone') |
|
463 | } |
||
464 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.