static fixes (#4860)

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Tomas Votruba 2020-12-12 13:08:34 +01:00 committed by GitHub
parent d7337f7959
commit db5e02e724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 116 additions and 466 deletions

View File

@ -54,7 +54,7 @@
"symplify/coding-standard": "^9.0.5",
"symplify/easy-testing": "^9.0.5",
"symplify/phpstan-extensions": "^9.0.5",
"symplify/phpstan-rules": "^9.0.5",
"symplify/phpstan-rules": "dev-master",
"tracy/tracy": "^2.7"
},
"replace": {

View File

@ -2,9 +2,9 @@
declare(strict_types=1);
use Rector\Symfony4\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
use Rector\Symfony\Rector\MethodCall\GetParameterToConstructorInjectionRector;
use Rector\Symfony\Rector\MethodCall\GetToConstructorInjectionRector;
use Rector\Symfony4\Rector\MethodCall\ContainerGetToConstructorInjectionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {

View File

@ -49,12 +49,13 @@ final class FileHashComputer
{
$fileLocator = new FileLocator([$fileInfo->getPath()]);
$loaderResolver = new LoaderResolver([
$fileLoaders = [
new GlobFileLoader($containerBuilder, $fileLocator),
new PhpFileLoader($containerBuilder, $fileLocator),
new YamlFileLoader($containerBuilder, $fileLocator),
]);
];
$loaderResolver = new LoaderResolver($fileLoaders);
$loader = $loaderResolver->resolve($fileInfo->getRealPath());
if (! $loader) {
throw new ShouldNotHappenException();

View File

@ -53,7 +53,6 @@ final class ParsedClassConstFetchNodeCollector
}
$constantName = $this->nodeNameResolver->getName($node->name);
if ($constantName === 'class' || $constantName === null) {
// this is not a manual constant
return;

View File

@ -1,98 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\NodeTypeResolver\Tests\NodeVisitor\FunctionMethodAndClassNodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeVisitor\FunctionMethodAndClassNodeVisitor;
use Rector\Testing\PHPUnit\AbstractNodeVisitorTestCase;
final class FunctionMethodAndClassNodeVisitorTest extends AbstractNodeVisitorTestCase
{
/**
* @var FunctionMethodAndClassNodeVisitor
*/
private $functionMethodAndClassNodeVisitor;
protected function setUp(): void
{
parent::setUp();
$this->bootKernel(RectorKernel::class);
$this->functionMethodAndClassNodeVisitor = self::$container->get(FunctionMethodAndClassNodeVisitor::class);
}
public function testMethodName(): void
{
$parsedAttributes = $this->parseFileToAttribute(__DIR__ . '/Fixture/simple.php.inc', AttributeKey::METHOD_NAME);
$classAttributes = $parsedAttributes[3];
$this->assertSame(null, $classAttributes[AttributeKey::METHOD_NAME]);
$classMethodAttributes = $parsedAttributes[4];
$this->assertSame('bar', $classMethodAttributes[AttributeKey::METHOD_NAME]);
$methodNameAttributes = $parsedAttributes[5];
$this->assertSame('bar', $methodNameAttributes[AttributeKey::METHOD_NAME]);
}
public function testClassName(): void
{
$parsedAttributes = $this->parseFileToAttribute(__DIR__ . '/Fixture/simple.php.inc', AttributeKey::CLASS_NAME);
$classMethodAttributes = $parsedAttributes[3];
$this->assertSame(
'Rector\NodeTypeResolver\Tests\NodeVisitor\FunctionMethodAndClassNodeVisitorTest\Fixture\Foo',
$classMethodAttributes[AttributeKey::CLASS_NAME]
);
$classAttributes = $parsedAttributes[4];
$this->assertSame(
'Rector\NodeTypeResolver\Tests\NodeVisitor\FunctionMethodAndClassNodeVisitorTest\Fixture\Foo',
$classAttributes[AttributeKey::CLASS_NAME]
);
}
public function testClassNode(): void
{
$parsedAttributes = $this->parseFileToAttribute(__DIR__ . '/Fixture/simple.php.inc', AttributeKey::CLASS_NODE);
$classMethodAttributes = $parsedAttributes[3];
$this->assertInstanceOf(Class_::class, $classMethodAttributes[AttributeKey::CLASS_NODE]);
}
public function testAnonymousClassName(): void
{
$parsedAttributes = $this->parseFileToAttribute(
__DIR__ . '/Fixture/anonymous_class.php.inc',
AttributeKey::CLASS_NAME
);
$funcCallAttributes = $parsedAttributes[10];
$this->assertSame(
'Rector\NodeTypeResolver\Tests\NodeVisitor\FunctionMethodAndClassNodeVisitorTest\Fixture\AnonymousClass',
$funcCallAttributes[AttributeKey::CLASS_NAME]
);
// method in the anonymous class has no class name
$anoymousClassMethodAttributes = $parsedAttributes[8];
$this->assertNull($anoymousClassMethodAttributes[AttributeKey::CLASS_NAME]);
}
/**
* @param Node[] $nodes
*/
public function traverseNodes(array $nodes): void
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NameResolver());
$nodeTraverser->addVisitor($this->functionMethodAndClassNodeVisitor);
$nodeTraverser->traverse($nodes);
}
}

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Rector\Testing\Application;
use Rector\Core\Configuration\ChangeConfiguration;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
@ -17,13 +17,13 @@ final class EnabledRectorsProvider
private $enabledRectorsWithConfiguration = [];
/**
* @var ChangeConfiguration
* @var RenamedClassesDataCollector
*/
private $changeConfiguration;
private $renamedClassesDataCollector;
public function __construct(ChangeConfiguration $changeConfiguration)
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector)
{
$this->changeConfiguration = $changeConfiguration;
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
}
/**
@ -39,7 +39,7 @@ final class EnabledRectorsProvider
return;
}
// only in unit tests
$this->changeConfiguration->setOldToNewClasses($configuration[RenameClassRector::OLD_TO_NEW_CLASSES] ?? []);
$this->renamedClassesDataCollector->setOldToNewClasses($configuration[RenameClassRector::OLD_TO_NEW_CLASSES] ?? []);
}
public function reset(): void

View File

@ -1,38 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Testing\Node;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use Rector\Testing\NodeVisitor\AttributeCollectingNodeVisitor;
final class NodeAttributeExtractor
{
/**
* @var AttributeCollectingNodeVisitor
*/
private $attributeCollectingNodeVisitor;
public function __construct(AttributeCollectingNodeVisitor $attributeCollectingNodeVisitor)
{
$this->attributeCollectingNodeVisitor = $attributeCollectingNodeVisitor;
}
/**
* @param Node[] $nodes
* @return mixed[]
*/
public function extract(array $nodes, string $relevantAttribute): array
{
$this->attributeCollectingNodeVisitor->reset();
$this->attributeCollectingNodeVisitor->setRelevantAttribute($relevantAttribute);
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($this->attributeCollectingNodeVisitor);
$nodeTraverser->traverse($nodes);
return $this->attributeCollectingNodeVisitor->getCollectedAttributes();
}
}

