Skip to content

Commit

Permalink
Merge pull request #133 from wmde/fingerprintSwitch
Browse files Browse the repository at this point in the history
Use more readable switch statements in FingerprintPatcher
  • Loading branch information
JonasKress committed Apr 27, 2016
2 parents 8d5611b + b5a2701 commit 6404325
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 50 deletions.
2 changes: 1 addition & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</rule>
<rule ref="Generic.Metrics.NestingLevel">
<properties>
<property name="nestingLevel" value="3" />
<property name="nestingLevel" value="4" />
</properties>
</rule>

Expand Down
131 changes: 82 additions & 49 deletions src/Diff/Internal/FingerprintPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,34 @@ private function patchTermList( TermList $terms, Diff $patch ) {
private function patchTerm( TermList $terms, $lang, AtomicDiffOp $diffOp ) {
$hasLang = $terms->hasTermForLanguage( $lang );

if ( $diffOp instanceof DiffOpAdd ) {
if ( !$hasLang ) {
$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
}
} elseif ( $diffOp instanceof DiffOpChange ) {
if ( $hasLang
&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
) {
$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
}
} elseif ( $diffOp instanceof DiffOpRemove ) {
if ( $hasLang
&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
) {
$terms->removeByLanguage( $lang );
}
} else {
throw new PatcherException( 'Invalid terms diff' );
switch ( true ) {
case $diffOp instanceof DiffOpAdd:
/** @var DiffOpAdd $diffOp */
if ( !$hasLang ) {
$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
}
break;

case $diffOp instanceof DiffOpChange:
/** @var DiffOpChange $diffOp */
if ( $hasLang
&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
) {
$terms->setTextForLanguage( $lang, $diffOp->getNewValue() );
}
break;

case $diffOp instanceof DiffOpRemove:
/** @var DiffOpRemove $diffOp */
if ( $hasLang
&& $terms->getByLanguage( $lang )->getText() === $diffOp->getOldValue()
) {
$terms->removeByLanguage( $lang );
}
break;

default:
throw new PatcherException( 'Invalid terms diff' );
}
}

Expand Down Expand Up @@ -106,22 +116,34 @@ private function patchAliasGroupList( AliasGroupList $groups, Diff $patch ) {
private function patchAliasGroup( AliasGroupList $groups, $lang, DiffOp $diffOp ) {
$hasLang = $groups->hasGroupForLanguage( $lang );

if ( $diffOp instanceof DiffOpAdd ) {
if ( !$hasLang ) {
$groups->setAliasesForLanguage( $lang, $diffOp->getNewValue() );
}
} elseif ( $diffOp instanceof DiffOpChange ) {
$this->applyAliasGroupChange( $groups, $lang, $diffOp );
} elseif ( $diffOp instanceof DiffOpRemove ) {
if ( $hasLang
&& $groups->getByLanguage( $lang )->getAliases() === $diffOp->getOldValue()
) {
$groups->removeByLanguage( $lang );
}
} elseif ( $diffOp instanceof Diff ) {
$this->applyAliasGroupDiff( $groups, $lang, $diffOp );
} else {
throw new PatcherException( 'Invalid aliases diff' );
switch ( true ) {
case $diffOp instanceof DiffOpAdd:
/** @var DiffOpAdd $diffOp */
if ( !$hasLang ) {
$groups->setAliasesForLanguage( $lang, $diffOp->getNewValue() );
}
break;

case $diffOp instanceof DiffOpChange:
/** @var DiffOpChange $diffOp */
$this->applyAliasGroupChange( $groups, $lang, $diffOp );
break;

case $diffOp instanceof DiffOpRemove:
/** @var DiffOpRemove $diffOp */
if ( $hasLang
&& $groups->getByLanguage( $lang )->getAliases() === $diffOp->getOldValue()
) {
$groups->removeByLanguage( $lang );
}
break;

case $diffOp instanceof Diff:
$this->applyAliasGroupDiff( $groups, $lang, $diffOp );
break;

default:
throw new PatcherException( 'Invalid aliases diff' );
}
}

Expand Down Expand Up @@ -173,24 +195,35 @@ private function containsOperationsOnOldValues( Diff $diff ) {
* @return string[]
*/
private function getPatchedAliases( array $aliases, Diff $patch ) {
/** @var DiffOp $diffOp */
foreach ( $patch as $diffOp ) {
if ( $diffOp instanceof DiffOpAdd ) {
$aliases[] = $diffOp->getNewValue();
} elseif ( $diffOp instanceof DiffOpChange ) {
$key = array_search( $diffOp->getOldValue(), $aliases, true );

if ( $key !== false ) {
unset( $aliases[$key] );
switch ( true ) {
case $diffOp instanceof DiffOpAdd:
/** @var DiffOpAdd $diffOp */
$aliases[] = $diffOp->getNewValue();
}
} elseif ( $diffOp instanceof DiffOpRemove ) {
$key = array_search( $diffOp->getOldValue(), $aliases, true );
break;

if ( $key !== false ) {
unset( $aliases[$key] );
}
} else {
throw new PatcherException( 'Invalid aliases diff' );
case $diffOp instanceof DiffOpChange:
/** @var DiffOpChange $diffOp */
$key = array_search( $diffOp->getOldValue(), $aliases, true );

if ( $key !== false ) {
unset( $aliases[$key] );
$aliases[] = $diffOp->getNewValue();
}
break;

case $diffOp instanceof DiffOpRemove:
/** @var DiffOpRemove $diffOp */
$key = array_search( $diffOp->getOldValue(), $aliases, true );

if ( $key !== false ) {
unset( $aliases[$key] );
}
break;

default:
throw new PatcherException( 'Invalid aliases diff' );
}
}

Expand Down

0 comments on commit 6404325

Please sign in to comment.