Skip to content

Commit bbb92e9

Browse files
committed
[CodeQuality] Skip with if else on AddInstanceofAssertForNullableInstanceRector
1 parent 76ce3a5 commit bbb92e9

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;
9+
10+
final class SkipWithIfElse extends TestCase
11+
{
12+
public function test(): void
13+
{
14+
$someObject = $this->getSomeObject();
15+
$value = $someObject->getSomeMethod();
16+
17+
if ($value !== null) {
18+
$this->assertSame(123, $value);
19+
20+
// we know the value here, no need to add instanceof
21+
$value = $someObject->getSomeMethod();
22+
} else {
23+
$this->assertNull($value);
24+
}
25+
}
26+
27+
private function getSomeObject(): ?SomeClassUsedInTests
28+
{
29+
if (mt_rand(0, 1)) {
30+
return new SomeClassUsedInTests();
31+
}
32+
33+
return null;
34+
}
35+
}
36+
37+
?>

rules/CodeQuality/NodeAnalyser/NullableObjectAssignCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function collect(ClassMethod|Foreach_ $stmtsAware): VariableNameToTypeCol
3737
// first round to collect assigns
3838
foreach ((array) $stmtsAware->stmts as $stmt) {
3939
if (! $stmt instanceof Expression) {
40-
continue;
40+
return new VariableNameToTypeCollection([]);
4141
}
4242

4343
if (! $stmt->expr instanceof Assign) {

0 commit comments

Comments
 (0)