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
5 changes: 4 additions & 1 deletion config/sets/phpunit-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveDataProviderParamKeysRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\TestWithToDataProviderRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\AddInstanceofAssertForNullableInstanceRector;
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\DataProviderArrayItemsNewLinedRector;
Expand Down Expand Up @@ -60,10 +61,12 @@
NarrowSingleWillReturnCallbackRector::class,
SingleWithConsecutiveToWithRector::class,

// type declarations
TypeWillReturnCallableArrowFunctionRector::class,

NarrowUnusedSetUpDefinedPropertyRector::class,

// specific asserts

AssertCompareOnCountableWithMethodToAssertCountRector::class,
AssertComparisonToSpecificMethodRector::class,
AssertNotOperatorRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeFinalMockedClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class AssignedFinalInSetup extends TestCase
{
private MockObject $someFinalMockedClass;

protected function setUp(): void
{
$this->someFinalMockedClass = $this->createMock(SomeFinalMockedClass::class);
}

public function test($value): void
{
$this->someFinalMockedClass
->method('anotherMethod')
->willReturnCallback(fn ($age) => $value);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeFinalMockedClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

final class AssignedFinalInSetup extends TestCase
{
private MockObject $someFinalMockedClass;

protected function setUp(): void
{
$this->someFinalMockedClass = $this->createMock(SomeFinalMockedClass::class);
}

public function test($value): void
{
$this->someFinalMockedClass
->method('anotherMethod')
->willReturnCallback(fn (int $age): float => $value);
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;

final class FillKnownParamType extends TestCase
{
public function test($value): void
{
$this->createMock(SomeMockedClass::class)
->method('someMethod')
->willReturnCallback(fn ($name) => $value);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;

final class FillKnownParamType extends TestCase
{
public function test($value): void
{
$this->createMock(SomeMockedClass::class)
->method('someMethod')
->willReturnCallback(fn (string $name): int => $value);
}
}

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

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;

final class IncludeMatcher extends TestCase
{
public function test($value): void
{
$this->createMock(SomeMockedClass::class)
->expects($this->any())
->method('someMethod')
->willReturnCallback(fn ($name) => $value);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source\SomeMockedClass;

final class IncludeMatcher extends TestCase
{
public function test($value): void
{
$this->createMock(SomeMockedClass::class)
->expects($this->any())
->method('someMethod')
->willReturnCallback(fn (string $name): int => $value);
}
}

?>
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\Class_\TypeWillReturnCallableArrowFunctionRector\Source;

final class SomeFinalMockedClass
{
public function anotherMethod(int $age): float
{
return 25.55;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector\Source;

// non final on purpose so PHPStan can analyze it
class SomeMockedClass
{
public function someMethod(string $name): int
{
return 100;
}
}
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\Class_\TypeWillReturnCallableArrowFunctionRector;

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

final class TypeWillReturnCallableArrowFunctionRectorTest 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,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector;

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

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;

use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\ValueObject\MethodName;
use Webmozart\Assert\Assert;

final readonly class SetUpAssignedMockTypesResolver
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {
}

/**
* @return array<string, string>
*/
public function resolveFromClass(Class_ $class): array
{
$setUpClassMethod = $class->getMethod(MethodName::SET_UP);
if (! $setUpClassMethod instanceof ClassMethod) {
return [];
}

$propertyNameToMockedTypes = [];
foreach ((array) $setUpClassMethod->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}

$assign = $stmt->expr;
if (! $assign->expr instanceof MethodCall) {
continue;
}

if (! $this->nodeNameResolver->isNames($assign->expr->name, ['createMock', 'getMockBuilder'])) {
continue;
}

if (! $assign->var instanceof PropertyFetch && ! $assign->var instanceof Variable) {
continue;
}

$mockedClassNameExpr = $assign->expr->getArgs()[0]
->value;
if (! $mockedClassNameExpr instanceof ClassConstFetch) {
continue;
}

$propertyOrVariableName = $this->resolvePropertyOrVariableName($assign->var);
$mockedClass = $this->nodeNameResolver->getName($mockedClassNameExpr->class);

Assert::string($mockedClass);

$propertyNameToMockedTypes[$propertyOrVariableName] = $mockedClass;
}

return $propertyNameToMockedTypes;
}

private function resolvePropertyOrVariableName(PropertyFetch|Variable $propertyFetchOrVariable): ?string
{
if ($propertyFetchOrVariable instanceof Variable) {
return $this->nodeNameResolver->getName($propertyFetchOrVariable);
}

return $this->nodeNameResolver->getName($propertyFetchOrVariable->name);
}
}
Loading
Loading