View File

@ -1,68 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Testing\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeVisitorAbstract;
use Rector\Core\Exception\ShouldNotHappenException;
final class AttributeCollectingNodeVisitor extends NodeVisitorAbstract
{
/**
* @var string
*/
private $relevantAttribute;
/**
* @var mixed[]
*/
private $attributes = [];
public function setRelevantAttribute(string $relevantAttribute): void
{
$this->relevantAttribute = $relevantAttribute;
}
public function enterNode(Node $node): ?Node
{
if ($this->relevantAttribute === null) {
throw new ShouldNotHappenException();
}
if ($node instanceof Expression) {
return null;
}
$attributes = $this->getFilteredAttributes($node);
$this->attributes[] = array_merge([
'node_class' => get_class($node),
], $attributes);
return null;
}
/**
* @return mixed[]
*/
public function getCollectedAttributes(): array
{
return $this->attributes;
}
public function reset(): void
{
$this->attributes = [];
}
/**
* @return mixed[]
*/
private function getFilteredAttributes(Node $node): array
{
$attributes = $node->getAttributes();
return array_intersect_key($attributes, array_flip([$this->relevantAttribute]));
}
}

View File

@ -1,52 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Testing\PHPUnit;
use PhpParser\Node;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\Core\PhpParser\Parser\Parser;
use Rector\Testing\Contract\NodeTraversableInterface;
use Rector\Testing\Node\NodeAttributeExtractor;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* Class can be used to test node visitors
* To update the fixture run phpunit as follows
* $ UPDATE_FIXTURE=1 vendor/bin/phpunit
*/
abstract class AbstractNodeVisitorTestCase extends AbstractKernelTestCase implements NodeTraversableInterface
{
/**
* @var NodeAttributeExtractor
*/
protected $nodeAttributeExtractor;
/**
* @var Parser
*/
protected $parser;
protected function setUp(): void
{
$this->bootKernelWithConfigs(RectorKernel::class, []);
$this->parser = static::$container->get(Parser::class);
$this->nodeAttributeExtractor = static::$container->get(NodeAttributeExtractor::class);
}
/**
* @return mixed[]
*/
protected function parseFileToAttribute(string $file, string $relevantAttribute): array
{
$fileInfo = new SmartFileInfo($file);
$nodes = $this->parser->parseFileInfo($fileInfo);
$this->traverseNodes($nodes);
return $this->nodeAttributeExtractor->extract($nodes, $relevantAttribute);
}
}

