Skip to content

Commit

Permalink
Refactor the switch in Set
Browse files Browse the repository at this point in the history
  • Loading branch information
Taluu committed Dec 27, 2015
1 parent a2afd1d commit 9235570
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions src/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,41 +178,31 @@ private function computeEntry(AbstractSnapshot $old, AbstractSnapshot $new, $key
return new Removal($this->getRawData($old[$key]));
}

$values = ['old' => $this->getRawData($old[$key]),
'new' => $this->getRawData($new[$key])];

switch (true) {
// type verification
case gettype($old[$key]) !== gettype($new[$key]):
return new Modification($values['old'], $values['new']);

// could we compare two snapshots ?
case $old[$key] instanceof AbstractSnapshot:
if (!$new[$key] instanceof AbstractSnapshot) {
return new Modification($values['old'], $values['new']);
}

if (!$old[$key]->isComparable($new[$key])) {
return new Modification($values['old'], $values['new']);
}

$set = new static;
$set->compute($old[$key], $new[$key]);

if (0 < count($set)) {
return $set;
}

return null;

// unknown type : compare raw data
case $values['old'] !== $values['new']:
return new Modification($values['old'], $values['new']);
// PHPUnit coverage wtf start
// @codeCoverageIgnoreStart
$raw = [
'old' => $this->getRawData($old[$key]),
'new' => $this->getRawData($new[$key])
];

if (!$old[$key] instanceof AbstractSnapshot) {
return $raw['old'] !== $raw['new'] ? new Modification($raw['old'], $raw['new']) : null;
}

if (!$new[$key] instanceof AbstractSnapshot) {
return new Modification($raw['old'], $raw['new']);
}

if (!$new[$key]->isComparable($old[$key])) {
return new Modification($raw['old'], $raw['new']);
}
// @codeCoverageIgnoreEnd
// PHPUnit coverage wtf end

$set = new static;
$set->compute($old[$key], $new[$key]);

if (0 < count($set)) {
return $set;
}

return null;
}

/**
Expand Down

0 comments on commit 9235570

Please sign in to comment.