Completed
Push — master ( 7cb780...532294 )
by
unknown
04:14 queued 02:10
created

Utilities   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A pregSplitArray() 0 16 4
A pregSplitFlatArray() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * GitElephant - An abstraction layer for git written in PHP
7
 * Copyright (C) 2013  Matteo Giachino
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
21
 */
22
23
namespace GitElephant;
24
25
/**
26
 * @author Matteo Giachino <[email protected]>
27
 */
28
class Utilities
29
{
30
    /**
31
     * explode an array by lines that match a regular expression
32
     *
33
     * @param string[] $list  a flat array
34
     * @param string $pattern a regular expression
35
     *
36
     * @return string[]
37
     */
38 4
    public static function pregSplitArray(array $list, string $pattern): array
39
    {
40 4
        $slices = [];
41 4
        $index = -1;
42 4
        foreach ($list as $value) {
43 4
            if (preg_match($pattern, $value) === 1) {
44 4
                ++$index;
45
            }
46
47 4
            if ($index !== -1) {
48 4
                $slices[$index][] = $value;
49
            }
50
        }
51
52 4
        return $slices;
53
    }
54
55
    /**
56
     * @param string[] $list  a flat array
57
     * @param string $pattern a regular expression
58
     *
59
     * @return string[]
60
     */
61 20
    public static function pregSplitFlatArray(array $list, string $pattern): array
62
    {
63 20
        $slices = [];
64 20
        $index = -1;
65 20
        foreach ($list as $value) {
66 20
            if (preg_match($pattern, $value) === 1) {
67 20
                ++$index;
68
            }
69
70 20
            $slices[$index + 1][] = $value;
71
        }
72
73 20
        return $slices;
74
    }
75
}
76