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,91 @@
<?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 MultipleAssert extends TestCase
{
public function test(): void
{
$someObject = $this->getSomeObject();
$value = $someObject->getSomeMethod();

$this->assertSame(123, $value);

$someObject2 = $this->getSomeObject2();
$value2 = $someObject2->getSomeMethod();

$this->assertSame(123, $value2);
}

private function getSomeObject(): ?SomeClassUsedInTests
{
if (mt_rand(0, 1)) {
return new SomeClassUsedInTests();
}

return null;
}

private function getSomeObject2(): ?SomeClassUsedInTests
{
if (mt_rand(0, 1)) {
return new SomeClassUsedInTests();
}

return null;
}
}

?>
-----
<?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 MultipleAssert extends TestCase
{
public function test(): void
{
$someObject = $this->getSomeObject();
$this->assertInstanceof(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests::class, $someObject);
$value = $someObject->getSomeMethod();

$this->assertSame(123, $value);

$someObject2 = $this->getSomeObject2();
$this->assertInstanceof(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector\Source\SomeClassUsedInTests::class, $someObject2);
$value2 = $someObject2->getSomeMethod();

$this->assertSame(123, $value2);
}

private function getSomeObject(): ?SomeClassUsedInTests
{
if (mt_rand(0, 1)) {
return new SomeClassUsedInTests();
}

return null;
}

private function getSomeObject2(): ?SomeClassUsedInTests
{
if (mt_rand(0, 1)) {
return new SomeClassUsedInTests();
}

return null;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function refactor(Node $node): ?Node
$hasChanged = false;
$variableNameToTypeCollection = $this->nullableObjectAssignCollector->collect($node);

$next = 0;
foreach ($node->stmts as $key => $stmt) {
// has callable on nullable variable of already collected name?
$matchedNullableVariableNameToType = $this->matchedNullableVariableNameToType(
Expand All @@ -130,13 +131,15 @@ public function refactor(Node $node): ?Node

// adding type here + popping the variable name out
$assertInstanceofExpression = $this->createAssertInstanceof($matchedNullableVariableNameToType);
array_splice($node->stmts, $key, 0, [$assertInstanceofExpression]);
array_splice($node->stmts, $key + $next, 0, [$assertInstanceofExpression]);

// remove variable name from nullable ones
$hasChanged = true;

// from now on, the variable is not nullable, remove to avoid double asserts
$variableNameToTypeCollection->remove($matchedNullableVariableNameToType);

++$next;
}

if (! $hasChanged) {
Expand Down
Loading