From 01b44047fe871fe37a31932f37b11d4ce5a450e2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 4 Mar 2025 23:06:31 +0700 Subject: [PATCH 1/3] [PHPUnit] Handle mix AssertCountWithZeroToAssertEmptyRector + AssertEmptyNullableObjectToAssertInstanceofRector cause invalid assertNotInstanceof() --- tests/Issues/EmptyUnion/EmptyUnionTest.php | 28 +++++++++++++++++++ .../EmptyUnion/Fixture/some_test.php.inc | 19 +++++++++++++ .../EmptyUnion/config/configured_rule.php | 14 ++++++++++ 3 files changed, 61 insertions(+) create mode 100644 tests/Issues/EmptyUnion/EmptyUnionTest.php create mode 100644 tests/Issues/EmptyUnion/Fixture/some_test.php.inc create mode 100644 tests/Issues/EmptyUnion/config/configured_rule.php 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, + ]); +}; From 3d183ca59149b2afecf884f73e164560917d494f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 4 Mar 2025 23:16:44 +0700 Subject: [PATCH 2/3] Fix --- .../MethodCall/AssertCountWithZeroToAssertEmptyRector.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php b/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php index 48c822d8..e40788bf 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(); From d95018d6cfb5fdfc90aaba31a7eaafc00aa65ca8 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 4 Mar 2025 16:17:34 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- .../MethodCall/AssertCountWithZeroToAssertEmptyRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php b/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php index e40788bf..d45491b4 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php @@ -75,7 +75,7 @@ public function refactor(Node $node): MethodCall|StaticCall|null return null; } - $secondType = $this->getType($node->getArgs()[1]->value); + $secondType = $this->getType($node->getArgs()[1]->value); if ($secondType instanceof UnionType) { return null; }