View File

@ -486,29 +486,11 @@ parameters:
# complex - single repository for all nodes
- '#Class cognitive complexity for "NodeRepository" is \d+, keep it under 50#'
# symplify rules fix later
- '#Use another value object over string with value object arrays#'
- '#Use decouled factory service to create "(.*?)" object#'
# hotskips, fix sooner
- '#Post operation are forbidden, as they make 2 values at the same line\. Use pre instead#'
- '#Use value object over multi array assign#'
# - '#(.*?) expects class\-string, string given#'
- '#Method "(.*?)" is using too many parameters \- \d+\. Make it under 8#'
-
message: '#Instead of "Symfony\\Component\\Finder\\SplFileInfo" class/interface use "Symplify\\SmartFileSystem\\SmartFileInfo"#'
paths:
- src/FileSystem/FilesFinder.php
-
message: '#Node "errorsuppress" is fobidden to use#'
paths:
- rules/symfony/src/ServiceMapProvider.php # 67
- utils/project-validator/src/CpuCoreCountResolver.php # 35
-
message: '#Instead of container injection, use specific service#'
paths:
@ -537,12 +519,6 @@ parameters:
- packages/phpstan-static-type-mapper/src/PHPStanStaticTypeMapper.php # 46
- rules/nette-code-quality/src/NodeResolver/MethodNamesByInputNamesResolver.php # 25
-
message: '#new <class\> is limited to 3 "new <class\>\(new <class\>\)\)" nesting to each other\. You have \d+ nesting#'
paths:
- *Rector.php
- packages/caching/src/Config/FileHashComputer.php
-
message: '#Property with protected modifier is not allowed\. Use interface instead#'
paths:
@ -553,9 +529,6 @@ parameters:
- packages/testing/src/PHPUnit/AbstractGenericRectorTestCase.php # 68
- packages/testing/src/PHPUnit/AbstractRectorTestCase.php # 24
- '#Method call in if or elseif is not allowed#'
- '#Method or Static call in foreach is not allowed#'
-
message: '#Do not use static property#'
paths:
@ -590,53 +563,8 @@ parameters:
- utils/doctrine-annotation-parser-syncer/src/Rector/Namespace_/RenameAnnotationReaderClassRector.php # 41
- utils/doctrine-annotation-parser-syncer/src/Rector/Namespace_/RenameDocParserClassRector.php # 41
-
message: '#Do not use setter on a service#'
paths:
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareExtendsTagValueNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareImplementsTagValueNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareMethodTagValueNodeFactory.php # 53
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareMethodTagValueParameterNodeFactory.php # 50
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareParamTagValueNodeFactory.php # 48
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwarePhpDocNodeFactory.php # 61
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwarePropertyTagValueNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareReturnTagValueNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareThrowsTagValueNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/PhpDoc/AttributeAwareVarTagValueNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareArrayShapeItemNodeFactory.php # 41
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareArrayShapeNodeFactory.php # 45
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareArrayTypeNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareCallableTypeNodeFactory.php # 48
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareGenericTypeNodeFactory.php # 47
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareIntersectionTypeNodeFactory.php # 44
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareNullableTypeNodeFactory.php # 42
- packages/attribute-aware-php-doc/src/AttributeAwareNodeFactory/Type/AttributeAwareUnionTypeNodeFactory.php # 44
- packages/better-php-doc-parser/src/PhpDocNodeFactory/PHPUnitDataProviderDocNodeFactory.php # 52
- packages/better-php-doc-parser/src/PhpDocNodeFactory/ParamPhpDocNodeFactory.php # 81
- packages/node-type-resolver/src/FileSystem/CurrentFileInfoProvider.php # 35
- packages/phpstan-static-type-mapper/src/TypeMapper/ObjectTypeMapper.php # 123
- packages/phpstan-static-type-mapper/src/TypeMapper/ObjectWithoutClassTypeMapper.php # 72
- packages/rector-generator/src/Rector/Closure/AddNewServiceToSymfonyPhpConfigRector.php # 39
- rules/nette-code-quality/src/FormControlTypeResolver/AssignedVariablesMethodCallsFormTypeResolver.php # 50
- rules/nette-code-quality/src/FormControlTypeResolver/ClassMethodFormTypeResolver.php # 67
- rules/nette-code-quality/src/FormControlTypeResolver/ConstructorFormControlTypeResolver.php # 62
- rules/nette-code-quality/src/FormControlTypeResolver/GetComponentMethodCallFormControlTypeResolver.php # 110
- rules/nette-code-quality/src/FormControlTypeResolver/MagicNetteFactoryInterfaceFormControlTypeResolver.php # 95
- rules/nette-code-quality/src/FormControlTypeResolver/MethodCallFormControlTypeResolver.php # 59
- rules/nette-code-quality/src/FormControlTypeResolver/NewFormControlTypeResolver.php # 61
- rules/nette-code-quality/src/FormControlTypeResolver/ReturnFormControlTypeResolver.php # 54
- rules/nette-code-quality/src/FormControlTypeResolver/ThisVariableInAnotherMethodFormControlTypeResolver.php # 52
- rules/nette-code-quality/src/FormControlTypeResolver/VariableConstructorFormControlTypeResolver.php # 84
- src/Configuration/ChangeConfiguration.php # 17
- src/Configuration/Configuration.php # 228
- src/Configuration/CurrentNodeProvider.php # 16
- src/HttpKernel/RectorKernel.php # 71
- packages/testing/src/NodeVisitor/AttributeCollectingNodeVisitor.php # 24
- '#Parameter \#1 \$node of method Rector\\DeadCode\\Rector\\Plus\\RemoveDeadZeroAndOneOperationRector\:\:refactor\(\) expects PhpParser\\Node\\Expr\\AssignOp\\Div\|PhpParser\\Node\\Expr\\AssignOp\\Minus\|PhpParser\\Node\\Expr\\AssignOp\\Mul\|PhpParser\\Node\\Expr\\AssignOp\\Plus\|PhpParser\\Node\\Expr\\BinaryOp\\Div\|PhpParser\\Node\\Expr\\BinaryOp\\Minus\|PhpParser\\Node\\Expr\\BinaryOp\\Mul\|PhpParser\\Node\\Expr\\BinaryOp\\Plus, PhpParser\\Node\\Expr\\AssignOp\|PhpParser\\Node\\Expr\\BinaryOp given#'
- '#Cognitive complexity for "Rector\\CodeQuality\\Naming\\MethodCallToVariableNameResolver\:\:resolveVariableName\(\)" is 12, keep it under 9#'
-
message: '#Do not use trait#'
paths:
@ -648,35 +576,13 @@ parameters:
- packages/post-rector/src/Rector/AbstractRector/NodeCommandersTrait.php # 31
- packages/reporting/src/Rector/AbstractRector/NodeReportCollectorTrait.php # 14
- rules/doctrine/src/AbstractRector/DoctrineTrait.php # 13
- src/Rector/AbstractRector/AbstractRectorTrait.php # 15
- src/Rector/AbstractRector/BetterStandardPrinterTrait.php # 16
- src/Rector/AbstractRector/CallableNodeTraverserTrait.php # 14
- src/Rector/AbstractRector/ComplexRemovalTrait.php # 32
- src/Rector/AbstractRector/ConstFetchAnalyzerTrait.php # 14
- src/Rector/AbstractRector/NameResolverTrait.php # 23
- src/Rector/AbstractRector/NodeCommentingTrait.php # 15
- src/Rector/AbstractRector/NodeFactoryTrait.php # 28
- src/Rector/AbstractRector/NodeTypeResolverTrait.php # 29
- src/Rector/AbstractRector/PhpDocTrait.php # 19
- src/Rector/AbstractRector/RemovedAndAddedFilesTrait.php # 19
- src/Rector/AbstractRector/ValueResolverTrait.php # 14
- src/Rector/AbstractRector/VisibilityTrait.php # 19
- src/Rector/AbstractRector/*
-
message: '#Do not use scalar or array as constructor parameter\. Use "Symplify\\PackageBuilder\\Parameter\\ParameterProvider" service instead#'
paths:
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/DataProviderTagValueNode.php # 22
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/AttributeAwareParamTagValueNode.php # 33
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/AttributeAwareParamTagValueNode.php # 33
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/AttributeAwareParamTagValueNode.php # 33
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/AttributeAwareParamTagValueNode.php # 35
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/AttributeAwareTemplateTagValueNode.php # 30
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/AttributeAwareTemplateTagValueNode.php # 30
- packages/attribute-aware-php-doc/src/Ast/PhpDoc/AttributeAwareTemplateTagValueNode.php # 32
- packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareArrayShapeItemNode.php # 30
- packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareArrayShapeItemNode.php # 33
- packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareArrayShapeItemNode.php # 34
- packages/attribute-aware-php-doc/src/Ast/Type/AttributeAwareUnionTypeNode.php # 35
# value objects
- packages/attribute-aware-php-doc/src/Ast/*
- packages/better-php-doc-parser/src/PhpDocInfo/PhpDocInfo.php # 108
- rules/coding-style/src/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php # 47
- rules/php70/src/EregToPcreTransformer.php # 66
@ -687,16 +593,6 @@ parameters:
- rules/restoration/src/Rector/Class_/RemoveUselessJustForSakeInterfaceRector.php # 42
- rules/type-declaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php # 82
- src/PhpParser/Builder/UseBuilder.php # 17
- src/RectorDefinition/CodeSample.php # 28
- src/RectorDefinition/CodeSample.php # 29
- src/RectorDefinition/CodeSample.php # 30
- src/RectorDefinition/ComposerJsonAwareCodeSample.php # 37
- src/RectorDefinition/ComposerJsonAwareCodeSample.php # 38
- src/RectorDefinition/ComposerJsonAwareCodeSample.php # 39
- src/RectorDefinition/ComposerJsonAwareCodeSample.php # 40
- src/RectorDefinition/ConfiguredCodeSample.php # 40
- src/RectorDefinition/ConfiguredCodeSample.php # 41
- src/RectorDefinition/ConfiguredCodeSample.php # 43
- src/RectorDefinition/RectorDefinition.php # 31
- packages/testing/src/PHPUnit/Runnable/NodeVisitor/PrefixingClassLikeNamesNodeVisitor.php # 35
- packages/testing/src/PHPUnit/Runnable/NodeVisitor/PrefixingClassLikeNamesNodeVisitor.php # 36
@ -704,9 +600,7 @@ parameters:
-
message: '#Use local named constant instead of inline string for regex to explain meaning by constant name#'
paths:
- packages/better-php-doc-parser/src/PartPhpDocTagPrinter/Behavior/ArrayPartPhpDocTagPrinterTrait.php # 30
- packages/better-php-doc-parser/src/PartPhpDocTagPrinter/Behavior/ArrayPartPhpDocTagPrinterTrait.php # 33
- packages/better-php-doc-parser/src/PartPhpDocTagPrinter/Behavior/ArrayPartPhpDocTagPrinterTrait.php # 70
- packages/better-php-doc-parser/src/PartPhpDocTagPrinter/Behavior/* # 30
- packages/better-php-doc-parser/src/PhpDocNode/PrintTagValueNodeTrait.php # 54
# allow in new <types> config
@ -714,8 +608,6 @@ parameters:
message: '#new <class\> is limited to 3 "new <class\>\(new <class\>\)\)" nesting to each other\. You have \d+ nesting#'
path: config/set/*
- '#Cognitive complexity for "Rector\\CodeQuality\\Naming\\MethodCallToVariableNameResolver\:\:resolveVariableName\(\)" is 12, keep it under 9#'
# nodes/stmts
- '#Parameter \#1 \$nodes of method Rector\\Core\\PhpParser\\Node\\BetterNodeFinder\:\:findFirst\(\) expects array<PhpParser\\Node\>\|PhpParser\\Node, array<PhpParser\\Node\\Stmt\>\|null given#'
# finder type
@ -809,5 +701,19 @@ parameters:
- packages/better-php-doc-parser/tests/PhpDocParser/AbstractPhpDocInfoTest.php
- packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/AbstractPhpDocInfoPrinterTest.php
- '#"@simplexml_load_string\(\$fileContents\)" is forbidden to use#'
- '#@ intentionally#'
-
message: '#Use value object over multi array assign#'
paths:
- packages/node-collector/src/NodeCollector/ParsedClassConstFetchNodeCollector.php
-
message: '#Do not use setter on a service#'
paths:
# setters for tests
- src/Configuration/Configuration.php
# data collector
- src/Configuration/RenamedClassesDataCollector.php
# custom internal rule
- packages/rector-generator/src/Rector/Closure/AddNewServiceToSymfonyPhpConfigRector.php
# hacking around closed design
- packages/better-php-doc-parser/src/PhpDocNodeFactory/ParamPhpDocNodeFactory.php

View File

@ -5,12 +5,10 @@ declare(strict_types=1);
namespace Rector\DowngradePhp71\Rector\String_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -83,7 +81,9 @@ CODE_SAMPLE
/** @var UnaryMinus $dim */
$dim = $parentOfNextNode->dim;
$parentOfNextNode->dim = new Minus(new FuncCall(new Name('strlen'), [new Arg($string)]), $dim->expr);
$strlenFuncCall = $this->createFuncCall('strlen', [$string]);
$parentOfNextNode->dim = new Minus($strlenFuncCall, $dim->expr);
return $string;
}
@ -104,10 +104,8 @@ CODE_SAMPLE
return null;
}
$funcCall->args[2]->value = new Minus(
new FuncCall(new Name('strlen'), [new Arg($args[0]->value)]),
$args[2]->value->expr
);
$strlenFuncCall = $this->createFuncCall('strlen', [$args[0]]);
$funcCall->args[2]->value = new Minus($strlenFuncCall, $args[2]->value->expr);
return $funcCall;
}

