Updated Rector to commit ac7b2d4783

ac7b2d4783 Extract bool type analyzer (#2564)
This commit is contained in:
Tomas Votruba 2022-06-25 14:47:31 +00:00
parent c10f29b7bb
commit 535101d1ee
7 changed files with 66 additions and 49 deletions

View File

@ -5,18 +5,11 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use Rector\TypeDeclaration\TypeAnalyzer\AlwaysStrictBoolExprAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
@ -24,6 +17,15 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
*/
final class ReturnTypeFromStrictReturnExprRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeAnalyzer\AlwaysStrictBoolExprAnalyzer
*/
private $alwaysStrictBoolExprAnalyzer;
public function __construct(AlwaysStrictBoolExprAnalyzer $alwaysStrictBoolExprAnalyzer)
{
$this->alwaysStrictBoolExprAnalyzer = $alwaysStrictBoolExprAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add strict return type based on returned strict expr type', [new CodeSample(<<<'CODE_SAMPLE'
@ -77,32 +79,6 @@ CODE_SAMPLE
$node->returnType = new Identifier('bool');
return $node;
}
private function isStrictBoolExpr(Expr $expr) : bool
{
// detect strict type here :)
if ($expr instanceof Empty_) {
return \true;
}
if ($expr instanceof BooleanAnd) {
return \true;
}
if ($expr instanceof BooleanOr) {
return \true;
}
if ($expr instanceof Equal) {
return \true;
}
if ($expr instanceof NotEqual) {
return \true;
}
if ($expr instanceof Identical) {
return \true;
}
if ($expr instanceof NotIdentical) {
return \true;
}
return $expr instanceof ConstFetch && \in_array($expr->name->toLowerString(), ['true', 'false'], \true);
}
private function hasSingleStrictReturn(ClassMethod $classMethod) : bool
{
if ($classMethod->stmts === null) {
@ -116,7 +92,7 @@ CODE_SAMPLE
if (!$stmt->expr instanceof Expr) {
return \false;
}
if ($this->isStrictBoolExpr($stmt->expr)) {
if ($this->alwaysStrictBoolExprAnalyzer->isStrictBoolExpr($stmt->expr)) {
return \true;
}
}

View File

@ -0,0 +1,39 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\TypeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Empty_;
final class AlwaysStrictBoolExprAnalyzer
{
/**
* @var array<class-string<Expr>>
*/
private const BOOL_TYPE_NODES = [
// detect strict type here :)
Empty_::class,
BooleanAnd::class,
BooleanOr::class,
Equal::class,
NotEqual::class,
Identical::class,
NotIdentical::class,
];
public function isStrictBoolExpr(Expr $expr) : bool
{
foreach (self::BOOL_TYPE_NODES as $boolTypeNode) {
if (\is_a($expr, $boolTypeNode, \true)) {
return \true;
}
}
return $expr instanceof ConstFetch && \in_array($expr->name->toLowerString(), ['true', 'false'], \true);
}
}

View File

@ -16,11 +16,11 @@ final class VersionResolver
/**
* @var string
*/
public const PACKAGE_VERSION = '42707709f7ad4ab383d737c0bd6510e6c3609f1d';
public const PACKAGE_VERSION = 'ac7b2d478356a7ede03c0c6e34049bfcaaaee211';
/**
* @var string
*/
public const RELEASE_DATE = '2022-06-25 14:32:05';
public const RELEASE_DATE = '2022-06-25 14:40:59';
/**
* @var int
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -3039,6 +3039,7 @@ return array(
'Rector\\TypeDeclaration\\Sorter\\PriorityAwareSorter' => $baseDir . '/rules/TypeDeclaration/Sorter/PriorityAwareSorter.php',
'Rector\\TypeDeclaration\\TypeAlreadyAddedChecker\\ReturnTypeAlreadyAddedChecker' => $baseDir . '/rules/TypeDeclaration/TypeAlreadyAddedChecker/ReturnTypeAlreadyAddedChecker.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\AdvancedArrayAnalyzer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/AdvancedArrayAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\AlwaysStrictBoolExprAnalyzer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictBoolExprAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\GenericClassStringTypeNormalizer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/GenericClassStringTypeNormalizer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\IterableTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\ObjectTypeComparator' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/ObjectTypeComparator.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f
class ComposerAutoloaderInite0c92f5c18237696f7adede999ed63b3
{
private static $loader;
@ -22,19 +22,19 @@ class ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInite0c92f5c18237696f7adede999ed63b3', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInite0c92f5c18237696f7adede999ed63b3', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit93f0bbaa77876060210357e31273c54f::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInite0c92f5c18237696f7adede999ed63b3::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit93f0bbaa77876060210357e31273c54f::$files;
$includeFiles = \Composer\Autoload\ComposerStaticInite0c92f5c18237696f7adede999ed63b3::$files;
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire93f0bbaa77876060210357e31273c54f($fileIdentifier, $file);
composerRequiree0c92f5c18237696f7adede999ed63b3($fileIdentifier, $file);
}
return $loader;
@ -46,7 +46,7 @@ class ComposerAutoloaderInit93f0bbaa77876060210357e31273c54f
* @param string $file
* @return void
*/
function composerRequire93f0bbaa77876060210357e31273c54f($fileIdentifier, $file)
function composerRequiree0c92f5c18237696f7adede999ed63b3($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 ComposerStaticInit93f0bbaa77876060210357e31273c54f
class ComposerStaticInite0c92f5c18237696f7adede999ed63b3
{
public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@ -3340,6 +3340,7 @@ class ComposerStaticInit93f0bbaa77876060210357e31273c54f
'Rector\\TypeDeclaration\\Sorter\\PriorityAwareSorter' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Sorter/PriorityAwareSorter.php',
'Rector\\TypeDeclaration\\TypeAlreadyAddedChecker\\ReturnTypeAlreadyAddedChecker' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAlreadyAddedChecker/ReturnTypeAlreadyAddedChecker.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\AdvancedArrayAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/AdvancedArrayAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\AlwaysStrictBoolExprAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictBoolExprAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\GenericClassStringTypeNormalizer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/GenericClassStringTypeNormalizer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\IterableTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\TypeDeclaration\\TypeAnalyzer\\ObjectTypeComparator' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/ObjectTypeComparator.php',
@ -3402,9 +3403,9 @@ class ComposerStaticInit93f0bbaa77876060210357e31273c54f
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit93f0bbaa77876060210357e31273c54f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit93f0bbaa77876060210357e31273c54f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit93f0bbaa77876060210357e31273c54f::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInite0c92f5c18237696f7adede999ed63b3::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite0c92f5c18237696f7adede999ed63b3::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite0c92f5c18237696f7adede999ed63b3::$classMap;
}, null, ClassLoader::class);
}