Updated Rector to commit 861022a70bd9f9c8b91f20c7dfacb0d297d33e9a

861022a70b Remove RemoveAlwaysTrueConditionSetInConstructor, as overly specific and not practical; better handle with PHPStan (#4065)
This commit is contained in:
Tomas Votruba 2023-06-04 17:57:18 +00:00
parent f0c9d40cc2
commit 18f8207b59
9 changed files with 15 additions and 256 deletions

View File

@ -44,7 +44,6 @@ use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
use Rector\CodeQuality\Rector\FuncCall\StrvalToTypeCastRector;
use Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector;
use Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector;
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
use Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector;
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
@ -109,5 +108,5 @@ return static function (RectorConfig $rectorConfig) : void {
'mbstrrpos' => 'mb_strrpos',
'mbsubstr' => 'mb_substr',
]);
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SimplifyForeachToArrayFilterRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, StrvalToTypeCastRector::class, FloatvalToTypeCastRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, RemoveAlwaysTrueConditionSetInConstructorRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, IntvalToTypeCastRector::class, BoolvalToTypeCastRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, ArrayKeysAndInArrayToArrayKeyExistsRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, SwitchTrueToIfRector::class, CleanupUnneededNullsafeOperatorRector::class, DisallowedEmptyRuleFixerRector::class]);
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SimplifyForeachToArrayFilterRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, StrvalToTypeCastRector::class, FloatvalToTypeCastRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, IntvalToTypeCastRector::class, BoolvalToTypeCastRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, ArrayKeysAndInArrayToArrayKeyExistsRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, SwitchTrueToIfRector::class, CleanupUnneededNullsafeOperatorRector::class, DisallowedEmptyRuleFixerRector::class]);
};

View File

