Skip to content

Commit

Permalink
Replaced setComparisonCallback with constructor argument
Browse files Browse the repository at this point in the history
Fixes #76
  • Loading branch information
JeroenDeDauw committed Apr 21, 2017
1 parent a5bd072 commit 79fd2bb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 74 deletions.
2 changes: 2 additions & 0 deletions RELEASE-NOTES.md
Expand Up @@ -10,13 +10,15 @@ Latest release:
* Added return type hints where possible
* Added scalar type hints where possible
* Added strict_types declare statements to all files
* Added `StubValueComparer` to help test services that use a `ValueComparer`

#### Breaking changes

* Dropped support for PHP 5.x
* Dropped class aliases deprecated since Diff 1.0
* Removed ListDiff and MapDiff, deprecated since Diff 0.5
* Removed ListDiffer::MODE_NATIVE and ListDiffer::MODE_STRICT, deprecated since Diff 0.8
* Removed MapDiffer::setComparisonCallback in favour of a new constructor argument

## Version 2.1 (2016-09-01)

Expand Down
27 changes: 27 additions & 0 deletions src/Comparer/StubValueComparer.php
@@ -0,0 +1,27 @@
<?php

declare( strict_types = 1 );

namespace Diff\Comparer;

/**
* @since 3.0
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class StubValueComparer implements ValueComparer {

private $returnValue;

public function __construct( bool $returnValue ) {
$this->returnValue = $returnValue;
}

// @codingStandardsIgnoreStart
public function valuesAreEqual( $firstValue, $secondValue ): bool {
// @codingStandardsIgnoreEnd
return $this->returnValue;
}

}
59 changes: 7 additions & 52 deletions src/Differ/MapDiffer.php
Expand Up @@ -4,6 +4,8 @@

namespace Diff\Differ;

use Diff\Comparer\StrictComparer;
use Diff\Comparer\ValueComparer;
use Diff\DiffOp\Diff\Diff;
use Diff\DiffOp\DiffOp;
use Diff\DiffOp\DiffOpAdd;
Expand All @@ -23,43 +25,17 @@
*/
class MapDiffer implements Differ {

/**
* @var bool
*/
private $recursively;

/**
* @var Differ
*/
private $listDiffer;
private $valueComparer;

/**
* @var callable|null
* The third argument ($comparer) was added in 3.0
*/
private $comparisonCallback = null;

/**
* @since 0.4
*
* @param bool $recursively
* @param Differ|null $listDiffer
*/
public function __construct( bool $recursively = false, Differ $listDiffer = null ) {
public function __construct( bool $recursively = false, Differ $listDiffer = null, ValueComparer $comparer = null ) {
$this->recursively = $recursively;

$this->listDiffer = $listDiffer ?? new ListDiffer();
}

/**
* Sets a callback to use for comparison. The callback should accept two
* arguments.
*
* @since 0.5
*
* @param callable $comparisonCallback
*/
public function setComparisonCallback( callable $comparisonCallback ) {
$this->comparisonCallback = $comparisonCallback;
$this->valueComparer = $comparer ?? new StrictComparer();
}

/**
Expand Down Expand Up @@ -184,33 +160,12 @@ private function arrayDiffAssoc( array $from, array $to ): array {
$diff = array();

foreach ( $from as $key => $value ) {
if ( !array_key_exists( $key, $to ) || !$this->valuesAreEqual( $to[$key], $value ) ) {
if ( !array_key_exists( $key, $to ) || !$this->valueComparer->valuesAreEqual( $to[$key], $value ) ) {
$diff[$key] = $value;
}
}

return $diff;
}

/**
* @param mixed $value0
* @param mixed $value1
*
* @return bool
* @throws Exception
*/
private function valuesAreEqual( $value0, $value1 ): bool {
if ( $this->comparisonCallback === null ) {
return $value0 === $value1;
}

$areEqual = call_user_func_array( $this->comparisonCallback, array( $value0, $value1 ) );

if ( !is_bool( $areEqual ) ) {
throw new Exception( 'Comparison callback returned a non-boolean value' );
}

return $areEqual;
}

}
25 changes: 3 additions & 22 deletions tests/phpunit/Differ/MapDifferTest.php
Expand Up @@ -5,6 +5,7 @@
namespace Diff\Tests\Differ;

use Diff\ArrayComparer\NativeArrayComparer;
use Diff\Comparer\StubValueComparer;
use Diff\Differ\Differ;
use Diff\Differ\ListDiffer;
use Diff\Differ\MapDiffer;
Expand Down Expand Up @@ -315,11 +316,7 @@ public function testDoDiff( $old, $new, $expected, $message = '', $recursively =
}

public function testCallbackComparisonReturningFalse() {
$differ = new MapDiffer();

$differ->setComparisonCallback( function( $foo, $bar ) {
return false;
} );
$differ = new MapDiffer( false, null, new StubValueComparer( false ) );

$actual = $differ->doDiff( array( 1, '2', 3 ), array( 1, '2', 4, 'foo' ) );

Expand All @@ -337,11 +334,7 @@ public function testCallbackComparisonReturningFalse() {
}

public function testCallbackComparisonReturningTrue() {
$differ = new MapDiffer();

$differ->setComparisonCallback( function( $foo, $bar ) {
return true;
} );
$differ = new MapDiffer( false, null, new StubValueComparer( true ) );

$actual = $differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) );

Expand All @@ -354,16 +347,4 @@ public function testCallbackComparisonReturningTrue() {
);
}

public function testCallbackComparisonReturningNyanCat() {
$differ = new MapDiffer();

$differ->setComparisonCallback( function( $foo, $bar ) {
return '~=[,,_,,]:3';
} );

$this->expectException( 'Exception' );

$differ->doDiff( array( 1, '2', 'baz' ), array( 1, 'foo', '2' ) );
}

}

0 comments on commit 79fd2bb

Please sign in to comment.