mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 17:00:51 +00:00
[Downgrade] Report changed widening in another class (#6193)
Co-authored-by: kaizen-ci <info@kaizen-ci.org>
This commit is contained in:
parent
507bd85c3e
commit
94df140c4d
|
@ -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',
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
35
packages/NodeTypeResolver/NodeVisitor/FileNodeVisitor.php
Normal file
35
packages/NodeTypeResolver/NodeVisitor/FileNodeVisitor.php
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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)) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Core\Exception\Node;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class MissingParentNodeException extends Exception
|
||||
{
|
||||
}
|
|
@ -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_) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user