Skip to content

Commit

Permalink
AddReturnTypeDeclarationBasedOnParentClassMethodRector
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas committed Jul 15, 2022
1 parent db7012e commit 735456b
Show file tree
Hide file tree
Showing 14 changed files with 401 additions and 19 deletions.
21 changes: 20 additions & 1 deletion packages/VendorLocker/ParentClassMethodTypeOverrideGuard.php
Expand Up @@ -15,6 +15,8 @@
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer;
use Symplify\SmartFileSystem\Normalizer\PathNormalizer;

Expand All @@ -25,7 +27,9 @@ public function __construct(
private readonly PathNormalizer $pathNormalizer,
private readonly AstResolver $astResolver,
private readonly ParamTypeInferer $paramTypeInferer,
private readonly ReflectionResolver $reflectionResolver
private readonly ReflectionResolver $reflectionResolver,
private readonly TypeComparator $typeComparator,
private readonly StaticTypeMapper $staticTypeMapper
) {
}

Expand Down Expand Up @@ -138,4 +142,19 @@ public function getParentClassMethod(ClassMethod $classMethod): ?MethodReflectio

return null;
}

public function shouldSkipReturnTypeChange(ClassMethod $classMethod, Type $parentType): bool
{
if ($classMethod->returnType === null) {
return false;
}

$currentReturnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($classMethod->returnType);

if ($this->typeComparator->isSubtype($currentReturnType, $parentType)) {
return true;
}

return $this->typeComparator->areTypesEqual($currentReturnType, $parentType);
}
}
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class AddReturnTypeDeclarationBasedOnParentClassMethodRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
@@ -0,0 +1,25 @@
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithReturnType;

class MyClass extends SomeClassWithReturnType
{
public function run()
{
}
}

?>
-----
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithReturnType;

class MyClass extends SomeClassWithReturnType
{
public function run(): int
{
}
}

?>
@@ -0,0 +1,25 @@
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithReturnMixed;

class MyClass extends SomeClassWithReturnMixed
{
public function run()
{
}
}

?>
-----
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithReturnMixed;

class MyClass extends SomeClassWithReturnMixed
{
public function run(): mixed
{
}
}

?>
@@ -0,0 +1,25 @@
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithReturnMixed;

class MyClass extends SomeClassWithReturnMixed
{
public function run(): string
{
}
}

?>
-----
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithReturnMixed;

class MyClass extends SomeClassWithReturnMixed
{
public function run(): string
{
}
}

?>
@@ -0,0 +1,25 @@
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithoutReturnType;

class MyClass extends SomeClassWithoutReturnType
{
public function run()
{
}
}

?>
-----
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeClassWithoutReturnType;

class MyClass extends SomeClassWithoutReturnType
{
public function run()
{
}
}

?>
@@ -0,0 +1,25 @@
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeInterfaceWithReturnType;

abstract class MyClass implements SomeInterfaceWithReturnType
{
public function run()
{
}
}

?>
-----
<?php

use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source\SomeInterfaceWithReturnType;

abstract class MyClass implements SomeInterfaceWithReturnType
{
public function run(): string
{
}
}

?>
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source;

class SomeClassWithReturnMixed
{
public function run(): mixed
{
return 5;
}
}
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source;

class SomeClassWithReturnType
{
public function run(): int
{
return 5;
}
}
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source;

class SomeClassWithoutReturnType
{
public function run()
{
return 5;
}
}
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector\Source;

interface SomeInterfaceWithReturnType
{
public function run(): string;
}
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddReturnTypeDeclarationBasedOnParentClassMethodRector::class);

$rectorConfig->phpVersion(PhpVersionFeature::MIXED_TYPE);
};

0 comments on commit 735456b

Please sign in to comment.