SplitFunctions   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 59
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getPatternByType() 0 1 ?
checkResultIsOkOrThrowException() 0 1 ?
A split() 0 20 4
1
<?php
2
namespace Mcustiel\PhpSimpleRegex\PregFunctions;
3
4
trait SplitFunctions
5
{
6
7
    /**
8
     * Splits a string using the given regular expression as separator.
9
     * See @link http://php.net/manual/es/function.preg-split.php
10
     *
11
     * @param string|array|\VerbalExpressions\PHPVerbalExpressions\VerbalExpressions|SelvinOrtiz\Utils\Flux\Flux|MarkWilson\VerbalExpression
12
     *          $pattern
13
     * @param string
14
     *          $string
15
     * @param integer
16
     *          $limit
17
     * @param boolean
18
     *          $returnOnlyNotEmpty
19
     * @param boolean
20
     *          $captureOffset
21
     * @param boolean
22
     *          $captureSubpatterns
23
     *
24
     * @throws \InvalidArgumentException
25
     * @return array
26
     */
27 3
    public function split(
28
        $pattern,
29
        $string,
30
        $limit = -1,
31
        $returnOnlyNotEmpty = false,
32
        $captureOffset = false,
33
        $captureSubpatterns = false
34
    ) {
35 3
        $result = @preg_split(
36 3
            $this->getPatternByType($pattern),
37 3
            $string,
38 3
            $limit,
39 3
            ($returnOnlyNotEmpty ? PREG_SPLIT_NO_EMPTY : 0)
40 3
            | ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0)
41 3
            | ($captureSubpatterns ? PREG_SPLIT_DELIM_CAPTURE : 0)
42
        );
43 3
        $this->checkResultIsOkOrThrowException($result, $pattern);
44
45 2
        return $result;
46
    }
47
48
    /**
49
     * @param mixed $pattern
50
     * @throws \InvalidArgumentException
51
     * @return string
52
     */
53
    abstract protected function getPatternByType($pattern);
54
55
    /**
56
     * @param mixed  $result
57
     * @param string $pattern
58
     *
59
     * @throws \RuntimeException
60
     */
61
    abstract protected function checkResultIsOkOrThrowException($result, $pattern);
62
}
63