Skip to content

Commit fbf0a2f

Browse files
authored
[CodeQuality] Skip with if else condition result on AddInstanceofAssertForNullableInstanceRector (#489)
* [CodeQuality] Skip with if else on AddInstanceofAssertForNullableInstanceRector * update * update * update
1 parent 76ce3a5 commit fbf0a2f

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
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 ($someObject !== 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($someObject);
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) {

rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,10 @@ public function refactor(Node $node): ?Node
104104
}
105105

106106
if ($firstArgumentName === 'is_a') {
107-
/**
108-
* @var FuncCall $firstArgumentValue
109-
* @var array<Arg> $args
110-
**/
107+
/** @var FuncCall $firstArgumentValue */
111108
$args = $firstArgumentValue->getArgs();
109+
110+
/** @var array<Arg> $args */
112111
if ($args === []) {
113112
return null;
114113
}

0 commit comments

Comments
 (0)