View File

@ -155,7 +155,7 @@ CODE_SAMPLE
*/
private function fillMissingArgumentsWithDefaultValues(array $args): array
{
for ($i = 1; $i < $this->highestIndex; $i++) {
for ($i = 1; $i < $this->highestIndex; ++$i) {
if (isset($args[$i])) {
continue;
}

View File

@ -160,11 +160,11 @@ CODE_SAMPLE
$count = 0;
$listItemsCount = count($listItems);
// Start from the end => right-side-most params
for ($i = $listItemsCount - 1; $i >= 0; $i--) {
for ($i = $listItemsCount - 1; $i >= 0; --$i) {
$listItem = $listItems[$i];
// Also include null items, since they can be removed
if ($listItem === null || $listItem->byRef) {
$count++;
++$count;
continue;
}
// If it is a nested list, check if all its items are by reference
@ -173,7 +173,7 @@ CODE_SAMPLE
/** @var List_|Array_ */
$nestedList = $listItem->value;
if ($this->hasAllItemsByRef($nestedList->items)) {
$count++;
++$count;
continue;
}
}

View File

@ -94,7 +94,9 @@ CODE_SAMPLE
// The return type name could either be a classname, without the leading "\",
// or one among the reserved identifiers ("static", "self", "iterable", etc)
// To find out which is the case, check if this name exists as a class
$newType = ClassExistenceStaticHelper::doesClassLikeExist($parentReflectionMethodName) ? new FullyQualified($parentReflectionMethodName) : new Name($parentReflectionMethodName);
$newType = ClassExistenceStaticHelper::doesClassLikeExist($parentReflectionMethodName) ? new FullyQualified(
$parentReflectionMethodName
) : new Name($parentReflectionMethodName);
// Make it nullable?
if ($node->returnType instanceof NullableType) {

View File

@ -5,9 +5,6 @@ declare(strict_types=1);
namespace Rector\Generic\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
@ -127,8 +124,7 @@ CODE_SAMPLE
private function createParentStaticCall(string $method): Expression
{
$staticCall = new StaticCall(new Name('parent'), new Identifier($method));
$staticCall = $this->createStaticCall('parent', $method);
return new Expression($staticCall);
}

View File

@ -101,7 +101,7 @@ CODE_SAMPLE
$attempt = 0;
while (! $sortedAndOriginalClassMethods->hasOrderSame()) {
$attempt++;
++$attempt;
if ($attempt >= self::MAX_ATTEMPTS) {
throw new ShouldNotHappenException('Number of attempts to reorder the methods exceeded');
}

View File

@ -222,13 +222,14 @@ final class EregToPcreTransformer
/** @var string $cls */
[$cls, $i] = $this->processCharacterClass($s, $i, $cls);
} else {
$a = $s[$i++];
$a = $s[$i];
++$i;
if ($a === '-' && ! $start && ! ($i < $l && $s[$i] === ']')) {
throw new InvalidEregException('"-" is invalid for the start character in the brackets');
}
if ($i < $l && $s[$i] === '-') {
$b = $s[++$i];
++$i;
$b = $s[$i++];
if ($b === ']') {
$cls .= $this->_ere2pcre_escape($a) . '\-';
break;

View File

@ -74,7 +74,7 @@ final class RemoveExtraParametersRector extends AbstractRector
$numberOfParameters = count($parametersAcceptor->getParameters());
$numberOfArguments = count((array) $node->args);
for ($i = $numberOfParameters; $i <= $numberOfArguments; $i++) {
for ($i = $numberOfParameters; $i <= $numberOfArguments; ++$i) {
unset($node->args[$i]);
}

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Rector\PSR4\Collector;
use Rector\Core\Configuration\ChangeConfiguration;
use Rector\Core\Configuration\RenamedClassesDataCollector;
final class RenamedClassesCollector
{
@ -14,13 +14,13 @@ final class RenamedClassesCollector
private $oldToNewClass = [];
/**
* @var ChangeConfiguration
* @var RenamedClassesDataCollector
*/
private $changeConfiguration;
private $renamedClassesDataCollector;
public function __construct(ChangeConfiguration $changeConfiguration)
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector)
{
$this->changeConfiguration = $changeConfiguration;
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
}
public function addClassRename(string $oldClass, string $newClass): void
@ -33,6 +33,6 @@ final class RenamedClassesCollector
*/
public function getOldToNewClasses(): array
{
return array_merge($this->oldToNewClass, $this->changeConfiguration->getOldToNewClasses());
return array_merge($this->oldToNewClass, $this->renamedClassesDataCollector->getOldToNewClasses());
}
}

View File

@ -11,7 +11,7 @@ use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Configuration\ChangeConfiguration;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
@ -40,14 +40,14 @@ final class RenameClassRector extends AbstractRector implements ConfigurableRect
private $classRenamer;
/**
* @var ChangeConfiguration
* @var RenamedClassesDataCollector
*/
private $changeConfiguration;
private $renamedClassesDataCollector;
public function __construct(ChangeConfiguration $changeConfiguration, ClassRenamer $classRenamer)
public function __construct(RenamedClassesDataCollector $renamedClassesDataCollector, ClassRenamer $classRenamer)
{
$this->classRenamer = $classRenamer;
$this->changeConfiguration = $changeConfiguration;
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
}
public function getRuleDefinition(): RuleDefinition
@ -117,7 +117,7 @@ CODE_SAMPLE
{
$this->oldToNewClasses = $configuration[self::OLD_TO_NEW_CLASSES] ?? [];
if ($this->oldToNewClasses !== []) {
$this->changeConfiguration->setOldToNewClasses($this->oldToNewClasses);
$this->renamedClassesDataCollector->setOldToNewClasses($this->oldToNewClasses);
}
}
}

View File

@ -36,24 +36,28 @@ final class RenameStaticMethodRector extends AbstractRector implements Configura
public function getRuleDefinition(): RuleDefinition
{
$renameClassConfiguration = [
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', 'AnotherExampleClass', 'newStaticMethod'),
],
];
$renameMethodConfiguration = [
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', self::SOME_CLASS, 'newStaticMethod'),
],
];
return new RuleDefinition('Turns method names to new ones.', [
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'AnotherExampleClass::newStaticMethod();',
[
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', 'AnotherExampleClass', 'newStaticMethod'),
],
]
$renameClassConfiguration
),
new ConfiguredCodeSample(
'SomeClass::oldStaticMethod();',
'SomeClass::newStaticMethod();',
[
self::OLD_TO_NEW_METHODS_BY_CLASSES => [
new RenameStaticMethod(self::SOME_CLASS, 'oldMethod', self::SOME_CLASS, 'newStaticMethod'),
],
]
$renameMethodConfiguration
),
]);
}

View File

@ -9,7 +9,6 @@ use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
@ -175,15 +174,14 @@ CODE_SAMPLE
}
if (count($types) > 1) {
$assertStatements[] = new Expression(new StaticCall(new Name('\Webmozart\Assert\Assert'), 'isAnyOf', [
new Arg(new Variable($key)),
new Arg(new Array_($types)),
]));
$args = [new Arg(new Variable($key)), new Arg(new Array_($types))];
$staticCall = $this->createStaticCall('Webmozart\Assert\Assert', 'isAnyOf', $args);
$assertStatements[] = new Expression($staticCall);
} else {
$assertStatements[] = new Expression(new StaticCall(new Name('\Webmozart\Assert\Assert'), 'isAOf', [
new Arg(new Variable($key)),
new Arg(new ConstFetch(new Name($toBeProcessedType[0]))),
]));
$args = [new Arg(new Variable($key)), new Arg(new ConstFetch(new Name($toBeProcessedType[0])))];
$staticCall = $this->createStaticCall('Webmozart\Assert\Assert', 'isAOf', $args);
$assertStatements[] = new Expression($staticCall);
}
}

View File

@ -64,8 +64,8 @@ final class ServiceMapProvider
private function createServiceMapFromXml(string $fileContents): ServiceMap
{
// "@" intentionally
$xml = @simplexml_load_string($fileContents);
if (! $xml) {
throw new XmlContainerNotExistsException(sprintf(
'Container %s cannot be parsed', $this->getSymfonyContainerXmlPath()

View File

@ -20,10 +20,7 @@ final class ContainerGetToConstructorInjectionRectorTest extends AbstractRectorT
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(
Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER,
__DIR__ . '/xml/services.xml'
);
$this->setParameter(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER, __DIR__ . '/xml/services.xml');
$this->doTestFileInfo($fileInfo);
}

View File

@ -33,6 +33,17 @@ final class PropertyToMethodRector extends AbstractRector implements Configurabl
public function getRuleDefinition(): RuleDefinition
{
$firstConfiguration = [
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyToMethod('SomeObject', 'property', 'getProperty', 'setProperty'),
],
];
$secondConfiguration = [
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyToMethod('SomeObject', 'property', 'getConfig', null, ['someArg']),
],
];
return new RuleDefinition('Replaces properties assign calls be defined methods.', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
@ -45,11 +56,7 @@ $result = $object->getProperty();
$object->setProperty($value);
CODE_SAMPLE
,
[
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyToMethod('SomeObject', 'property', 'getProperty', 'setProperty'),
],
]
$firstConfiguration
),
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
@ -60,11 +67,7 @@ CODE_SAMPLE
$result = $object->getProperty('someArg');
CODE_SAMPLE
,
[
self::PROPERTIES_TO_METHOD_CALLS => [
new PropertyToMethod('SomeObject', 'property', 'getConfig', null, ['someArg']),
],
]
$secondConfiguration
),
]);
}

