Updated Rector to commit 84bcfebdf7e9f522c8f9f9e97814c82d3d5d3315

84bcfebdf7 [CodingStyle] Remove AddArrayDefaultToArrayPropertyRector as based on docblock types and public contract, better use type declaration set instad (#5298)
This commit is contained in:
Tomas Votruba 2023-11-28 20:19:53 +00:00
parent 84b97fe47c
commit f8dc5ef396
11 changed files with 11 additions and 339 deletions

View File

@ -6,7 +6,6 @@ namespace RectorPrefix202311;
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
use Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector;
use Rector\CodingStyle\Rector\ClassConst\RemoveFinalFromConstRector;
use Rector\CodingStyle\Rector\ClassConst\SplitGroupedClassConstantsRector;
use Rector\CodingStyle\Rector\ClassMethod\FuncGetArgsToVariadicParamRector;
@ -37,5 +36,5 @@ use Rector\Transform\Rector\FuncCall\FuncCallToConstFetchRector;
use Rector\Visibility\Rector\ClassMethod\ExplicitPublicClassMethodRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->ruleWithConfiguration(FuncCallToConstFetchRector::class, ['php_sapi_name' => 'PHP_SAPI', 'pi' => 'M_PI']);
$rectorConfig->rules([SeparateMultiUseImportsRector::class, PostIncDecToPreIncDecRector::class, NewlineAfterStatementRector::class, RemoveFinalFromConstRector::class, NullableCompareToNullRector::class, ConsistentImplodeRector::class, TernaryConditionVariableAssignmentRector::class, SymplifyQuoteEscapeRector::class, StringClassNameToClassConstantRector::class, CatchExceptionNameMatchingTypeRector::class, UseIncrementAssignRector::class, SplitDoubleAssignRector::class, EncapsedStringsToSprintfRector::class, WrapEncapsedVariableInCurlyBracesRector::class, NewlineBeforeNewAssignSetRector::class, AddArrayDefaultToArrayPropertyRector::class, MakeInheritedMethodVisibilitySameAsParentRector::class, CallUserFuncArrayToVariadicRector::class, VersionCompareFuncCallToConstantRector::class, StaticArrowFunctionRector::class, StaticClosureRector::class, CountArrayToEmptyArrayComparisonRector::class, CallUserFuncToMethodCallRector::class, FuncGetArgsToVariadicParamRector::class, StrictArraySearchRector::class, UseClassKeywordForClassNameResolutionRector::class, SplitGroupedPropertiesRector::class, SplitGroupedClassConstantsRector::class, ExplicitPublicClassMethodRector::class, ArraySpreadInsteadOfArrayMergeRector::class]);
$rectorConfig->rules([SeparateMultiUseImportsRector::class, PostIncDecToPreIncDecRector::class, NewlineAfterStatementRector::class, RemoveFinalFromConstRector::class, NullableCompareToNullRector::class, ConsistentImplodeRector::class, TernaryConditionVariableAssignmentRector::class, SymplifyQuoteEscapeRector::class, StringClassNameToClassConstantRector::class, CatchExceptionNameMatchingTypeRector::class, UseIncrementAssignRector::class, SplitDoubleAssignRector::class, EncapsedStringsToSprintfRector::class, WrapEncapsedVariableInCurlyBracesRector::class, NewlineBeforeNewAssignSetRector::class, MakeInheritedMethodVisibilitySameAsParentRector::class, CallUserFuncArrayToVariadicRector::class, VersionCompareFuncCallToConstantRector::class, StaticArrowFunctionRector::class, StaticClosureRector::class, CountArrayToEmptyArrayComparisonRector::class, CallUserFuncToMethodCallRector::class, FuncGetArgsToVariadicParamRector::class, StrictArraySearchRector::class, UseClassKeywordForClassNameResolutionRector::class, SplitGroupedPropertiesRector::class, SplitGroupedClassConstantsRector::class, ExplicitPublicClassMethodRector::class, ArraySpreadInsteadOfArrayMergeRector::class]);
};

View File

