Skip to content

Commit

Permalink
[FileProcessor] Run untill the file is fixed completelly (#432)
Browse files Browse the repository at this point in the history
* try file doule run

* improve test fixtures by removed old class names

* remove ValueObjectWrapArgRector, not very useful

* [CodeQualityStrict] Remove VarInlineAnnotationToAssertRector, way too strict and unreliable

* [CodeQualityStrict] Remove ParamTypeToAssertTypeRector, way too strict and better union types

* improve DowngradeStripTagsCallWithArrayRector on downgrade

* correct NormalizeNamespaceByPSR4ComposerAutoloadRector

* decouple UndefinedVariableResolver

* work in rogres on assign array

* [PHP 7.1] Improve AssignArrayToStringRector to work with assign node directly
  • Loading branch information
TomasVotruba committed Jul 14, 2021
1 parent 4f2d2d1 commit 294bea2
Show file tree
Hide file tree
Showing 99 changed files with 865 additions and 1,843 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"rector/extension-installer": "^0.10.2",
"rector/rector-cakephp": "^0.11.3",
"rector/rector-doctrine": "^0.11.9",
"rector/rector-laravel": "^0.11.2",
"rector/rector-nette": "^0.11.14",
"rector/rector-phpunit": "^0.11.3",
"rector/rector-laravel": "dev-main",
"rector/rector-nette": "dev-main",
"rector/rector-phpunit": "dev-main",
"rector/rector-symfony": "^0.11.8",
"sebastian/diff": "^4.0.4",
"ssch/typo3-rector": "dev-main#f5fd76a",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<?php
// some php
?>
<h2><?php echo $hello2; ?></h2>
<?php
// some more php
?>
Expand All @@ -17,11 +16,5 @@
// some php
?>
<?php
/**
* @var string $hello2
*/
?>
<h2><?php echo $hello2; ?></h2>
<?php
// some more php
?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Stmt\InlineHTML;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\MixedType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -23,19 +24,24 @@ public function getNodeTypes(): array
}

/**
* @param InlineHTML $inlineHtml
* @param InlineHTML $node
*/
public function refactor(Node $inlineHtml): ?Node
public function refactor(Node $node): ?Node
{
if (strpos($inlineHtml->value, '<h1>') !== false) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($inlineHtml);
$phpDocInfo->addTagValueNode(new VarTagValueNode(new IdentifierTypeNode('string'), '$hello1', ''));
if (! str_contains($node->value, '<h1>')) {
return null;
}
if (strpos($inlineHtml->value, '<h2>') !== false) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($inlineHtml);
$phpDocInfo->addTagValueNode(new VarTagValueNode(new IdentifierTypeNode('string'), '$hello2', ''));

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

$currentVarTagValueNode = $phpDocInfo->getVarTagValueNode();
if ($currentVarTagValueNode !== null) {
return null;
}

return null;
$varTagValueNode = new VarTagValueNode(new IdentifierTypeNode('string'), '$hello1', '');
$phpDocInfo->addTagValueNode($varTagValueNode);

return $node;
}
}
6 changes: 5 additions & 1 deletion packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@ public function isName(Node | array $node, string $name): bool
return false;
}

