Skip to content

Commit 5cd4572

Browse files
committed
[BCB] Removed NodeConnectingVisitor
1 parent 622b112 commit 5cd4572

12 files changed

+32
-83
lines changed

UPGRADING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ If you want to change `$overwrite` or `$rootExpr` (previous parameters also used
148148

149149
This method now longer accepts `Expr $rootExpr`. If you want to change it, call `setRootExpr()` on [`SpecifiedTypes`](https://apiref.phpstan.org/2.0.x/PHPStan.Analyser.SpecifiedTypes.html) (object returned by `TypeSpecifier::specifyTypesInCondition()`). `setRootExpr()` method returns a new object (SpecifiedTypes is immutable).
150150

151+
### Node attributes `parent`, `previous`, `next` are no longer available
152+
153+
Learn more: https://phpstan.org/blog/preprocessing-ast-for-custom-rules
154+
151155
### Removed config parameter `scopeClass`
152156

153157
As a replacement you can implement [`PHPStan\Type\ExpressionTypeResolverExtension`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.ExpressionTypeResolverExtension.html) interface instead and register it as a service.

changelog-2.0.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Major new features 🚀
77

88
* **Array `list` type** ([#1751](https://github.com/phpstan/phpstan-src/pull/1751)), #3311, #8185, #6243, thanks @rvanvelzen!
99
* Lists are arrays with sequential integer keys starting at 0
10+
* Lower memory consumption thanks to breaking up of reference cycles
11+
* [Learn more »](https://phpstan.org/blog/preprocessing-ast-for-custom-rules)
12+
* In testing the memory consumption was reduced by 50–70 %.
1013
* **Enhancements in Handling Parameters Passed by Reference**
1114
* [Learn more on phpstan.org](https://phpstan.org/blog/enhancements-in-handling-parameters-passed-by-reference)
1215
* [#2941](https://github.com/phpstan/phpstan-src/pull/2941), thanks @ljmaskey!
@@ -35,9 +38,6 @@ Bleeding edge (TODO move to other sections)
3538

3639
* Report useless `array_filter()` calls ([#1077](https://github.com/phpstan/phpstan-src/pull/1077)), #6840, thanks @leongersen!
3740
* Specify explicit mixed array type via `is_array` ([#1191](https://github.com/phpstan/phpstan-src/pull/1191)), thanks @herndlm!
38-
* Lower memory consumption thanks to breaking up of reference cycles
39-
* This is a BC break for rules that use `'parent'`, `'next'`, and `'previous'` node attributes. [Learn more »](https://phpstan.org/blog/preprocessing-ast-for-custom-rules)
40-
* In testing the memory consumption was reduced by 50–70 %.
4141
* ArrayUnpackingRule (level 3) ([#856](https://github.com/phpstan/phpstan-src/pull/856)), thanks @canvural!
4242
* Rules for checking direct calls to `__construct()` (level 2) ([#1208](https://github.com/phpstan/phpstan-src/pull/1208)), #7022, thanks @muno92!
4343
* ConstantLooseComparisonRule - level 4 (https://github.com/phpstan/phpstan-src/commit/6ebf2361a3c831dd105a815521889428c295dc9f)

conf/bleedingEdge.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ parameters:
88
arrayFilter: true
99
arrayUnpacking: true
1010
arrayValues: true
11-
nodeConnectingVisitorCompatibility: false
12-
nodeConnectingVisitorRule: true
1311
strictUnnecessaryNullsafePropertyFetch: true
1412
looseComparison: true
1513
consistentConstructor: true

conf/config.level0.neon

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ parameters:
22
customRulesetUsed: false
33

44
conditionalTags:
5-
PHPStan\Rules\Api\NodeConnectingVisitorAttributesRule:
6-
phpstan.rules.rule: %featureToggles.nodeConnectingVisitorRule%
75
PHPStan\Rules\Properties\MissingReadOnlyByPhpDocPropertyAssignRule:
86
phpstan.rules.rule: %featureToggles.readOnlyByPhpDoc%
97
PHPStan\Rules\Properties\ReadOnlyByPhpDocPropertyRule:
@@ -40,6 +38,7 @@ rules:
4038
- PHPStan\Rules\Api\ApiStaticCallRule
4139
- PHPStan\Rules\Api\ApiTraitUseRule
4240
- PHPStan\Rules\Api\GetTemplateTypeRule
41+
- PHPStan\Rules\Api\NodeConnectingVisitorAttributesRule
4342
- PHPStan\Rules\Api\PhpStanNamespaceIn3rdPartyPackageRule
4443
- PHPStan\Rules\Arrays\DuplicateKeysInLiteralArraysRule
4544
- PHPStan\Rules\Arrays\EmptyArrayItemRule
@@ -132,8 +131,6 @@ services:
132131
deprecationRulesInstalled: %deprecationRulesInstalled%
133132
tags:
134133
- phpstan.rules.rule
135-
-
136-
class: PHPStan\Rules\Api\NodeConnectingVisitorAttributesRule
137134
-
138135
class: PHPStan\Rules\Api\RuntimeReflectionFunctionRule
139136
-

conf/config.neon

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ parameters:
2828
arrayFilter: false
2929
arrayUnpacking: false
3030
arrayValues: false
31-
nodeConnectingVisitorCompatibility: true
32-
nodeConnectingVisitorRule: false
3331
illegalConstructorMethodCall: false
3432
strictUnnecessaryNullsafePropertyFetch: false
3533
looseComparison: false
@@ -253,8 +251,6 @@ conditionalTags:
253251
phpstan.rules.rule: %exceptions.check.tooWideThrowType%
254252
PHPStan\Rules\Exceptions\TooWideMethodThrowTypeRule:
255253
phpstan.rules.rule: %exceptions.check.tooWideThrowType%
256-
PhpParser\NodeVisitor\NodeConnectingVisitor:
257-
phpstan.parser.richParserNodeVisitor: %featureToggles.nodeConnectingVisitorCompatibility%
258254
PHPStan\Parser\CurlSetOptArgVisitor:
259255
phpstan.parser.richParserNodeVisitor: %featureToggles.curlSetOptTypes%
260256
PHPStan\Parser\TypeTraverserInstanceofVisitor:
@@ -349,9 +345,6 @@ services:
349345
tags:
350346
- phpstan.parser.richParserNodeVisitor
351347

352-
-
353-
class: PhpParser\NodeVisitor\NodeConnectingVisitor
354-
355348
-
356349
class: PHPStan\Node\Printer\ExprPrinter
357350

conf/parametersSchema.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ parametersSchema:
3434
arrayFilter: bool(),
3535
arrayUnpacking: bool(),
3636
arrayValues: bool(),
37-
nodeConnectingVisitorCompatibility: bool(),
38-
nodeConnectingVisitorRule: bool(),
3937
illegalConstructorMethodCall: bool(),
4038
strictUnnecessaryNullsafePropertyFetch: bool(),
4139
looseComparison: bool(),

src/Rules/Api/NodeConnectingVisitorAttributesRule.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\MethodCall;
7-
use PhpParser\NodeVisitor\NodeConnectingVisitor;
87
use PHPStan\Analyser\Scope;
9-
use PHPStan\DependencyInjection\Container;
10-
use PHPStan\Parser\RichParser;
118
use PHPStan\Rules\Rule;
129
use PHPStan\Rules\RuleErrorBuilder;
1310
use PHPStan\Type\Constant\ConstantStringType;
1411
use PHPStan\Type\ObjectType;
1512
use function array_keys;
16-
use function get_class;
1713
use function in_array;
1814
use function sprintf;
1915
use function str_starts_with;
@@ -24,10 +20,6 @@
2420
final class NodeConnectingVisitorAttributesRule implements Rule
2521
{
2622

27-
public function __construct(private Container $container)
28-
{
29-
}
30-
3123
public function getNodeType(): string
3224
{
3325
return MethodCall::class;
@@ -74,20 +66,6 @@ public function processNode(Node $node, Scope $scope): array
7466
return [];
7567
}
7668

77-
$isVisitorRegistered = false;
78-
foreach ($this->container->getServicesByTag(RichParser::VISITOR_SERVICE_TAG) as $service) {
79-
if (get_class($service) !== NodeConnectingVisitor::class) {
80-
continue;
81-
}
82-
83-
$isVisitorRegistered = true;
84-
break;
85-
}
86-
87-
if ($isVisitorRegistered) {
88-
return [];
89-
}
90-
9169
return [
9270
RuleErrorBuilder::message(sprintf('Node attribute \'%s\' is no longer available.', $argType->getValue()))
9371
->identifier('phpParser.nodeConnectingAttribute')

tests/PHPStan/Parser/CachedParserTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,34 @@ public function testParseTheSameFileWithDifferentMethod(): void
9090
$contents = FileReader::read($path);
9191
$stmts = $parser->parseString($contents);
9292
$this->assertInstanceOf(Namespace_::class, $stmts[0]);
93-
$this->assertNull($stmts[0]->stmts[0]->getAttribute('parent'));
93+
$this->assertInstanceOf(Node\Stmt\Expression::class, $stmts[0]->stmts[0]);
94+
$this->assertInstanceOf(Node\Expr\Assign::class, $stmts[0]->stmts[0]->expr);
95+
$this->assertInstanceOf(Node\Expr\New_::class, $stmts[0]->stmts[0]->expr->expr);
96+
$this->assertNull($stmts[0]->stmts[0]->expr->expr->class->getAttribute(AnonymousClassVisitor::ATTRIBUTE_LINE_INDEX));
9497

9598
$stmts = $parser->parseFile($path);
9699
$this->assertInstanceOf(Namespace_::class, $stmts[0]);
97-
$this->assertInstanceOf(Namespace_::class, $stmts[0]->stmts[0]->getAttribute('parent'));
100+
$this->assertInstanceOf(Node\Stmt\Expression::class, $stmts[0]->stmts[0]);
101+
$this->assertInstanceOf(Node\Expr\Assign::class, $stmts[0]->stmts[0]->expr);
102+
$this->assertInstanceOf(Node\Expr\New_::class, $stmts[0]->stmts[0]->expr->expr);
103+
$this->assertSame(1, $stmts[0]->stmts[0]->expr->expr->class->getAttribute(AnonymousClassVisitor::ATTRIBUTE_LINE_INDEX));
104+
105+
$this->assertInstanceOf(Node\Stmt\Expression::class, $stmts[0]->stmts[1]);
106+
$this->assertInstanceOf(Node\Expr\Assign::class, $stmts[0]->stmts[1]->expr);
107+
$this->assertInstanceOf(Node\Expr\New_::class, $stmts[0]->stmts[1]->expr->expr);
108+
$this->assertSame(2, $stmts[0]->stmts[1]->expr->expr->class->getAttribute(AnonymousClassVisitor::ATTRIBUTE_LINE_INDEX));
98109

99110
$stmts = $parser->parseString($contents);
100111
$this->assertInstanceOf(Namespace_::class, $stmts[0]);
101-
$this->assertInstanceOf(Namespace_::class, $stmts[0]->stmts[0]->getAttribute('parent'));
112+
$this->assertInstanceOf(Node\Stmt\Expression::class, $stmts[0]->stmts[0]);
113+
$this->assertInstanceOf(Node\Expr\Assign::class, $stmts[0]->stmts[0]->expr);
114+
$this->assertInstanceOf(Node\Expr\New_::class, $stmts[0]->stmts[0]->expr->expr);
115+
$this->assertSame(1, $stmts[0]->stmts[0]->expr->expr->class->getAttribute(AnonymousClassVisitor::ATTRIBUTE_LINE_INDEX));
116+
117+
$this->assertInstanceOf(Node\Stmt\Expression::class, $stmts[0]->stmts[1]);
118+
$this->assertInstanceOf(Node\Expr\Assign::class, $stmts[0]->stmts[1]->expr);
119+
$this->assertInstanceOf(Node\Expr\New_::class, $stmts[0]->stmts[1]->expr->expr);
120+
$this->assertSame(2, $stmts[0]->stmts[1]->expr->expr->class->getAttribute(AnonymousClassVisitor::ATTRIBUTE_LINE_INDEX));
102121
}
103122

104123
}

tests/PHPStan/Parser/data/test.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22

33
namespace CachedParserBug;
44

5-
class Foo
6-
{
7-
8-
}
5+
$a = new class () {}; $b = new class () {};

tests/PHPStan/Rules/Api/NodeConnectingVisitorAttributesRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class NodeConnectingVisitorAttributesRuleTest extends RuleTestCase
1313

1414
protected function getRule(): Rule
1515
{
16-
return new NodeConnectingVisitorAttributesRule(self::getContainer());
16+
return new NodeConnectingVisitorAttributesRule();
1717
}
1818

1919
public function testRule(): void

0 commit comments

Comments
 (0)