From bbb92e9b10a729f08ea1211949c96e920999508d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 17 May 2025 18:39:45 +0700 Subject: [PATCH 1/4] [CodeQuality] Skip with if else on AddInstanceofAssertForNullableInstanceRector --- .../Fixture/skip_with_if_else.php.inc | 37 +++++++++++++++++++ .../NullableObjectAssignCollector.php | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc new file mode 100644 index 00000000..f7e76259 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc @@ -0,0 +1,37 @@ +getSomeObject(); + $value = $someObject->getSomeMethod(); + + if ($value !== null) { + $this->assertSame(123, $value); + + // we know the value here, no need to add instanceof + $value = $someObject->getSomeMethod(); + } else { + $this->assertNull($value); + } + } + + private function getSomeObject(): ?SomeClassUsedInTests + { + if (mt_rand(0, 1)) { + return new SomeClassUsedInTests(); + } + + return null; + } +} + +?> \ No newline at end of file diff --git a/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php b/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php index 681220e6..5e5f41a7 100644 --- a/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php +++ b/rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php @@ -37,7 +37,7 @@ public function collect(ClassMethod|Foreach_ $stmtsAware): VariableNameToTypeCol // first round to collect assigns foreach ((array) $stmtsAware->stmts as $stmt) { if (! $stmt instanceof Expression) { - continue; + return new VariableNameToTypeCollection([]); } if (! $stmt->expr instanceof Assign) { From 687e369709e8b66a8658097b14fb1faa5cd88abf Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 17 May 2025 18:42:25 +0700 Subject: [PATCH 2/4] update --- .../Fixture/skip_with_if_else.php.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc b/rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc index f7e76259..095918fc 100644 --- a/rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc +++ b/rules-tests/CodeQuality/Rector/ClassMethod/AddInstanceofAssertForNullableInstanceRector/Fixture/skip_with_if_else.php.inc @@ -14,13 +14,13 @@ final class SkipWithIfElse extends TestCase $someObject = $this->getSomeObject(); $value = $someObject->getSomeMethod(); - if ($value !== null) { + if ($someObject !== null) { $this->assertSame(123, $value); // we know the value here, no need to add instanceof $value = $someObject->getSomeMethod(); } else { - $this->assertNull($value); + $this->assertNull($someObject); } } From 7305eb47f45905f75882ffdaa9d9599d3f30a603 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 17 May 2025 18:42:54 +0700 Subject: [PATCH 3/4] update --- .../Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php index 0b2d8604..e69640ac 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php @@ -106,7 +106,7 @@ public function refactor(Node $node): ?Node if ($firstArgumentName === 'is_a') { /** * @var FuncCall $firstArgumentValue - * @var array $args + * @var array **/ $args = $firstArgumentValue->getArgs(); if ($args === []) { From d9b3f0aad6ec1b1219f5a4fe075c3982a4cf36d9 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 17 May 2025 18:43:49 +0700 Subject: [PATCH 4/4] update --- .../MethodCall/AssertTrueFalseToSpecificMethodRector.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php index e69640ac..7605d9c4 100644 --- a/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php +++ b/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php @@ -104,11 +104,10 @@ public function refactor(Node $node): ?Node } if ($firstArgumentName === 'is_a') { - /** - * @var FuncCall $firstArgumentValue - * @var array - **/ + /** @var FuncCall $firstArgumentValue */ $args = $firstArgumentValue->getArgs(); + + /** @var array $args */ if ($args === []) { return null; }