Completed
Push — develop ( 5247fd...3c6b2e )
by
unknown
9s
created

Diff::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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