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
22 changes: 11 additions & 11 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ parameters:
narrow_return: true

ignoreErrors:
# required generics on interface array, not helpful
-
message: '#Method (.*?)\:\:__construct\(\) has parameter#'
identifier: missingType.generics
-
message: '#(.*?) with generic interface (.*?)Interface does not specify its types#'
identifier: missingType.generics

# phpstan class instance
Expand Down Expand Up @@ -135,7 +140,6 @@ parameters:

# internal reflection
- '#Instead of "new ClassReflection\(\)" use ReflectionProvider service or "\(new PHPStan\\Reflection\\ClassReflection\(<desired_type>\)\)" for static reflection to work#'

- '#Callable callable\(PHPStan\\Type\\Type\)\: PHPStan\\Type\\Type invoked with 2 parameters, 1 required#'

# known value
Expand Down Expand Up @@ -181,10 +185,15 @@ parameters:
# fixture class
- '#Class "Rector\\Tests\\Issues\\ScopeNotAvailable\\Variable\\ArrayItemForeachValueRector" is missing @see annotation with test case class reference#'

# classes are part of *.php.inc fixture
-
message: '#Class (.*?) not found#'
paths:
- rules-tests/*/config/*
- rules-tests/Arguments/Rector/ClassMethod/ReplaceArgumentDefaultValueRector/config
- rules-tests/TypeDeclaration/Rector/ClassMethod/AddReturnTypeDeclarationRector/config
- rules-tests/Renaming/Rector/Name/RenameClassRector/config
- rules-tests/Renaming/Rector/MethodCall/RenameMethodRector/config
- rules-tests/Arguments/Rector/ClassMethod/ArgumentAdderRector/config

-
message: '#Function "(function_exists|dump_node)\(\)" cannot be used/left in the code#'
Expand Down Expand Up @@ -334,12 +343,6 @@ parameters:
identifier: arrayValues.list
path: rules/CodingStyle/Application/UseImportsAdder.php

-
message: '#^Register "Rector\\Php74\\Rector\\Double\\RealToFloatTypeCastRector" service to "php74\.php" config set$#'
identifier: rector.upgradeDowngradeRegisteredInSet
count: 1
path: rules/Php74/Rector/Double/RealToFloatTypeCastRector.php

-
message: '#Offset float\|int\|string might not exist on string#'
path: rules/Php70/EregToPcreTransformer.php
Expand Down Expand Up @@ -387,14 +390,11 @@ parameters:
-
paths:
- rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php
- rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php
identifier: rector.noOnlyNullReturnInRefactor

-
identifier: rector.noIntegerRefactorReturn
paths:
- rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php

# valid, as use REMOVE_NODE
- rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php
- rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php
Expand Down
59 changes: 15 additions & 44 deletions src/PhpParser/NodeTraverser/AbstractImmutableNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,10 @@
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverserInterface;
use PhpParser\NodeVisitor;
use Rector\Exception\ShouldNotHappenException;

abstract class AbstractImmutableNodeTraverser implements NodeTraverserInterface
{
/**
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CHILDREN instead.
*/
public const DONT_TRAVERSE_CHILDREN = NodeVisitor::DONT_TRAVERSE_CHILDREN;

/**
* @deprecated Use NodeVisitor::STOP_TRAVERSAL instead.
*/
public const STOP_TRAVERSAL = NodeVisitor::STOP_TRAVERSAL;

/**
* @deprecated Use NodeVisitor::REMOVE_NODE instead.
*/
public const REMOVE_NODE = NodeVisitor::REMOVE_NODE;

/**
* @deprecated Use NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN instead.
*/
public const DONT_TRAVERSE_CURRENT_AND_CHILDREN = NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;

/**
* @var list<NodeVisitor> Visitors
*/
Expand Down Expand Up @@ -63,15 +44,9 @@ public function addVisitor(NodeVisitor $visitor): void
$this->visitors[] = $visitor;
}

/**
* Removes an added visitor.
*/
public function removeVisitor(NodeVisitor $visitor): void
{
$index = array_search($visitor, $this->visitors, true);
if ($index !== false) {
array_splice($this->visitors, $index, 1, []);
}
throw new ShouldNotHappenException('The immutable node traverser does not support removing visitors.');
}

/**
Expand Down Expand Up @@ -106,11 +81,6 @@ public function traverse(array $nodes): array
*/
abstract public function getVisitorsForNode(Node $node): array;

/**
* Recursively traverse a node.
*
* @param Node $node Node to traverse.
*/
protected function traverseNode(Node $node): void
{
foreach ($node->getSubNodeNames() as $name) {
Expand Down Expand Up @@ -189,10 +159,7 @@ protected function traverseNode(Node $node): void
}

/**
* Recursively traverse array (usually of nodes).
*
* @param Node[] $nodes Array to traverse
*
* @param Node[] $nodes
* @return array Result of traversal (may be original array or changed one)
*/
protected function traverseArray(array $nodes): array
Expand Down Expand Up @@ -285,16 +252,20 @@ protected function traverseArray(array $nodes): array

private function ensureReplacementReasonable(Node $old, Node $new): void
{
if ($old instanceof Stmt && $new instanceof Expr) {
throw new LogicException(
sprintf('Trying to replace statement (%s) ', $old->getType()) . sprintf(
'with expression (%s). Are you missing a ',
$new->getType()
) . 'Stmt_Expression wrapper?'
);
if ($old instanceof Stmt) {
if ($new instanceof Expr) {
throw new LogicException(
sprintf('Trying to replace statement (%s) ', $old->getType()) . sprintf(
'with expression (%s). Are you missing a ',
$new->getType()
) . 'Stmt_Expression wrapper?'
);
}

return;
}

if ($old instanceof Expr && $new instanceof Stmt) {
if ($new instanceof Stmt) {
throw new LogicException(
sprintf('Trying to replace expression (%s) ', $old->getType()) . sprintf(
'with statement (%s)',
Expand Down
16 changes: 0 additions & 16 deletions tests/PhpParser/NodeTraverser/RectorNodeTraverserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,4 @@ public function testGetVisitorsForNodeWhenAllVisitorsMatch(): void

$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
}

public function testGetVisitorsForNodeUsesCachedValue(): void
{
$class = new Class_('test');
$this->rectorNodeTraverser->addVisitor($this->ruleUsingClassRector);
$this->rectorNodeTraverser->addVisitor($this->ruleUsingClassLikeRector);

$visitors = $this->rectorNodeTraverser->getVisitorsForNode($class);

$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);

$this->rectorNodeTraverser->removeVisitor($this->ruleUsingClassRector);
$visitors = $this->rectorNodeTraverser->getVisitorsForNode($class);

$this->assertEquals([$this->ruleUsingClassRector, $this->ruleUsingClassLikeRector], $visitors);
}
}