| Conditions | 15 |
| Paths | 36 |
| Total Lines | 40 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 73 | public function toString(?string $format = 'c (a) s'): string |
||
| 74 | { |
||
| 75 | if ($format === null) { |
||
| 76 | $format = 'c (a) s'; |
||
| 77 | } |
||
| 78 | $countryCode = $this->dataBag->get('phone.countryCode'); |
||
| 79 | $areaCode = $this->dataBag->get('phone.areaCode'); |
||
| 80 | $subscriberNumber = $this->dataBag->get('phone.subscriberNumber'); |
||
| 81 | if (!empty($countryCode) && \strpos($countryCode, '+') !== 0) { |
||
| 82 | $countryCode = '+' . $countryCode; |
||
| 83 | } |
||
| 84 | |||
| 85 | if (empty($areaCode) && empty($subscriberNumber)) { |
||
| 86 | return ''; |
||
| 87 | } |
||
| 88 | if (empty($areaCode)) { |
||
| 89 | $areaCode = ''; // remove '0' values |
||
| 90 | $format = (string)\preg_replace('/\(\s?a\s?\)\s?/', '', $format); |
||
| 91 | } |
||
| 92 | if (!empty($countryCode) && \strpos($format, 'c') !== false) { |
||
| 93 | $areaCode = \ltrim($areaCode, '0'); |
||
| 94 | } |
||
| 95 | if (strpos($areaCode, '0') !== 0 && (empty($countryCode) || \strpos($format, 'c') === false)) { |
||
| 96 | $areaCode = '0' . $areaCode; |
||
| 97 | } |
||
| 98 | return trim( |
||
| 99 | (string)\preg_replace_callback( |
||
| 100 | '![cas]!', |
||
| 101 | static function (array $matches) use ($countryCode, $areaCode, $subscriberNumber) { |
||
| 102 | switch ($matches[0]) { |
||
| 103 | case 'c': |
||
| 104 | return $countryCode; |
||
| 105 | case 'a': |
||
| 106 | return $areaCode; |
||
| 107 | case 's': |
||
| 108 | return $subscriberNumber; |
||
| 109 | } |
||
| 110 | return ''; |
||
| 111 | }, |
||
| 112 | $format |
||
| 113 | ) |
||
| 162 |