Passed
Push — main ( 604bfc...f70419 )
by Sebastian
04:51
created

Util   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 30
rs 10
ccs 7
cts 7
cp 1
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isZeroHash() 0 3 1
A extractBranchInfo() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Git\Rev;
13
14
/**
15
 * Util class
16
 *
17
 * Does some simple format and validation stuff
18
 *
19
 * @package CaptainHook
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/captainhookphp/captainhook
22
 * @since   Class available since Release 5.15.0
23
 */
24
abstract class Util
25
{
26
    /**
27
     * Indicates if commit hash is a zero hash 0000000000000000000000000000000000000000
28
     *
29
     * @param  string $hash
30
     * @return bool
31
     */
32 7
    public static function isZeroHash(string $hash): bool
33
    {
34 7
        return (bool) preg_match('/^0+$/', $hash);
35
    }
36
37
    /**
38
     * Splits remote and branch
39
     *
40
     *   origin/main     => ['remote' => 'origin', 'branch' => 'main']
41
     *   main            => ['remote' => 'origin', 'branch' => 'main']
42
     *   ref/origin/main => ['remote' => 'origin', 'branch' => 'main']
43
     *
44
     * @param string $ref
45
     * @return array<string, string>
46
     */
47 13
    public static function extractBranchInfo(string $ref): array
48
    {
49 13
        $parts = explode('/', $ref);
50
51 13
        return [
52 13
            'branch' => array_pop($parts),
53 13
            'remote' => array_pop($parts) ?? 'origin',
54 13
        ];
55
    }
56
}
57