Skip to content
This repository was archived by the owner on Mar 6, 2022. It is now read-only.

Commit fd3973a

Browse files
authored
Fix static properties (#35)
* Index static properties without the $. * Add static property indexer test * Remove stray dump * Index use statements and trait usings. * -- Index trait usings as references -- Added tests for trait usings for IndexedReferenceFinder -- Added traits for TraitUseCaluseIndexer * Coding standards * change worse-reflection version requirement * fix comments * Index static properties without the $. * Add static property indexer test * Remove stray dump * Index use statements and trait usings. * -- Index trait usings as references -- Added tests for trait usings for IndexedReferenceFinder -- Added traits for TraitUseCaluseIndexer * Coding standards * change worse-reflection version requirement * fix comments
1 parent 8274902 commit fd3973a

File tree

8 files changed

+132
-6
lines changed

8 files changed

+132
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/tests/Assets/workspace
55
/composer.lock
66
/.php_cs.cache
7+
/.vscode
78
/stubs
89
/.phpunit.result.cache
910
.phpunit.result.cache

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"phpactor/source-code-filesystem": "~0.1.5",
2121
"phpactor/source-code-filesystem-extension": "^0.1.3",
2222
"phpactor/text-document": "^1.1.3",
23-
"phpactor/worse-reflection": "^0.4",
23+
"phpactor/worse-reflection": "^0.4.4",
2424
"phpactor/worse-reflection-extension": "^0.2.0",
2525
"thecodingmachine/safe": "^1.0"
2626
},

lib/Adapter/Tolerant/Indexer/ClassLikeReferenceIndexer.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Microsoft\PhpParser\Node;
66
use Microsoft\PhpParser\Node\Expression\CallExpression;
77
use Microsoft\PhpParser\Node\QualifiedName;
8+
use Microsoft\PhpParser\Node\TraitUseClause;
89
use Phpactor\Indexer\Model\Index;
910
use Phpactor\Indexer\Model\RecordReference;
1011
use Phpactor\Indexer\Model\Record\ClassRecord;
1112
use Phpactor\Indexer\Model\Record\FileRecord;
13+
use Phpactor\WorseReflection\Bridge\TolerantParser\Patch\TolerantQualifiedNameResolver;
1214
use SplFileInfo;
1315

1416
class ClassLikeReferenceIndexer extends AbstractClassLikeIndexer
@@ -48,10 +50,13 @@ public function beforeParse(Index $index, SplFileInfo $info): void
4850
public function index(Index $index, SplFileInfo $info, Node $node): void
4951
{
5052
assert($node instanceof QualifiedName);
53+
54+
$name =
55+
$node->parent->parent instanceof TraitUseClause ?
56+
TolerantQualifiedNameResolver::getResolvedName($node) :
57+
$node->getResolvedName();
5158

52-
$name = $node->getResolvedName() ? $node->getResolvedName() : null;
53-
54-
if (null === $name) {
59+
if (empty($name)) {
5560
return;
5661
}
5762

lib/Adapter/Tolerant/Indexer/MemberIndexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private function resolveScopedPropertyAccessName(ScopedPropertyAccessExpression
106106
return (string)$memberName->getText($node->getFileContents());
107107
}
108108

109-
return $memberName->getText();
109+
return $memberName->getName();
110110
}
111111

112112
private function indexMemberAccessExpression(Index $index, SplFileInfo $info, MemberAccessExpression $node): void

tests/Adapter/ReferenceFinder/IndexedReferenceFinderTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ protected function setUp(): void
1919

2020
/**
2121
* @dataProvider provideClasses
22+
* @dataProvider provideTraits
2223
* @dataProvider provideFunctions
2324
* @dataProvider provideMembers
2425
* @dataProvider provideUnknown
@@ -102,6 +103,35 @@ class Bar extends Foo {}
102103
];
103104
}
104105

106+
107+
/**
108+
* @return Generator<mixed>
109+
*/
110+
public function provideTraits(): Generator
111+
{
112+
yield 'single trait' => [
113+
<<<'EOT'
114+
// File: project/subject.php
115+
<?php class Foo { use Ba<>r; };
116+
EOT
117+
,
118+
1
119+
];
120+
121+
yield 'implementation' => [
122+
<<<'EOT'
123+
// File: project/subject.php
124+
<?php class Foo { use Ba<>r; };
125+
126+
// File: project/other.php
127+
<?php
128+
$b = new Foo();
129+
EOT
130+
,
131+
2
132+
];
133+
}
134+
105135
/**
106136
* @return Generator<mixed>
107137
*/
@@ -189,6 +219,21 @@ public function provideMembers(): Generator
189219
,
190220
2, 4 // total number is multipled due to implementation recursion
191221
];
222+
223+
yield 'static properties' => [
224+
<<<'EOT'
225+
// File: project/foobar.php
226+
<?php class Foobar { public static $staticProp; function f(){ self::$staticProp = 5; } }
227+
228+
// File: project/subject.php
229+
<?php Foobar::$st<>aticProp = 5;
230+
231+
// File: project/class1.php
232+
<?php var $b = Foobar::$staticProp;
233+
EOT
234+
,
235+
3
236+
];
192237
}
193238

