Skip to content
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Fixture;

use PHPUnit\Framework\TestCase;

final class ConstantArraySameKeyOrder extends TestCase
{
public function test()
{
$expected = ['pack_keys' => 'DEFAULT', 'row_format' => 'REDUNDANT'];
$actual = ['pack_keys' => 'DEFAULT', 'row_format' => 'REDUNDANT'];
$this->assertEquals($expected, $actual);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Fixture;

use PHPUnit\Framework\TestCase;

final class ConstantArraySameKeyOrder extends TestCase
{
public function test()
{
$expected = ['pack_keys' => 'DEFAULT', 'row_format' => 'REDUNDANT'];
$actual = ['pack_keys' => 'DEFAULT', 'row_format' => 'REDUNDANT'];
$this->assertSame($expected, $actual);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipConstantArrayDifferentKeyOrder extends TestCase
{
public function test()
{
$expected = ['pack_keys' => 'DEFAULT', 'row_format' => 'REDUNDANT'];
$actual = ['row_format' => 'REDUNDANT', 'pack_keys' => 'DEFAULT'];
$this->assertEquals($expected, $actual);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipConstantArrayUnknownActual extends TestCase
{
public function test(array $result)
{
$expected = [1 => 2];
$this->assertEquals($expected, $result);
}
}

?>
35 changes: 30 additions & 5 deletions rules/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -107,7 +109,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->shouldSkipConstantArrayType($firstArgValue)) {
if ($this->shouldSkipConstantArrayType($firstArgValue, $args[1]->value)) {
return null;
}

Expand Down Expand Up @@ -161,15 +163,38 @@ private function shouldSkipLooseComparison(array $args): bool
return $args[0]->value instanceof String_ && is_numeric($args[0]->value->value);
}

private function shouldSkipConstantArrayType(Expr $expr): bool
private function shouldSkipConstantArrayType(Expr $expectedExpr, Expr $actualExpr): bool
{
$type = $this->nodeTypeResolver->getNativeType($expr);
$expectedType = $this->nodeTypeResolver->getNativeType($expectedExpr);

if (! $type instanceof ConstantArrayType) {
if (! $expectedType instanceof ConstantArrayType) {
return false;
}

return $this->hasNonScalarType($type);
if ($this->hasNonScalarType($expectedType)) {
return true;
}

// assertSame() compares arrays strictly, including key order, while assertEquals() ignores it;
// only safe to narrow when the actual value is a constant array with the very same keys in the same order
$actualType = $this->nodeTypeResolver->getNativeType($actualExpr);
if (! $actualType instanceof ConstantArrayType) {
return true;
}

return ! $this->hasSameKeyOrder($expectedType, $actualType);
}

private function hasSameKeyOrder(ConstantArrayType $expectedType, ConstantArrayType $actualType): bool
{
$expectedKeyTypes = $expectedType->getKeyTypes();
$actualKeyTypes = $actualType->getKeyTypes();

if (count($expectedKeyTypes) !== count($actualKeyTypes)) {
return false;
}

return array_all($expectedKeyTypes, fn (ConstantIntegerType|ConstantStringType $expectedKeyType, $position): bool => $expectedKeyType->equals($actualKeyTypes[$position]));
}

private function hasNonScalarType(ConstantArrayType $constantArrayType): bool
Expand Down
Loading