Complex classes like ShortNumberInfo 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 ShortNumberInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ShortNumberInfo |
||
15 | { |
||
16 | const META_DATA_FILE_PREFIX = 'ShortNumberMetadata'; |
||
17 | /** |
||
18 | * @var ShortNumberInfo |
||
19 | */ |
||
20 | protected static $instance = null; |
||
21 | /** |
||
22 | * @var MatcherAPIInterface |
||
23 | */ |
||
24 | protected $matcherAPI; |
||
25 | protected $currentFilePrefix; |
||
26 | protected $regionToMetadataMap = array(); |
||
27 | protected $countryCallingCodeToRegionCodeMap = array(); |
||
28 | protected $countryCodeToNonGeographicalMetadataMap = array(); |
||
29 | protected static $regionsWhereEmergencyNumbersMustBeExact = array( |
||
30 | 'BR', |
||
31 | 'CL', |
||
32 | 'NI', |
||
33 | ); |
||
34 | |||
35 | 30 | protected function __construct(MatcherAPIInterface $matcherAPI) |
|
36 | { |
||
37 | 30 | $this->matcherAPI = $matcherAPI; |
|
38 | |||
39 | // TODO: Create ShortNumberInfo for a given map |
||
40 | 30 | $this->countryCallingCodeToRegionCodeMap = CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap; |
|
41 | |||
42 | 30 | $this->currentFilePrefix = dirname(__FILE__) . '/data/' . static::META_DATA_FILE_PREFIX; |
|
43 | |||
44 | // Initialise PhoneNumberUtil to make sure regex's are setup correctly |
||
45 | 30 | PhoneNumberUtil::getInstance(); |
|
46 | 30 | } |
|
47 | |||
48 | /** |
||
49 | * Returns the singleton instance of ShortNumberInfo |
||
50 | * |
||
51 | * @return \libphonenumber\ShortNumberInfo |
||
52 | */ |
||
53 | 5621 | public static function getInstance() |
|
61 | |||
62 | 29 | public static function resetInstance() |
|
66 | |||
67 | /** |
||
68 | * Returns a list with teh region codes that match the specific country calling code. For |
||
69 | * non-geographical country calling codes, the region code 001 is returned. Also, in the case |
||
70 | * of no region code being found, an empty list is returned. |
||
71 | * |
||
72 | * @param int $countryCallingCode |
||
73 | * @return array |
||
74 | */ |
||
75 | 734 | protected function getRegionCodesForCountryCode($countryCallingCode) |
|
85 | |||
86 | /** |
||
87 | * Helper method to check that the country calling code of the number matches the region it's |
||
88 | * being dialed from. |
||
89 | * @param PhoneNumber $number |
||
90 | * @param string $regionDialingFrom |
||
91 | * @return bool |
||
92 | */ |
||
93 | 733 | protected function regionDialingFromMatchesNumber(PhoneNumber $number, $regionDialingFrom) |
|
99 | |||
100 | public function getSupportedRegions() |
||
104 | |||
105 | /** |
||
106 | * Gets a valid short number for the specified region. |
||
107 | * |
||
108 | * @param $regionCode String the region for which an example short number is needed |
||
109 | * @return string a valid short number for the specified region. Returns an empty string when the |
||
110 | * metadata does not contain such information. |
||
111 | */ |
||
112 | 240 | public function getExampleShortNumber($regionCode) |
|
126 | |||
127 | /** |
||
128 | * @param $regionCode |
||
129 | * @return PhoneMetadata|null |
||
130 | */ |
||
131 | 2182 | public function getMetadataForRegion($regionCode) |
|
145 | |||
146 | 261 | protected function loadMetadataFromFile($filePrefix, $regionCode, $countryCallingCode) |
|
147 | { |
||
148 | 261 | $isNonGeoRegion = PhoneNumberUtil::REGION_CODE_FOR_NON_GEO_ENTITY === $regionCode; |
|
149 | 261 | $fileName = $filePrefix . '_' . ($isNonGeoRegion ? $countryCallingCode : $regionCode) . '.php'; |
|
150 | 261 | if (!is_readable($fileName)) { |
|
151 | throw new \Exception('missing metadata: ' . $fileName); |
||
152 | } |
||
153 | |||
154 | 261 | $metadataLoader = new DefaultMetadataLoader(); |
|
155 | 261 | $data = $metadataLoader->loadMetadata($fileName); |
|
156 | |||
157 | 261 | $metadata = new PhoneMetadata(); |
|
158 | 261 | $metadata->fromArray($data); |
|
159 | 261 | if ($isNonGeoRegion) { |
|
160 | $this->countryCodeToNonGeographicalMetadataMap[$countryCallingCode] = $metadata; |
||
161 | } else { |
||
162 | 261 | $this->regionToMetadataMap[$regionCode] = $metadata; |
|
163 | } |
||
164 | 261 | } |
|
165 | |||
166 | /** |
||
167 | * Gets a valid short number for the specified cost category. |
||
168 | * |
||
169 | * @param string $regionCode the region for which an example short number is needed |
||
170 | * @param int $cost the cost category of number that is needed |
||
171 | * @return string a valid short number for the specified region and cost category. Returns an empty string |
||
172 | * when the metadata does not contain such information, or the cost is UNKNOWN_COST. |
||
173 | */ |
||
174 | 958 | public function getExampleShortNumberForCost($regionCode, $cost) |
|
204 | |||
205 | /** |
||
206 | * Returns true if the given number, exactly as dialed, might be used to connect to an emergency |
||
207 | * service in the given region. |
||
208 | * <p> |
||
209 | * This method accepts a string, rather than a PhoneNumber, because it needs to distinguish |
||
210 | * cases such as "+1 911" and "911", where the former may not connect to an emergency service in |
||
211 | * all cases but the latter would. This method takes into account cases where the number might |
||
212 | * contain formatting, or might have additional digits appended (when it is okay to do that in |
||
213 | * the specified region). |
||
214 | * |
||
215 | * @param string $number the phone number to test |
||
216 | * @param string $regionCode the region where the phone number if being dialled |
||
217 | * @return boolean whether the number might be used to connect to an emergency service in the given region |
||
218 | */ |
||
219 | 10 | public function connectsToEmergencyNumber($number, $regionCode) |
|
223 | |||
224 | /** |
||
225 | * @param string $number |
||
226 | * @param string $regionCode |
||
227 | * @param bool $allowPrefixMatch |
||
228 | * @return bool |
||
229 | */ |
||
230 | 263 | protected function matchesEmergencyNumberHelper($number, $regionCode, $allowPrefixMatch) |
|
255 | |||
256 | /** |
||
257 | * Given a valid short number, determines whether it is carrier-specific (however, nothing is |
||
258 | * implied about its validity). Carrier-specific numbers may connect to a different end-point, or |
||
259 | * not connect at all, depending on the user's carrier. If it is important that the number is |
||
260 | * valid, then its validity must first be checked using {@link #isValidShortNumber} or |
||
261 | * {@link #isValidShortNumberForRegion}. |
||
262 | * |
||
263 | * @param PhoneNumber $number the valid short number to check |
||
264 | * @return boolean whether the short number is carrier-specific, assuming the input was a valid short |
||
265 | * number |
||
266 | */ |
||
267 | 1 | public function isCarrierSpecific(PhoneNumber $number) |
|
268 | { |
||
269 | 1 | $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode()); |
|
270 | 1 | $regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes); |
|
271 | 1 | $nationalNumber = $this->getNationalSignificantNumber($number); |
|
272 | 1 | $phoneMetadata = $this->getMetadataForRegion($regionCode); |
|
273 | |||
274 | 1 | return ($phoneMetadata !== null) && ($this->matchesPossibleNumberAndNationalNumber( |
|
275 | 1 | $nationalNumber, |
|
276 | 1 | $phoneMetadata->getCarrierSpecific() |
|
277 | )); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Given a valid short number, determines whether it is carrier-specific when dialed from the |
||
282 | * given region (however, nothing is implied about its validity). Carrier-specific numbers may |
||
283 | * connect to a different end-point, or not connect at all, depending on the user's carrier. If |
||
284 | * it is important that the number is valid, then its validity must first be checked using |
||
285 | * {@link #isValidShortNumber} or {@link #isValidShortNumberForRegion}. Returns false if the |
||
286 | * number doesn't match the region provided. |
||
287 | * @param PhoneNumber $number The valid short number to check |
||
288 | * @param string $regionDialingFrom The region from which the number is dialed |
||
289 | * @return bool Whether the short number is carrier-specific in the provided region, assuming the |
||
290 | * input was a valid short number |
||
291 | */ |
||
292 | 73 | public function isCarrierSpecificForRegion(PhoneNumber $number, $regionDialingFrom) |
|
304 | |||
305 | /** |
||
306 | * Given a valid short number, determines whether it is an SMS service (however, nothing is |
||
307 | * implied about its validity). An SMS service is where the primary or only intended usage is to |
||
308 | * receive and/or send text messages (SMSs). This includes MMS as MMS numbers downgrade to SMS if |
||
309 | * the other party isn't MMS-capable. If it is important that the number is valid, then its |
||
310 | * validity must first be checked using {@link #isValidShortNumber} or {@link |
||
311 | * #isValidShortNumberForRegion}. Returns false if the number doesn't match the region provided. |
||
312 | * |
||
313 | * @param PhoneNumber $number The valid short number to check |
||
314 | * @param string $regionDialingFrom The region from which the number is dialed |
||
315 | * @return bool Whether the short number is an SMS service in the provided region, assuming the input |
||
316 | * was a valid short number. |
||
317 | */ |
||
318 | 81 | public function isSmsServiceForRegion(PhoneNumber $number, $regionDialingFrom) |
|
332 | |||
333 | /** |
||
334 | * Helper method to get the region code for a given phone number, from a list of possible region |
||
335 | * codes. If the list contains more than one region, the first region for which the number is |
||
336 | * valid is returned. |
||
337 | * |
||
338 | * @param PhoneNumber $number |
||
339 | * @param $regionCodes |
||
340 | * @return String|null Region Code (or null if none are found) |
||
341 | */ |
||
342 | 244 | protected function getRegionCodeForShortNumberFromRegionList(PhoneNumber $number, $regionCodes) |
|
363 | |||
364 | /** |
||
365 | * Check whether a short number is a possible number. If a country calling code is shared by |
||
366 | * multiple regions, this returns true if it's possible in any of them. This provides a more |
||
367 | * lenient check than {@link #isValidShortNumber}. See {@link |
||
368 | * #IsPossibleShortNumberForRegion(PhoneNumber, String)} for details. |
||
369 | * |
||
370 | * @param $number PhoneNumber the short number to check |
||
371 | * @return boolean whether the number is a possible short number |
||
372 | */ |
||
373 | 2 | public function isPossibleShortNumber(PhoneNumber $number) |
|
392 | |||
393 | /** |
||
394 | * Check whether a short number is a possible number when dialled from a region, given the number |
||
395 | * in the form of a string, and the region where the number is dialled from. This provides a more |
||
396 | * lenient check than {@link #isValidShortNumber}. |
||
397 | * |
||
398 | * @param PhoneNumber $shortNumber The short number to check |
||
399 | * @param string $regionDialingFrom Region dialing From |
||
400 | * @return boolean whether the number is a possible short number |
||
401 | */ |
||
402 | 393 | public function isPossibleShortNumberForRegion(PhoneNumber $shortNumber, $regionDialingFrom) |
|
417 | |||
418 | /** |
||
419 | * Tests whether a short number matches a valid pattern. If a country calling code is shared by |
||
420 | * multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify |
||
421 | * the number is actually in use, which is impossible to tell by just looking at the number |
||
422 | * itself. See {@link #isValidShortNumberForRegion(PhoneNumber, String)} for details. |
||
423 | * |
||
424 | * @param $number PhoneNumber the short number for which we want to test the validity |
||
425 | * @return boolean whether the short number matches a valid pattern |
||
426 | */ |
||
427 | 243 | public function isValidShortNumber(PhoneNumber $number) |
|
439 | |||
440 | /** |
||
441 | * Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify |
||
442 | * the number is actually in use, which is impossible to tell by just looking at the number |
||
443 | * itself. |
||
444 | * |
||
445 | * @param PhoneNumber $number The Short number for which we want to test the validity |
||
446 | * @param string $regionDialingFrom the region from which the number is dialed |
||
447 | * @return boolean whether the short number matches a valid pattern |
||
448 | */ |
||
449 | 244 | public function isValidShortNumberForRegion(PhoneNumber $number, $regionDialingFrom) |
|
472 | |||
473 | /** |
||
474 | * Gets the expected cost category of a short number when dialled from a region (however, nothing is |
||
475 | * implied about its validity). If it is important that the number is valid, then its validity |
||
476 | * must first be checked using {@link isValidShortNumberForRegion}. Note that emergency numbers |
||
477 | * are always considered toll-free. |
||
478 | * Example usage: |
||
479 | * <pre>{@code |
||
480 | * $shortInfo = ShortNumberInfo::getInstance(); |
||
481 | * $shortNumber = PhoneNumberUtil::parse("110", "US); |
||
482 | * $regionCode = "FR"; |
||
483 | * if ($shortInfo->isValidShortNumberForRegion($shortNumber, $regionCode)) { |
||
484 | * $cost = $shortInfo->getExpectedCostForRegion($shortNumber, $regionCode); |
||
485 | * // Do something with the cost information here. |
||
486 | * }}</pre> |
||
487 | * |
||
488 | * @param PhoneNumber $number the short number for which we want to know the expected cost category, |
||
489 | * as a string |
||
490 | * @param string $regionDialingFrom the region from which the number is dialed |
||
491 | * @return int the expected cost category for that region of the short number. Returns UNKNOWN_COST if |
||
492 | * the number does not match a cost category. Note that an invalid number may match any cost |
||
493 | * category. |
||
494 | */ |
||
495 | 337 | public function getExpectedCostForRegion(PhoneNumber $number, $regionDialingFrom) |
|
536 | |||
537 | /** |
||
538 | * Gets the expected cost category of a short number (however, nothing is implied about its |
||
539 | * validity). If the country calling code is unique to a region, this method behaves exactly the |
||
540 | * same as {@link #getExpectedCostForRegion(PhoneNumber, String)}. However, if the country calling |
||
541 | * code is shared by multiple regions, then it returns the highest cost in the sequence |
||
542 | * PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of |
||
543 | * UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE |
||
544 | * or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it |
||
545 | * might be a PREMIUM_RATE number. |
||
546 | * |
||
547 | * <p> |
||
548 | * For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected |
||
549 | * cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same |
||
550 | * country calling code. |
||
551 | * </p> |
||
552 | * |
||
553 | * Note: If the region from which the number is dialed is known, it is highly preferable to call |
||
554 | * {@link #getExpectedCostForRegion(PhoneNumber, String)} instead. |
||
555 | * |
||
556 | * @param PhoneNumber $number the short number for which we want to know the expected cost category |
||
557 | * @return int the highest expected cost category of the short number in the region(s) with the given |
||
558 | * country calling code |
||
559 | */ |
||
560 | 3 | public function getExpectedCost(PhoneNumber $number) |
|
592 | |||
593 | /** |
||
594 | * Returns true if the given number exactly matches an emergency service number in the given |
||
595 | * region. |
||
596 | * <p> |
||
597 | * This method takes into account cases where the number might contain formatting, but doesn't |
||
598 | * allow additional digits to be appended. Note that {@code isEmergencyNumber(number, region)} |
||
599 | * implies {@code connectsToEmergencyNumber(number, region)}. |
||
600 | * |
||
601 | * @param string $number the phone number to test |
||
602 | * @param string $regionCode the region where the phone number is being dialled |
||
603 | * @return boolean whether the number exactly matches an emergency services number in the given region |
||
604 | */ |
||
605 | 253 | public function isEmergencyNumber($number, $regionCode) |
|
609 | |||
610 | /** |
||
611 | * Gets the national significant number of the a phone number. Note a national significant number |
||
612 | * doesn't contain a national prefix or any formatting. |
||
613 | * <p> |
||
614 | * This is a temporary duplicate of the {@code getNationalSignificantNumber} method from |
||
615 | * {@code PhoneNumberUtil}. Ultimately a canonical static version should exist in a separate |
||
616 | * utility class (to prevent {@code ShortNumberInfo} needing to depend on PhoneNumberUtil). |
||
617 | * |
||
618 | * @param PhoneNumber $number the phone number for which the national significant number is needed |
||
619 | * @return string the national significant number of the PhoneNumber object passed in |
||
620 | */ |
||
621 | 733 | protected function getNationalSignificantNumber(PhoneNumber $number) |
|
634 | |||
635 | /** |
||
636 | * TODO: Once we have benchmarked ShortnumberInfo, consider if it is worth keeping |
||
637 | * this performance optimization. |
||
638 | * @param string $number |
||
639 | * @param PhoneNumberDesc $numberDesc |
||
640 | * @return bool |
||
641 | */ |
||
642 | 730 | protected function matchesPossibleNumberAndNationalNumber($number, PhoneNumberDesc $numberDesc) |
|
650 | } |
||
651 |