Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListPatcher: use non-strict comparison when removing objects #92

Merged
merged 1 commit into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Patcher/ListPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function patch( array $base, Diff $diff ): array {
if ( $diffOp instanceof DiffOpAdd ) {
$base[] = $diffOp->getNewValue();
} elseif ( $diffOp instanceof DiffOpRemove ) {
$key = array_search( $diffOp->getOldValue(), $base, true );
$needle = $diffOp->getOldValue();
$key = array_search( $needle, $base, !is_object( $needle ) );

if ( $key === false ) {
$this->handleError( 'Cannot remove an element from a list if it is not present' );
Expand Down
20 changes: 20 additions & 0 deletions tests/phpunit/Patcher/ListPatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Diff\Patcher\ListPatcher;
use Diff\Patcher\Patcher;
use Diff\Tests\DiffTestCase;
use stdClass;

/**
* @covers Diff\Patcher\ListPatcher
Expand Down Expand Up @@ -91,9 +92,28 @@ public function patchProvider() {

$argLists[] = array( $patcher, $base, $diff, $expected );

$patcher = new ListPatcher();
$base = array(
$this->newObject( 'foo' ),
$this->newObject( 'bar' ),
);
$diff = new Diff( array(
new DiffOpRemove( $this->newObject( 'foo') ),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaceeeeeeeeeeeeee

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed on master skipping the PR. PHPCS wake up!

new DiffOpAdd( $this->newObject( 'baz' ) ),
) );
$expected = array( $this->newObject( 'bar' ), $this->newObject( 'baz' ) );

$argLists[] = array( $patcher, $base, $diff, $expected );

return $argLists;
}

private function newObject( $value ) : stdClass {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First time I see stfClass as return type hint. Might also be the last, since as of PHP 7.2 object is probably the better one

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdclass as a return type hint indeed seems awkward. This is what using ancient language versions does with you

$object = new stdClass();
$object->element = $value;
return $object;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return (object)[ 'element' => $value ] is a clever 1-liner to do the same. ;-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way you also avoid the assignment to a field that was not explicitly defined

}

/**
* @dataProvider patchProvider
*
Expand Down