X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fdiff%2FDairikiDiff.php;h=264c87fdfcd816bf5bf1c63d8a5f7d1a0f91cf22;hb=e3bd13db0c285f312e31bb1b7271af4628cca80c;hp=d327433fce7ef8427ada7bbbc4c6e0cbfb481cdf;hpb=ddffe4453958f13805a99e5bff58229d203c5ea5;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/diff/DairikiDiff.php b/includes/diff/DairikiDiff.php index d327433fce..6272e7e992 100644 --- a/includes/diff/DairikiDiff.php +++ b/includes/diff/DairikiDiff.php @@ -26,7 +26,11 @@ */ /** - * @todo document + * The base class for all other DiffOp classes. + * + * The classes that extend DiffOp are: DiffOpCopy, DiffOpDelete, DiffOpAdd and + * DiffOpChange. FakeDiffOp also extends DiffOp, but it is not located in this file. + * * @private * @ingroup DifferenceEngine */ @@ -93,7 +97,9 @@ abstract class DiffOp { } /** - * @todo document + * Extends DiffOp. Used to mark strings that have been + * copied from one string array to the other. + * * @private * @ingroup DifferenceEngine */ @@ -117,7 +123,9 @@ class DiffOpCopy extends DiffOp { } /** - * @todo document + * Extends DiffOp. Used to mark strings that have been + * deleted from the first string array. + * * @private * @ingroup DifferenceEngine */ @@ -138,7 +146,9 @@ class DiffOpDelete extends DiffOp { } /** - * @todo document + * Extends DiffOp. Used to mark strings that have been + * added from the first string array. + * * @private * @ingroup DifferenceEngine */ @@ -159,7 +169,9 @@ class DiffOpAdd extends DiffOp { } /** - * @todo document + * Extends DiffOp. Used to mark strings that have been + * changed from the first string array (both added and subtracted). + * * @private * @ingroup DifferenceEngine */ @@ -208,10 +220,10 @@ class DiffEngine { protected $xchanged, $ychanged; - protected $xv = array(), $yv = array(); - protected $xind = array(), $yind = array(); + protected $xv = [], $yv = []; + protected $xind = [], $yind = []; - protected $seq = array(), $in_seq = array(); + protected $seq = [], $in_seq = []; protected $lcs = 0; @@ -234,14 +246,14 @@ class DiffEngine { $n_from = count( $from_lines ); $n_to = count( $to_lines ); - $edits = array(); + $edits = []; $xi = $yi = 0; while ( $xi < $n_from || $yi < $n_to ) { - assert( '$yi < $n_to || $this->xchanged[$xi]' ); - assert( '$xi < $n_from || $this->ychanged[$yi]' ); + assert( $yi < $n_to || $this->xchanged[$xi] ); + assert( $xi < $n_from || $this->ychanged[$yi] ); // Skip matching "snake". - $copy = array(); + $copy = []; while ( $xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi] ) { @@ -253,12 +265,12 @@ class DiffEngine { } // Find deletes & adds. - $delete = array(); + $delete = []; while ( $xi < $n_from && $this->xchanged[$xi] ) { $delete[] = $from_lines[$xi++]; } - $add = array(); + $add = []; while ( $yi < $n_to && $this->ychanged[$yi] ) { $add[] = $to_lines[$yi++]; } @@ -293,12 +305,12 @@ class DiffEngine { // old diff $n_from = count( $from_lines ); $n_to = count( $to_lines ); - $this->xchanged = $this->ychanged = array(); - $this->xv = $this->yv = array(); - $this->xind = $this->yind = array(); - unset( $this->seq ); - unset( $this->in_seq ); - unset( $this->lcs ); + $this->xchanged = $this->ychanged = []; + $this->xv = $this->yv = []; + $this->xind = $this->yind = []; + $this->seq = []; + $this->in_seq = []; + $this->lcs = 0; // Skip leading common lines. for ( $skip = 0; $skip < $n_from && $skip < $n_to; $skip++ ) { @@ -324,7 +336,8 @@ class DiffEngine { for ( $yi = $skip; $yi < $n_to - $endskip; $yi++ ) { $line = $to_lines[$yi]; - if ( ( $this->ychanged[$yi] = empty( $xhash[$this->lineHash( $line )] ) ) ) { + $this->ychanged[$yi] = empty( $xhash[$this->lineHash( $line )] ); + if ( $this->ychanged[$yi] ) { continue; } $yhash[$this->lineHash( $line )] = 1; @@ -333,7 +346,8 @@ class DiffEngine { } for ( $xi = $skip; $xi < $n_from - $endskip; $xi++ ) { $line = $from_lines[$xi]; - if ( ( $this->xchanged[$xi] = empty( $yhash[$this->lineHash( $line )] ) ) ) { + $this->xchanged[$xi] = empty( $yhash[$this->lineHash( $line )] ); + if ( $this->xchanged[$xi] ) { continue; } $this->xv[] = $line; @@ -392,7 +406,7 @@ class DiffEngine { // Things seems faster (I'm not sure I understand why) // when the shortest sequence in X. $flip = true; - list( $xoff, $xlim, $yoff, $ylim ) = array( $yoff, $ylim, $xoff, $xlim ); + list( $xoff, $xlim, $yoff, $ylim ) = [ $yoff, $ylim, $xoff, $xlim ]; } if ( $flip ) { @@ -407,8 +421,8 @@ class DiffEngine { $this->lcs = 0; $this->seq[0] = $yoff - 1; - $this->in_seq = array(); - $ymids[0] = array(); + $this->in_seq = []; + $ymids[0] = []; $numer = $xlim - $xoff + $nchunks - 1; $x = $xoff; @@ -420,7 +434,7 @@ class DiffEngine { } $x1 = $xoff + (int)( ( $numer + ( $xlim - $xoff ) * $chunk ) / $nchunks ); - // @codingStandardsIgnoreFile Ignore Squiz.WhiteSpace.SemicolonSpacing.Incorrect + // @codingStandardsIgnoreStart Ignore Squiz.WhiteSpace.SemicolonSpacing.Incorrect for ( ; $x < $x1; $x++ ) { // @codingStandardsIgnoreEnd $line = $flip ? $this->yv[$x] : $this->xv[$x]; @@ -434,7 +448,7 @@ class DiffEngine { while ( list( , $y ) = each( $matches ) ) { if ( empty( $this->in_seq[$y] ) ) { $k = $this->lcsPos( $y ); - assert( '$k > 0' ); + assert( $k > 0 ); $ymids[$k] = $ymids[$k - 1]; break; } @@ -442,31 +456,31 @@ class DiffEngine { while ( list( , $y ) = each( $matches ) ) { if ( $y > $this->seq[$k - 1] ) { - assert( '$y < $this->seq[$k]' ); + assert( $y < $this->seq[$k] ); // Optimization: this is a common case: - // next match is just replacing previous match. + // next match is just replacing previous match. $this->in_seq[$this->seq[$k]] = false; $this->seq[$k] = $y; $this->in_seq[$y] = 1; } elseif ( empty( $this->in_seq[$y] ) ) { $k = $this->lcsPos( $y ); - assert( '$k > 0' ); + assert( $k > 0 ); $ymids[$k] = $ymids[$k - 1]; } } } } - $seps[] = $flip ? array( $yoff, $xoff ) : array( $xoff, $yoff ); + $seps[] = $flip ? [ $yoff, $xoff ] : [ $xoff, $yoff ]; $ymid = $ymids[$this->lcs]; for ( $n = 0; $n < $nchunks - 1; $n++ ) { $x1 = $xoff + (int)( ( $numer + ( $xlim - $xoff ) * $n ) / $nchunks ); $y1 = $ymid[$n] + 1; - $seps[] = $flip ? array( $y1, $x1 ) : array( $x1, $y1 ); + $seps[] = $flip ? [ $y1, $x1 ] : [ $x1, $y1 ]; } - $seps[] = $flip ? array( $ylim, $xlim ) : array( $xlim, $ylim ); + $seps[] = $flip ? [ $ylim, $xlim ] : [ $xlim, $ylim ]; - return array( $this->lcs, $seps ); + return [ $this->lcs, $seps ]; } /** @@ -493,7 +507,7 @@ class DiffEngine { } } - assert( '$ypos != $this->seq[$end]' ); + assert( $ypos != $this->seq[$end] ); $this->in_seq[$this->seq[$end]] = false; $this->seq[$end] = $ypos; @@ -581,7 +595,7 @@ class DiffEngine { $i = 0; $j = 0; - assert( 'count($lines) == count($changed)' ); + assert( count( $lines ) == count( $changed ) ); $len = count( $lines ); $other_len = count( $other_changed ); @@ -602,7 +616,7 @@ class DiffEngine { } while ( $i < $len && !$changed[$i] ) { - assert( '$j < $other_len && ! $other_changed[$j]' ); + assert( $j < $other_len && ! $other_changed[$j] ); $i++; $j++; while ( $j < $other_len && $other_changed[$j] ) { @@ -639,11 +653,11 @@ class DiffEngine { while ( $start > 0 && $changed[$start - 1] ) { $start--; } - assert( '$j > 0' ); + assert( $j > 0 ); while ( $other_changed[--$j] ) { continue; } - assert( '$j >= 0 && !$other_changed[$j]' ); + assert( $j >= 0 && !$other_changed[$j] ); } /* @@ -667,7 +681,7 @@ class DiffEngine { $i++; } - assert( '$j < $other_len && ! $other_changed[$j]' ); + assert( $j < $other_len && ! $other_changed[$j] ); $j++; if ( $j < $other_len && $other_changed[$j] ) { $corresponding = $i; @@ -685,11 +699,11 @@ class DiffEngine { while ( $corresponding < $i ) { $changed[--$start] = 1; $changed[--$i] = 0; - assert( '$j > 0' ); + assert( $j > 0 ); while ( $other_changed[--$j] ) { continue; } - assert( '$j >= 0 && !$other_changed[$j]' ); + assert( $j >= 0 && !$other_changed[$j] ); } } } @@ -741,7 +755,7 @@ class Diff { */ public function reverse() { $rev = $this; - $rev->edits = array(); + $rev->edits = []; /** @var DiffOp $edit */ foreach ( $this->edits as $edit ) { $rev->edits[] = $edit->reverse(); @@ -792,7 +806,7 @@ class Diff { * @return string[] The original sequence of strings. */ public function orig() { - $lines = array(); + $lines = []; foreach ( $this->edits as $edit ) { if ( $edit->orig ) { @@ -812,7 +826,7 @@ class Diff { * @return string[] The sequence of strings. */ public function closing() { - $lines = array(); + $lines = []; foreach ( $this->edits as $edit ) { if ( $edit->closing ) { @@ -853,8 +867,8 @@ class MappedDiff extends Diff { public function __construct( $from_lines, $to_lines, $mapped_from_lines, $mapped_to_lines ) { - assert( 'count( $from_lines ) == count( $mapped_from_lines )' ); - assert( 'count( $to_lines ) == count( $mapped_to_lines )' ); + assert( count( $from_lines ) == count( $mapped_from_lines ) ); + assert( count( $to_lines ) == count( $mapped_to_lines ) ); parent::__construct( $mapped_from_lines, $mapped_to_lines ); @@ -889,7 +903,7 @@ class HWLDFWordAccumulator { public $insClass = ' class="diffchange diffchange-inline"'; public $delClass = ' class="diffchange diffchange-inline"'; - private $lines = array(); + private $lines = []; private $line = ''; private $group = ''; private $tag = ''; @@ -945,7 +959,7 @@ class HWLDFWordAccumulator { $this->flushLine( $tag ); $word = substr( $word, 1 ); } - assert( '!strstr( $word, "\n" )' ); + assert( !strstr( $word, "\n" ) ); $this->group .= $word; } } @@ -988,8 +1002,8 @@ class WordLevelDiff extends MappedDiff { */ private function split( $lines ) { - $words = array(); - $stripped = array(); + $words = []; + $stripped = []; $first = true; foreach ( $lines as $line ) { # If the line is too long, just pretend the entire line is one big word @@ -1004,7 +1018,7 @@ class WordLevelDiff extends MappedDiff { $words[] = $line; $stripped[] = $line; } else { - $m = array(); + $m = []; if ( preg_match_all( '/ ( [^\S\n]+ | [0-9_A-Za-z\x80-\xff]+ | . ) (?: (?!< \n) [^\S\n])? /xs', $line, $m ) ) { @@ -1018,7 +1032,7 @@ class WordLevelDiff extends MappedDiff { } } - return array( $words, $stripped ); + return [ $words, $stripped ]; } /**