Skip to content

Commit

Permalink
[Downgrade PHP 7.0] Move Throwable out of type hints
Browse files Browse the repository at this point in the history
It is only available on PHP 7.0+:
https://www.php.net/manual/en/class.throwable.php
  • Loading branch information
jtojnar committed Jan 1, 2022
1 parent 311ffc6 commit 85bf63b
Show file tree
Hide file tree
Showing 17 changed files with 498 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/set/downgrade-php70.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\DowngradePhp70\Rector\FuncCall\DowngradeDirnameLevelsRector;
use Rector\DowngradePhp70\Rector\FuncCall\DowngradeSessionStartArrayOptionsRector;
use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeScalarTypeDeclarationRector;
use Rector\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector;
use Rector\DowngradePhp70\Rector\GroupUse\SplitGroupedUseImportsRector;
use Rector\DowngradePhp70\Rector\MethodCall\DowngradeClosureCallRector;
use Rector\DowngradePhp70\Rector\MethodCall\DowngradeMethodCallOnCloneRector;
Expand All @@ -26,6 +27,7 @@

$services = $containerConfigurator->services();
$services->set(DowngradeScalarTypeDeclarationRector::class);
$services->set(DowngradeThrowableTypeDeclarationRector::class);
$services->set(DowngradeStrictTypeDeclarationRector::class);
$services->set(DowngradeSelfTypeDeclarationRector::class);
$services->set(DowngradeAnonymousClassRector::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ private function isTypeMatch(ComplexType|Identifier|Name $typeNode, Type $requir
if ($returnType instanceof UnionType) {
$returnType = $this->typeUnwrapper->unwrapNullableType($returnType);
}
if ($returnType instanceof ObjectType) {
return $returnType->equals($requireType);
}

return $returnType::class === $requireType::class;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector;

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

final class DowngradeThrowableTypeDeclarationRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP 8.0
* @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,27 @@
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class ArrowFunctionParamThrowable
{
public function run()
{
$value = fn (\Throwable $param): bool => true;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class ArrowFunctionParamThrowable
{
public function run()
{
$value = fn ($param): bool => true;
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

final class ArrowFunctionReturnThrowable
{
public function run()
{
$value = fn () : \Throwable => new \Exception('Yikes!');
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

final class ArrowFunctionReturnThrowable
{
public function run()
{
$value = fn () => new \Exception('Yikes!');
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class DocblockExists {
/**
* This property is the best one
*/
public function someFunction(\Throwable $anything)
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class DocblockExists {
/**
* This property is the best one
* @param \Throwable $anything
*/
public function someFunction($anything)
{
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class DocblockTagExists {
/**
* This property is the best one
* @param \Throwable $anything
*/
public function someFunction(\Throwable $anything)
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class DocblockTagExists {
/**
* This property is the best one
* @param \Throwable $anything
*/
public function someFunction($anything)
{
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class Fixture
{
public function someFunction(\Throwable $anything)
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class Fixture
{
/**
* @param \Throwable $anything
*/
public function someFunction($anything)
{
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class MultipleMatchingParams
{
public function someFunction(\Throwable $anything, string $someOtherVar, \Throwable $someOtherObject)
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class MultipleMatchingParams
{
/**
* @param \Throwable $anything
* @param \Throwable $someOtherObject
*/
public function someFunction($anything, string $someOtherVar, $someOtherObject)
{
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class MultipleParams
{
public function someFunction(\Throwable $anything, string $someOtherVar)
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class MultipleParams
{
/**
* @param \Throwable $anything
*/
public function someFunction($anything, string $someOtherVar)
{
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class Fixture
{
public function someFunction(?\Throwable $anything): ?\Throwable
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class Fixture
{
/**
* @param \Throwable|null $anything
* @return \Throwable|null
*/
public function someFunction($anything)
{
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class OnClosure
{
public function run()
{
$foo = array_filter(
$list,
function (\Throwable $fieldArgValue) {
return is_null($fieldArgValue);
}
);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class OnClosure
{
public function run()
{
$foo = array_filter(
$list,
function ($fieldArgValue) {
return is_null($fieldArgValue);
}
);
}
}

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

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class ReturnDocblockExists {
/**
* This property is the best one
*/
public function getAnything(): \Throwable
{
return new \Exception('Yikes!');
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\FunctionLike\DowngradeThrowableTypeDeclarationRector\Fixture;

class ReturnDocblockExists {
/**
* This property is the best one
* @return \Throwable
*/
public function getAnything()
{
return new \Exception('Yikes!');
}
}

?>
Loading

0 comments on commit 85bf63b

Please sign in to comment.