Completed
Pull Request — develop (#140)
by
unknown
08:01 queued 02:05
created

Diff   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 52.38%

Importance

Changes 0
Metric Value
wmc 25
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 249
ccs 33
cts 63
cp 0.5238
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A __construct() 0 6 1
B createFromCommand() 0 28 6
A parseOutputLines() 0 8 2
A getCaller() 0 4 1
A setRepository() 0 4 1
A getRepository() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 2
A offsetSet() 0 9 2
A offsetUnset() 0 4 1
A count() 0 4 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
1
<?php
2
/**
3
 * GitElephant - An abstraction layer for git written in PHP
4
 * Copyright (C) 2013  Matteo Giachino
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
18
 */
19
20
namespace GitElephant\Objects\Diff;
21
22
use \GitElephant\Utilities;
23
use \GitElephant\Repository;
24
use \GitElephant\Command\DiffTreeCommand;
25
use \GitElephant\Command\DiffCommand;
26
27
/**
28
 * Represent a collection of diffs between two trees
29
 *
30
 * @author Matteo Giachino <[email protected]>
31
 */
32
class Diff implements \ArrayAccess, \Countable, \Iterator
33
{
34
    /**
35
     * @var \GitElephant\Repository
36
     */
37
    private $repository;
38
39
    /**
40
     * the cursor position
41
     *
42
     * @var int
43
     */
44
    private $position;
45
46
    /**
47
     * DiffObject instances
48
     *
49
     * @var array
50
     */
51
    private $diffObjects;
52
53
    /**
54
     * static generator to generate a Diff object
55
     *
56
     * @param \GitElephant\Repository                 $repository repository
57
     * @param null|string|\GitElephant\Objects\Commit $commit1    first commit
58
     * @param null|string|\GitElephant\Objects\Commit $commit2    second commit
59
     * @param null|string                             $path       path to consider
60
     *
61
     * @throws \RuntimeException
62
     * @throws \InvalidArgumentException
63
     * @throws \Symfony\Component\Process\Exception\RuntimeException
64
     * @return Diff
65
     */
66 2
    public static function create(Repository $repository, $commit1 = null, $commit2 = null, string $path = null)
67
    {
68 2
        $commit = new self($repository);
69 2
        $commit->createFromCommand($commit1, $commit2, $path);
0 ignored issues
show
Bug introduced by
It seems like $commit1 defined by parameter $commit1 on line 66 can also be of type object<GitElephant\Objects\Commit> or string; however, GitElephant\Objects\Diff\Diff::createFromCommand() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $commit2 defined by parameter $commit2 on line 66 can also be of type object<GitElephant\Objects\Commit> or string; however, GitElephant\Objects\Diff\Diff::createFromCommand() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $path defined by parameter $path on line 66 can also be of type string; however, GitElephant\Objects\Diff\Diff::createFromCommand() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
70
71 2
        return $commit;
72
    }
73
74
    /**
75
     * Class constructor
76
     * bare Diff object
77
     *
78
     * @param \GitElephant\Repository $repository  repository instance
79
     * @param null                    $diffObjects diff objects
80
     */
81 2
    public function __construct(Repository $repository, array $diffObjects = null)
82
    {
83 2
        $this->position = 0;
84 2
        $this->repository = $repository;
85 2
        $this->diffObjects = $diffObjects;
0 ignored issues
show
Documentation Bug introduced by
It seems like $diffObjects can be null. However, the property $diffObjects is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
86 2
    }
87
88
    /**
89
     * get the commit properties from command
90
     *
91
     * @param null $commit1 commit 1
92
     * @param null $commit2 commit 2
93
     * @param null $path    path
94
     *
95
     * @throws \RuntimeException
96
     * @throws \Symfony\Component\Process\Exception\InvalidArgumentException
97
     * @throws \Symfony\Component\Process\Exception\LogicException
98
     * @throws \InvalidArgumentException
99
     * @throws \Symfony\Component\Process\Exception\RuntimeException
100
     * @see ShowCommand::commitInfo
101
     */
102 2
    public function createFromCommand($commit1 = null, $commit2 = null, $path = null)
103
    {
104 2
        if (null === $commit1) {
105
            $commit1 = $this->getRepository()->getCommit();
106
        }
107
108 2
        if (is_string($commit1)) {
109 1
            $commit1 = $this->getRepository()->getCommit($commit1);
110
        }
111
112 2
        if ($commit2 === null) {
113 2
            if ($commit1->isRoot()) {
0 ignored issues
show
Bug introduced by
It seems like $commit1 is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
114
                $command = DiffTreeCommand::getInstance($this->repository)->rootDiff($commit1);
0 ignored issues
show
Bug introduced by
It seems like $commit1 defined by parameter $commit1 on line 102 can be null; however, GitElephant\Command\DiffTreeCommand::rootDiff() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
115
            }
116
            else {
117 2
                $command = DiffCommand::getInstance($this->repository)->diff($commit1);
0 ignored issues
show
Bug introduced by
It seems like $commit1 defined by parameter $commit1 on line 102 can be null; however, GitElephant\Command\DiffCommand::diff() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
118
            }
119
        }
120
        else {
121
            if (is_string($commit2)) {
122
                $commit2 = $this->getRepository()->getCommit($commit2);
123
            }
124
            $command = DiffCommand::getInstance($this->repository)->diff($commit1, $commit2, $path);
0 ignored issues
show
Bug introduced by
It seems like $commit1 defined by parameter $commit1 on line 102 can be null; however, GitElephant\Command\DiffCommand::diff() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
125
        }
126
127 2
        $outputLines = $this->getCaller()->execute($command)->getOutputLines();
128 2
        $this->parseOutputLines($outputLines);
129 2
    }
130
131
    /**
132
     * parse the output of a git command showing a commit
133
     *
134
     * @param array $outputLines output lines
135
     *
136
     * @throws \InvalidArgumentException
137
     */
138 2
    private function parseOutputLines(array $outputLines)
139
    {
140 2
        $this->diffObjects = [];
141 2
        $splitArray = Utilities::pregSplitArray($outputLines, '/^diff --git SRC\/(.*) DST\/(.*)$/');
142 2
        foreach ($splitArray as $diffObjectLines) {
143 2
            $this->diffObjects[] = new DiffObject($diffObjectLines);
144
        }
145 2
    }
146
147
    /**
148
     * @return \GitElephant\Command\Caller\Caller
149
     */
150 2
    private function getCaller()
151
    {
152 2
        return $this->getRepository()->getCaller();
153
    }
154
155
    /**
156
     * Repository setter
157
     *
158
     * @param \GitElephant\Repository $repository the repository variable
159
     */
160
    public function setRepository(Repository $repository)
161
    {
162
        $this->repository = $repository;
163
    }
164
165
    /**
166
     * Repository getter
167
     *
168
     * @return \GitElephant\Repository
169
     */
170 2
    public function getRepository()
171
    {
172 2
        return $this->repository;
173
    }
174
175
    /**
176
     * ArrayAccess interface
177
     *
178
     * @param int $offset offset
179
     *
180
     * @return bool
181
     */
182
    public function offsetExists($offset)
183
    {
184
        return isset($this->diffObjects[$offset]);
185
    }
186
187
    /**
188
     * ArrayAccess interface
189
     *
190
     * @param int $offset offset
191
     *
192
     * @return null|mixed
193
     */
194 1
    public function offsetGet($offset)
195
    {
196 1
        return isset($this->diffObjects[$offset]) ? $this->diffObjects[$offset] : null;
197
    }
198
199
    /**
200
     * ArrayAccess interface
201
     *
202
     * @param int   $offset offset
203
     * @param mixed $value  value
204
     */
205
    public function offsetSet($offset, $value)
206
    {
207
        if (is_null($offset)) {
208
            $this->diffObjects[] = $value;
209
        }
210
        else {
211
            $this->diffObjects[$offset] = $value;
212
        }
213
    }
214
215
    /**
216
     * ArrayAccess interface
217
     *
218
     * @param int $offset offset
219
     */
220
    public function offsetUnset($offset)
221
    {
222
        unset($this->diffObjects[$offset]);
223
    }
224
225
    /**
226
     * Countable interface
227
     *
228
     * @return int|void
229
     */
230 2
    public function count()
231
    {
232 2
        return count($this->diffObjects);
233
    }
234
235
    /**
236
     * Iterator interface
237
     *
238
     * @return mixed
239
     */
240
    public function current()
241
    {
242
        return $this->diffObjects[$this->position];
243
    }
244
245
    /**
246
     * Iterator interface
247
     */
248
    public function next()
249
    {
250
        ++$this->position;
251
    }
252
253
    /**
254
     * Iterator interface
255
     *
256
     * @return int
257
     */
258
    public function key()
259
    {
260
        return $this->position;
261
    }
262
263
    /**
264
     * Iterator interface
265
     *
266
     * @return bool
267
     */
268
    public function valid()
269
    {
270
        return isset($this->diffObjects[$this->position]);
271
    }
272
273
    /**
274
     * Iterator interface
275
     */
276
    public function rewind()
277
    {
278
        $this->position = 0;
279
    }
280
}
281