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
4 changes: 4 additions & 0 deletions config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@

// avoid call on nullable object
AddInstanceofAssertForNullableInstanceRector::class,

// enable next after testing
// \Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector::class,

AssertArrayCastedObjectToAssertSameRector::class,

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddInstanceofAssertForNullableArgumentRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

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

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

final class PassingNullableArg extends TestCase
{
public function test(): void
{
$someObject = $this->getSomeObject();
$this->process($someObject);
}

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

return null;
}

private function process(SomeClassUsedInTests $someObject): void
{
// non nullable required
}
}

?>
-----
<?php

declare(strict_types=1);

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

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

final class PassingNullableArg extends TestCase
{
public function test(): void
{
$someObject = $this->getSomeObject();
$this->assertInstanceOf(\Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source\SomeClassUsedInTests::class, $someObject);
$this->process($someObject);
}

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

return null;
}

private function process(SomeClassUsedInTests $someObject): void
{
// non nullable required
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

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

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

final class SkipNonNullableType extends TestCase
{
public function test(): void
{
$someObject = $this->getSomeObject();

$this->process($someObject);
}

private function getSomeObject(): SomeClassUsedInTests
{
return new SomeClassUsedInTests();
}

private function process(SomeClassUsedInTests $someObject)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source;

final class SomeClassUsedInTests
{
public function getSomeMethod(): int
{
return 1000;
}

public function getSomeNestedObject(): ?SomeNestedObject
{
return new SomeNestedObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector\Source;

final class SomeNestedObject
{
public function getNumber(): int
{
return 456;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableArgumentRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddInstanceofAssertForNullableArgumentRector::class);
};
28 changes: 28 additions & 0 deletions rules/CodeQuality/NodeFactory/AssertMethodCallFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeFactory;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Expression;
use Rector\PHPUnit\CodeQuality\ValueObject\VariableNameToType;

final class AssertMethodCallFactory
{
public function createAssertInstanceOf(VariableNameToType $variableNameToType): Expression
{
$args = [
new Arg(new ClassConstFetch(new FullyQualified($variableNameToType->getObjectType()), 'class')),
new Arg(new Variable($variableNameToType->getVariableName())),
];

$methodCall = new MethodCall(new Variable('this'), 'assertInstanceOf', $args);

return new Expression($methodCall);
}
}
Loading