From 561b75e01fb22104ab7d4f7f0ee8a22102df51cd Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Thu, 26 Nov 2026 18:41:03 +0900 Subject: [PATCH 1/2] fix: Remove all matching values in RemoveOperation::_apply RemoveOperation::_apply looped over every value to remove for each existing value and appended the existing value once per non-matching target. With two or more values to remove, matched values were kept and unmatched values were duplicated, e.g. removing [1, 2] from [1, 2, 3] returned [1, 2, 3, 3]. Removing an empty list also cleared the array. The broken result is not only local: when a remove follows a set on the same key, the operations merge into a SetOperation built from _apply, so setArray('arr', [1, 2, 3]) followed by remove('arr', [1, 2]) saved [1, 2, 3, 3] to the server. Keep an existing value unless it matches any of the values to remove. The match rules (strict equality, ParseObjects by objectId when not dirty) are unchanged. --- src/Parse/Internal/RemoveOperation.php | 42 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/Parse/Internal/RemoveOperation.php b/src/Parse/Internal/RemoveOperation.php index bee46f58..c4b57b7b 100644 --- a/src/Parse/Internal/RemoveOperation.php +++ b/src/Parse/Internal/RemoveOperation.php @@ -116,24 +116,36 @@ public function _apply($oldValue, $obj, $key) $newValue = []; foreach ($oldValue as $oldObject) { - foreach ($this->objects as $newObject) { - if ($oldObject instanceof ParseObject) { - if ($newObject instanceof ParseObject - && !$oldObject->isDirty() - && $oldObject->getObjectId() == $newObject->getObjectId() - ) { - // found the object, won't add it. - } else { - $newValue[] = $oldObject; - } - } else { - if ($oldObject !== $newObject) { - $newValue[] = $oldObject; - } - } + if (!$this->isRemoved($oldObject)) { + $newValue[] = $oldObject; } } return $newValue; } + + /** + * Checks if a value matches any of the objects to remove. + * + * @param mixed $oldObject Value to check. + * + * @return bool + */ + private function isRemoved($oldObject) + { + foreach ($this->objects as $newObject) { + if ($oldObject instanceof ParseObject) { + if ($newObject instanceof ParseObject + && !$oldObject->isDirty() + && $oldObject->getObjectId() == $newObject->getObjectId() + ) { + return true; + } + } elseif ($oldObject === $newObject) { + return true; + } + } + + return false; + } } From 2eb00c9f69a2af80785f7f33839ad01fb8975977 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Thu, 26 Nov 2026 18:42:10 +0900 Subject: [PATCH 2/2] test: Cover removing multiple values in RemoveOperation Add cases for RemoveOperation::_apply removing two or more values, removing every duplicate, removing nothing, and removing ParseObjects by objectId. Also check that merging with a previous SetOperation produces the correct value, since that value is sent to the server. These cases fail without the fix in RemoveOperation::_apply. --- tests/Parse/RemoveOperationTest.php | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/Parse/RemoveOperationTest.php b/tests/Parse/RemoveOperationTest.php index 794d9989..f137b848 100644 --- a/tests/Parse/RemoveOperationTest.php +++ b/tests/Parse/RemoveOperationTest.php @@ -9,11 +9,17 @@ use Parse\Internal\DeleteOperation; use Parse\Internal\RemoveOperation; use Parse\Internal\SetOperation; +use Parse\ParseObject; use PHPUnit\Framework\TestCase; class RemoveOperationTest extends TestCase { + public static function setUpBeforeClass() : void + { + Helper::setUp(); + } + /** * @group remove-op */ @@ -82,4 +88,66 @@ public function testEmptyApply() ]); $this->assertEmpty($removeOp->_apply([], null, null)); } + + /** + * @group remove-op + */ + public function testApplyRemovesMultipleValues() + { + $removeOp = new RemoveOperation([1, 2]); + $this->assertSame([3], $removeOp->_apply([1, 2, 3], null, null)); + } + + /** + * @group remove-op + */ + public function testApplyRemovesAllDuplicates() + { + $removeOp = new RemoveOperation(['a', 'b']); + $this->assertSame( + ['c'], + $removeOp->_apply(['a', 'b', 'a', 'c', 'b'], null, null) + ); + } + + /** + * @group remove-op + */ + public function testApplyWithNothingToRemove() + { + $removeOp = new RemoveOperation([]); + $this->assertSame([1, 2], $removeOp->_apply([1, 2], null, null)); + } + + /** + * @group remove-op + */ + public function testApplyRemovesParseObjectsByObjectId() + { + $removeOp = new RemoveOperation([ + ParseObject::create('TestObject', 'id1'), + ParseObject::create('TestObject', 'id2'), + ]); + $kept = ParseObject::create('TestObject', 'id3'); + + $result = $removeOp->_apply([ + ParseObject::create('TestObject', 'id1'), + ParseObject::create('TestObject', 'id2'), + $kept, + ], null, null); + + $this->assertSame([$kept], $result); + } + + /** + * @group remove-op-merge + */ + public function testMergeWithSetRemovesMultipleValues() + { + $removeOp = new RemoveOperation([1, 2]); + $merged = $removeOp->_mergeWithPrevious(new SetOperation([1, 2, 3])); + + $this->assertTrue($merged instanceof SetOperation); + $this->assertSame([3], $merged->getValue()); + } }