Conditions | 10 |
Paths | 8 |
Total Lines | 33 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 19 |
CRAP Score | 10 |
Changes | 1 | ||
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 |
||
8159 | 18 | public static function str_slice( |
|
8160 | string $str, |
||
8161 | int $start, |
||
8162 | int $end = null, |
||
8163 | string $encoding = 'UTF-8' |
||
8164 | ) { |
||
8165 | 18 | if ($encoding === 'UTF-8') { |
|
8166 | 7 | if ($end === null) { |
|
8167 | 1 | $length = (int) \mb_strlen($str); |
|
8168 | 6 | } elseif ($end >= 0 && $end <= $start) { |
|
8169 | 2 | return ''; |
|
8170 | 4 | } elseif ($end < 0) { |
|
8171 | 1 | $length = (int) \mb_strlen($str) + $end - $start; |
|
8172 | } else { |
||
8173 | 3 | $length = $end - $start; |
|
8174 | } |
||
8175 | |||
8176 | 5 | return \mb_substr($str, $start, $length); |
|
8177 | } |
||
8178 | |||
8179 | 11 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
8180 | |||
8181 | 11 | if ($end === null) { |
|
8182 | 5 | $length = (int) self::strlen($str, $encoding); |
|
8183 | 6 | } elseif ($end >= 0 && $end <= $start) { |
|
8184 | 2 | return ''; |
|
8185 | 4 | } elseif ($end < 0) { |
|
8186 | 1 | $length = (int) self::strlen($str, $encoding) + $end - $start; |
|
8187 | } else { |
||
8188 | 3 | $length = $end - $start; |
|
8189 | } |
||
8190 | |||
8191 | 9 | return self::substr($str, $start, $length, $encoding); |
|
8192 | } |
||
14823 |