[Downgrade] Report changed widening in another class (#6193)

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
Tomas Votruba 2021-04-22 13:29:04 +02:00 committed by GitHub
parent 507bd85c3e
commit 94df140c4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 80 additions and 22 deletions

View File

@ -18,6 +18,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
__DIR__ . '/../packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php',
__DIR__ . '/../packages/Testing/PHPUnit',
__DIR__ . '/../packages/BetterPhpDocParser/PhpDoc',
__DIR__ . '/../packages/NodeTypeResolver/NodeVisitor/FileNodeVisitor.php',
// used in PHPStan
__DIR__ . '/../packages/NodeTypeResolver/Reflection/BetterReflection/RectorBetterReflectionSourceLocatorFactory.php',

View File

@ -6,6 +6,7 @@ namespace Rector\FileSystemRector\Parser;
use PhpParser\Node;
use Rector\Core\PhpParser\Parser\Parser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Symplify\SmartFileSystem\SmartFileInfo;
@ -30,9 +31,11 @@ final class FileInfoParser
/**
* @return Node[]
*/
public function parseFileInfoToNodesAndDecorate(SmartFileInfo $fileInfo): array
public function parseFileInfoToNodesAndDecorate(SmartFileInfo $smartFileInfo): array
{
$oldStmts = $this->parser->parseFileInfo($fileInfo);
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($oldStmts, $fileInfo);
$oldStmts = $this->parser->parseFileInfo($smartFileInfo);
$file = new File($smartFileInfo, $smartFileInfo->getContents());
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $oldStmts, $smartFileInfo);
}
}

View File

@ -176,4 +176,12 @@ final class AttributeKey
* @var string
*/
public const FUNC_ARGS_TRAILING_COMMA = 'trailing_comma';
/**
* Contains current file object
* @see \Rector\Core\ValueObject\Application\File
*
* @var string
*/
public const FILE = 'file';
}

View File

@ -9,7 +9,9 @@ use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\NodeConnectingVisitor;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeCollector\NodeVisitor\NodeCollectorNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FileNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionLikeParamArgPositionNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionMethodAndClassNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\NamespaceNodeVisitor;
@ -83,7 +85,7 @@ final class NodeScopeAndMetadataDecorator
* @param Stmt[] $nodes
* @return Stmt[]
*/
public function decorateNodesFromFile(array $nodes, SmartFileInfo $smartFileInfo): array
public function decorateNodesFromFile(File $file, array $nodes, SmartFileInfo $smartFileInfo): array
{
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new NameResolver(null, [
@ -114,6 +116,9 @@ final class NodeScopeAndMetadataDecorator
$nodeTraverser->addVisitor($this->namespaceNodeVisitor);
$nodeTraverser->addVisitor($this->functionLikeParamArgPositionNodeVisitor);
$fileNodeVisitor = new FileNodeVisitor($file);
$nodeTraverser->addVisitor($fileNodeVisitor);
$nodes = $nodeTraverser->traverse($nodes);
// this split is needed, so nodes have names, classes and namespaces

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Rector\NodeTypeResolver\NodeVisitor;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* Useful for modification of class outside current node tree
*/
final class FileNodeVisitor extends NodeVisitorAbstract
{
/**
* @var File
*/
private $file;
public function __construct(File $file)
{
$this->file = $file;
}
/**
* @return Node
*/
public function enterNode(Node $node): ?Node
{
$node->setAttribute(AttributeKey::FILE, $this->file);
return $node;
}
}

View File

@ -15,7 +15,7 @@ use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Unset_;
use Rector\Core\Exception\Node\MissingParentNodeException;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\AssignManipulator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -65,7 +65,7 @@ final class ReadWritePropertyAnalyzer
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
throw new MissingParentNodeException();
throw new ShouldNotHappenException();
}
$parent = $this->unwrapPostPreIncDec($parent);
@ -89,7 +89,7 @@ final class ReadWritePropertyAnalyzer
if ($node instanceof PreInc || $node instanceof PreDec || $node instanceof PostInc || $node instanceof PostDec) {
$node = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $node instanceof Node) {
throw new MissingParentNodeException();
throw new ShouldNotHappenException();
}
}
@ -105,7 +105,7 @@ final class ReadWritePropertyAnalyzer
{
$parentParent = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentParent instanceof Node) {
throw new MissingParentNodeException();
throw new ShouldNotHappenException();
}
if (! $this->assignManipulator->isLeftPartOfAssign($arrayDimFetch)) {

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use Rector\Core\Configuration\Option;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Parser\Parser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SmartFileSystem\SmartFileInfo;
@ -58,7 +59,9 @@ final class TestingParser
$this->parameterProvider->changeParameter(Option::SOURCE, [$file]);
$nodes = $this->parser->parseFileInfo($smartFileInfo);
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($nodes, $smartFileInfo);
$file = new File($smartFileInfo, $smartFileInfo->getContents());
return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $nodes, $smartFileInfo);
}
/**

View File

@ -13,7 +13,9 @@ use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\Application\File;
use Rector\DowngradePhp72\NodeAnalyzer\ClassLikeWithTraitsClassMethodResolver;
use Rector\DowngradePhp72\NodeAnalyzer\NativeTypeClassTreeResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
@ -186,6 +188,13 @@ CODE_SAMPLE
// Remove the type
$param->type = null;
// file from another file
$file = $param->getAttribute(AttributeKey::FILE);
if ($file instanceof File) {
$rectorWithLineChange = new RectorWithLineChange($this, $param->getLine());
$file->addRectorClassWithLine($rectorWithLineChange);
}
}
private function removeParamTypeFromMethodForChildren(

View File

@ -59,7 +59,7 @@ final class FileProcessor
$oldStmts = $this->parser->parseFileInfo($smartFileInfo);
$oldTokens = $this->lexer->getTokens();
$newStmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($oldStmts, $smartFileInfo);
$newStmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $oldStmts, $smartFileInfo);
$file->hydrateStmtsAndTokens($newStmts, $oldStmts, $oldTokens);
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Exception\Node;
use Exception;
final class MissingParentNodeException extends Exception
{
}

View File

@ -9,6 +9,7 @@ use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeFinder;
use PhpParser\Parser;
use PHPStan\Reflection\MethodReflection;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;
@ -62,7 +63,11 @@ class FunctionLikeReflectionParser
}
$nodes = (array) $this->parser->parse($fileContent);
$nodes = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($nodes, new SmartFileInfo($fileName));
$smartFileInfo = new SmartFileInfo($fileName);
$file = new File($smartFileInfo, $smartFileInfo->getContents());
$nodes = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $nodes, $smartFileInfo);
$class = $this->nodeFinder->findFirstInstanceOf($nodes, Class_::class);
if (! $class instanceof Class_) {