Passed
Push — master ( 7ee14d...0a7486 )
by Sebastian
01:43
created

Diff::compareIndexTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of SebastianFeldmann\Git.
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 SebastianFeldmann\Git\Operator;
13
14
use SebastianFeldmann\Git\Command\Diff\Compare;
15
use SebastianFeldmann\Git\Command\DiffTree\ChangedFiles;
16
17
/**
18
 * Diff operator
19
 *
20
 * @package SebastianFeldmann\Git
21
 * @author  Sebastian Feldmann <[email protected]>
22
 * @link    https://github.com/sebastianfeldmann/git
23
 * @since   Class available since Release 1.2.0
24
 */
25
class Diff extends Base
26
{
27
    /**
28
     * Returns a list of files and their changes.
29
     *
30
     * @param  string $from
31
     * @param  string $to
32 1
     * @return \SebastianFeldmann\Git\Diff\File[]
33
     */
34 1
    public function compare(string $from, string $to): array
35 1
    {
36
        $compare = (new Compare($this->repo->getRoot()))->revisions($from, $to)
37 1
                                                        ->ignoreWhitespacesAtEndOfLine();
38
39 1
        $result = $this->runner->run($compare, new Compare\FullDiffList());
40
41
        return $result->getFormattedOutput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result->getFormattedOutput() returns the type iterable which is incompatible with the type-hinted return array.
Loading history...
42
    }
43
44
    /**
45
     * Returns a list of files and their changes
46
     *
47
     * @param  string $to
48
     * @return \SebastianFeldmann\Git\Diff\File[]
49 1
     */
50
    public function compareIndexTo(string $to = 'head'): array
51 1
    {
52 1
        $compare = (new Compare($this->repo->getRoot()))->indexTo($to)
53
                                                        ->withContextLines(0)
54 1
                                                        ->ignoreWhitespacesAtEndOfLine();
55
56
        $result = $this->runner->run($compare, new Compare\FullDiffList());
57
58
        return $result->getFormattedOutput();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result->getFormattedOutput() returns the type iterable which is incompatible with the type-hinted return array.
Loading history...
59
    }
60
61
    /**
62
     * Uses 'diff-tree' to list the files that changed between two revisions
63
     *
64
     * @param  string $from
65
     * @param  string $to
66
     * @return string[]
67
     */
68
    public function getChangedFiles(string $from, string $to): array
69
    {
70
        $cmd    = (new ChangedFiles($this->repo->getRoot()))->fromRevision($from)->toRevision($to);
71
        $result = $this->runner->run($cmd);
72
73
        return $result->getBufferedOutput();
74
    }
75
}
76