Completed
Push — develop ( 29df12...700ecb )
by Matteo
03:39
created

Utilities::pregSplitArray()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0073

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 17
cp 0.9412
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 14
nc 10
nop 2
crap 6.0073
1
<?php
2
/**
3
 * GitElephant - An abstraction layer for git written in PHP
4
 * Copyright (C) 2013  Matteo Giachino
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
18
 */
19
20
namespace GitElephant;
21
22
/**
23
 * Utilities class
24
 *
25
 * @author Matteo Giachino <[email protected]>
26
 */
27
class Utilities
28
{
29
    /**
30
     * Replace / with the system directory separator
31
     *
32
     * @param string $path the original path
33
     *
34
     * @return mixed
35
     */
36 1
    public static function normalizeDirectorySeparator($path)
37
    {
38 1
        return str_replace(DIRECTORY_SEPARATOR, '/', $path);
39
    }
40
41
    /**
42
    * explode an array by lines that match a regular expression
43
    *
44
    * @param array  $array  the original array, should be a non-associative array
45
    * @param string $regexp the regular expression
46
    *
47
    * @return array an array of array pieces
48
    * @throws \InvalidArgumentException
49
    */
50 3
    public static function pregSplitArray($array, $regexp)
51
    {
52 3
        if (static::isAssociative($array)) {
53
            throw new \InvalidArgumentException('pregSplitArray only accepts non-associative arrays.');
54
        }
55 3
        $lineNumbers = array();
56 3
        $arrOut      = array();
57 3
        foreach ($array as $i => $line) {
58 3
            if (preg_match($regexp, $line)) {
59 3
                $lineNumbers[] = $i;
60 3
            }
61 3
        }
62
63 3
        foreach ($lineNumbers as $i => $lineNum) {
64 3
            if (isset($lineNumbers[$i + 1])) {
65 2
                $arrOut[] = array_slice($array, $lineNum, $lineNumbers[$i + 1] - $lineNum);
66 2
            } else {
67 3
                $arrOut[] = array_slice($array, $lineNum);
68
            }
69 3
        }
70
71 3
        return $arrOut;
72
    }
73
74
    /**
75
     * @param array  $array  a flat array
76
     * @param string $regexp a regular expression
77
     *
78
     * @return array
79
     */
80 20
    public static function pregSplitFlatArray($array, $regexp)
81
    {
82 20
        $index = 0;
83 20
        $slices = array();
84 20
        $slice = array();
85 20
        foreach ($array as $val) {
86 20
            if (preg_match($regexp, $val) && !empty($slice)) {
87 14
                $slices[$index] = $slice;
88 14
                ++$index;
89 14
                $slice = array();
90 14
            }
91 20
            $slice[] = $val;
92 20
        }
93 20
        if (!empty($slice)) {
94 20
            $slices[$index] = $slice;
95 20
        }
96
97 20
        return $slices;
98
    }
99
100
    /**
101
     * Tell if an array is associative
102
     *
103
     * @param array $arr an array
104
     *
105
     * @return bool
106
     */
107 3
    public static function isAssociative($arr)
108
    {
109 3
        return array_keys($arr) !== range(0, count($arr) - 1);
110
    }
111
}
112