Skip to content

AssertEqualsToSameRector: skip arrays whose actual value key order is not provably identical#727

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-assert-equals-to-same-array-key-order
Jul 14, 2026
Merged

AssertEqualsToSameRector: skip arrays whose actual value key order is not provably identical#727
TomasVotruba merged 1 commit into
mainfrom
fix-assert-equals-to-same-array-key-order

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9813.

assertEquals() compares arrays with == (key order ignored), while assertSame() uses === (key order enforced). The rule narrowed assertEquals()assertSame() on constant arrays after only inspecting the expected argument, so it broke passing tests whenever the actual value had the same pairs in a different order.

Bad (before)

$expect = ['pack_keys' => 'DEFAULT', 'row_format' => 'REDUNDANT'];
self::assertEquals($expect, getActual()); // getActual() returns the same pairs, different key order
-self::assertEquals($expect, getActual());
+self::assertSame($expect, getActual()); // now FAILS: === is order-sensitive

Good (after)

Narrowing now only happens when the actual value is also a constant array with the same keys in the same order:

// skipped - actual key order unknown (runtime array / param)
$this->assertEquals(['a' => 1], $result);

// skipped - keys in different order
$this->assertEquals(['a' => 1, 'b' => 2], ['b' => 2, 'a' => 1]);

// still narrowed - identical key order, scalar values
$this->assertEquals(['a' => 1, 'b' => 2], ['a' => 1, 'b' => 2]); // -> assertSame

Scalar (non-array) narrowing is unchanged.

Fix

shouldSkipConstantArrayType() now also receives the actual argument and skips unless it is a ConstantArrayType whose key sequence matches the expected one (hasSameKeyOrder()).

The old assert_same_constant_array fixture (narrowed an unknown-order array param — the exact unsafe case reported) is replaced by skip_constant_array_unknown_actual, plus new fixtures for the different-order (skip) and same-order (narrow) cases.

@TomasVotruba TomasVotruba merged commit b12526b into main Jul 14, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the fix-assert-equals-to-same-array-key-order branch July 14, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

AssertEqualsToSameRector incorrectly applied on arrays

1 participant