Passed
Push — master ( 20ad1a...1e5204 )
by Martijn
03:13 queued 01:48
created

Multibyte::split()   D

Complexity

Conditions 19
Paths 65

Size

Total Lines 65
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 65
rs 4.5166
c 0
b 0
f 0
cc 19
nc 65
nop 4

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Vanderlee\Sentence;
4
5
/**
6
 * Multibyte-safe utility functions
7
 */
8
class Multibyte
9
{
10
    /**
11
     * Multibyte.php safe version of standard trim() function.
12
     *
13
     * @param string $string
14
     * @return string
15
     */
16
    public static function trim($string)
17
    {
18
        return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string);
19
    }
20
21
    /**
22
     * A cross between mb_split and preg_split, adding the preg_split flags
23
     * to mb_split.
24
     *
25
     * @param string $pattern
26
     * @param string $string
27
     * @param int $limit
28
     * @param int $flags
29
     * @return array
30
     */
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
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! $is_delimiter || $del...ngth[0] || $split_empty, Probably Intended Meaning: ! $is_delimiter || ($del...gth[0] || $split_empty)
Loading history...
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
}