ParserHelper   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 98.11%

Importance

Changes 0
Metric Value
dl 0
loc 82
c 0
b 0
f 0
wmc 27
cbo 1
ccs 52
cts 53
cp 0.9811
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
F staticSplitExpression() 0 53 21
A implodeKeyedDouble() 0 9 3
A implodeKeyed() 0 9 3
1
<?php
2
namespace Goetas\Twital\Helper;
3
4
use Goetas\Twital\Exception;
5
6
/**
7
 *
8
 * @author Asmir Mustafic <[email protected]>
9
 *
10
 */
11
class ParserHelper
12
{
13
    private static $closing = array(
14
        '}' => '{',
15
        ')' => '(',
16
        ']' => '['
17
    );
18
19 41
    public static function staticSplitExpression($str, $splitter, $limit = 0)
20
    {
21 41
        $in = array();
22 41
        $inApex = false;
23 41
        $parts = array();
24 41
        $prev = 0;
25
26 41
        for ($i = 0, $l = strlen($str); $i < $l; $i++) {
27 41
            $chr = $str[$i];
28
29 41
            if ($chr == "'" || $chr == '"') {
30 24
                $j = 1;
31 24
                while ($i >= $j && $str[$i - $j] === '\\') {
32 1
                    $j++;
33 1
                }
34
35 24
                if ($j % 2 !== 0) {
36 24
                    if (!$inApex) {
37 24
                        $inApex = $chr;
38 24
                    } elseif ($inApex === $chr) {
39 22
                        $inApex = false;
40 22
                    }
41 24
                }
42 24
            }
43
44 41
            if (!$inApex) {
45 41
                if (in_array($chr, self::$closing)) {
46 8
                    array_push($in, $chr);
47 41
                } elseif (isset(self::$closing[$chr]) && self::$closing[$chr] === end($in)) {
48 8
                    array_pop($in);
49 41
                } elseif (isset(self::$closing[$chr]) && !count($in)) {
50 1
                    throw new Exception(sprintf('Unexpected "%s" next to "%s"', $chr, substr($str, 0, $i + 1)));
51
                }
52
53 41
                if (!count($in) && $chr === $splitter) {
54 28
                    $parts[] = substr($str, $prev, $i - $prev);
55 28
                    $prev = $i + 1;
56 28
                    if ($limit > 1 && count($parts) == ($limit - 1)) {
57 4
                        break;
58
                    }
59 25
                }
60 40
            }
61 40
        }
62 40
        if ($inApex) {
63 2
            throw new Exception(sprintf('Can\'t find the closing "%s" in "%s" expression', $inApex, $str));
64 38
        } elseif (count($in)) {
65
            throw new Exception(sprintf('Can\'t find the closing braces for "%s" in "%s" expression', implode(',', $in), $str));
66
        }
67
68 38
        $parts[] = substr($str, $prev);
69
70 38
        return array_map('trim', $parts);
71
    }
72
73 6
    public static function implodeKeyedDouble($glue, array $array, $quoteKeys = false)
74
    {
75 6
        $a = array();
76 6
        foreach ($array as $key => $val) {
77 6
            $a[] = ($quoteKeys ? "'$key'" : $key) . ":[" . implode(",", $val) . "]";
78 6
        }
79
80 6
        return implode($glue, $a);
81
    }
82
83 10
    public static function implodeKeyed($glue, array $array, $quoteKeys = false)
84
    {
85 10
        $a = array();
86 10
        foreach ($array as $key => $val) {
87 10
            $a[] = ($quoteKeys ? "'$key'" : $key) . ":$val";
88 10
        }
89
90 10
        return implode($glue, $a);
91
    }
92
}
93