diff --git a/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php b/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php index 48c822d8..d45491b4 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php @@ -75,6 +75,11 @@ public function refactor(Node $node): MethodCall|StaticCall|null return null; } + $secondType = $this->getType($node->getArgs()[1]->value); + if ($secondType instanceof UnionType) { + return null; + } + $value = ($type->getConstantScalarValues()[0] ?? null); if ($value === 0) { $args = $node->getArgs(); diff --git a/tests/Issues/EmptyUnion/EmptyUnionTest.php b/tests/Issues/EmptyUnion/EmptyUnionTest.php new file mode 100644 index 00000000..172f2461 --- /dev/null +++ b/tests/Issues/EmptyUnion/EmptyUnionTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/EmptyUnion/Fixture/some_test.php.inc b/tests/Issues/EmptyUnion/Fixture/some_test.php.inc new file mode 100644 index 00000000..0ed8cb7c --- /dev/null +++ b/tests/Issues/EmptyUnion/Fixture/some_test.php.inc @@ -0,0 +1,19 @@ +assertCount(0, $this->someCall()); + } + + public function someCall(): ?ArrayIterator + { + return new ArrayIterator([]); + } +} diff --git a/tests/Issues/EmptyUnion/config/configured_rule.php b/tests/Issues/EmptyUnion/config/configured_rule.php new file mode 100644 index 00000000..50002ef0 --- /dev/null +++ b/tests/Issues/EmptyUnion/config/configured_rule.php @@ -0,0 +1,14 @@ +rules([ + AssertCountWithZeroToAssertEmptyRector::class, + AssertEmptyNullableObjectToAssertInstanceofRector::class, + ]); +};