public function getName(Node $node): ?string
public function getName(Node | string $node): ?string
{
if (is_string($node)) {
return $node;
}

if ($node instanceof MethodCall || $node instanceof StaticCall) {
if ($this->isCallOrIdentifier($node->name)) {
return null;
Expand Down
7 changes: 4 additions & 3 deletions packages/NodeNestingScope/ContextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ final class ContextAnalyzer

public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeTypeResolver $nodeTypeResolver
private NodeTypeResolver $nodeTypeResolver,
private ParentFinder $parentFinder
) {
}

public function isInLoop(Node $node): bool
{
$stopNodes = array_merge(self::LOOP_NODES, self::BREAK_NODES);

$firstParent = $this->betterNodeFinder->findParentTypes($node, $stopNodes);
$firstParent = $this->parentFinder->findByTypes($node, $stopNodes);
if (! $firstParent instanceof Node) {
return false;
}
Expand All @@ -67,7 +68,7 @@ public function isInIf(Node $node): bool
{
$breakNodes = array_merge([If_::class], self::BREAK_NODES);

$previousNode = $this->betterNodeFinder->findParentTypes($node, $breakNodes);
$previousNode = $this->parentFinder->findByTypes($node, $breakNodes);

if (! $previousNode instanceof Node) {
return false;
Expand Down
41 changes: 41 additions & 0 deletions packages/NodeNestingScope/ParentFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Rector\NodeNestingScope;

use PhpParser\Node;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Webmozart\Assert\Assert;

final class ParentFinder
{
/**
* @template T of \PhpParser\Node
* @param array<class-string<T>> $types
* @return T|null
*/
public function findByTypes(Node $node, array $types): ?Node
{
Assert::allIsAOf($types, Node::class);

$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
return null;
}

do {
foreach ($types as $type) {
if (is_a($parent, $type, true)) {
return $parent;
}
}

if ($parent === null) {
return null;
}
} while ($parent = $parent->getAttribute(AttributeKey::PARENT_NODE));

return null;
}
}
5 changes: 2 additions & 3 deletions packages/NodeNestingScope/ParentScopeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;

final class ParentScopeFinder
{
public function __construct(
private BetterNodeFinder $betterNodeFinder
private ParentFinder $parentFinder
) {
}

public function find(Node $node): ClassMethod | Function_ | Class_ | Namespace_ | Closure | null
{
return $this->betterNodeFinder->findParentTypes($node, [
return $this->parentFinder->findByTypes($node, [
Closure::class,
Function_::class,
ClassMethod::class,
Expand Down
9 changes: 5 additions & 4 deletions packages/NodeNestingScope/ScopeNestingComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ final class ScopeNestingComparator

public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeComparator $nodeComparator
private NodeComparator $nodeComparator,
private ParentFinder $parentFinder
) {
}

public function areReturnScopeNested(Return_ $return, Node $secondNodeScopeNode): bool
{
$firstNodeScopeNode = $this->betterNodeFinder->findParentTypes(
$firstNodeScopeNode = $this->parentFinder->findByTypes(
$return,
ControlStructure::RETURN_ISOLATING_SCOPE_NODE_TYPES
);
Expand All @@ -47,7 +48,7 @@ public function areScopeNestingEqual(Node $firstNode, Node $secondNode): bool

public function isNodeConditionallyScoped(Expr $expr): bool
{
$foundParent = $this->betterNodeFinder->findParentTypes(
$foundParent = $this->parentFinder->findByTypes(
$expr,
ControlStructure::CONDITIONAL_NODE_SCOPE_TYPES + [FunctionLike::class]
);
Expand Down Expand Up @@ -103,6 +104,6 @@ public function isInBothIfElseBranch(Node $foundParentNode, Expr $seekedExpr): b

private function findParentControlStructure(Node $node): ?Node
{
return $this->betterNodeFinder->findParentTypes($node, ControlStructure::BREAKING_SCOPE_NODE_TYPES);
return $this->parentFinder->findByTypes($node, ControlStructure::BREAKING_SCOPE_NODE_TYPES);
}
}
10 changes: 10 additions & 0 deletions packages/NodeRemoval/NodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function removeParam(ClassMethod $classMethod, int | Param $keyOrParam):
throw new ShouldNotHappenException();
}

// already removed
if (! isset($classMethod->params[$key])) {
return;
}

// notify about remove node
$this->rectorChangeCollector->notifyNodeFileInfo($classMethod->params[$key]);

Expand All @@ -95,6 +100,11 @@ public function removeArg(FuncCall | MethodCall | StaticCall $node, int $key): v
throw new ShouldNotHappenException();
}

// already removed
if (! isset($node->args[$key])) {
return;
}

// notify about remove node
$this->rectorChangeCollector->notifyNodeFileInfo($node->args[$key]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -43,6 +44,11 @@ public function resolve(Node $node): Type
}

$fullyQualifiedName = $this->resolveFullyQualifiedName($node);

if ($node->toString() === 'array') {
return new ArrayType(new MixedType(), new MixedType());
}

return new ObjectType($fullyQualifiedName);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,18 @@ private function processNodeName(Name $name, File $file): ?Node
return $name;
}

$currentUses = $this->betterNodeFinder->findInstanceOf($file->getOldStmts(), Use_::class);
// @todo test if old stmts or new stmts! or both? :)
/** @var Use_[] $currentUses */
$currentUses = $this->betterNodeFinder->findInstanceOf($file->getNewStmts(), Use_::class);

if (substr_count($name->toCodeString(), '\\') <= 1) {
return $this->nameImporter->importName($name, $currentUses);
}

if (! $this->classNameImportSkipper->isFoundInUse($name, $currentUses)) {
return $this->nameImporter->importName($name, $currentUses);
}

if ($this->classNameImportSkipper->isAlreadyImported($name, $currentUses)) {
return $this->nameImporter->importName($name, $currentUses);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/ReadWrite/NodeAnalyzer/ReadWritePropertyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\AssignManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNestingScope\ParentFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\ReadWrite\Guard\VariableToConstantGuard;

Expand All @@ -27,7 +28,8 @@ public function __construct(
private VariableToConstantGuard $variableToConstantGuard,
private AssignManipulator $assignManipulator,
private ReadExprAnalyzer $readExprAnalyzer,
private BetterNodeFinder $betterNodeFinder
private BetterNodeFinder $betterNodeFinder,
private ParentFinder $parentFinder
) {
}

Expand Down Expand Up @@ -68,7 +70,7 @@ private function unwrapPostPreIncDec(Node $node): Node

private function isNotInsideIssetUnset(ArrayDimFetch $arrayDimFetch): bool
{
return ! (bool) $this->betterNodeFinder->findParentTypes($arrayDimFetch, [Isset_::class, Unset_::class]);
return ! (bool) $this->parentFinder->findByTypes($arrayDimFetch, [Isset_::class, Unset_::class]);
}

private function isArrayDimFetchRead(ArrayDimFetch $arrayDimFetch): bool
Expand Down
9 changes: 6 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ parameters:
- '#Property PhpParser\\Node\\Scalar\\DNumber\:\:\$value \(float\|int\) does not accept string#'


- '#Unable to resolve the template type T in call to method Rector\\Core\\PhpParser\\Node\\BetterNodeFinder\:\:findParentTypes\(\)#'
- '#Unable to resolve the template type T in call to method Rector\\Core\\PhpParser\\Node\\ParentFinder\:\:(.*?)#'
- '#Unable to resolve the template type T in call to method Rector\\NodeNestingScope\\ParentFinder\:\:findByTypes\(\)#'

# mimics original doctrine/annotations parser, improve later when finished
-
Expand Down Expand Up @@ -542,8 +543,10 @@ parameters:
# should be allowed, as continue check - fix in symplify
-
message: '#foreach\(\.\.\.\), while\(\), for\(\) or if\(\.\.\.\) cannot contains a complex expression\. Extract it to a new variable assign on line before#'
path: src/Application/ApplicationFileProcessor.php

paths:
- src/Application/ApplicationFileProcessor.php
- packages/NodeNestingScope/ParentFinder.php

- '#Callable callable\(PHPStan\\Type\\Type\)\: PHPStan\\Type\\Type invoked with 2 parameters, 1 required#'
- '#Cognitive complexity for "Rector\\Php80\\Rector\\Class_\\AnnotationToAttributeRector\:\:processDoctrineAnnotationClasses\(\)" is 11, keep it under 9#'
- '#Method Rector\\NodeNestingScope\\ParentFinder\:\:findByTypes\(\) should return T of PhpParser\\Node\|null but returns class\-string<T of PhpParser\\Node\>\|T of PhpParser\\Node#'

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 294bea2

Please sign in to comment.