Completed
Push — master ( d048e0...15889a )
by Christian
02:23
created

GitChangeSet::getAllChanges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace uuf6429\ElderBrother\Change;
4
5
class GitChangeSet
6
{
7
    /**
8
     * Returns all files that have been created, changed, moved or deleted.
9
     *
10
     * @return FileList
11
     */
12
    public static function getAllChanges()
13
    {
14
        return static::getFiltered();
15
    }
16
17
    /**
18
     * Returns only the files created by this changeset.
19
     *
20
     * @return FileList
21
     */
22
    public static function getAdded()
23
    {
24
        return static::getFiltered('A');
25
    }
26
27
    /**
28
     * Returns files to be deleted by this changeset.
29
     *
30
     * @return FileList
31
     */
32
    public static function getRemoved()
33
    {
34
        return static::getFiltered('D');
35
    }
36
37
    /**
38
     * Returns all modified files.
39
     *
40
     * @return FileList
41
     */
42
    public static function getModified()
43
    {
44
        return static::getFiltered('M');
45
    }
46
47
    /**
48
     * Returns all files that have been copied or renmaed by this changeset.
49
     *
50
     * @return FileList
51
     */
52
    public static function getCopiedOrMoved()
53
    {
54
        return static::getFiltered('CR');
55
    }
56
57
    /**
58
     * Returns all changed files except those that have been deleted.
59
     *
60
     * @return FileList
61
     */
62
    public static function getAddedCopiedModified()
63
    {
64
        return static::getFiltered('ACMR');
65
    }
66
67
    /**
68
     * @param string|null $filter See https://git-scm.com/docs/git-diff (--diff-filter)
69
     *
70
     * @return FileList
71
     */
72
    protected static function getFiltered($filter = null)
73
    {
74
        return new FileList(
75
            __METHOD__ . '(' . $filter . ')',
76
            function () use (&$filter) {
77
                $output = [];
78
                $command = 'git diff --cached --name-status';
79
                if ($filter) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filter of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
80
                    $command .= ' --diff-filter=' . escapeshellarg($filter);
81
                }
82
83
                exec($command, $output);
84
85
                return array_unique(
86
                    array_map(
87
                        function ($line) {
88
                            return trim(substr($line, 1));
89
                        },
90
                        $output
91
                    )
92
                );
93
            }
94
        );
95
    }
96
}
97