Skip to content

Commit 97e546d

Browse files
authored
[CodeQuality] Skip has object inside array on UseIdenticalOverEqualWithSameTypeRector (#7418)
* [CodeQuality] Skip has object inside array on UseIdenticalOverEqualWithSameTypeRector * [CodeQuality] Skip has object inside array on UseIdenticalOverEqualWithSameTypeRector * Fix
1 parent 1b4fb1c commit 97e546d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector\Fixture;
4+
5+
/**
6+
* @see https://3v4l.org/qU0Ou
7+
*/
8+
class SkipObjectsInsideArray
9+
{
10+
public function run(\stdClass $firstValue, \stdClass $secondValue)
11+
{
12+
return [$firstValue] == [$secondValue];
13+
}
14+
}

rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\MixedType;
1717
use PHPStan\Type\StringType;
1818
use PHPStan\Type\Type;
19+
use PHPStan\Type\TypeTraverser;
1920
use Rector\Rector\AbstractRector;
2021
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2122
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -78,7 +79,7 @@ public function refactor(Node $node): ?Node
7879
$rightStaticType = $this->nodeTypeResolver->getNativeType($node->right);
7980

8081
// objects can be different by content
81-
if (! $leftStaticType->isObject()->no() || ! $rightStaticType->isObject()->no()) {
82+
if ($this->hasObjectType($leftStaticType) || $this->hasObjectType($rightStaticType)) {
8283
return null;
8384
}
8485

@@ -96,6 +97,21 @@ public function refactor(Node $node): ?Node
9697
return $this->processIdenticalOrNotIdentical($node);
9798
}
9899

100+
private function hasObjectType(Type $type): bool
101+
{
102+
$hasObjecType = false;
103+
TypeTraverser::map($type, function (Type $type, callable $traverseCallback) use (&$hasObjecType): Type {
104+
// maybe has object type? mark as object type
105+
if (! $type->isObject()->no()) {
106+
$hasObjecType = true;
107+
}
108+
109+
return $traverseCallback($type);
110+
});
111+
112+
return $hasObjecType;
113+
}
114+
99115
private function normalizeScalarType(Type $type): Type
100116
{
101117
if ($type->isString()->yes()) {

0 commit comments

Comments
 (0)