Skip to content

Commit d72de75

Browse files
ondrejmirtesclaude
andcommitted
Make the exported node tests run on PHP 7.4
Three ways the new tests did not survive the 7.4 job: - The name scope collecting visitor was an anonymous class taking the result array as a by-reference promoted property. simple-downgrade expands promotion into a plain assignment, which drops the reference, so the caller kept its empty array. It is a named test class now and hands the scopes back through a getter. - The property hooks in exported-properties-hooks.php do not parse below 8.4 - the parser follows the analysed PHP version, which defaults to the running one - so the test that needs them is marked #[RequiresPhp('>= 8.4.0')]. - That data file is not in the lint exclude list either, so its first line carries `// lint >= 8.4`. Verified against a downgraded tree on a real php 7.4 binary, not just the php-parser grammar. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VDcVRMjAXM7WQGSzysw7WH
1 parent eed4c8a commit d72de75

4 files changed

Lines changed: 48 additions & 29 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Dependency;
4+
5+
use Override;
6+
use PhpParser\Node;
7+
use PhpParser\NodeVisitorAbstract;
8+
9+
/**
10+
* Records the name scope in effect at every class declaration of a file.
11+
*/
12+
final class ExportedNameScopeCollectingVisitor extends NodeVisitorAbstract
13+
{
14+
15+
/** @var array<string, ExportedNameScope> */
16+
private array $scopes = [];
17+
18+
public function __construct(private ExportedNameScopeTracker $tracker)
19+
{
20+
}
21+
22+
/**
23+
* @return array<string, ExportedNameScope>
24+
*/
25+
public function getScopes(): array
26+
{
27+
return $this->scopes;
28+
}
29+
30+
#[Override]
31+
public function enterNode(Node $node): ?int
32+
{
33+
$this->tracker->enterNode($node);
34+
if ($node instanceof Node\Stmt\Class_ && isset($node->namespacedName)) {
35+
$this->scopes[$node->namespacedName->toString()] = $this->tracker->getNameScope();
36+
}
37+
38+
return null;
39+
}
40+
41+
}

‎tests/PHPStan/Dependency/ExportedNameScopeTrackerTest.php‎

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace PHPStan\Dependency;
44

5-
use Override;
6-
use PhpParser\Node;
75
use PhpParser\NodeTraverser;
8-
use PhpParser\NodeVisitorAbstract;
96
use PHPStan\Parser\Parser;
107
use PHPStan\Testing\PHPStanTestCase;
118
use PHPStan\Type\FileTypeMapper;
@@ -20,36 +17,13 @@ private function collectScopes(string $file): array
2017
{
2118
/** @var Parser $parser */
2219
$parser = self::getContainer()->getService('defaultAnalysisParser');
23-
$tracker = new ExportedNameScopeTracker();
24-
$scopes = [];
25-
26-
$visitor = new class ($tracker, $scopes) extends NodeVisitorAbstract {
27-
28-
/**
29-
* @param array<string, ExportedNameScope> $scopes
30-
*/
31-
public function __construct(private ExportedNameScopeTracker $tracker, public array &$scopes)
32-
{
33-
}
34-
35-
#[Override]
36-
public function enterNode(Node $node): ?int
37-
{
38-
$this->tracker->enterNode($node);
39-
if ($node instanceof Node\Stmt\Class_ && isset($node->namespacedName)) {
40-
$this->scopes[$node->namespacedName->toString()] = $this->tracker->getNameScope();
41-
}
42-
43-
return null;
44-
}
45-
46-
};
4720

21+
$visitor = new ExportedNameScopeCollectingVisitor(new ExportedNameScopeTracker());
4822
$traverser = new NodeTraverser();
4923
$traverser->addVisitor($visitor);
5024
$traverser->traverse($parser->parseFile($file));
5125

52-
return $scopes;
26+
return $visitor->getScopes();
5327
}
5428

5529
public function testTracksNamespacesAndUses(): void

‎tests/PHPStan/Dependency/ExportedNodeResolverTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPStan\Parser\Parser;
77
use PHPStan\Reflection\ReflectionProvider\DummyReflectionProvider;
88
use PHPStan\Testing\PHPStanTestCase;
9+
use PHPUnit\Framework\Attributes\RequiresPhp;
910

1011
final class ExportedNodeResolverTest extends PHPStanTestCase
1112
{
@@ -38,6 +39,9 @@ public function testPropertiesWithoutHooksDoNotLookUpTheClass(): void
3839
$this->assertSame(0, $reflectionProvider->hasClassCallCount);
3940
}
4041

42+
// The parser follows the analysed PHP version, which defaults to the running one, so the hooks
43+
// in the data file do not parse below 8.4 and no nodes come out at all.
44+
#[RequiresPhp('>= 8.4.0')]
4145
public function testHookedPropertyStillLooksUpTheClass(): void
4246
{
4347
$reflectionProvider = new CountingReflectionProvider(new DummyReflectionProvider());

‎tests/PHPStan/Dependency/data/exported-properties-hooks.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types = 1);
1+
<?php declare(strict_types = 1); // lint >= 8.4
22

33
namespace ExportedPropertiesHooks;
44

0 commit comments

Comments
 (0)