Conditions | 12 |
Paths | 20 |
Total Lines | 37 |
Code Lines | 27 |
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 |
||
83 | public function toString(?string $format = 'c (a) s'): string |
||
84 | { |
||
85 | if ($format === null) { |
||
86 | $format = 'c (a) s'; |
||
87 | } |
||
88 | $countryCode = $this->dataBag->get('phone.countryCode'); |
||
89 | $areaCode = $this->dataBag->get('phone.areaCode'); |
||
90 | $subscriberNumber = $this->dataBag->get('phone.subscriberNumber'); |
||
91 | if (!empty($countryCode) && \strpos($countryCode, '+') !== 0) { |
||
92 | $countryCode = '+' . $countryCode; |
||
93 | } |
||
94 | |||
95 | if (empty($areaCode) && empty($subscriberNumber)) { |
||
96 | return ''; |
||
97 | } |
||
98 | if (empty($areaCode)) { |
||
99 | $areaCode = ''; // remove '0' values |
||
100 | $format = (string)\preg_replace('/\(\s?a\s?\)\s?/', '', $format); |
||
101 | } |
||
102 | if (!empty($countryCode) && \strpos($format, 'c') !== false) { |
||
103 | $areaCode = \ltrim($areaCode, '0'); |
||
104 | } |
||
105 | return trim( |
||
106 | (string)\preg_replace_callback( |
||
107 | '![cas]!', |
||
108 | static function (array $matches) use ($countryCode, $areaCode, $subscriberNumber) { |
||
109 | switch ($matches[0]) { |
||
110 | case 'c': |
||
111 | return $countryCode; |
||
112 | case 'a': |
||
113 | return $areaCode; |
||
114 | case 's': |
||
115 | return $subscriberNumber; |
||
116 | } |
||
117 | return ''; |
||
118 | }, |
||
119 | $format |
||
120 | ) |
||
159 |