194239
/**

tests/Adapter/Tolerant/Indexer/ClassLikeReferenceIndexerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,28 @@ public function provideClasses(): Generator
7373
[0, 0, 1]
7474
];
7575

76+
yield 'class multiple implements 2' => [
77+
"// File: src/file1.php\n<?php class Barfoo implements Foo,Bar {};",
78+
'Bar',
79+
[0, 0, 1]
80+
];
81+
7682
yield 'abstract class implements' => [
7783
"// File: src/file1.php\n<?php abstract class Barfoo implements Foobar,Barfoo {};",
7884
'Foobar',
7985
[0, 0, 1]
8086
];
87+
88+
yield 'use trait (basic)' => [
89+
"// File: src/file1.php\n<?php class C { use T; }",
90+
'T',
91+
[0, 0, 1]
92+
];
93+
94+
yield 'use trait (namespaced)' => [
95+
"// File: src/file1.php\n<?php use N\T; class C { use T; }",
96+
'N\T',
97+
[0, 0, 1]
98+
];
8199
}
82100
}

tests/Adapter/Tolerant/Indexer/MemberIndexerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function provideStaticAccess(): Generator
8686

8787
yield MemberRecord::TYPE_PROPERTY => [
8888
"// File: src/file1.php\n<?php Foobar::\$foobar;",
89-
MemberReference::create(MemberRecord::TYPE_PROPERTY, 'Foobar', '$foobar'),
89+
MemberReference::create(MemberRecord::TYPE_PROPERTY, 'Foobar', 'foobar'),
9090
[ 0, 0, 1 ]
9191
];
9292

@@ -102,6 +102,12 @@ public function provideStaticAccess(): Generator
102102
[ 0, 0, 1 ]
103103
];
104104

105+
yield 'self (property)' => [
106+
"// File: src/file1.php\n<?php class Foobar { static \$barProp; function foo() { self::\$barProp = 5; \$var1 = self::\$barProp; } }",
107+
MemberReference::create(MemberRecord::TYPE_PROPERTY, 'Foobar', 'barProp'),
108+
[ 0, 0, 2 ]
109+
];
110+
105111
yield 'parent' => [
106112
"// File: src/file1.php\n<?php class Foobar { function bar() {}} class Barfoo extends Foobar { function foo() { parent::bar(); } }",
107113
MemberReference::create(MemberRecord::TYPE_METHOD, 'Foobar', 'bar'),
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Phpactor\Indexer\Tests\Adapter\Tolerant\Indexer;
4+
5+
use Generator;
6+
use Phpactor\Indexer\Adapter\Tolerant\Indexer\TraitUseClauseIndexer;
7+
use Phpactor\Indexer\Tests\Adapter\Tolerant\TolerantIndexerTestCase;
8+
9+
class TraitUseClauseIndexerTest extends TolerantIndexerTestCase
10+
{
11+
/**
12+
* @dataProvider provideImplementations
13+
*/
14+
public function testImplementations(string $manifest, string $fqn, int $expectedCount): void
15+
{
16+
$this->workspace()->reset();
17+
$this->workspace()->loadManifest($manifest);
18+
$agent = $this->runIndexer(new TraitUseClauseIndexer(), 'src');
19+
self::assertEquals($expectedCount, count($agent->query()->class()->implementing($fqn)));
20+
}
21+
22+
/**
23+
* @return Generator<mixed>
24+
*/
25+
public function provideImplementations(): Generator
26+
{
27+
yield 'use trait (basic)' => [
28+
"// File: src/file1.php\n<?php namespace N; use T; class C { use T; }",
29+
'T',
30+
1
31+
];
32+
33+
yield 'use trait (namespaced)' => [
34+
"// File: src/file1.php\n<?php use N\T; class C { use T; }",
35+
'N\T',
36+
1
37+
];
38+
39+
yield 'use trait (namespaced & grouped)' => [
40+
"// File: src/file1.php\n<?php use N\{T}; class C { use T; }",
41+
'N\T',
42+
1
43+
];
44+
45+
yield 'use trait (aliased)' => [
46+
"// File: src/file1.php\n<?php use N\T as G; class C { use G; }",
47+
'N\T',
48+
1
49+
];
50+
}
51+
}

0 commit comments

Comments
 (0)