Compare   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
c 2
b 0
f 0
dl 0
loc 124
ccs 14
cts 14
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A revisions() 0 4 1
A getGitCommand() 0 8 1
A ignoreWhitespacesAtEndOfLine() 0 4 1
A withContextLines() 0 4 2
A statsOnly() 0 4 1
A ignoreWhitespaces() 0 4 1
A indexTo() 0 4 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\Command\Diff;
13
14
use SebastianFeldmann\Git\Command\Base;
15
16
/**
17
 * Class Between
18
 *
19
 * @package SebastianFeldmann\Git
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/sebastianfeldmann/git
22
 * @since   Class available since Release 1.2.0
23
 */
24
class Compare extends Base
25
{
26
    /**
27
     * Compare A/B command snippet.
28
     *
29
     * @var string
30
     */
31
    protected $compare = '';
32
33
    /**
34
     * Ignore line endings.
35
     *
36
     * @var string
37
     */
38
    protected $ignoreEOL = '';
39
40
    /**
41
     * Show statistics only.
42
     *
43
     * @var string
44
     */
45
    protected $stats = '';
46
47
    /**
48
     * Ignore all whitespaces.
49
     *
50
     * @var string
51
     */
52
    private $ignoreWhitespaces = '';
53
54
    /**
55
     * Number of context lines before and after the diff
56
     *
57
     * @var string
58
     */
59 6
    private $unified = '';
60
61 6
    /**
62 6
     * Compare two given revisions.
63
     *
64
     * @param  string $from
65
     * @param  string $to
66
     * @return \SebastianFeldmann\Git\Command\Diff\Compare
67
     */
68
    public function revisions(string $from, string $to): Compare
69
    {
70
        $this->compare = escapeshellarg($from) . ' ' . escapeshellarg($to);
71 1
        return $this;
72
    }
73 1
74 1
    /**
75
     * Compares the index to a given commit hash
76
     *
77
     * @param  string $to
78
     * @return \SebastianFeldmann\Git\Command\Diff\Compare
79
     */
80
    public function indexTo(string $to = 'HEAD'): Compare
81
    {
82
        $this->compare = '--staged ' . $to;
83 3
        return $this;
84
    }
85 3
86 3
    /**
87
     * Generate diffs with $amount lines of context instead of the usual three
88
     *
89
     * @param  int $amount
90
     * @return \SebastianFeldmann\Git\Command\Diff\Compare
91
     */
92
    public function withContextLines(int $amount): Compare
93
    {
94
        $this->unified = $amount === 3 ? '' : ' --unified=' . $amount;
95 2
        return $this;
96
    }
97 2
98 2
    /**
99
     * Set diff statistics option.
100
     *
101
     * @param  bool $bool
102
     * @return \SebastianFeldmann\Git\Command\Diff\Compare
103
     */
104
    public function statsOnly(bool $bool = true): Compare
105
    {
106
        $this->stats = $this->useOption('--numstat', $bool);
107 5
        return $this;
108
    }
109 5
110
    /**
111
     * Set ignore spaces at end of line.
112
     *
113
     * @param  bool $bool
114
     * @return \SebastianFeldmann\Git\Command\Diff\Compare
115
     */
116
    public function ignoreWhitespacesAtEndOfLine(bool $bool = true): Compare
117
    {
118
        $this->ignoreEOL = $this->useOption('--ignore-space-at-eol', $bool);
119
        return $this;
120
    }
121
122
    /**
123
     * Set ignore all whitespaces.
124
     *
125
     * @param  bool $bool
126
     * @return \SebastianFeldmann\Git\Command\Diff\Compare
127
     */
128
    public function ignoreWhitespaces(bool $bool = true): Compare
129
    {
130
        $this->ignoreWhitespaces = $this->useOption('-w', $bool);
131
        return $this;
132
    }
133
134
    /**
135
     * Return the command to execute.
136
     *
137
     * @return string
138
     * @throws \RuntimeException
139
     */
140
    protected function getGitCommand(): string
141
    {
142
        return 'diff'
143
               . $this->unified
144
               . $this->ignoreWhitespaces
145
               . $this->ignoreEOL
146
               . $this->stats
147
               . ' ' . $this->compare;
148
    }
149
}
150