@ -6,7 +6,7 @@
- [Arguments](#arguments) (6)
- [CodeQuality](#codequality) (74)
- [CodeQuality](#codequality) (73)
- [CodingStyle](#codingstyle) (34)
@ -1075,34 +1075,6 @@ Move required parameters after optional ones
<br>
### RemoveAlwaysTrueConditionSetInConstructorRector
If conditions is always true, perform the content right away
- class: [`Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector`](../rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php)
```diff
final class SomeClass
{
private $value;
public function __construct(stdClass $value)
{
$this->value = $value;
}
public function go()
{
- if ($this->value) {
- return 'yes';
- }
+ return 'yes';
}
}
```
<br>
### RemoveSoleValueSprintfRector
Remove `sprintf()` wrapper if not needed

View File

@ -1,210 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\CodeQuality\Rector\FunctionLike;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeTypeResolver\PHPStan\Type\StaticTypeAnalyzer;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://phpstan.org/r/e909844a-084e-427e-92ac-fed3c2aeabab
*
* @see \Rector\Tests\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector\RemoveAlwaysTrueConditionSetInConstructorRectorTest
*/
final class RemoveAlwaysTrueConditionSetInConstructorRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Type\StaticTypeAnalyzer
*/
private $staticTypeAnalyzer;
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
*/
private $typeFactory;
public function __construct(StaticTypeAnalyzer $staticTypeAnalyzer, TypeFactory $typeFactory)
{
$this->staticTypeAnalyzer = $staticTypeAnalyzer;
$this->typeFactory = $typeFactory;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('If conditions is always true, perform the content right away', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
private $value;
public function __construct(stdClass $value)
{
$this->value = $value;
}
public function go()
{
if ($this->value) {
return 'yes';
}
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
private $value;
public function __construct(stdClass $value)
{
$this->value = $value;
}
public function go()
{
return 'yes';
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
* @return null|If_|Stmt[]
*/
public function refactor(Node $node)
{
$ifStmt = $this->matchTruableIf($node);
if (!$ifStmt instanceof If_) {
return null;
}
if ($ifStmt->stmts === []) {
$this->removeNode($ifStmt);
return $ifStmt;
}
return $ifStmt->stmts;
}
private function matchTruableIf(If_ $if) : ?\PhpParser\Node\Stmt\If_
{
// just one if
if ($if->elseifs !== []) {
return null;
}
// there is some else
if ($if->else instanceof Else_) {
return null;
}
// only property fetch, because of constructor set
if (!$if->cond instanceof PropertyFetch) {
return null;
}
$propertyFetchType = $this->resolvePropertyFetchType($if->cond);
if (!$this->staticTypeAnalyzer->isAlwaysTruableType($propertyFetchType)) {
return null;
}
return $if;
}
private function resolvePropertyFetchType(PropertyFetch $propertyFetch) : Type
{
$classLike = $this->betterNodeFinder->findParentType($propertyFetch, Class_::class);
if (!$classLike instanceof Class_) {
return new MixedType();
}
$propertyName = $this->getName($propertyFetch);
if ($propertyName === null) {
return new MixedType();
}
$property = $classLike->getProperty($propertyName);
if (!$property instanceof Property) {
return new MixedType();
}
// anything but private can be changed from outer scope
if (!$property->isPrivate()) {
return new MixedType();
}
// set in constructor + changed in class
$propertyType = $this->resolvePropertyTypeAfterConstructor($classLike, $propertyName);
$resolvedTypes = [$propertyType];
$defaultValue = $property->props[0]->default;
if ($defaultValue instanceof Expr) {
$resolvedTypes[] = $this->getType($defaultValue);
}
$resolveAssignedType = $this->resolveAssignedTypeInStmtsByPropertyName($classLike->stmts, $propertyName);
if ($resolveAssignedType instanceof Type) {
$resolvedTypes[] = $resolveAssignedType;
}
return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($resolvedTypes);
}
private function resolvePropertyTypeAfterConstructor(Class_ $class, string $propertyName) : Type
{
$propertyTypeFromConstructor = null;
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
if ($constructClassMethod instanceof ClassMethod) {
$propertyTypeFromConstructor = $this->resolveAssignedTypeInStmtsByPropertyName((array) $constructClassMethod->stmts, $propertyName);
}
if ($propertyTypeFromConstructor instanceof Type) {
return $propertyTypeFromConstructor;
}
// undefined property is null by default
return new NullType();
}
/**
* @param Stmt[] $stmts
*/
private function resolveAssignedTypeInStmtsByPropertyName(array $stmts, string $propertyName) : ?Type
{
$resolvedTypes = [];
$this->traverseNodesWithCallable($stmts, function (Node $node) use($propertyName, &$resolvedTypes) : ?int {
if ($node instanceof ClassMethod && $this->isName($node, MethodName::CONSTRUCT)) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if (!$this->isPropertyFetchAssignOfPropertyName($node, $propertyName)) {
return null;
}
if (!$node instanceof Assign) {
return null;
}
$resolvedTypes[] = $this->getType($node->expr);
return null;
});
if ($resolvedTypes === []) {
return null;
}
return $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($resolvedTypes);
}
/**
* E.g. $this->{value} = x
*/
private function isPropertyFetchAssignOfPropertyName(Node $node, string $propertyName) : bool
{
if (!$node instanceof Assign) {
return \false;
}
if (!$node->var instanceof PropertyFetch) {
return \false;
}
return $this->isName($node->var, $propertyName);
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '106bc8ed2efbf98e99c22d6098a78ec2d6588c58';
public const PACKAGE_VERSION = '861022a70bd9f9c8b91f20c7dfacb0d297d33e9a';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-06-04 18:48:27';
public const RELEASE_DATE = '2023-06-04 18:52:51';
/**
* @var int
*/

View File

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

2
vendor/autoload.php vendored
View File

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

View File

@ -1267,7 +1267,6 @@ return array(
'Rector\\CodeQuality\\Rector\\FuncCall\\SingleInArrayToCompareRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\StrvalToTypeCastRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/StrvalToTypeCastRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\UnwrapSprintfOneArgumentRector' => $baseDir . '/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php',
'Rector\\CodeQuality\\Rector\\FunctionLike\\RemoveAlwaysTrueConditionSetInConstructorRector' => $baseDir . '/rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php',
'Rector\\CodeQuality\\Rector\\FunctionLike\\SimplifyUselessVariableRector' => $baseDir . '/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php',
'Rector\\CodeQuality\\Rector\\Identical\\BooleanNotIdenticalToNotIdenticalRector' => $baseDir . '/rules/CodeQuality/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php',
'Rector\\CodeQuality\\Rector\\Identical\\FlipTypeControlToUseExclusiveTypeRector' => $baseDir . '/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit66e33d12c7cef6d358659906bd886349
class ComposerAutoloaderInitb1a1501f14311534647d60d598b6a23f
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit66e33d12c7cef6d358659906bd886349
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit66e33d12c7cef6d358659906bd886349', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInitb1a1501f14311534647d60d598b6a23f', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit66e33d12c7cef6d358659906bd886349', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInitb1a1501f14311534647d60d598b6a23f', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit66e33d12c7cef6d358659906bd886349::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInitb1a1501f14311534647d60d598b6a23f::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit66e33d12c7cef6d358659906bd886349::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInitb1a1501f14311534647d60d598b6a23f::$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 ComposerStaticInit66e33d12c7cef6d358659906bd886349
class ComposerStaticInitb1a1501f14311534647d60d598b6a23f
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1509,7 +1509,6 @@ class ComposerStaticInit66e33d12c7cef6d358659906bd886349
'Rector\\CodeQuality\\Rector\\FuncCall\\SingleInArrayToCompareRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/SingleInArrayToCompareRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\StrvalToTypeCastRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/StrvalToTypeCastRector.php',
'Rector\\CodeQuality\\Rector\\FuncCall\\UnwrapSprintfOneArgumentRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FuncCall/UnwrapSprintfOneArgumentRector.php',
'Rector\\CodeQuality\\Rector\\FunctionLike\\RemoveAlwaysTrueConditionSetInConstructorRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php',
'Rector\\CodeQuality\\Rector\\FunctionLike\\SimplifyUselessVariableRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/FunctionLike/SimplifyUselessVariableRector.php',
'Rector\\CodeQuality\\Rector\\Identical\\BooleanNotIdenticalToNotIdenticalRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Identical/BooleanNotIdenticalToNotIdenticalRector.php',
'Rector\\CodeQuality\\Rector\\Identical\\FlipTypeControlToUseExclusiveTypeRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Identical/FlipTypeControlToUseExclusiveTypeRector.php',
@ -3056,9 +3055,9 @@ class ComposerStaticInit66e33d12c7cef6d358659906bd886349
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit66e33d12c7cef6d358659906bd886349::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit66e33d12c7cef6d358659906bd886349::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit66e33d12c7cef6d358659906bd886349::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInitb1a1501f14311534647d60d598b6a23f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitb1a1501f14311534647d60d598b6a23f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitb1a1501f14311534647d60d598b6a23f::$classMap;
}, null, ClassLoader::class);
}