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..095918fc --- /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 ($someObject !== null) { + $this->assertSame(123, $value); + + // we know the value here, no need to add instanceof + $value = $someObject->getSomeMethod(); + } else { + $this->assertNull($someObject); + } + } + + 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) { diff --git a/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php b/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php index 0b2d8604..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 $args - **/ + /** @var FuncCall $firstArgumentValue */ $args = $firstArgumentValue->getArgs(); + + /** @var array $args */ if ($args === []) { return null; }