Skip to content

Commit

Permalink
Various little improvement (#2297)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 12, 2022
1 parent cd2a644 commit 39e552c
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build/target-repository/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2|^8.0",
"phpstan/phpstan": "^1.6"
"phpstan/phpstan": "^1.6.8"
},
"autoload": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 4 additions & 9 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\(\)#'
Expand Down Expand Up @@ -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#'
Expand Down Expand Up @@ -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#'

Expand Down Expand Up @@ -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
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace MyNamespace;

class AnotherMyClass
{
/**
* @return AnotherMyClass
*/
public function createSelf(): AnotherMyClass
{
return new AnotherMyClass;
Expand All @@ -19,9 +16,6 @@ class AnotherMyClass

class MyNewClassWithoutNamespace
{
/**
* @return \MyNewClassWithoutNamespace
*/
public function createSelf(): \MyNewClassWithoutNamespace
{
return new \MyNewClassWithoutNamespace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function getNodeTypes(): array
/**
* @param BooleanNot $node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): ?BinaryOp
{
if (! $node->expr instanceof BooleanOr) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,22 @@ public function refactor(Node $node): ?Node
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
Expand All @@ -145,10 +150,7 @@ private function refactorPropertyFetch(PropertyFetch $propertyFetch): MethodCall
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()) {
Expand Down
2 changes: 1 addition & 1 deletion rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function importName(Name $name, File $file, array $uses): ?Name

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
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;

Expand Down Expand Up @@ -43,28 +41,29 @@ public function getRuleDefinition(): RuleDefinition
*/
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
Expand All @@ -74,7 +73,6 @@ private function processToList(Array_ $array): FuncCall
$args[] = $arrayItem instanceof ArrayItem ? new Arg($arrayItem->value) : null;
}

/** @var Arg[]|VariadicPlaceholder[] $args */
return new FuncCall(new Name('list'), $args);
}
}
8 changes: 4 additions & 4 deletions rules/Removing/NodeManipulator/ComplexNodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public function removePropertyAndUsages(
$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) {
Expand All @@ -68,7 +68,7 @@ public function removePropertyAndUsages(

// skip double assigns
if ($assign->expr instanceof Assign) {
$isPartoFAnotherAssign = true;
$isPartOfAnotherAssign = true;
return null;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ public function removePropertyAndUsages(
return;
}

if ($isPartoFAnotherAssign) {
if ($isPartOfAnotherAssign) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion rules/Renaming/NodeManipulator/ClassRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private function renameClassImplements(ClassLike $classLike, array $oldToNewClas

$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;
}
Expand Down
2 changes: 1 addition & 1 deletion rules/Renaming/Rector/FuncCall/RenameFunctionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function refactor(Node $node): ?Node
}

// not to refactor here
$isVirtual = (bool) $node->name->getAttribute(AttributeKey::VIRTUAL_NODE, false);
$isVirtual = (bool) $node->name->getAttribute(AttributeKey::VIRTUAL_NODE);
if ($isVirtual) {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions rules/Renaming/Rector/Name/RenameClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
if ($oldToNewClasses === []) {
return null;
}

if (! $node instanceof Use_) {
return $this->classRenamer->renameNode($node, $oldToNewClasses);
Expand Down

0 comments on commit 39e552c

Please sign in to comment.