1 | <?php |
||
27 | class DiffChunk implements \ArrayAccess, \Countable, \Iterator |
||
28 | { |
||
29 | /** |
||
30 | * the cursor position |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | private $position; |
||
35 | |||
36 | /** |
||
37 | * diff start line from original file |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | private $originStartLine; |
||
42 | |||
43 | /** |
||
44 | * diff end line from original file |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | private $originEndLine; |
||
49 | |||
50 | /** |
||
51 | * diff start line from destination file |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | private $destStartLine; |
||
56 | |||
57 | /** |
||
58 | * diff end line from destination file |
||
59 | * |
||
60 | * @var int |
||
61 | */ |
||
62 | private $destEndLine; |
||
63 | |||
64 | /** |
||
65 | * hunk header line |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private $headerLine; |
||
70 | |||
71 | /** |
||
72 | * array of lines |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private $lines; |
||
77 | |||
78 | /** |
||
79 | * Class constructor |
||
80 | * |
||
81 | * @param array $lines output lines from git binary |
||
82 | * |
||
83 | * @throws \Exception |
||
84 | */ |
||
85 | 2 | public function __construct(array $lines) |
|
92 | |||
93 | /** |
||
94 | * Parse lines |
||
95 | * |
||
96 | * @param array $lines output lines |
||
97 | * |
||
98 | * @throws \Exception |
||
99 | */ |
||
100 | 2 | private function parseLines(array $lines) |
|
101 | { |
||
102 | 2 | $originUnchanged = $this->originStartLine; |
|
103 | 2 | $destUnchanged = $this->destStartLine; |
|
104 | |||
105 | 2 | $deleted = $this->originStartLine; |
|
106 | 2 | $new = $this->destStartLine; |
|
107 | 2 | foreach ($lines as $line) { |
|
108 | 2 | if (preg_match('/^\+(.*)/', $line)) { |
|
109 | 2 | $this->lines[] = new DiffChunkLineAdded($new++, preg_replace('/\+(.*)/', ' $1', $line)); |
|
110 | 2 | $destUnchanged++; |
|
111 | 2 | } else if (preg_match('/^-(.*)/', $line)) { |
|
112 | 1 | $this->lines[] = new DiffChunkLineDeleted($deleted++, preg_replace('/-(.*)/', ' $1', $line)); |
|
113 | 1 | $originUnchanged++; |
|
114 | 2 | } else if (preg_match('/^ (.*)/', $line) || $line == '') { |
|
115 | 2 | $this->lines[] = new DiffChunkLineUnchanged($originUnchanged++, $destUnchanged++, $line); |
|
116 | 2 | $deleted++; |
|
117 | 2 | $new++; |
|
118 | 2 | } else if (!preg_match('/\\ No newline at end of file/', $line)) { |
|
119 | throw new \Exception(sprintf('GitElephant was unable to parse the line %s', $line)); |
||
120 | } |
||
121 | } |
||
122 | 2 | } |
|
123 | |||
124 | /** |
||
125 | * Get line numbers |
||
126 | * |
||
127 | * @param string $line a single line |
||
128 | */ |
||
129 | 2 | private function getLinesNumbers(string $line) |
|
149 | |||
150 | /** |
||
151 | * destStartLine getter |
||
152 | * |
||
153 | * @return int |
||
154 | */ |
||
155 | public function getDestStartLine() |
||
159 | |||
160 | /** |
||
161 | * destEndLine getter |
||
162 | * |
||
163 | * @return int |
||
164 | */ |
||
165 | public function getDestEndLine() |
||
169 | |||
170 | /** |
||
171 | * originStartLine getter |
||
172 | * |
||
173 | * @return int |
||
174 | */ |
||
175 | public function getOriginStartLine() |
||
179 | |||
180 | /** |
||
181 | * originEndLine getter |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | public function getOriginEndLine() |
||
189 | |||
190 | /** |
||
191 | * Get hunk header line |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getHeaderLine() |
||
208 | |||
209 | /** |
||
210 | * Get Lines |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | public function getLines() |
||
218 | |||
219 | /** |
||
220 | * ArrayAccess interface |
||
221 | * |
||
222 | * @param int $offset offset |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | public function offsetExists($offset) |
||
230 | |||
231 | /** |
||
232 | * ArrayAccess interface |
||
233 | * |
||
234 | * @param int $offset offset |
||
235 | * |
||
236 | * @return null |
||
237 | */ |
||
238 | public function offsetGet($offset) |
||
242 | |||
243 | /** |
||
244 | * ArrayAccess interface |
||
245 | * |
||
246 | * @param int $offset offset |
||
247 | * @param mixed $value value |
||
248 | */ |
||
249 | public function offsetSet($offset, $value) |
||
257 | |||
258 | /** |
||
259 | * ArrayAccess interface |
||
260 | * |
||
261 | * @param int $offset offset |
||
262 | */ |
||
263 | public function offsetUnset($offset) |
||
267 | |||
268 | /** |
||
269 | * Countable interface |
||
270 | * |
||
271 | * @return int|void |
||
272 | */ |
||
273 | 1 | public function count() |
|
277 | |||
278 | /** |
||
279 | * Iterator interface |
||
280 | * |
||
281 | * @return mixed |
||
282 | */ |
||
283 | 1 | public function current() |
|
287 | |||
288 | /** |
||
289 | * Iterator interface |
||
290 | */ |
||
291 | 1 | public function next() |
|
295 | |||
296 | /** |
||
297 | * Iterator interface |
||
298 | * |
||
299 | * @return int |
||
300 | */ |
||
301 | public function key() |
||
305 | |||
306 | /** |
||
307 | * Iterator interface |
||
308 | * |
||
309 | * @return bool |
||
310 | */ |
||
311 | 1 | public function valid() |
|
315 | |||
316 | /** |
||
317 | * Iterator interface |
||
318 | */ |
||
319 | 1 | public function rewind() |
|
323 | } |
||
324 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.