Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ final class FunctionMethodAndClassNodeVisitor extends NodeVisitorAbstract
/**
* @var ClassLike|null
*/
private $classNode;
private $classLike;

/**
* @var ClassMethod|null
*/
private $methodNode;
private $classMethod;

/**
* @var Function_|null
Expand All @@ -72,10 +72,10 @@ public function __construct(ClassNaming $classNaming)
*/
public function beforeTraverse(array $nodes): ?array
{
$this->classNode = null;
$this->classLike = null;
$this->className = null;
$this->methodName = null;
$this->methodNode = null;
$this->classMethod = null;
$this->functionNode = null;

return null;
Expand All @@ -99,7 +99,7 @@ public function leaveNode(Node $node)
$this->setClassNodeAndName(array_pop($this->classStack));
}
if ($node instanceof ClassMethod) {
$this->methodNode = array_pop($this->methodStack);
$this->classMethod = array_pop($this->methodStack);
$this->methodName = (string) $this->methodName;
}
return null;
Expand All @@ -108,30 +108,30 @@ public function leaveNode(Node $node)
private function processClass(Node $node): void
{
if ($node instanceof ClassLike) {
$this->classStack[] = $this->classNode;
$this->classStack[] = $this->classLike;
$this->setClassNodeAndName($node);
}

$node->setAttribute(AttributeKey::CLASS_NODE, $this->classNode);
$node->setAttribute(AttributeKey::CLASS_NODE, $this->classLike);
$node->setAttribute(AttributeKey::CLASS_NAME, $this->className);
$node->setAttribute(AttributeKey::CLASS_SHORT_NAME, $this->classShortName);

if ($this->classNode instanceof Class_) {
$this->setParentClassName($this->classNode, $node);
if ($this->classLike instanceof Class_) {
$this->setParentClassName($this->classLike, $node);
}
}

private function processMethod(Node $node): void
{
if ($node instanceof ClassMethod) {
$this->methodStack[] = $this->methodNode;
$this->methodStack[] = $this->classMethod;

$this->methodNode = $node;
$this->classMethod = $node;
$this->methodName = (string) $node->name;
}

$node->setAttribute(AttributeKey::METHOD_NAME, $this->methodName);
$node->setAttribute(AttributeKey::METHOD_NODE, $this->methodNode);
$node->setAttribute(AttributeKey::METHOD_NODE, $this->classMethod);
}

private function processFunction(Node $node): void
Expand All @@ -145,7 +145,7 @@ private function processFunction(Node $node): void

private function setClassNodeAndName(?ClassLike $classLike): void
{
$this->classNode = $classLike;
$this->classLike = $classLike;
if ($classLike === null || $classLike->name === null) {
$this->className = null;
} elseif (isset($classLike->namespacedName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class ParentAndNextNodeVisitor extends NodeVisitorAbstract
/**
* @var Node|null
*/
private $prev;
private $previousNode;

/**
* @param Node[] $nodes
Expand All @@ -30,7 +30,7 @@ final class ParentAndNextNodeVisitor extends NodeVisitorAbstract
public function afterTraverse(array $nodes): ?array
{
$this->stack = [];
$this->prev = null;
$this->previousNode = null;

return null;
}
Expand All @@ -44,11 +44,13 @@ public function enterNode(Node $node)
$node->setAttribute(AttributeKey::PARENT_NODE, $this->stack[count($this->stack) - 1]);
}

if ($this->prev &&
$this->prev->getAttribute(AttributeKey::PARENT_NODE) === $node->getAttribute(AttributeKey::PARENT_NODE)
if ($this->previousNode &&
$this->previousNode->getAttribute(AttributeKey::PARENT_NODE) === $node->getAttribute(
AttributeKey::PARENT_NODE
)
) {
$node->setAttribute(AttributeKey::PREVIOUS_NODE, $this->prev);
$this->prev->setAttribute(AttributeKey::NEXT_NODE, $node);
$node->setAttribute(AttributeKey::PREVIOUS_NODE, $this->previousNode);
$this->previousNode->setAttribute(AttributeKey::NEXT_NODE, $node);
}

$this->stack[] = $node;
Expand All @@ -61,7 +63,7 @@ public function enterNode(Node $node)
*/
public function leaveNode(Node $node)
{
$this->prev = $node;
$this->previousNode = $node;
array_pop($this->stack);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ final class StatementNodeVisitor extends NodeVisitorAbstract
/**
* @var Stmt|null
*/
private $previousStatement;
private $previousStmt;

/**
* @param Node[] $nodes
* @return Node[]|null
*/
public function beforeTraverse(array $nodes): ?array
{
$this->previousStatement = null;
$this->previousStmt = null;

return null;
}
Expand All @@ -39,9 +39,9 @@ public function enterNode(Node $node)
throw new ShouldNotHappenException('Only statement can appear at top level');
}

$node->setAttribute(AttributeKey::PREVIOUS_STATEMENT, $this->previousStatement);
$node->setAttribute(AttributeKey::PREVIOUS_STATEMENT, $this->previousStmt);
$node->setAttribute(AttributeKey::CURRENT_STATEMENT, $node);
$this->previousStatement = $node;
$this->previousStmt = $node;
}

if (isset($node->stmts)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class RemoveDeepChainMethodCallNodeVisitor extends NodeVisitorAbstract
/**
* @var Expression|null
*/
private $nodeToRemove;
private $removingExpression;

public function __construct(BetterNodeFinder $betterNodeFinder, int $nestedChainMethodCallLimit)
{
Expand All @@ -50,7 +50,7 @@ public function enterNode(Node $node)
if ($node->expr instanceof MethodCall && $node->expr->var instanceof MethodCall) {
$nestedChainMethodCalls = $this->betterNodeFinder->findInstanceOf([$node->expr], MethodCall::class);
if (count($nestedChainMethodCalls) > $this->nestedChainMethodCallLimit) {
$this->nodeToRemove = $node;
$this->removingExpression = $node;

return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
Expand All @@ -64,7 +64,7 @@ public function enterNode(Node $node)
*/
public function leaveNode(Node $node)
{
if ($node === $this->nodeToRemove) {
if ($node === $this->removingExpression) {
// keep any node, so we don't remove it permanently
$nopNode = new Nop();
$nopNode->setAttributes($node->getAttributes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Rector\StaticTypeMapper\PhpParser;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\Type;
use Rector\PHPStan\Type\AliasedObjectType;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;

Expand All @@ -22,6 +24,14 @@ public function getNodeType(): string
*/
public function mapToPHPStan(Node $node): Type
{
$originalName = (string) $node->getAttribute('originalName');
$fullyQualifiedName = $node->toString();

// is aliased?
if ($originalName !== $fullyQualifiedName && ! Strings::endsWith($fullyQualifiedName, '\\' . $originalName)) {
return new AliasedObjectType($originalName, $fullyQualifiedName);
}

return new FullyQualifiedObjectType($node->toString());
}
}
1 change: 1 addition & 0 deletions rector-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ parameters:
- 'nette-utils-code-quality'
- 'solid'
- 'privatization'
- 'naming'

paths:
- 'src'
Expand Down
2 changes: 0 additions & 2 deletions rector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ parameters:
- "/*Source/"
- "/Fixture/"
- "/Expected/"
- "packages/Symfony/src/Bridge/DefaultAnalyzedSymfonyApplicationContainer.php"
- "packages/Php/tests/Rector/Name/ReservedObjectRector/*"

# autoload-buggy cases
- "*.php.inc"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$patterns = $this->regexPatternArgumentManipulator->matchCallArgumentWithRegexPattern($node);
if ($patterns === []) {
return null;
}

foreach ($patterns as $pattern) {
foreach (self::COMPLEX_PATTERN_TO_SIMPLE as $complexPattern => $simple) {
Expand Down
7 changes: 7 additions & 0 deletions rules/coding-style/src/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStan\Type\AliasedObjectType;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down Expand Up @@ -81,6 +82,12 @@ public function importName(Name $name): ?Name
}

$staticType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($name);

// propagate to fqn, that's what we need here
if ($staticType instanceof AliasedObjectType) {
$staticType = new FullyQualifiedObjectType($staticType->getFullyQualifiedClass());
}

if (! $staticType instanceof FullyQualifiedObjectType) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,19 @@ public function refactor(Node $node): ?Node
return null;
}

private function refactorFuncCall(FuncCall $funcCall): FuncCall
private function refactorFuncCall(FuncCall $funcCall): ?FuncCall
{
foreach (self::FUNCTIONS_WITH_REGEX_PATTERN as $function => $position) {
if (! $this->isName($funcCall, $function)) {
continue;
}

$this->refactorArgument($funcCall->args[$position]);

return $funcCall;
}

return $funcCall;
return null;
}

private function refactorArgument(Arg $arg): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function refactor(Node $node): ?Node
$defaultValues = $this->resolveDefaultValuesFromCall($node);

$keysToRemove = $this->resolveKeysToRemove($node, $defaultValues);
if ($keysToRemove === []) {
return null;
}

foreach ($keysToRemove as $keyToRemove) {
$this->removeArg($node, $keyToRemove);
}
Expand Down
12 changes: 10 additions & 2 deletions rules/naming/src/Naming/ConflictingNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\NodeNameResolver\NodeNameResolver;

final class ConflictingNameResolver
{
Expand All @@ -14,9 +15,15 @@ final class ConflictingNameResolver
*/
private $expectedNameResolver;

public function __construct(ExpectedNameResolver $expectedNameResolver)
/**
* @var NodeNameResolver
*/
private $nodeNameResolver;

public function __construct(ExpectedNameResolver $expectedNameResolver, NodeNameResolver $nodeNameResolver)
{
$this->expectedNameResolver = $expectedNameResolver;
$this->nodeNameResolver = $nodeNameResolver;
}

/**
Expand All @@ -28,7 +35,8 @@ public function resolveConflictingPropertyNames(ClassLike $classLike): array
foreach ($classLike->getProperties() as $property) {
$expectedName = $this->expectedNameResolver->resolveForProperty($property);
if ($expectedName === null) {
continue;
/** @var string $expectedName */
$expectedName = $this->nodeNameResolver->getName($property);
}

$expectedNames[] = $expectedName;
Expand Down
Loading