View File

@ -49,6 +49,10 @@ final class AddParamTypeDeclarationRector extends AbstractRector implements Conf
public function getRuleDefinition(): RuleDefinition
{
$configuration = [
self::PARAMETER_TYPEHINTS => [new AddParamTypeDeclaration('SomeClass', 'process', 0, new StringType())],
];
return new RuleDefinition('Add param types where needed', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
@ -68,9 +72,8 @@ class SomeClass
}
}
CODE_SAMPLE
, [
self::PARAMETER_TYPEHINTS => [new AddParamTypeDeclaration('SomeClass', 'process', 0, new StringType())],
]),
,
$configuration),
]);
}

View File

@ -44,6 +44,11 @@ final class AddReturnTypeDeclarationRector extends AbstractRector implements Con
public function getRuleDefinition(): RuleDefinition
{
$arrayType = new ArrayType(new MixedType(), new MixedType());
$configuration = [
self::METHOD_RETURN_TYPES => [new AddReturnTypeDeclaration('SomeClass', 'getData', $arrayType)],
];
return new RuleDefinition('Changes defined return typehint of method and class.', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
@ -64,14 +69,7 @@ class SomeClass
}
CODE_SAMPLE
,
[
self::METHOD_RETURN_TYPES => [
new AddReturnTypeDeclaration('SomeClass', 'getData', new ArrayType(
new MixedType(),
new MixedType()
)),
],
]
$configuration
),
]);
}

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Rector\Core\Configuration;
final class ChangeConfiguration
final class RenamedClassesDataCollector
{
/**
* @var array<string, string>

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Rector\Core\NonPhpFile;
use Rector\Core\Configuration\ChangeConfiguration;
use Rector\Core\Configuration\Configuration;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\PSR4\Collector\RenamedClassesCollector;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\SmartFileSystem\SmartFileInfo;
@ -22,9 +22,9 @@ final class NonPhpFileProcessor
private $configuration;
/**
* @var ChangeConfiguration
* @var RenamedClassesDataCollector
*/
private $changeConfiguration;
private $renamedClassesDataCollector;
/**
* @var SymfonyStyle
@ -47,7 +47,7 @@ final class NonPhpFileProcessor
private $nonPhpFileClassRenamer;
public function __construct(
ChangeConfiguration $changeConfiguration,
RenamedClassesDataCollector $renamedClassesDataCollector,
Configuration $configuration,
RenamedClassesCollector $renamedClassesCollector,
SmartFileSystem $smartFileSystem,
@ -55,7 +55,7 @@ final class NonPhpFileProcessor
NonPhpFileClassRenamer $nonPhpFileClassRenamer
) {
$this->configuration = $configuration;
$this->changeConfiguration = $changeConfiguration;
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
$this->symfonyStyle = $symfonyStyle;
$this->renamedClassesCollector = $renamedClassesCollector;
$this->smartFileSystem = $smartFileSystem;
@ -77,7 +77,7 @@ final class NonPhpFileProcessor
$oldContents = $smartFileInfo->getContents();
$classRenames = array_merge(
$this->changeConfiguration->getOldToNewClasses(),
$this->renamedClassesDataCollector->getOldToNewClasses(),
$this->renamedClassesCollector->getOldToNewClasses()
);