Completed
Push — master ( 3eec25...40b41a )
by Christian
02:11
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
use Symfony\Component\Process\Process;
6
7
class GitChangeSet
8
{
9
    /**
10
     * Returns all files that have been created, changed, moved or deleted.
11
     *
12
     * @return FileList
13
     */
14
    public static function getAllChanges()
15
    {
16
        return static::getFiltered();
17
    }
18
19
    /**
20
     * Returns only the files created by this changeset.
21
     *
22
     * @return FileList
23
     */
24
    public static function getAdded()
25
    {
26
        return static::getFiltered('A');
27
    }
28
29
    /**
30
     * Returns files to be deleted by this changeset.
31
     *
32
     * @return FileList
33
     */
34
    public static function getRemoved()
35
    {
36
        return static::getFiltered('D');
37
    }
38
39
    /**
40
     * Returns all modified files.
41
     *
42
     * @return FileList
43
     */
44
    public static function getModified()
45
    {
46
        return static::getFiltered('M');
47
    }
48
49
    /**
50
     * Returns all files that have been copied or renmaed by this changeset.
51
     *
52
     * @return FileList
53
     */
54
    public static function getCopiedOrMoved()
55
    {
56
        return static::getFiltered('CR');
57
    }
58
59
    /**
60
     * Returns all changed files except those that have been deleted.
61
     *
62
     * @return FileList
63
     */
64
    public static function getAddedCopiedModified()
65
    {
66
        return static::getFiltered('ACMR');
67
    }
68
69
    /**
70
     * @param string|null $filter See https://git-scm.com/docs/git-diff (--diff-filter)
71
     *
72
     * @return FileList
73
     */
74
    protected static function getFiltered($filter = null)
75
    {
76
        return new FileList(
77
            __METHOD__ . '(' . $filter . ')',
78
            function () use (&$filter) {
79
                $command = 'git diff -z --cached --name-only';
80
81
                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...
82
                    $command .= ' --diff-filter=' . escapeshellarg($filter);
83
                }
84
85
                $process = new Process($command);
86
                $process->mustRun();
87
88
                return array_filter(
89
                    array_unique(
90
                        explode("\0", $process->getOutput())
91
                    )
92
                );
93
            }
94
        );
95
    }
96
}
97