GitUtils::extractCommitedFiles()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche application.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Tools;
12
13
/**
14
 * GitUtils.
15
 *
16
 * @author Karel Osorio Ramírez <[email protected]>
17
 * @author Ivannis Suárez Jérez <[email protected]>
18
 */
19
class GitUtils
20
{
21
    const PHP_FILES = '/(\.php)$/';
22
23
    /**
24
     * @var array
25
     */
26
    private static $files = null;
27
28
    /**
29
     * @return array
30
     */
31
    public static function extractCommitedFiles()
32
    {
33
        if (self::$files === null) {
34
            self::$files = array();
35
            $rc = 0;
36
            exec('git rev-parse --verify HEAD 2> /dev/null', self::$files, $rc);
37
            $against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
38
            if ($rc === 0) {
39
                $against = 'HEAD';
40
            }
41
            exec("git diff-index --cached --name-status $against | egrep '^(A|M)' | awk '{print $2;}'", self::$files);
42
        }
43
44
        return self::$files;
45
    }
46
47
    /**
48
     * @param string $pattern
49
     *
50
     * @return Generator
51
     */
52
    public static function commitedFiles($pattern = self::PHP_FILES)
53
    {
54
        foreach (self::extractCommitedFiles() as $file) {
0 ignored issues
show
Bug introduced by
The expression self::extractCommitedFiles() of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
55
            if (preg_match($pattern, $file) === 1) {
56
                yield $file;
57
            }
58
        }
59
    }
60
}
61