Updated Rector to commit bbc100ce821ca98123ac0996673dbf48fc2b71e8

bbc100ce82 [TypeDeclaration] Add ReturnTypeFromStrictTernaryRector (#3318)
This commit is contained in:
Tomas Votruba 2023-01-29 12:39:10 +00:00
parent 9dda9c3cc2
commit e25df78489
9 changed files with 226 additions and 15 deletions

View File

@ -7,6 +7,7 @@ use Rector\CodeQuality\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRe
use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
use Rector\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector;
use Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromPropertyTypeRector;
@ -37,5 +38,5 @@ use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodRe
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
use Rector\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class]);
$rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class]);
};

View File

@ -1,4 +1,4 @@
# 421 Rules Overview
# 422 Rules Overview
<br>
@ -64,7 +64,7 @@
- [Transform](#transform) (34)
- [TypeDeclaration](#typedeclaration) (40)
- [TypeDeclaration](#typedeclaration) (41)
- [Visibility](#visibility) (3)
@ -9689,6 +9689,25 @@ Add strict return array type based on created empty array and returned
<br>
### ReturnTypeFromStrictTernaryRector
Add method return type based on strict ternary values
- class: [`Rector\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector`](../rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php)
```diff
final class SomeClass
{
- public function getValue($number)
+ public function getValue($number): int
{
return $number ? 100 : 500;
}
}
```
<br>
### ReturnTypeFromStrictTypedCallRector
Add return type from strict return type of call

View File

@ -0,0 +1,155 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ConstantType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\ValueObject\TernaryIfElseTypes;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\Class_\ReturnTypeFromStrictTernaryRector\ReturnTypeFromStrictTernaryRectorTest
*/
final class ReturnTypeFromStrictTernaryRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\VendorLocker\ParentClassMethodTypeOverrideGuard
*/
private $parentClassMethodTypeOverrideGuard;
public function __construct(ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard)
{
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add method return type based on strict ternary values', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
public function getValue($number)
{
return $number ? 100 : 500;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
public function getValue($number): int
{
return $number ? 100 : 500;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
$hasChanged = \false;
foreach ($node->getMethods() as $classMethod) {
if ($classMethod->returnType instanceof Node) {
continue;
}
$onlyStmt = $classMethod->stmts[0] ?? null;
if (!$onlyStmt instanceof Return_) {
continue;
}
if (!$onlyStmt->expr instanceof Ternary) {
continue;
}
$ternary = $onlyStmt->expr;
// has scalar in if/else of ternary
$ternaryIfElseTypes = $this->matchScalarTernaryIfElseTypes($ternary);
if (!$ternaryIfElseTypes instanceof TernaryIfElseTypes) {
continue;
}
$ifType = $ternaryIfElseTypes->getFirstType();
$elseType = $ternaryIfElseTypes->getSecondType();
if (!$this->areTypesEqual($ifType, $elseType)) {
continue;
}
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($ifType, TypeKind::RETURN);
if ($this->parentClassMethodTypeOverrideGuard->shouldSkipReturnTypeChange($classMethod, $ifType)) {
continue;
}
if (!$returnTypeNode instanceof Node) {
continue;
}
$classMethod->returnType = $returnTypeNode;
$hasChanged = \true;
}
if ($hasChanged) {
return $node;
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::SCALAR_TYPES;
}
private function isAlwaysScalarExpr(?Expr $expr) : bool
{
// check if Scalar node
if ($expr instanceof Scalar) {
return \true;
}
// check if constant
if ($expr instanceof ConstFetch) {
return \true;
}
// check if class constant
return $expr instanceof ClassConstFetch;
}
private function areTypesEqual(Type $firstType, Type $secondType) : bool
{
// this is needed to make comparison tolerant to constant values, e.g. 5 and 10 are same only then
if ($firstType instanceof ConstantType) {
$firstType = $firstType->generalize(GeneralizePrecision::lessSpecific());
}
if ($secondType instanceof ConstantType) {
$secondType = $secondType->generalize(GeneralizePrecision::lessSpecific());
}
return $firstType->equals($secondType);
}
private function matchScalarTernaryIfElseTypes(Ternary $ternary) : ?TernaryIfElseTypes
{
if (!$this->isAlwaysScalarExpr($ternary->if)) {
return null;
}
if (!$this->isAlwaysScalarExpr($ternary->else)) {
return null;
}
/** @var Node\Expr $if */
$if = $ternary->if;
/** @var Node\Expr $else */
$else = $ternary->else;
$ifType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($if);
$elseType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($else);
return new TernaryIfElseTypes($ifType, $elseType);
}
}

View File

@ -0,0 +1,32 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\ValueObject;
use PHPStan\Type\Type;
final class TernaryIfElseTypes
{
/**
* @readonly
* @var \PHPStan\Type\Type
*/
private $firstType;
/**
* @readonly
* @var \PHPStan\Type\Type
*/
private $secondType;
public function __construct(Type $firstType, Type $secondType)
{
$this->firstType = $firstType;
$this->secondType = $secondType;
}
public function getFirstType() : Type
{
return $this->firstType;
}
public function getSecondType() : Type
{
return $this->secondType;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '3450bed50204db27ca622d2056a82580a59914fb';
public const PACKAGE_VERSION = 'bbc100ce821ca98123ac0996673dbf48fc2b71e8';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-01-29 12:24:32';
public const RELEASE_DATE = '2023-01-29 12:33:59';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit237f60ed8c00a6a6ce0e2d6d91af6957::getLoader();
return ComposerAutoloaderInitdbfcbece273e5de83f5ecd5e170d7d08::getLoader();

View File

@ -2768,6 +2768,7 @@ return array(
'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\\Class_\\PropertyTypeFromStrictSetterGetterRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php',
'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php',
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => $baseDir . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
@ -2803,6 +2804,7 @@ return array(
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => $baseDir . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => $baseDir . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php',
'Rector\\TypeDeclaration\\ValueObject\\ReturnFalseAndReturnOther' => $baseDir . '/rules/TypeDeclaration/ValueObject/ReturnFalseAndReturnOther.php',
'Rector\\TypeDeclaration\\ValueObject\\TernaryIfElseTypes' => $baseDir . '/rules/TypeDeclaration/ValueObject/TernaryIfElseTypes.php',
'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => $vendorDir . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodParamVendorLockResolver' => $baseDir . '/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodReturnTypeOverrideGuard' => $baseDir . '/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit237f60ed8c00a6a6ce0e2d6d91af6957
class ComposerAutoloaderInitdbfcbece273e5de83f5ecd5e170d7d08
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit237f60ed8c00a6a6ce0e2d6d91af6957
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit237f60ed8c00a6a6ce0e2d6d91af6957', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitdbfcbece273e5de83f5ecd5e170d7d08', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit237f60ed8c00a6a6ce0e2d6d91af6957', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitdbfcbece273e5de83f5ecd5e170d7d08', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitdbfcbece273e5de83f5ecd5e170d7d08::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitdbfcbece273e5de83f5ecd5e170d7d08::$files;
$requireFile = static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957
class ComposerStaticInitdbfcbece273e5de83f5ecd5e170d7d08
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3013,6 +3013,7 @@ class ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957
'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\\Class_\\PropertyTypeFromStrictSetterGetterRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php',
'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php',
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
@ -3048,6 +3049,7 @@ class ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
'Rector\\TypeDeclaration\\ValueObject\\NestedArrayType' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/NestedArrayType.php',
'Rector\\TypeDeclaration\\ValueObject\\ReturnFalseAndReturnOther' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/ReturnFalseAndReturnOther.php',
'Rector\\TypeDeclaration\\ValueObject\\TernaryIfElseTypes' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/TernaryIfElseTypes.php',
'Rector\\ValueObject\\ClassMethodWillChangeReturnType' => __DIR__ . '/..' . '/rector/rector-downgrade-php/src/ValueObject/ClassMethodWillChangeReturnType.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodParamVendorLockResolver' => __DIR__ . '/../..' . '/packages/VendorLocker/NodeVendorLocker/ClassMethodParamVendorLockResolver.php',
'Rector\\VendorLocker\\NodeVendorLocker\\ClassMethodReturnTypeOverrideGuard' => __DIR__ . '/../..' . '/packages/VendorLocker/NodeVendorLocker/ClassMethodReturnTypeOverrideGuard.php',
@ -3080,9 +3082,9 @@ class ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit237f60ed8c00a6a6ce0e2d6d91af6957::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitdbfcbece273e5de83f5ecd5e170d7d08::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitdbfcbece273e5de83f5ecd5e170d7d08::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitdbfcbece273e5de83f5ecd5e170d7d08::$classMap;
}, null, ClassLoader::class);
}