Completed
Push — master ( 3952c5...023801 )
by Sebastian
02:12
created

Index::resolveFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\Git\Operator;
11
12
use RuntimeException;
13
use SebastianFeldmann\Git\Command\DiffIndex\GetStagedFiles;
14
use SebastianFeldmann\Git\Command\DiffIndex\GetStagedFiles\FilterByStatus;
15
use SebastianFeldmann\Git\Command\RevParse\GetCommitHash;
16
17
class Index extends Base
18
{
19
    /**
20
     * List of changed files.
21
     *
22
     * @var array
23
     */
24
    private $files;
25
26
    /**
27
     * Changed files by file type
28
     *
29
     * @var array[]
30
     */
31
    private $types = [];
32
33
    /**
34
     * Files sorted by suffix yet
35
     *
36
     * @var bool
37
     */
38
    private $typesResolved = false;
39
40
    /**
41
     * Get the lst of files that changed.
42
     *
43
     * @return array
44
     */
45 4
    public function getStagedFiles() : array
46
    {
47 4
        if (null === $this->files) {
48 4
            $this->resolveFiles();
49
        }
50 4
        return $this->files;
51
    }
52
53
    /**
54
     * Where there files changed of a given type.
55
     *
56
     * @param  string $suffix
57
     * @return bool
58
     */
59 1
    public function hasStagedFilesOfType($suffix) : bool
60
    {
61 1
        return count($this->getStagedFilesOfType($suffix)) > 0;
62
    }
63
64
    /**
65
     * Return list of changed files of a given type.
66
     *
67
     * @param  string $suffix
68
     * @return array
69
     */
70 2
    public function getStagedFilesOfType($suffix) : array
71
    {
72 2
        if (!$this->typesResolved) {
73 2
            $this->resolveFileTypes();
74
        }
75 2
        return isset($this->types[$suffix]) ? $this->types[$suffix] : [];
76
    }
77
78
    /**
79
     * Resolve the list of files that changed.
80
     */
81 4
    private function resolveFiles()
82
    {
83 4
        $this->files = [];
84
85 4
        if ($this->isHeadValid()) {
86 3
            $cmd         = new GetStagedFiles($this->repo->getRoot());
87 3
            $formatter   = new FilterByStatus(['A', 'M']);
88 3
            $result      = $this->runner->run($cmd, $formatter);
89 3
            $this->files = $result->getFormattedOutput();
90
        }
91 4
    }
92
93
    /**
94
     * Sort files by file suffix.
95
     */
96 2
    private function resolveFileTypes()
97
    {
98 2
        foreach ($this->getStagedFiles() as $file) {
99 2
            $ext                 = strtolower(pathinfo($file, PATHINFO_EXTENSION));
100 2
            $this->types[$ext][] = $file;
101
        }
102 2
        $this->typesResolved = true;
103 2
    }
104
105
    /**
106
     * Check head validity.
107
     *
108
     * @return bool
109
     */
110 4
    private function isHeadValid() : bool
111
    {
112
        try {
113 4
            $cmd    = new GetCommitHash($this->repo->getRoot());
114 4
            $result = $this->runner->run($cmd);
115 3
            return $result->wasSuccessful();
116 1
        } catch (RuntimeException $e) {
117 1
            return false;
118
        }
119
    }
120
}
121