@ -1,4 +1,4 @@
# 352 Rules Overview
# 351 Rules Overview
<br>
@ -8,7 +8,7 @@
- [CodeQuality](#codequality) (72)
- [CodingStyle](#codingstyle) (28)
- [CodingStyle](#codingstyle) (27)
- [DeadCode](#deadcode) (42)
@ -1572,31 +1572,6 @@ Use ===/!== over ==/!=, it values have the same type
## CodingStyle
### AddArrayDefaultToArrayPropertyRector
Adds array default value to property to prevent foreach over null error
- class: [`Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector`](../rules/CodingStyle/Rector/Class_/AddArrayDefaultToArrayPropertyRector.php)
```diff
class SomeClass
{
/**
* @var int[]
*/
- private $values;
+ private $values = [];
public function isEmpty()
{
- return $this->values === null;
+ return $this->values === [];
}
}
```
<br>
### ArraySpreadInsteadOfArrayMergeRector
Change `array_merge()` to spread operator

View File

@ -1,258 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\CodingStyle\TypeAnalyzer\IterableTypeAnalyzer;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\Core\Rector\AbstractRector;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector\AddArrayDefaultToArrayPropertyRectorTest
* @changelog https://3v4l.org/dPlUg
*/
final class AddArrayDefaultToArrayPropertyRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;
/**
* @readonly
* @var \Rector\CodingStyle\TypeAnalyzer\IterableTypeAnalyzer
*/
private $iterableTypeAnalyzer;
/**
* @readonly
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
*/
private $visibilityManipulator;
/**
* @readonly
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
*/
private $constructorAssignDetector;
/**
* @readonly
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
*/
private $phpDocInfoFactory;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
public function __construct(PropertyFetchAnalyzer $propertyFetchAnalyzer, IterableTypeAnalyzer $iterableTypeAnalyzer, VisibilityManipulator $visibilityManipulator, ConstructorAssignDetector $constructorAssignDetector, PhpDocInfoFactory $phpDocInfoFactory, ValueResolver $valueResolver, BetterNodeFinder $betterNodeFinder)
{
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
$this->iterableTypeAnalyzer = $iterableTypeAnalyzer;
$this->visibilityManipulator = $visibilityManipulator;
$this->constructorAssignDetector = $constructorAssignDetector;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->valueResolver = $valueResolver;
$this->betterNodeFinder = $betterNodeFinder;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Adds array default value to property to prevent foreach over null error', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var int[]
*/
private $values;
public function isEmpty()
{
return $this->values === null;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var int[]
*/
private $values = [];
public function isEmpty()
{
return $this->values === [];
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
if ($node->isReadonly()) {
return null;
}
$changedProperties = $this->collectPropertyNamesWithMissingDefaultArray($node);
if ($changedProperties === []) {
return null;
}
$this->completeDefaultArrayToPropertyNames($node, $changedProperties);
// $this->variable !== null && count($this->variable) > 0 → count($this->variable) > 0
$this->clearNotNullBeforeCount($node, $changedProperties);
// $this->variable === null → $this->variable === []
$this->replaceNullComparisonOfArrayPropertiesWithArrayComparison($node, $changedProperties);
return $node;
}
/**
* @return string[]
*/
private function collectPropertyNamesWithMissingDefaultArray(Class_ $class) : array
{
$propertyNames = [];
$this->traverseNodesWithCallable($class, function (Node $node) use($class, &$propertyNames) {
if (!$node instanceof Property) {
return null;
}
foreach ($node->props as $propertyProperty) {
if ($propertyProperty->default instanceof Expr) {
continue;
}
$varType = $this->resolveVarType($node);
if (!$this->iterableTypeAnalyzer->detect($varType)) {
continue;
}
if ($this->visibilityManipulator->isReadonly($node)) {
return null;
}
$propertyName = $this->getName($propertyProperty);
if ($this->constructorAssignDetector->isPropertyAssigned($class, $propertyName)) {
return null;
}
$propertyNames[] = $propertyName;
}
return null;
});
return $propertyNames;
}
/**
* @param string[] $propertyNames
*/
private function completeDefaultArrayToPropertyNames(Class_ $class, array $propertyNames) : void
{
$this->traverseNodesWithCallable($class, function (Node $node) use($propertyNames) : ?PropertyProperty {
if (!$node instanceof PropertyProperty) {
return null;
}
if (!$this->isNames($node, $propertyNames)) {
return null;
}
$node->default = new Array_();
return $node;
});
}
/**
* @param string[] $propertyNames
*/
private function clearNotNullBeforeCount(Class_ $class, array $propertyNames) : void
{
$this->traverseNodesWithCallable($class, function (Node $node) use($propertyNames) : ?Expr {
if (!$node instanceof BooleanAnd) {
return null;
}
if (!$this->isLocalPropertyOfNamesNotIdenticalToNull($node->left, $propertyNames)) {
return null;
}
$isNextNodeCountingProperty = (bool) $this->betterNodeFinder->findFirst($node->right, function (Node $node) use($propertyNames) : bool {
if (!$node instanceof FuncCall) {
return \false;
}
if ($node->isFirstClassCallable()) {
return \false;
}
if (!$this->isName($node, 'count')) {
return \false;
}
$countedArgument = $node->getArgs()[0]->value;
if (!$countedArgument instanceof PropertyFetch) {
return \false;
}
return $this->isNames($countedArgument, $propertyNames);
});
if (!$isNextNodeCountingProperty) {
return null;
}
return $node->right;
});
}
/**
* @param string[] $propertyNames
*/
private function replaceNullComparisonOfArrayPropertiesWithArrayComparison(Class_ $class, array $propertyNames) : void
{
// replace comparison to "null" with "[]"
$this->traverseNodesWithCallable($class, function (Node $node) use($propertyNames) : ?BinaryOp {
if (!$node instanceof BinaryOp) {
return null;
}
if ($this->propertyFetchAnalyzer->isLocalPropertyOfNames($node->left, $propertyNames) && $this->valueResolver->isNull($node->right)) {
$node->right = new Array_();
}
if ($this->propertyFetchAnalyzer->isLocalPropertyOfNames($node->right, $propertyNames) && $this->valueResolver->isNull($node->left)) {
$node->left = new Array_();
}
return $node;
});
}
private function resolveVarType(Property $property) : Type
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
return $phpDocInfo->getVarType();
}
/**
* @param string[] $propertyNames
*/
private function isLocalPropertyOfNamesNotIdenticalToNull(Expr $expr, array $propertyNames) : bool
{
if (!$expr instanceof NotIdentical) {
return \false;
}
if ($this->propertyFetchAnalyzer->isLocalPropertyOfNames($expr->left, $propertyNames) && $this->valueResolver->isNull($expr->right)) {
return \true;
}
if (!$this->propertyFetchAnalyzer->isLocalPropertyOfNames($expr->right, $propertyNames)) {
return \false;
}
return $this->valueResolver->isNull($expr->left);
}
}

View File

@ -1,29 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\CodingStyle\TypeAnalyzer;
use PHPStan\Type\IterableType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
final class IterableTypeAnalyzer
{
public function detect(Type $type) : bool
{
if ($type instanceof IterableType) {
return \true;
}
if ($type->isArray()->yes()) {
return \true;
}
if ($type instanceof UnionType) {
foreach ($type->getTypes() as $unionedType) {
if (!$this->detect($unionedType)) {
return \false;
}
}
return \true;
}
return \false;
}
}

View File

@ -116,6 +116,7 @@ final class VisibilityManipulator
$this->addVisibilityFlag($node, Visibility::READONLY);
}
/**
* @api
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $node
*/
public function isReadonly($node) : bool

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'c3840325600449b562691e34612279dcd4316043';
public const PACKAGE_VERSION = '84bcfebdf7e9f522c8f9f9e97814c82d3d5d3315';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-11-29 00:39:41';
public const RELEASE_DATE = '2023-11-28 21:17:42';
/**
* @var int
*/

View File

@ -4,7 +4,6 @@ declare (strict_types=1);
namespace Rector\Core\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
@ -161,17 +160,6 @@ final class PropertyFetchAnalyzer
}
return \false;
}
/**
* @param string[] $propertyNames
*/
public function isLocalPropertyOfNames(Expr $expr, array $propertyNames) : bool
{
if (!$this->isLocalPropertyFetch($expr)) {
return \false;
}
/** @var PropertyFetch $expr */
return $this->nodeNameResolver->isNames($expr->name, $propertyNames);
}
private function isTraitLocalPropertyFetch(Node $node) : bool
{
if ($node instanceof PropertyFetch) {

View File

@ -1120,7 +1120,6 @@ return array(
'Rector\\CodingStyle\\Rector\\ClassMethod\\FuncGetArgsToVariadicParamRector' => $baseDir . '/rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php',
'Rector\\CodingStyle\\Rector\\ClassMethod\\MakeInheritedMethodVisibilitySameAsParentRector' => $baseDir . '/rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php',
'Rector\\CodingStyle\\Rector\\ClassMethod\\NewlineBeforeNewAssignSetRector' => $baseDir . '/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php',
'Rector\\CodingStyle\\Rector\\Class_\\AddArrayDefaultToArrayPropertyRector' => $baseDir . '/rules/CodingStyle/Rector/Class_/AddArrayDefaultToArrayPropertyRector.php',
'Rector\\CodingStyle\\Rector\\Closure\\StaticClosureRector' => $baseDir . '/rules/CodingStyle/Rector/Closure/StaticClosureRector.php',
'Rector\\CodingStyle\\Rector\\Encapsed\\EncapsedStringsToSprintfRector' => $baseDir . '/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php',
'Rector\\CodingStyle\\Rector\\Encapsed\\WrapEncapsedVariableInCurlyBracesRector' => $baseDir . '/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php',
@ -1141,7 +1140,6 @@ return array(
'Rector\\CodingStyle\\Rector\\Ternary\\TernaryConditionVariableAssignmentRector' => $baseDir . '/rules/CodingStyle/Rector/Ternary/TernaryConditionVariableAssignmentRector.php',
'Rector\\CodingStyle\\Rector\\Use_\\SeparateMultiUseImportsRector' => $baseDir . '/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php',
'Rector\\CodingStyle\\Reflection\\VendorLocationDetector' => $baseDir . '/rules/CodingStyle/Reflection/VendorLocationDetector.php',
'Rector\\CodingStyle\\TypeAnalyzer\\IterableTypeAnalyzer' => $baseDir . '/rules/CodingStyle/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\CodingStyle\\ValueObject\\ObjectMagicMethods' => $baseDir . '/rules/CodingStyle/ValueObject/ObjectMagicMethods.php',
'Rector\\Comments\\CommentRemover' => $baseDir . '/packages/Comments/CommentRemover.php',
'Rector\\Comments\\NodeDocBlock\\DocBlockUpdater' => $baseDir . '/packages/Comments/NodeDocBlock/DocBlockUpdater.php',

View File

@ -1338,7 +1338,6 @@ class ComposerStaticInita55c41c7fa52abd86138c6f32df1d185
'Rector\\CodingStyle\\Rector\\ClassMethod\\FuncGetArgsToVariadicParamRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/ClassMethod/FuncGetArgsToVariadicParamRector.php',
'Rector\\CodingStyle\\Rector\\ClassMethod\\MakeInheritedMethodVisibilitySameAsParentRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/ClassMethod/MakeInheritedMethodVisibilitySameAsParentRector.php',
'Rector\\CodingStyle\\Rector\\ClassMethod\\NewlineBeforeNewAssignSetRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php',
'Rector\\CodingStyle\\Rector\\Class_\\AddArrayDefaultToArrayPropertyRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Class_/AddArrayDefaultToArrayPropertyRector.php',
'Rector\\CodingStyle\\Rector\\Closure\\StaticClosureRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Closure/StaticClosureRector.php',
'Rector\\CodingStyle\\Rector\\Encapsed\\EncapsedStringsToSprintfRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Encapsed/EncapsedStringsToSprintfRector.php',
'Rector\\CodingStyle\\Rector\\Encapsed\\WrapEncapsedVariableInCurlyBracesRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Encapsed/WrapEncapsedVariableInCurlyBracesRector.php',
@ -1359,7 +1358,6 @@ class ComposerStaticInita55c41c7fa52abd86138c6f32df1d185
'Rector\\CodingStyle\\Rector\\Ternary\\TernaryConditionVariableAssignmentRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Ternary/TernaryConditionVariableAssignmentRector.php',
'Rector\\CodingStyle\\Rector\\Use_\\SeparateMultiUseImportsRector' => __DIR__ . '/../..' . '/rules/CodingStyle/Rector/Use_/SeparateMultiUseImportsRector.php',
'Rector\\CodingStyle\\Reflection\\VendorLocationDetector' => __DIR__ . '/../..' . '/rules/CodingStyle/Reflection/VendorLocationDetector.php',
'Rector\\CodingStyle\\TypeAnalyzer\\IterableTypeAnalyzer' => __DIR__ . '/../..' . '/rules/CodingStyle/TypeAnalyzer/IterableTypeAnalyzer.php',
'Rector\\CodingStyle\\ValueObject\\ObjectMagicMethods' => __DIR__ . '/../..' . '/rules/CodingStyle/ValueObject/ObjectMagicMethods.php',
'Rector\\Comments\\CommentRemover' => __DIR__ . '/../..' . '/packages/Comments/CommentRemover.php',
'Rector\\Comments\\NodeDocBlock\\DocBlockUpdater' => __DIR__ . '/../..' . '/packages/Comments/NodeDocBlock/DocBlockUpdater.php',

View File

@ -504,8 +504,8 @@
},
{
"name": "illuminate\/container",
"version": "v10.34.1",
"version_normalized": "10.34.1.0",
"version": "v10.34.2",
"version_normalized": "10.34.2.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/illuminate\/container.git",
@ -561,8 +561,8 @@
},
{
"name": "illuminate\/contracts",
"version": "v10.34.1",
"version_normalized": "10.34.1.0",
"version": "v10.34.2",
"version_normalized": "10.34.2.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/illuminate\/contracts.git",

File diff suppressed because one or more lines are too long