Completed
Branch master (939199)
by
unknown
39:35
created

includes/diff/ArrayDiffFormatter.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Portions taken from phpwiki-1.3.3.
4
 *
5
 * Copyright © 2000, 2001 Geoffrey T. Dairiki <[email protected]>
6
 * You may copy this code freely under the conditions of the GPL.
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 2 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program; if not, write to the Free Software Foundation, Inc.,
20
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
 * http://www.gnu.org/copyleft/gpl.html
22
 *
23
 * @file
24
 * @ingroup DifferenceEngine
25
 */
26
27
/**
28
 * A pseudo-formatter that just passes along the Diff::$edits array
29
 * @ingroup DifferenceEngine
30
 */
31
class ArrayDiffFormatter extends DiffFormatter {
32
33
	/**
34
	 * @param Diff $diff A Diff object.
35
	 *
36
	 * @return array[] List of associative arrays, each describing a difference.
37
	 */
38
	public function format( $diff ) {
39
		$oldline = 1;
40
		$newline = 1;
41
		$retval = [];
42
		foreach ( $diff->getEdits() as $edit ) {
43
			switch ( $edit->getType() ) {
44
				case 'add':
45
					foreach ( $edit->getClosing() as $line ) {
0 ignored issues
show
The expression $edit->getClosing() of type array<integer,string>|string|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
						$retval[] = [
47
							'action' => 'add',
48
							'new' => $line,
49
							'newline' => $newline++
50
						];
51
					}
52
					break;
53
				case 'delete':
54
					foreach ( $edit->getOrig() as $line ) {
55
						$retval[] = [
56
							'action' => 'delete',
57
							'old' => $line,
58
							'oldline' => $oldline++,
59
						];
60
					}
61
					break;
62
				case 'change':
63
					foreach ( $edit->getOrig() as $key => $line ) {
64
						$retval[] = [
65
							'action' => 'change',
66
							'old' => $line,
67
							'new' => $edit->getClosing( $key ),
68
							'oldline' => $oldline++,
69
							'newline' => $newline++,
70
						];
71
					}
72
					break;
73
				case 'copy':
74
					$oldline += count( $edit->getOrig() );
75
					$newline += count( $edit->getOrig() );
76
			}
77
		}
78
79
		return $retval;
80
	}
81
82
}
83