Skip to content

Commit 17690b1

Browse files
Merge pull request #8883 from troizet/fix_gh_8527
PHP: Fixed an issue where a trait used in an anonymous class was defined as being used in a wrapper class
2 parents d44ead7 + 365446a commit 17690b1

File tree

12 files changed

+130
-28
lines changed

12 files changed

+130
-28
lines changed

php/php.editor/src/org/netbeans/modules/php/editor/model/nodes/ASTNodeInfo.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.netbeans.modules.php.editor.parser.astnodes.ArrayAccess;
3232
import org.netbeans.modules.php.editor.parser.astnodes.ArrayCreation;
3333
import org.netbeans.modules.php.editor.parser.astnodes.ArrowFunctionDeclaration;
34+
import org.netbeans.modules.php.editor.parser.astnodes.Block;
3435
import org.netbeans.modules.php.editor.parser.astnodes.ClassInstanceCreation;
3536
import org.netbeans.modules.php.editor.parser.astnodes.ClassName;
3637
import org.netbeans.modules.php.editor.parser.astnodes.Expression;
@@ -48,10 +49,12 @@
4849
import org.netbeans.modules.php.editor.parser.astnodes.ReturnStatement;
4950
import org.netbeans.modules.php.editor.parser.astnodes.Scalar;
5051
import org.netbeans.modules.php.editor.parser.astnodes.SingleUseStatementPart;
52+
import org.netbeans.modules.php.editor.parser.astnodes.Statement;
5153
import org.netbeans.modules.php.editor.parser.astnodes.StaticConstantAccess;
5254
import org.netbeans.modules.php.editor.parser.astnodes.StaticDispatch;
5355
import org.netbeans.modules.php.editor.parser.astnodes.StaticFieldAccess;
5456
import org.netbeans.modules.php.editor.parser.astnodes.StaticMethodInvocation;
57+
import org.netbeans.modules.php.editor.parser.astnodes.UseTraitStatement;
5558
import org.netbeans.modules.php.editor.parser.astnodes.UseTraitStatementPart;
5659
import org.netbeans.modules.php.editor.parser.astnodes.Variable;
5760
import org.netbeans.modules.php.editor.parser.astnodes.Variadic;
@@ -454,8 +457,14 @@ static class UsedTraitsVisitor extends DefaultVisitor {
454457
private final List<UseTraitStatementPart> useParts = new LinkedList<>();
455458

456459
@Override
457-
public void visit(UseTraitStatementPart node) {
458-
useParts.add(node);
460+
public void visit(Block node) {
461+
for (Statement statement : node.getStatements()) {
462+
if (statement instanceof UseTraitStatement useTraitStatement) {
463+
for (UseTraitStatementPart parts : useTraitStatement.getParts()) {
464+
useParts.add(parts);
465+
}
466+
}
467+
}
459468
}
460469

461470
public Collection<QualifiedName> getUsedTraits() {

php/php.editor/src/org/netbeans/modules/php/editor/model/nodes/ClassInstanceCreationInfo.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.util.Collection;
2222
import java.util.HashSet;
23-
import java.util.LinkedList;
2423
import java.util.List;
2524
import java.util.Set;
2625
import org.netbeans.modules.csl.api.OffsetRange;
@@ -31,8 +30,6 @@
3130
import org.netbeans.modules.php.editor.parser.astnodes.ClassInstanceCreation;
3231
import org.netbeans.modules.php.editor.parser.astnodes.ClassName;
3332
import org.netbeans.modules.php.editor.parser.astnodes.Expression;
34-
import org.netbeans.modules.php.editor.parser.astnodes.UseTraitStatementPart;
35-
import org.netbeans.modules.php.editor.parser.astnodes.visitors.DefaultVisitor;
3633

3734
/**
3835
* Info for anonymous classes.
@@ -106,25 +103,4 @@ public List<Attribute> getAttributes() {
106103
return getOriginalNode().getAttributes();
107104
}
108105

109-
//~ Inner classes
110-
private static final class UsedTraitsVisitor extends DefaultVisitor {
111-
112-
private final List<UseTraitStatementPart> useParts = new LinkedList<>();
113-
114-
115-
@Override
116-
public void visit(UseTraitStatementPart node) {
117-
useParts.add(node);
118-
}
119-
120-
public Collection<QualifiedName> getUsedTraits() {
121-
Collection<QualifiedName> retval = new HashSet<>();
122-
for (UseTraitStatementPart useTraitStatementPart : useParts) {
123-
retval.add(QualifiedName.create(useTraitStatementPart.getName()));
124-
}
125-
return retval;
126-
}
127-
128-
}
129-
130106
}

php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/csl/NavigatorTest/structure/anonymousClassInClassScope.pass

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
|-ClassScopeTestInterface [16, 48] : ESCAPED{ClassScopeTestInterface}
22
|-ClassScopeTestTrait [56, 84] : ESCAPED{ClassScopeTestTrait}
3-
|-ClassScopeTest [92, 1512] : ESCAPED{ClassScopeTest}<font color="#999999">#ESCAPED{ClassScopeTestTrait}</font>
3+
|-ClassScopeTest [92, 1512] : ESCAPED{ClassScopeTest}
44
|--test1 [130, 1059] : ESCAPED{test1}ESCAPED{(}ESCAPED{)}
55
|--test2 [1081, 1256] : ESCAPED{test2}ESCAPED{(}ESCAPED{)}
66
|--test3 [1278, 1510] : ESCAPED{test3}ESCAPED{(}ESCAPED{)}

php/php.editor/test/unit/data/goldenfiles/org/netbeans/modules/php/editor/csl/NavigatorTest/structure/anonymousClassInTraitScope.pass

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
|-TraitScopeTestInterface [16, 48] : ESCAPED{TraitScopeTestInterface}
22
|-TraitScopeTestTrait [56, 84] : ESCAPED{TraitScopeTestTrait}
33
|-TraitScopeClass [92, 116] : ESCAPED{TraitScopeClass}
4-
|-TraitScopeTest [124, 1544] : ESCAPED{TraitScopeTest}<font color="#999999">#ESCAPED{TraitScopeTestTrait}</font>
4+
|-TraitScopeTest [124, 1544] : ESCAPED{TraitScopeTest}
55
|--test1 [162, 1091] : ESCAPED{test1}ESCAPED{(}ESCAPED{)}
66
|--test2 [1113, 1288] : ESCAPED{test2}ESCAPED{(}ESCAPED{)}
77
|--test3 [1310, 1542] : ESCAPED{test3}ESCAPED{(}ESCAPED{)}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
|-MyTrait [13, 107] : ESCAPED{MyTrait}
2+
|--doSomething [45, 56] : ESCAPED{doSomething}ESCAPED{(}ESCAPED{)}
3+
|--doNothing [81, 105] : ESCAPED{doNothing}ESCAPED{(}ESCAPED{)}
4+
|-MysClass [115, 510] : ESCAPED{MysClass}
5+
|--getObject [146, 508] : ESCAPED{getObject}ESCAPED{(}ESCAPED{)}
6+
|-#anon#gh8527_php#2 [183, 501] : ESCAPED{{}}
7+
|--getMyTraitAwareObject [222, 491] : ESCAPED{getMyTraitAwareObject}ESCAPED{(}ESCAPED{)}
8+
|-#anon#gh8527_php#1 [287, 476] : ESCAPED{{}}<font color="#999999">#ESCAPED{MyTrait}</font>
9+
|--doSomething [400, 458] : ESCAPED{doSomething}ESCAPED{(}ESCAPED{)}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
trait MyTrait
4+
{
5+
abstract function doSomething();
6+
7+
public function doNothing()
8+
{
9+
10+
}
11+
}
12+
13+
class MysClass
14+
{
15+
public function getObject()
16+
{
17+
return new class() {
18+
19+
public function getMyTraitAwareObject()
20+
{
21+
return new class() {
22+
use MyTrait;
23+
24+
#[\Override]
25+
public function doSomething()
26+
{
27+
28+
}
29+
};
30+
}
31+
};
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
trait MyTrait
4+
{
5+
abstract function doSomething();
6+
7+
public function doNothing()
8+
{
9+
10+
}
11+
}
12+
13+
class MysClass
14+
{
15+
public function getObject()
16+
{
17+
return new class() {
18+
19+
public function getMyTraitAwareObject()
20+
{
21+
return new class() {
22+
use MyTrait;
23+
24+
public function doSomething()
25+
{
26+
27+
}
28+
};
29+
}
30+
};
31+
}
32+
}

php/php.editor/test/unit/data/testfiles/verification/ImplementAbstractMethodsHintError/gh8527.php.testGH8527.hints

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
trait MyTrait
4+
{
5+
abstract function doSomething();
6+
7+
public function doNothing()
8+
{
9+
10+
}
11+
}
12+
13+
class MysClass
14+
{
15+
public function getObject()
16+
{
17+
return new class() {
18+
19+
public function getMyTraitAwareObject()
20+
{
21+
return new class() {
22+
use MyTrait;
23+
};
24+
}
25+
};
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
return new class() {
2+
-----
3+
HINT:Anonymous class is not abstract and does not override abstract method doSomething() in \MyTrait
4+
FIX:Implement All Abstract Methods

0 commit comments

Comments
 (0)