| Conditions | 19 | 
| Paths | 65 | 
| Total Lines | 65 | 
| Code Lines | 42 | 
| 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 | $strlen = strlen($string); // bytes! | ||
| 38 | mb_ereg_search_init($string); | ||
| 39 | |||
| 40 | $lengths = array(); | ||
| 41 | $position = 0; | ||
| 42 |         while (($array = mb_ereg_search_pos($pattern, '')) !== false) { | ||
| 43 | // capture split | ||
| 44 | $lengths[] = array($array[0] - $position, false, null); | ||
| 45 | |||
| 46 | // move position | ||
| 47 | $position = $array[0] + $array[1]; | ||
| 48 | |||
| 49 | // capture delimiter | ||
| 50 | $regs = mb_ereg_search_getregs(); | ||
| 51 | $lengths[] = array($array[1], true, isset($regs[1]) && $regs[1]); | ||
| 52 | |||
| 53 | // Continue on? | ||
| 54 |             if ($position >= $strlen) { | ||
| 55 | break; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | // Add last bit, if not ending with split | ||
| 60 | $lengths[] = array($strlen - $position, false, null); | ||
| 61 | |||
| 62 | // Substrings | ||
| 63 | $parts = array(); | ||
| 64 | $position = 0; | ||
| 65 | $count = 1; | ||
| 66 |         foreach ($lengths as $length) { | ||
| 67 | $split_empty = $length[0] || !$split_no_empty; | ||
| 68 | $is_delimiter = $length[1]; | ||
| 69 | $is_captured = $length[2]; | ||
| 70 | |||
| 71 | if ($limit > 0 | ||
| 72 | && !$is_delimiter | ||
| 73 | && $split_empty | ||
| 74 |                 && ++$count > $limit) { | ||
| 75 | if ($length[0] > 0 | ||
| 76 |                     || $split_empty) { | ||
| 77 | $parts[] = $offset_capture | ||
| 78 | ? array(mb_strcut($string, $position), $position) | ||
| 79 | : mb_strcut($string, $position); | ||
| 80 | } | ||
| 81 | break; | ||
| 82 | } elseif ((!$is_delimiter | ||
|  | |||
| 83 | || ($delim_capture | ||
| 84 | && $is_captured)) | ||
| 85 | && ($length[0] | ||
| 86 |                     || $split_empty)) { | ||
| 87 | $parts[] = $offset_capture | ||
| 88 | ? array(mb_strcut($string, $position, $length[0]), $position) | ||
| 89 | : mb_strcut($string, $position, $length[0]); | ||
| 90 | } | ||
| 91 | |||
| 92 | $position += $length[0]; | ||
| 93 | } | ||
| 94 | |||
| 95 | return $parts; | ||
| 96 | } | ||
| 97 | } |