2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php70\Rector\FunctionLike;
|
2018-10-03 16:24:47 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PhpParser\Node\NullableType;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\Util\StringUtils;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-10-03 16:24:47 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://wiki.php.net/rfc/typed_properties_v2#proposal
|
2019-09-22 18:57:03 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php70\Rector\FunctionLike\ExceptionHandlerTypehintRector\ExceptionHandlerTypehintRectorTest
|
2018-10-03 16:24:47 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ExceptionHandlerTypehintRector extends AbstractRector implements MinPhpVersionInterface
|
2018-10-03 16:24:47 +00:00
|
|
|
{
|
2020-09-23 09:16:40 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
2020-10-29 18:04:33 +00:00
|
|
|
* @see https://regex101.com/r/VBFXCR/1
|
2020-09-23 09:16:40 +00:00
|
|
|
*/
|
|
|
|
private const HANDLE_INSENSITIVE_REGEX = '#handle#i';
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-03 16:24:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change typehint from `Exception` to `Throwable`.', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-10-03 16:24:47 +00:00
|
|
|
function handler(Exception $exception) { ... }
|
|
|
|
set_exception_handler('handler');
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-10-03 16:24:47 +00:00
|
|
|
function handler(Throwable $exception) { ... }
|
|
|
|
set_exception_handler('handler');
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-10-03 16:24:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-03 16:24:47 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-03 16:24:47 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Function_::class, ClassMethod::class];
|
2018-10-03 16:24:47 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Function_|ClassMethod $node
|
2018-10-03 16:24:47 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-03 16:24:47 +00:00
|
|
|
{
|
|
|
|
// exception handle has 1 param exactly
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($node->params) !== 1) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-03 16:24:47 +00:00
|
|
|
}
|
|
|
|
$paramNode = $node->params[0];
|
2019-01-14 18:21:23 +00:00
|
|
|
if ($paramNode->type === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-03 16:24:47 +00:00
|
|
|
// handle only Exception typehint
|
2022-06-07 08:22:29 +00:00
|
|
|
$actualType = $paramNode->type instanceof NullableType ? $this->getName($paramNode->type->type) : $this->getName($paramNode->type);
|
2018-11-22 15:38:20 +00:00
|
|
|
if ($actualType !== 'Exception') {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-03 16:24:47 +00:00
|
|
|
}
|
|
|
|
// is probably handling exceptions
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!StringUtils::isMatch((string) $node->name, self::HANDLE_INSENSITIVE_REGEX)) {
|
2018-10-21 10:19:14 +00:00
|
|
|
return null;
|
2018-10-03 16:24:47 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$paramNode->type instanceof NullableType) {
|
|
|
|
$paramNode->type = new FullyQualified('Throwable');
|
2018-11-22 15:38:20 +00:00
|
|
|
} else {
|
2022-06-07 08:22:29 +00:00
|
|
|
$paramNode->type->type = new FullyQualified('Throwable');
|
2018-11-22 15:38:20 +00:00
|
|
|
}
|
2018-10-03 16:24:47 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::THROWABLE_TYPE;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2018-10-03 16:24:47 +00:00
|
|
|
}
|