Updated Rector to commit a4d6d414fb2f6ce98c792178895ab01664fab358

a4d6d414fb [TypeDeclaration] Add BoolReturnTypeFromStrictScalarReturnsRector (#3898)
This commit is contained in:
Tomas Votruba 2023-05-19 15:03:57 +00:00
parent b2768ca2ed
commit c6385617d2
10 changed files with 226 additions and 20 deletions

View File

@ -14,6 +14,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromPropertyTypeRector
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamAnnotationIncorrectNullableRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByParentCallTypeRector;
@ -38,5 +39,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, ReturnTypeFromStrictTernaryRector::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, BoolReturnTypeFromStrictScalarReturnsRector::class]);
};

View File

@ -1,4 +1,4 @@
# 404 Rules Overview
# 405 Rules Overview
<br>
@ -62,7 +62,7 @@
- [Transform](#transform) (29)
- [TypeDeclaration](#typedeclaration) (39)
- [TypeDeclaration](#typedeclaration) (40)
- [Visibility](#visibility) (3)
@ -9084,6 +9084,29 @@ Change && and || between nullable objects to instanceof compares
<br>
### BoolReturnTypeFromStrictScalarReturnsRector
Change return type based on strict returns type operations
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector`](../rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php)
```diff
class SomeClass
{
- public function resolve($first, $second)
+ public function resolve($first, $second): bool
{
if ($first) {
return false;
}
return $first > $second;
}
}
```
<br>
### DeclareStrictTypesRector
Add declare(strict_types=1) if missing

View File

@ -0,0 +1,183 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\BooleanType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector\BoolReturnTypeFromStrictScalarReturnsRectorTest
*/
final class BoolReturnTypeFromStrictScalarReturnsRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\TypeDeclaration\NodeAnalyzer\ReturnAnalyzer
*/
private $returnAnalyzer;
/**
* @readonly
* @var \PHPStan\Reflection\ReflectionProvider
*/
private $reflectionProvider;
public function __construct(ReturnAnalyzer $returnAnalyzer, ReflectionProvider $reflectionProvider)
{
$this->returnAnalyzer = $returnAnalyzer;
$this->reflectionProvider = $reflectionProvider;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change funcCall type based on strict returns type operations', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function resolve($first, $second)
{
if ($first) {
funcCall false;
}
funcCall $first > $second;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function resolve($first, $second): bool
{
if ($first) {
funcCall false;
}
funcCall $first > $second;
}
}
CODE_SAMPLE
)]);
}
/**
* @funcCall array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class, Function_::class];
}
/**
* @param ClassMethod|Function_ $node
*/
public function refactor(Node $node) : ?Node
{
if ($node->returnType instanceof Node) {
return null;
}
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($node, Return_::class);
if (!$this->hasOnlyBoolScalarReturnExprs($returns, $node)) {
return null;
}
$node->returnType = new Identifier('bool');
return $node;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::SCALAR_TYPES;
}
/**
* @param Return_[] $returns
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
*/
private function hasOnlyBoolScalarReturnExprs(array $returns, $functionLike) : bool
{
if ($returns === []) {
return \false;
}
if (!$this->returnAnalyzer->hasClassMethodRootReturn($functionLike)) {
return \false;
}
foreach ($returns as $return) {
if (!$return->expr instanceof Expr) {
return \false;
}
if ($this->valueResolver->isTrueOrFalse($return->expr)) {
continue;
}
if ($this->isBooleanBinaryOp($return->expr)) {
continue;
}
if ($return->expr instanceof FuncCall && $this->isNativeBooleanReturnTypeFuncCall($return->expr)) {
continue;
}
return \false;
}
return \true;
}
private function isNativeBooleanReturnTypeFuncCall(FuncCall $funcCall) : bool
{
$functionName = $this->getName($funcCall);
if (!\is_string($functionName)) {
return \false;
}
$functionReflection = $this->reflectionProvider->getFunction(new Name($functionName), null);
if (!$functionReflection->isBuiltin()) {
return \false;
}
foreach ($functionReflection->getVariants() as $parametersAcceptorWithPhpDoc) {
return $parametersAcceptorWithPhpDoc->getNativeReturnType() instanceof BooleanType;
}
return \false;
}
private function isBooleanBinaryOp(Expr $expr) : bool
{
if ($expr instanceof Smaller) {
return \true;
}
if ($expr instanceof SmallerOrEqual) {
return \true;
}
if ($expr instanceof Greater) {
return \true;
}
if ($expr instanceof GreaterOrEqual) {
return \true;
}
if ($expr instanceof BooleanOr) {
return \true;
}
if ($expr instanceof BooleanAnd) {
return \true;
}
if ($expr instanceof Identical) {
return \true;
}
if ($expr instanceof NotIdentical) {
return \true;
}
if ($expr instanceof Equal) {
return \true;
}
return $expr instanceof NotEqual;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '11b94117e5e915866695379b12dbb5cab0f90b79';
public const PACKAGE_VERSION = 'a4d6d414fb2f6ce98c792178895ab01664fab358';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-19 20:44:48';
public const RELEASE_DATE = '2023-05-19 14:59:11';
/**
* @var int
*/

View File

@ -15,7 +15,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v15';
private const CACHE_KEY = 'v16';
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface|null
*/

View File

@ -198,10 +198,7 @@ CODE_SAMPLE;
$this->file = $file;
return parent::beforeTraverse($nodes);
}
/**
* @return Node|null
*/
public final function enterNode(Node $node)
public final function enterNode(Node $node) : ?Node
{
$nodeClass = \get_class($node);
if (!$this->isMatchingNodeType($nodeClass)) {

2
vendor/autoload.php vendored
View File

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

View File

@ -2789,6 +2789,7 @@ return array(
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ArrayShapeFromConstantArrayReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ArrayShapeFromConstantArrayReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\BoolReturnTypeFromStrictScalarReturnsRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamAnnotationIncorrectNullableRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamAnnotationIncorrectNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit723e373d7da85ea5216966c3d24c4aef
class ComposerAutoloaderInit20322819280bd201d8f8f5b82d937345
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit723e373d7da85ea5216966c3d24c4aef
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit723e373d7da85ea5216966c3d24c4aef', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit20322819280bd201d8f8f5b82d937345', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit723e373d7da85ea5216966c3d24c4aef', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit20322819280bd201d8f8f5b82d937345', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit723e373d7da85ea5216966c3d24c4aef::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit20322819280bd201d8f8f5b82d937345::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit723e373d7da85ea5216966c3d24c4aef::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit20322819280bd201d8f8f5b82d937345::$files;
$requireFile = \Closure::bind(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 ComposerStaticInit723e373d7da85ea5216966c3d24c4aef
class ComposerStaticInit20322819280bd201d8f8f5b82d937345
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3031,6 +3031,7 @@ class ComposerStaticInit723e373d7da85ea5216966c3d24c4aef
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddReturnTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ArrayShapeFromConstantArrayReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ArrayShapeFromConstantArrayReturnRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\BoolReturnTypeFromStrictScalarReturnsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/BoolReturnTypeFromStrictScalarReturnsRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamAnnotationIncorrectNullableRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamAnnotationIncorrectNullableRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByMethodCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByMethodCallTypeRector.php',
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ParamTypeByParentCallTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ParamTypeByParentCallTypeRector.php',
@ -3114,9 +3115,9 @@ class ComposerStaticInit723e373d7da85ea5216966c3d24c4aef
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit723e373d7da85ea5216966c3d24c4aef::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit723e373d7da85ea5216966c3d24c4aef::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit723e373d7da85ea5216966c3d24c4aef::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit20322819280bd201d8f8f5b82d937345::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit20322819280bd201d8f8f5b82d937345::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit20322819280bd201d8f8f5b82d937345::$classMap;
}, null, ClassLoader::class);
}