Skip to content

Commit

Permalink
Updated Rector to commit efd09bbb400b2382be39effc206551e6964fa5ea
Browse files Browse the repository at this point in the history
rectorphp/rector-src@efd09bb Compatibility with BetterReflection 6.x on ClassFromEnumFactory (#3150)
  • Loading branch information
TomasVotruba committed Dec 4, 2022
1 parent 642698e commit 83accd4
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ public function enterNode(Node $node) : ?Node
if ($virtualNode === \true) {
return null;
}
$identifier = clone $node;
$identifier->name = $this->resolveNamespacedName($identifier, $phpParserNode, $node->name);
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($identifier, $phpParserNode);
$node->name = $this->resolveNamespacedName($node, $phpParserNode, $node->name);
$staticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType($node, $phpParserNode);
// make sure to compare FQNs
$objectType = $this->expandShortenedObjectType($staticType);
foreach ($this->oldToNewTypes as $oldToNewType) {
Expand Down Expand Up @@ -130,18 +129,21 @@ private function resolveNamespacedName(IdentifierTypeNode $identifierTypeNode, P
return $this->resolveNamefromUse($uses, $name);
}
$originalNode = $namespace->getAttribute(AttributeKey::ORIGINAL_NODE);
if (!$originalNode instanceof Namespace_) {
return $name;
}
$namespaceName = (string) $this->nodeNameResolver->getName($namespace);
if ($originalNode instanceof Namespace_ && !$this->nodeNameResolver->isName($originalNode, $namespaceName)) {
if (!$this->nodeNameResolver->isName($originalNode, $namespaceName)) {
return $name;
}
if ($uses === []) {
return $namespaceName . '\\' . $name;
return '\\' . \ltrim($namespaceName . '\\' . $name, '\\');
}
$nameFromUse = $this->resolveNamefromUse($uses, $name);
if ($nameFromUse !== $name) {
return $nameFromUse;
}
return $namespaceName . '\\' . $nameFromUse;
return '\\' . \ltrim($namespaceName . '\\' . $nameFromUse, '\\');
}
/**
* @param Use_[]|GroupUse[] $uses
Expand Down
5 changes: 5 additions & 0 deletions packages/Parallel/WorkerRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public function run(Encoder $encoder, Decoder $decoder, Configuration $configura
}
$currentErrorsAndFileDiffs = $this->phpFileProcessor->process($file, $configuration);
$errorAndFileDiffs = $this->arrayParametersMerger->merge($errorAndFileDiffs, $currentErrorsAndFileDiffs);
// warn about deprecated @noRector annotation
if (\substr_compare($file->getFilePath(), 'WorkerRunner.php', -\strlen('WorkerRunner.php')) !== 0 && (\strpos($file->getFileContent(), ' @noRector ') !== \false || \strpos($file->getFileContent(), ' @norector ') !== \false)) {
$systemErrors[] = new SystemError('The @noRector annotation was deprecated and removed due to hiding fixed errors. Use more precise $rectorConfig->skip() method in the rector.php config.', $file->getFilePath());
continue;
}
} catch (Throwable $throwable) {
++$systemErrorsCount;
$systemErrors = $this->collectSystemErrors($systemErrors, $throwable, $filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
/**
* @noRector final on purpose, so it can be extended by 3rd party
* @api
*/
class SimplePhpDocNode extends PhpDocNode
final class SimplePhpDocNode extends PhpDocNode
{
public function getParam(string $desiredParamName) : ?ParamTagValueNode
{
Expand Down
15 changes: 11 additions & 4 deletions rules/CodeQuality/NodeFactory/ArrayFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -36,22 +37,28 @@ public function createSimpleFuncCallAssign(Foreach_ $foreach, string $funcName,
$arrayFilterFuncCall = new FuncCall(new Name('array_filter'), $args);
return new Assign($arrayDimFetch->var, $arrayFilterFuncCall);
}
public function createWithClosure(ArrayDimFetch $arrayDimFetch, Variable $valueVariable, Expr $condExpr, Foreach_ $foreach) : Assign
/**
* @param Variable[] $uses
*/
public function createWithClosure(ArrayDimFetch $arrayDimFetch, Variable $valueVariable, Expr $condExpr, Foreach_ $foreach, array $uses) : Assign
{
$filterFunction = $this->createClosure($valueVariable, $condExpr);
$filterFunction = $this->createClosure($valueVariable, $condExpr, $uses);
$args = [new Arg($foreach->expr), new Arg($filterFunction)];
$arrayFilterFuncCall = new FuncCall(new Name('array_filter'), $args);
return new Assign($arrayDimFetch->var, $arrayFilterFuncCall);
}
/**
* @param Variable[] $uses
* @return \PhpParser\Node\Expr\ArrowFunction|\PhpParser\Node\Expr\Closure
*/
private function createClosure(Variable $valueVariable, Expr $condExpr)
private function createClosure(Variable $valueVariable, Expr $condExpr, array $uses)
{
$params = [new Param($valueVariable)];
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARROW_FUNCTION)) {
return new ArrowFunction(['params' => $params, 'expr' => $condExpr]);
}
return new Closure(['params' => $params, 'stmts' => [new Return_($condExpr)]]);
return new Closure(['params' => $params, 'stmts' => [new Return_($condExpr)], 'uses' => \array_map(static function (Variable $variable) : ClosureUse {
return new ClosureUse($variable);
}, $uses)]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\UnionType;
use Rector\CodeQuality\NodeFactory\ArrayFilterFactory;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer;
use Rector\ReadWrite\NodeAnalyzer\ReadExprAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -41,11 +43,17 @@ final class SimplifyForeachToArrayFilterRector extends AbstractRector
* @var \Rector\ReadWrite\NodeAnalyzer\ReadExprAnalyzer
*/
private $readExprAnalyzer;
public function __construct(ArrayFilterFactory $arrayFilterFactory, ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer, ReadExprAnalyzer $readExprAnalyzer)
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(ArrayFilterFactory $arrayFilterFactory, ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer, ReadExprAnalyzer $readExprAnalyzer, PhpVersionProvider $phpVersionProvider)
{
$this->arrayFilterFactory = $arrayFilterFactory;
$this->exprUsedInNodeAnalyzer = $exprUsedInNodeAnalyzer;
$this->readExprAnalyzer = $readExprAnalyzer;
$this->phpVersionProvider = $phpVersionProvider;
}
public function getRuleDefinition() : RuleDefinition
{
Expand Down Expand Up @@ -211,12 +219,32 @@ private function refactorAssign(Expression $expression, Variable $variable, Fore
return null;
}
// the keyvar must be variable in array dim fetch
if (!$foreach->keyVar instanceof Expr) {
$keyVar = $foreach->keyVar;
if (!$keyVar instanceof Variable) {
return null;
}
if (!$this->nodeComparator->areNodesEqual($arrayDimFetch->dim, $foreach->keyVar)) {
return null;
}
return $this->arrayFilterFactory->createWithClosure($assign->var, $variable, $condExpr, $foreach);
return $this->arrayFilterFactory->createWithClosure($assign->var, $variable, $condExpr, $foreach, $this->getUsedVariablesForClosure($keyVar, $variable, $condExpr));
}
/**
* @return Variable[]
*/
private function getUsedVariablesForClosure(Variable $keyVar, Variable $valueVar, Expr $condExpr) : array
{
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARROW_FUNCTION)) {
return [];
}
/** @var Variable[] $filteredVariables */
$filteredVariables = $this->betterNodeFinder->find($condExpr, function (Node $node) use($keyVar, $valueVar) : bool {
return $node instanceof Variable && !$this->nodeComparator->areNodesEqual($keyVar, $node) && !$this->nodeComparator->areNodesEqual($valueVar, $node) && !$this->nodeNameResolver->isName($node, 'this');
});
$uniqueVariables = [];
foreach ($filteredVariables as $filteredVariable) {
$variableName = $this->nodeNameResolver->getName($filteredVariable);
$uniqueVariables[$variableName] = $filteredVariable;
}
return \array_values($uniqueVariables);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,27 @@ public function getNodeTypes() : array
*/
public function refactor(Node $node)
{
$processNode = clone $node;
if ($this->inlineHTMLAnalyzer->hasInlineHTML($processNode)) {
if ($this->inlineHTMLAnalyzer->hasInlineHTML($node)) {
return null;
}
$expectedNamespace = $this->psr4AutoloadNamespaceMatcher->getExpectedNamespace($this->file, $processNode);
$expectedNamespace = $this->psr4AutoloadNamespaceMatcher->getExpectedNamespace($this->file, $node);
if ($expectedNamespace === null) {
return null;
}
// is namespace and already correctly named?
if ($processNode instanceof Namespace_ && $this->nodeNameResolver->isCaseSensitiveName($processNode, $expectedNamespace)) {
if ($node instanceof Namespace_ && $this->nodeNameResolver->isCaseSensitiveName($node, $expectedNamespace)) {
return null;
}
if ($processNode instanceof Namespace_ && $this->hasNamespaceInPreviousNamespace($processNode)) {
if ($node instanceof Namespace_ && $this->hasNamespaceInPreviousNamespace($node)) {
return null;
}
// to put declare_strict types on correct place
if ($processNode instanceof FileWithoutNamespace) {
return $this->refactorFileWithoutNamespace($processNode, $expectedNamespace);
if ($node instanceof FileWithoutNamespace) {
return $this->refactorFileWithoutNamespace($node, $expectedNamespace);
}
$processNode->name = new Name($expectedNamespace);
$this->fullyQualifyStmtsAnalyzer->process($processNode->stmts);
return $processNode;
$node->name = new Name($expectedNamespace);
$this->fullyQualifyStmtsAnalyzer->process($node->stmts);
return $node;
}
private function hasNamespaceInPreviousNamespace(Namespace_ $namespace) : bool
{
Expand Down
4 changes: 2 additions & 2 deletions rules/Php81/NodeFactory/ClassFromEnumFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function createFromEnum(Enum_ $enum) : Class_
continue;
}
$constValue = $this->createConstValue($stmt);
$classStmts[] = new ClassConst([new Const_($stmt->name, $constValue)], Visibility::PUBLIC);
$classStmts[] = new ClassConst([new Const_($stmt->name, $constValue)], Visibility::PUBLIC, ['startLine' => $stmt->getStartLine(), 'endLine' => $stmt->getEndLine()]);
}
$class = new Class_($shortClassName, ['stmts' => $classStmts]);
$class = new Class_($shortClassName, ['stmts' => $classStmts], ['startLine' => $enum->getStartLine(), 'endLine' => $enum->getEndLine()]);
$class->namespacedName = $enum->namespacedName;
return $class;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'e8a252fe262cac38eb74b9584f78e2daee006ffa';
public const PACKAGE_VERSION = 'efd09bbb400b2382be39effc206551e6964fa5ea';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2022-12-03 12:29:17';
public const RELEASE_DATE = '2022-12-04 13:10:11';
/**
* @var int
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

declare (strict_types=1);
namespace RectorPrefix202212\Symplify\AutowireArrayParameter\DependencyInjection\CompilerPass;
namespace Rector\Core\DependencyInjection\CompilerPass;

use RectorPrefix202212\Nette\Utils\Strings;
use Rector\Core\DependencyInjection\DefinitionFinder;
use Rector\Core\DependencyInjection\DocBlock\ParamTypeDocBlockResolver;
use Rector\Core\DependencyInjection\Skipper\ParameterSkipper;
use Rector\Core\DependencyInjection\TypeResolver\ParameterTypeResolver;
use ReflectionClass;
use ReflectionMethod;
use RectorPrefix202212\Symfony\Component\Config\Loader\LoaderInterface;
use RectorPrefix202212\Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use RectorPrefix202212\Symfony\Component\DependencyInjection\ContainerBuilder;
use RectorPrefix202212\Symfony\Component\DependencyInjection\Definition;
use RectorPrefix202212\Symfony\Component\DependencyInjection\Reference;
use RectorPrefix202212\Symplify\AutowireArrayParameter\DependencyInjection\DefinitionFinder;
use RectorPrefix202212\Symplify\AutowireArrayParameter\DocBlock\ParamTypeDocBlockResolver;
use RectorPrefix202212\Symplify\AutowireArrayParameter\Skipper\ParameterSkipper;
use RectorPrefix202212\Symplify\AutowireArrayParameter\TypeResolver\ParameterTypeResolver;
/**
* @inspiration https://github.com/nette/di/pull/178
* @see \Symplify\AutowireArrayParameter\Tests\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPassTest
*/
final class AutowireArrayParameterCompilerPass implements CompilerPassInterface
{
Expand All @@ -29,21 +29,22 @@ final class AutowireArrayParameterCompilerPass implements CompilerPassInterface
/**
* Classes that create circular dependencies
*
* @var string[]
* @noRector \Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector
* @noRector \Rector\Php55\Rector\String_\StringClassNameToClassConstantRector
* @var class-string<LoaderInterface>[]|string[]
*/
private $excludedFatalClasses = ['RectorPrefix202212\\Symfony\\Component\\Form\\FormExtensionInterface', 'RectorPrefix202212\\Symfony\\Component\\Asset\\PackageInterface', 'RectorPrefix202212\\Symfony\\Component\\Config\\Loader\\LoaderInterface', 'RectorPrefix202212\\Symfony\\Component\\VarDumper\\Dumper\\ContextProvider\\ContextProviderInterface', 'RectorPrefix202212\\EasyCorp\\Bundle\\EasyAdminBundle\\Form\\Type\\Configurator\\TypeConfiguratorInterface', 'RectorPrefix202212\\Sonata\\CoreBundle\\Model\\Adapter\\AdapterInterface', 'RectorPrefix202212\\Sonata\\Doctrine\\Adapter\\AdapterChain', 'RectorPrefix202212\\Sonata\\Twig\\Extension\\TemplateExtension', 'RectorPrefix202212\\Symfony\\Component\\HttpKernel\\KernelInterface'];
private const EXCLUDED_FATAL_CLASSES = ['Symfony\\Component\\Form\\FormExtensionInterface', 'Symfony\\Component\\Asset\\PackageInterface', 'Symfony\\Component\\Config\\Loader\\LoaderInterface', 'Symfony\\Component\\VarDumper\\Dumper\\ContextProvider\\ContextProviderInterface', 'EasyCorp\\Bundle\\EasyAdminBundle\\Form\\Type\\Configurator\\TypeConfiguratorInterface', 'Sonata\\CoreBundle\\Model\\Adapter\\AdapterInterface', 'Sonata\\Doctrine\\Adapter\\AdapterChain', 'Sonata\\Twig\\Extension\\TemplateExtension', 'Symfony\\Component\\HttpKernel\\KernelInterface'];
/**
* @var \Symplify\AutowireArrayParameter\DependencyInjection\DefinitionFinder
* @readonly
* @var \Rector\Core\DependencyInjection\DefinitionFinder
*/
private $definitionFinder;
/**
* @var \Symplify\AutowireArrayParameter\TypeResolver\ParameterTypeResolver
* @readonly
* @var \Rector\Core\DependencyInjection\TypeResolver\ParameterTypeResolver
*/
private $parameterTypeResolver;
/**
* @var \Symplify\AutowireArrayParameter\Skipper\ParameterSkipper
* @readonly
* @var \Rector\Core\DependencyInjection\Skipper\ParameterSkipper
*/
private $parameterSkipper;
/**
Expand Down Expand Up @@ -86,7 +87,7 @@ private function shouldSkipDefinition(ContainerBuilder $containerBuilder, Defini
if (Strings::match($resolvedClassName, $excludedNamespacePattern)) {
return \true;
}
if (\in_array($resolvedClassName, $this->excludedFatalClasses, \true)) {
if (\in_array($resolvedClassName, self::EXCLUDED_FATAL_CLASSES, \true)) {
return \true;
}
if ($definition->getFactory()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

declare (strict_types=1);
namespace RectorPrefix202212\Symplify\AutowireArrayParameter\DependencyInjection;
namespace Rector\Core\DependencyInjection;

use Rector\Core\DependencyInjection\Exception\DefinitionForTypeNotFoundException;
use RectorPrefix202212\Symfony\Component\DependencyInjection\ContainerBuilder;
use RectorPrefix202212\Symfony\Component\DependencyInjection\Definition;
use RectorPrefix202212\Symplify\AutowireArrayParameter\Exception\DependencyInjection\DefinitionForTypeNotFoundException;
use Throwable;
/**
* @api
* @see \Symplify\AutowireArrayParameter\Tests\DependencyInjection\DefinitionFinderTest
*/
final class DefinitionFinder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?php

declare (strict_types=1);
namespace RectorPrefix202212\Symplify\AutowireArrayParameter\DocBlock;
namespace Rector\Core\DependencyInjection\DocBlock;

use RectorPrefix202212\Nette\Utils\Strings;
/**
* @see \Symplify\AutowireArrayParameter\Tests\DocBlock\ParamTypeDocBlockResolverTest
*/
final class ParamTypeDocBlockResolver
{
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

declare (strict_types=1);
namespace RectorPrefix202212\Symplify\AutowireArrayParameter\Exception\DependencyInjection;
namespace Rector\Core\DependencyInjection\Exception;

use Exception;
final class DefinitionForTypeNotFoundException extends Exception
Expand Down
Loading

0 comments on commit 83accd4

Please sign in to comment.