Skip to content

Commit

Permalink
[PHP 8.1] Add ReturnNeverTypeRector (#6283)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 2, 2021
1 parent bbba530 commit 2037d55
Show file tree
Hide file tree
Showing 53 changed files with 579 additions and 46 deletions.
11 changes: 11 additions & 0 deletions config/set/php81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ReturnNeverTypeRector::class);
};
5 changes: 5 additions & 0 deletions packages/Set/ValueObject/SetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ final class SetList implements SetListInterface
*/
public const PHP_80 = __DIR__ . '/../../../config/set/php80.php';

/**
* @var string
*/
public const PHP_81 = __DIR__ . '/../../../config/set/php81.php';

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class DieSome
{
public function run()
{
echo 100;
die;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class DieSome
{
public function run(): never
{
echo 100;
die;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class ExitSome
{
public function run()
{
echo 100;
exit;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class ExitSome
{
public function run(): never
{
echo 100;
exit;
}
}

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

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

use Rector\Core\Exception\ShouldNotHappenException;

final class ImproveVoid
{
public function run(): void
{
throw new ShouldNotHappenException();
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

use Rector\Core\Exception\ShouldNotHappenException;

final class ImproveVoid
{
public function run(): never
{
throw new ShouldNotHappenException();
}
}

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

function run($key)
{
if ($key) {
echo 100;
exit;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class SkipNeverAlready
{
public function run(): never
{
throw new InvalidException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Rector\Core\Exception\ShouldNotHappenException;

final class SkipParentProtected implements SomeInterfaceWithReturnType
{
public function run()
{
throw new ShouldNotHappenException();
}
}

interface SomeInterfaceWithReturnType
{
public function run(): void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class SkipReturn
{
public function run()
{
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class SkipYield
{
public function run()
{
yield 1;
exit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class SomeClass
{
public function run()
{
throw new InvalidException();
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector\Fixture;

final class SomeClass
{
public function run(): never
{
throw new InvalidException();
}
}

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

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;

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

final class ReturnNeverTypeRectorTest 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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_81);

$services = $containerConfigurator->services();
$services->set(ReturnNeverTypeRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param array<string, ArgumentDefaultValueReplacer[]> $configuration
*/
public function configure(array $configuration): void
{
$replacedArguments = $configuration[self::REPLACED_ARGUMENTS] ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param array<string, SwapFuncCallArguments[]> $configuration
*/
public function configure(array $configuration): void
{
$functionArgumentSwaps = $configuration[self::FUNCTION_ARGUMENT_SWAPS] ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ public function __construct(NodeRepository $nodeRepository, NodeNameResolver $no
$this->nodeNameResolver = $nodeNameResolver;
}

public function hasParentMethodOutsideVendor(ClassMethod $classMethod): bool
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

$methodName = $classMethod->name->toString();

foreach ($classReflection->getAncestors() as $ancestorClassReflection) {
if ($classReflection === $ancestorClassReflection) {
continue;
}

if (! $ancestorClassReflection->hasMethod($methodName)) {
continue;
}

$parentClassMethodReflection = $ancestorClassReflection->getMethod($methodName, $scope);
$parentClassMethod = $this->nodeRepository->findClassMethodByMethodReflection(
$parentClassMethodReflection
);

if (! $parentClassMethod instanceof ClassMethod) {
return true;
}
}

return false;
}

public function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
{
// make sure return type is not protected by parent contract
Expand All @@ -45,7 +81,11 @@ public function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
);

// if null, we're unable to override → skip it
return $parentClassMethod !== null;
if (! $parentClassMethod instanceof ClassMethod) {
return true;
}

return $parentClassMethod->returnType === null;
}

private function getParentClassMethod(ClassMethod $classMethod): ?MethodReflection
Expand All @@ -63,7 +103,11 @@ private function getParentClassMethod(ClassMethod $classMethod): ?MethodReflecti
return null;
}

foreach ($classReflection->getParents() as $parentClassReflection) {
foreach ($classReflection->getAncestors() as $parentClassReflection) {
if ($classReflection === $parentClassReflection) {
continue;
}

if (! $parentClassReflection->hasMethod($methodName)) {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions rules/Defluent/Rector/ClassMethod/NormalToFluentRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param array<string, NormalToFluent[]> $configuration
*/
public function configure(array $configuration): void
{
$callsToFluent = $configuration[self::CALLS_TO_FLUENT] ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function matchSingleReturnThis(ClassMethod $classMethod): ?Return_

private function shouldSkip(Return_ $return, ClassMethod $classMethod): bool
{
if (! $this->parentClassMethodTypeOverrideGuard->isReturnTypeChangeAllowed($classMethod)) {
if ($this->parentClassMethodTypeOverrideGuard->hasParentMethodOutsideVendor($classMethod)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class AddMethodParentCallRector extends AbstractRector implements Configur
/**
* @var array<string, string>
*/
private $methodsByParentTypes = [];
private $methodByParentTypes = [];

public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -90,7 +90,7 @@ public function refactor(Node $node): ?Node
/** @var string $className */
$className = $node->getAttribute(AttributeKey::CLASS_NAME);

foreach ($this->methodsByParentTypes as $type => $method) {
foreach ($this->methodByParentTypes as $type => $method) {
if (! $this->isObjectType($classLike, new ObjectType($type))) {
continue;
}
Expand All @@ -112,9 +112,12 @@ public function refactor(Node $node): ?Node
return null;
}

/**
* @param array<string, array<string, string>> $configuration
*/
public function configure(array $configuration): void
{
$this->methodsByParentTypes = $configuration[self::METHODS_BY_PARENT_TYPES] ?? [];
$this->methodByParentTypes = $configuration[self::METHODS_BY_PARENT_TYPES] ?? [];
}

private function shouldSkipMethod(ClassMethod $classMethod, string $method): bool
Expand Down

0 comments on commit 2037d55

Please sign in to comment.