Updated Rector to commit 22d6fc2789f575fff16e76e7c7f963793e11f53b

22d6fc2789 [TypeDeclaration] Add ReturnTypeFromSymfonySerializerRector (#5908)
This commit is contained in:
Tomas Votruba 2024-05-23 13:35:45 +00:00
parent 43c823a1fe
commit 81725974a0
6 changed files with 158 additions and 4 deletions

View File

@ -1,4 +1,4 @@
# 376 Rules Overview # 377 Rules Overview
<br> <br>
@ -60,7 +60,7 @@
- [Transform](#transform) (25) - [Transform](#transform) (25)
- [TypeDeclaration](#typedeclaration) (47) - [TypeDeclaration](#typedeclaration) (48)
- [Visibility](#visibility) (3) - [Visibility](#visibility) (3)
@ -7337,6 +7337,27 @@ Add return method return type based on strict typed property
<br> <br>
### ReturnTypeFromSymfonySerializerRector
Add return type from symfony serializer
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromSymfonySerializerRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php)
```diff
final class SomeClass
{
private \Symfony\Component\Serializer\Serializer $serializer;
- public function resolveEntity($data)
+ public function resolveEntity($data): SomeType
{
return $this->serializer->deserialize($data, SomeType::class, 'json');
}
}
```
<br>
### ReturnUnionTypeRector ### ReturnUnionTypeRector
Add union return type Add union return type

View File

@ -0,0 +1,129 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use Rector\NodeAnalyzer\ArgsAnalyzer;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractScopeAwareRector;
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\ReturnTypeFromSymfonySerializerRector\ReturnTypeFromSymfonySerializerRectorTest
*/
final class ReturnTypeFromSymfonySerializerRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
*/
private $classMethodReturnTypeOverrideGuard;
/**
* @readonly
* @var \Rector\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
/**
* @readonly
* @var \Rector\NodeAnalyzer\ArgsAnalyzer
*/
private $argsAnalyzer;
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ValueResolver $valueResolver, ArgsAnalyzer $argsAnalyzer)
{
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
$this->valueResolver = $valueResolver;
$this->argsAnalyzer = $argsAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add return type from symfony serializer', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
private \Symfony\Component\Serializer\Serializer $serializer;
public function resolveEntity($data)
{
return $this->serializer->deserialize($data, SomeType::class, 'json');
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
private \Symfony\Component\Serializer\Serializer $serializer;
public function resolveEntity($data): SomeType
{
return $this->serializer->deserialize($data, SomeType::class, 'json');
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class];
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::HAS_RETURN_TYPE;
}
/**
* @param ClassMethod $node
*/
public function refactorWithScope(Node $node, Scope $scope) : ?Node
{
if ($node->stmts === null) {
return null;
}
if ($node->returnType instanceof Node) {
return null;
}
if ($this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
return null;
}
if (\count($node->stmts) !== 1) {
return null;
}
if (!$node->stmts[0] instanceof Return_ || !$node->stmts[0]->expr instanceof MethodCall) {
return null;
}
/** @var MethodCall $returnExpr */
$returnExpr = $node->stmts[0]->expr;
if (!$this->nodeNameResolver->isName($returnExpr->name, 'deserialize')) {
return null;
}
if ($returnExpr->isFirstClassCallable()) {
return null;
}
if (!$this->isObjectType($returnExpr->var, new ObjectType('Symfony\\Component\\Serializer\\Serializer'))) {
return null;
}
$args = $returnExpr->getArgs();
if ($this->argsAnalyzer->hasNamedArg($args)) {
return null;
}
if (\count($args) !== 3) {
return null;
}
$type = $this->valueResolver->getValue($args[1]->value);
if (!\is_string($type)) {
return null;
}
$node->returnType = new FullyQualified($type);
return $node;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = 'b2c9e52bf0776d2475e4d0533267cd11299826f6'; public const PACKAGE_VERSION = '22d6fc2789f575fff16e76e7c7f963793e11f53b';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2024-05-22 09:12:25'; public const RELEASE_DATE = '2024-05-23 15:32:34';
/** /**
* @var int * @var int
*/ */

View File

@ -32,6 +32,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictParamRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromSymfonySerializerRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector; use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector; use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
@ -68,6 +69,7 @@ final class TypeDeclarationLevel
ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromReturnDirectArrayRector::class,
ReturnTypeFromReturnNewRector::class, ReturnTypeFromReturnNewRector::class,
ReturnTypeFromReturnCastRector::class, ReturnTypeFromReturnCastRector::class,
ReturnTypeFromSymfonySerializerRector::class,
AddVoidReturnTypeWhereNoReturnRector::class, AddVoidReturnTypeWhereNoReturnRector::class,
// php 7.4 // php 7.4
EmptyOnNullableObjectToInstanceOfRector::class, EmptyOnNullableObjectToInstanceOfRector::class,

View File

@ -2408,6 +2408,7 @@ return array(
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictScalarReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictScalarReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromSymfonySerializerRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnUnionTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnUnionTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php',

View File

@ -2627,6 +2627,7 @@ class ComposerStaticInit8f3085135f9c0dec79e149b0c0400440
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictScalarReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictScalarReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromSymfonySerializerRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromSymfonySerializerRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnUnionTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnUnionTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnUnionTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php',