Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests;

final class SkipWithIfElse extends TestCase
{
public function test(): void
{
$someObject = $this->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;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ public function refactor(Node $node): ?Node
}

if ($firstArgumentName === 'is_a') {
/**
* @var FuncCall $firstArgumentValue
* @var array<Arg> $args
**/
/** @var FuncCall $firstArgumentValue */
$args = $firstArgumentValue->getArgs();

/** @var array<Arg> $args */
if ($args === []) {
return null;
}
Expand Down
Loading