Various little improvement (#2297)

This commit is contained in:
Tomas Votruba 2022-05-12 08:07:50 +02:00 committed by GitHub
parent cd2a644e0c
commit 39e552c4c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 41 additions and 48 deletions

View File

@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2|^8.0",
"phpstan/phpstan": "^1.6"
"phpstan/phpstan": "^1.6.8"
},
"autoload": {
"files": [

View File

@ -22,7 +22,7 @@
"nikic/php-parser": "^4.13.2",
"ondram/ci-detector": "^4.1",
"phpstan/phpdoc-parser": "^1.4.4",
"phpstan/phpstan": "^1.6",
"phpstan/phpstan": "^1.6.8",
"phpstan/phpstan-phpunit": "^1.0",
"psr/log": "^2.0",
"react/child-process": "^0.6.4",

View File

@ -191,8 +191,6 @@ parameters:
paths:
# on PhpParser Nodes
- packages/NodeNameResolver/NodeNameResolver.php
- packages/NodeNameResolver/NodeNameResolver/ClassNameResolver.php
- packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
# known types
- '#Call to an undefined method PHPStan\\Type\\ConstantType\:\:getValue\(\)#'
@ -462,8 +460,7 @@ parameters:
- rules/Composer/Application/FileProcessor/ComposerFileProcessor.php
- src/Contract/Processor/FileProcessorInterface.php
- packages/Parallel/Application/ParallelFileProcessor.php
- '#Call to function property_exists\(\) with PhpParser\\Node\\Stmt\\ClassLike and (.*?) will always evaluate to true#'
- rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php
# skipped on purpose, as ctor overrie
- '#Rector\\StaticTypeMapper\\ValueObject\\Type\\SimpleStaticType\:\:__construct\(\) does not call parent constructor from PHPStan\\Type\\StaticType#'
@ -499,6 +496,7 @@ parameters:
- packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php
- rules/EarlyReturn/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php
- rules/Php70/EregToPcreTransformer.php
- rules/CodeQuality/Rector/PropertyFetch/ExplicitMethodCallOverMagicGetSetRector.php
- '#Method Rector\\Core\\Application\\ApplicationFileProcessor\:\:runParallel\(\) should return array\{system_errors\: array<Rector\\Core\\ValueObject\\Error\\SystemError\>, file_diffs\: array<Rector\\Core\\ValueObject\\Reporting\\FileDiff\>\} but returns array#'
@ -630,18 +628,15 @@ parameters:
path: rules/Renaming/NodeManipulator/ClassRenamer.php
message: '#Use separate function calls with readable variable names#'
- '#Cognitive complexity for "Rector\\Core\\Rector\\AbstractRector\:\:enterNode\(\)" is \d+, keep it under 10#'
# false positive by php-parser, the args can be null
- '#Parameter \#2 \$args of class PhpParser\\Node\\Expr\\FuncCall constructor expects array<PhpParser\\Node\\Arg\|PhpParser\\Node\\VariadicPlaceholder>, array<int, PhpParser\\Node\\Arg\|null> given#'
# known type
- '#Anonymous variable in a `\$parentStmt\->\.\.\.\(\)` method call can lead to false dead methods\. Make sure the variable type is known#'
- '#Cognitive complexity for "Rector\\NodeTypeResolver\\PHPStan\\Scope\\PHPStanNodeScopeResolver\:\:processNodes\(\)" is \d+, keep it under 10#'
# depends on falsy docs
- '#Call to static method Webmozart\\Assert\\Assert\:\:allIsInstanceOf\(\) with array<PhpParser\\Node\\Stmt> and (.*?)Stmt(.*?) will always evaluate to true#'
# doc validation
-

View File

@ -4,9 +4,6 @@ namespace MyNamespace;
class AnotherMyClass
{
/**
* @return AnotherMyClass
*/
public function createSelf(): AnotherMyClass
{
return new AnotherMyClass;
@ -19,9 +16,6 @@ class AnotherMyClass
class MyNewClassWithoutNamespace
{
/**
* @return \MyNewClassWithoutNamespace
*/
public function createSelf(): \MyNewClassWithoutNamespace
{
return new \MyNewClassWithoutNamespace;

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\CodeQuality\Rector\BooleanNot;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BooleanNot;
use Rector\Core\NodeManipulator\BinaryOpManipulator;
@ -56,7 +57,7 @@ CODE_SAMPLE
/**
* @param BooleanNot $node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): ?BinaryOp
{
if (! $node->expr instanceof BooleanOr) {
return null;

View File

@ -115,17 +115,22 @@ CODE_SAMPLE
return $this->refactorPropertyFetch($node);
}
/**
* @return string[]
*/
public function resolvePossibleGetMethodNames(string $propertyName): array
{
return ['get' . ucfirst($propertyName), 'has' . ucfirst($propertyName), 'is' . ucfirst($propertyName)];
}
private function shouldSkipPropertyFetch(PropertyFetch $propertyFetch): bool
{
$parentAssign = $this->betterNodeFinder->findParentType($propertyFetch, Assign::class);
if (! $parentAssign instanceof Assign) {
$parent = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Assign) {
return false;
}
return (bool) $this->betterNodeFinder->findFirst(
$parentAssign->var,
fn (Node $subNode): bool => $subNode === $propertyFetch
);
return $parent->var === $propertyFetch;
}
private function refactorPropertyFetch(PropertyFetch $propertyFetch): MethodCall|null
@ -145,10 +150,7 @@ CODE_SAMPLE
return null;
}
$possibleGetterMethodNames = [];
$possibleGetterMethodNames[] = 'get' . ucfirst($propertyName);
$possibleGetterMethodNames[] = 'has' . ucfirst($propertyName);
$possibleGetterMethodNames[] = 'is' . ucfirst($propertyName);
$possibleGetterMethodNames = $this->resolvePossibleGetMethodNames($propertyName);
foreach ($possibleGetterMethodNames as $possibleGetterMethodName) {
if (! $callerType->hasMethod($possibleGetterMethodName)->yes()) {

View File

@ -68,7 +68,7 @@ final class NameImporter
private function shouldSkipName(Name $name): bool
{
$virtualNode = (bool) $name->getAttribute(AttributeKey::VIRTUAL_NODE, false);
$virtualNode = (bool) $name->getAttribute(AttributeKey::VIRTUAL_NODE);
if ($virtualNode) {
return true;
}

View File

@ -12,9 +12,7 @@ use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -43,28 +41,29 @@ CODE_SAMPLE
*/
public function getNodeTypes(): array
{
return [Array_::class];
return [Array_::class, Assign::class, Foreach_::class];
}
/**
* @param Array_ $node
* @param Array_|Assign|Foreach_ $node
*/
public function refactor(Node $node): ?Node
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Assign && $this->nodeComparator->areNodesEqual($node, $parentNode->var)) {
return $this->processToList($node);
}
if ($node instanceof Assign) {
if ($node->var instanceof Array_) {
$node->var = $this->processToList($node->var);
return $node;
}
if (! $parentNode instanceof Foreach_) {
return null;
}
if (! $this->nodeComparator->areNodesEqual($node, $parentNode->valueVar)) {
return null;
if ($node instanceof Foreach_ && $node->valueVar instanceof Array_) {
$node->valueVar = $this->processToList($node->valueVar);
return $node;
}
return $this->processToList($node);
return null;
}
private function processToList(Array_ $array): FuncCall
@ -74,7 +73,6 @@ CODE_SAMPLE
$args[] = $arrayItem instanceof ArrayItem ? new Arg($arrayItem->value) : null;
}
/** @var Arg[]|VariadicPlaceholder[] $args */
return new FuncCall(new Name('list'), $args);
}
}

View File

@ -44,13 +44,13 @@ final class ComplexNodeRemover
$propertyName = $this->nodeNameResolver->getName($property);
$hasSideEffect = false;
$isPartoFAnotherAssign = false;
$isPartOfAnotherAssign = false;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use (
$removeAssignSideEffect,
$propertyName,
&$hasSideEffect,
&$isPartoFAnotherAssign
&$isPartOfAnotherAssign
) {
// here should be checked all expr like stmts that can hold assign, e.f. if, foreach etc. etc.
if (! $node instanceof Expression) {
@ -68,7 +68,7 @@ final class ComplexNodeRemover
// skip double assigns
if ($assign->expr instanceof Assign) {
$isPartoFAnotherAssign = true;
$isPartOfAnotherAssign = true;
return null;
}
@ -96,7 +96,7 @@ final class ComplexNodeRemover
return;
}
if ($isPartoFAnotherAssign) {
if ($isPartOfAnotherAssign) {
return;
}

View File

@ -361,7 +361,7 @@ final class ClassRenamer
$classLike->implements = array_unique($classLike->implements);
foreach ($classLike->implements as $key => $implementName) {
$virtualNode = (bool) $implementName->getAttribute(AttributeKey::VIRTUAL_NODE, false);
$virtualNode = (bool) $implementName->getAttribute(AttributeKey::VIRTUAL_NODE);
if (! $virtualNode) {
continue;
}

View File

@ -57,7 +57,7 @@ final class RenameFunctionRector extends AbstractRector implements ConfigurableR
}
// not to refactor here
$isVirtual = (bool) $node->name->getAttribute(AttributeKey::VIRTUAL_NODE, false);
$isVirtual = (bool) $node->name->getAttribute(AttributeKey::VIRTUAL_NODE);
if ($isVirtual) {
continue;
}

View File

@ -95,6 +95,9 @@ CODE_SAMPLE
public function refactor(Node $node): ?Node
{
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
if ($oldToNewClasses === []) {
return null;
}
if (! $node instanceof Use_) {
return $this->classRenamer->renameNode($node, $oldToNewClasses);