Skip to content

Commit

Permalink
[PHP 8.1] Add ReturnNeverTypeRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 2, 2021
1 parent 51401aa commit 0c7d3ce
Show file tree
Hide file tree
Showing 18 changed files with 411 additions and 19 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

This file was deleted.

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 @@ -45,7 +45,11 @@ public function isReturnTypeChangeAllowed(ClassMethod $classMethod): bool
);

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

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

private function getParentClassMethod(ClassMethod $classMethod): ?MethodReflection
Expand All @@ -63,7 +67,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

0 comments on commit 0c7d3ce

Please sign in to comment.