| Conditions | 13 | 
| Paths | 21 | 
| Total Lines | 44 | 
| Code Lines | 29 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 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 | ||
| 31 | public static function split($pattern, $string, $limit = -1, $flags = 0) | ||
| 32 |     { | ||
| 33 | $split_no_empty = (bool)($flags & PREG_SPLIT_NO_EMPTY); | ||
| 34 | $offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE); | ||
| 35 | $delim_capture = (bool)($flags & PREG_SPLIT_DELIM_CAPTURE); | ||
| 36 | |||
| 37 | $lengths = self::getSplitLengths($pattern, $string); | ||
| 38 | |||
| 39 | // Substrings | ||
| 40 | $parts = []; | ||
| 41 | $position = 0; | ||
| 42 | $count = 1; | ||
| 43 |         foreach ($lengths as $length) { | ||
| 44 | $split_empty = !$split_no_empty || $length[0]; | ||
| 45 | $is_delimiter = $length[1]; | ||
| 46 | $is_captured = $delim_capture && $length[2]; | ||
| 47 | |||
| 48 | if ($limit > 0 | ||
| 49 | && !$is_delimiter | ||
| 50 | && $split_empty | ||
| 51 |                 && ++$count > $limit) { | ||
| 52 | |||
| 53 | $cut = mb_strcut($string, $position); | ||
| 54 | |||
| 55 | $parts[] = $offset_capture | ||
| 56 | ? [$cut, $position] | ||
| 57 | : $cut; | ||
| 58 | |||
| 59 | break; | ||
| 60 | } elseif ((!$is_delimiter | ||
| 61 | || $is_captured) | ||
| 62 |                 && $split_empty) { | ||
| 63 | |||
| 64 | $cut = mb_strcut($string, $position, $length[0]); | ||
| 65 | |||
| 66 | $parts[] = $offset_capture | ||
| 67 | ? [$cut, $position] | ||
| 68 | : $cut; | ||
| 69 | } | ||
| 70 | |||
| 71 | $position += $length[0]; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $parts; | ||
| 75 | } | ||
| 111 | } |