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($lines) |
|
86 | { |
||
87 | 2 | $this->position = 0; |
|
88 | |||
89 | 2 | $this->getLinesNumbers($lines[0]); |
|
90 | 2 | $this->parseLines(array_slice($lines, 1)); |
|
91 | 2 | } |
|
92 | |||
93 | /** |
||
94 | * Parse lines |
||
95 | * |
||
96 | * @param array $lines output lines |
||
97 | * |
||
98 | * @throws \Exception |
||
99 | */ |
||
100 | 2 | private function parseLines($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 { |
|
112 | 2 | if (preg_match('/^-(.*)/', $line)) { |
|
113 | 1 | $this->lines[] = new DiffChunkLineDeleted($deleted++, preg_replace('/-(.*)/', ' $1', $line)); |
|
114 | 1 | $originUnchanged++; |
|
115 | 1 | } else { |
|
116 | 2 | if (preg_match('/^ (.*)/', $line) || $line == '') { |
|
117 | 2 | $this->lines[] = new DiffChunkLineUnchanged($originUnchanged++, $destUnchanged++, $line); |
|
118 | 2 | $deleted++; |
|
119 | 2 | $new++; |
|
120 | 2 | } else { |
|
121 | 2 | if (!preg_match('/\\ No newline at end of file/', $line)) { |
|
122 | throw new \Exception(sprintf('GitElephant was unable to parse the line %s', $line)); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | 2 | } |
|
128 | 2 | } |
|
129 | |||
130 | /** |
||
131 | * Get line numbers |
||
132 | * |
||
133 | * @param string $line a single line |
||
134 | */ |
||
135 | 2 | private function getLinesNumbers($line) |
|
136 | { |
||
137 | 2 | $matches = array(); |
|
138 | 2 | preg_match('/@@ -(.*) \+(.*) @@?(.*)/', $line, $matches); |
|
139 | 2 | if (!strpos($matches[1], ',')) { |
|
140 | // one line |
||
141 | $this->originStartLine = $matches[1]; |
||
|
|||
142 | $this->originEndLine = $matches[1]; |
||
143 | } else { |
||
144 | 2 | list($this->originStartLine, $this->originEndLine) = explode(',', $matches[1]); |
|
145 | } |
||
146 | 2 | if (!strpos($matches[2], ',')) { |
|
147 | // one line |
||
148 | 1 | $this->destStartLine = $matches[2]; |
|
149 | 1 | $this->destEndLine = $matches[2]; |
|
150 | 1 | } else { |
|
151 | 1 | list($this->destStartLine, $this->destEndLine) = explode(',', $matches[2]); |
|
152 | } |
||
153 | 2 | } |
|
154 | |||
155 | /** |
||
156 | * destStartLine getter |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getDestStartLine() |
||
164 | |||
165 | /** |
||
166 | * destEndLine getter |
||
167 | * |
||
168 | * @return int |
||
169 | */ |
||
170 | public function getDestEndLine() |
||
174 | |||
175 | /** |
||
176 | * originStartLine getter |
||
177 | * |
||
178 | * @return int |
||
179 | */ |
||
180 | public function getOriginStartLine() |
||
184 | |||
185 | /** |
||
186 | * originEndLine getter |
||
187 | * |
||
188 | * @return int |
||
189 | */ |
||
190 | public function getOriginEndLine() |
||
194 | |||
195 | /** |
||
196 | * Get hunk header line |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | public function getHeaderLine() |
||
201 | { |
||
202 | if (null === $this->headerLine) { |
||
203 | $line = '@@'; |
||
204 | $line .= ' -' . $this->getOriginStartLine() . ',' . $this->getOriginEndLine(); |
||
205 | $line .= ' +' . $this->getDestStartLine() . ',' . $this->getDestEndLine(); |
||
206 | $line .= ' @@'; |
||
207 | |||
208 | $this->headerLine = $line; |
||
209 | } |
||
210 | |||
211 | return $this->headerLine; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get Lines |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | public function getLines() |
||
223 | |||
224 | /** |
||
225 | * ArrayAccess interface |
||
226 | * |
||
227 | * @param int $offset offset |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function offsetExists($offset) |
||
235 | |||
236 | /** |
||
237 | * ArrayAccess interface |
||
238 | * |
||
239 | * @param int $offset offset |
||
240 | * |
||
241 | * @return null |
||
242 | */ |
||
243 | public function offsetGet($offset) |
||
247 | |||
248 | /** |
||
249 | * ArrayAccess interface |
||
250 | * |
||
251 | * @param int $offset offset |
||
252 | * @param mixed $value value |
||
253 | */ |
||
254 | public function offsetSet($offset, $value) |
||
255 | { |
||
256 | if (is_null($offset)) { |
||
257 | $this->lines[] = $value; |
||
258 | } else { |
||
259 | $this->lines[$offset] = $value; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * ArrayAccess interface |
||
265 | * |
||
266 | * @param int $offset offset |
||
267 | */ |
||
268 | public function offsetUnset($offset) |
||
272 | |||
273 | /** |
||
274 | * Countable interface |
||
275 | * |
||
276 | * @return int|void |
||
277 | */ |
||
278 | 1 | public function count() |
|
282 | |||
283 | /** |
||
284 | * Iterator interface |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | 1 | public function current() |
|
292 | |||
293 | /** |
||
294 | * Iterator interface |
||
295 | */ |
||
296 | 1 | public function next() |
|
300 | |||
301 | /** |
||
302 | * Iterator interface |
||
303 | * |
||
304 | * @return int |
||
305 | */ |
||
306 | public function key() |
||
310 | |||
311 | /** |
||
312 | * Iterator interface |
||
313 | * |
||
314 | * @return bool |
||
315 | */ |
||
316 | 1 | public function valid() |
|
320 | |||
321 | /** |
||
322 | * Iterator interface |
||
323 | */ |
||
324 | 1 | public function rewind() |
|
328 | } |
||
329 |
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.