mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 13:28:18 +01:00
Updated Rector to commit b8847be9d485ec1b3cde5ee5e099ab3626479eb7
b8847be9d4
[TypeDeclaration] Add ReturnTypeFromReturnCastRector (#5905)
This commit is contained in:
parent
83b82dd7bb
commit
cee60661e8
@ -1,4 +1,4 @@
|
||||
# 374 Rules Overview
|
||||
# 376 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
- [Arguments](#arguments) (4)
|
||||
|
||||
- [Carbon](#carbon) (3)
|
||||
- [Carbon](#carbon) (4)
|
||||
|
||||
- [CodeQuality](#codequality) (75)
|
||||
|
||||
@ -60,7 +60,7 @@
|
||||
|
||||
- [Transform](#transform) (25)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (46)
|
||||
- [TypeDeclaration](#typedeclaration) (47)
|
||||
|
||||
- [Visibility](#visibility) (3)
|
||||
|
||||
@ -148,7 +148,7 @@ Replaces defined map of arguments in defined methods and their calls.
|
||||
|
||||
### DateFuncCallToCarbonRector
|
||||
|
||||
Convert `date()` function call to Carbon::*()
|
||||
Convert `date()` function call to `Carbon::now()->format(*)`
|
||||
|
||||
- class: [`Rector\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector`](../rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php)
|
||||
|
||||
@ -158,7 +158,7 @@ Convert `date()` function call to Carbon::*()
|
||||
public function run()
|
||||
{
|
||||
- $date = date('Y-m-d');
|
||||
+ $date = \Carbon\Carbon::now()->format('Y-m-d')
|
||||
+ $date = \Carbon\Carbon::now()->format('Y-m-d');
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -197,6 +197,25 @@ Convert new `DateTime()` with a method call to Carbon::*()
|
||||
|
||||
<br>
|
||||
|
||||
### TimeFuncCallToCarbonRector
|
||||
|
||||
Convert `time()` function call to `Carbon::now()->timestamp`
|
||||
|
||||
- class: [`Rector\Carbon\Rector\FuncCall\TimeFuncCallToCarbonRector`](../rules/Carbon/Rector/FuncCall/TimeFuncCallToCarbonRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $time = time();
|
||||
+ $time = \Carbon\Carbon::now()->timestamp;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## CodeQuality
|
||||
|
||||
### AbsolutizeRequireAndIncludePathRector
|
||||
@ -7043,6 +7062,30 @@ Add "never" return-type for methods that never return anything
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromReturnCastRector
|
||||
|
||||
Add return type to function like with return cast
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
- public function action($param)
|
||||
+ public function action($param): array
|
||||
{
|
||||
try {
|
||||
return (array) $param;
|
||||
} catch (Exception $exception) {
|
||||
// some logging
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromReturnDirectArrayRector
|
||||
|
||||
Add return type from return direct array
|
||||
|
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Cast;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
||||
use Rector\Rector\AbstractScopeAwareRector;
|
||||
use Rector\StaticTypeMapper\StaticTypeMapper;
|
||||
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
||||
use Rector\ValueObject\PhpVersionFeature;
|
||||
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector\ReturnTypeFromReturnCastRectorTest
|
||||
*/
|
||||
final class ReturnTypeFromReturnCastRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
|
||||
*/
|
||||
private $classMethodReturnTypeOverrideGuard;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
|
||||
*/
|
||||
private $returnTypeInferer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
||||
*/
|
||||
private $staticTypeMapper;
|
||||
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnTypeInferer $returnTypeInferer, BetterNodeFinder $betterNodeFinder, StaticTypeMapper $staticTypeMapper)
|
||||
{
|
||||
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
|
||||
$this->returnTypeInferer = $returnTypeInferer;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->staticTypeMapper = $staticTypeMapper;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add return type to function like with return cast', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function action($param)
|
||||
{
|
||||
try {
|
||||
return (array) $param;
|
||||
} catch (Exception $exception) {
|
||||
// some logging
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function action($param): array
|
||||
{
|
||||
try {
|
||||
return (array) $param;
|
||||
} catch (Exception $exception) {
|
||||
// some logging
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class, Function_::class, Closure::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod|Function_|Closure $node
|
||||
*/
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
||||
{
|
||||
if ($node->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
|
||||
return null;
|
||||
}
|
||||
$hasNonCastReturn = (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($node, static function (Node $subNode) : bool {
|
||||
return $subNode instanceof Return_ && !$subNode->expr instanceof Cast;
|
||||
});
|
||||
if ($hasNonCastReturn) {
|
||||
return null;
|
||||
}
|
||||
$returnType = $this->returnTypeInferer->inferFunctionLike($node);
|
||||
if ($returnType instanceof UnionType || $returnType->isVoid()->yes()) {
|
||||
return null;
|
||||
}
|
||||
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
|
||||
if (!$returnTypeNode instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
$node->returnType = $returnTypeNode;
|
||||
return $node;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersionFeature::SCALAR_TYPES;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'd1eb62e703651ec14712439700d6e9b2a60c0763';
|
||||
public const PACKAGE_VERSION = 'b8847be9d485ec1b3cde5ee5e099ab3626479eb7';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-05-20 08:46:14';
|
||||
public const RELEASE_DATE = '2024-05-20 20:32:55';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
@ -20,6 +20,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\NumericReturnTypeFromStrictScalarR
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnCastRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;
|
||||
@ -66,6 +67,7 @@ final class TypeDeclarationLevel
|
||||
ReturnTypeFromStrictScalarReturnExprRector::class,
|
||||
ReturnTypeFromReturnDirectArrayRector::class,
|
||||
ReturnTypeFromReturnNewRector::class,
|
||||
ReturnTypeFromReturnCastRector::class,
|
||||
AddVoidReturnTypeWhereNoReturnRector::class,
|
||||
// php 7.4
|
||||
EmptyOnNullableObjectToInstanceOfRector::class,
|
||||
|
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
@ -2396,6 +2396,7 @@ return array(
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnCastRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnDirectArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php',
|
||||
|
1
vendor/composer/autoload_static.php
vendored
1
vendor/composer/autoload_static.php
vendored
@ -2615,6 +2615,7 @@ class ComposerStaticInit8f3085135f9c0dec79e149b0c0400440
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnCastRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnCastRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnDirectArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnDirectArrayRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php',
|
||||
|
Loading…
x
Reference in New Issue
Block a user