Skip to content

Commit ef9ee04

Browse files
committed
replace "isConvertibleFrom" instance of check with PhpClassHierarchyUtils.processSupers
1 parent e0c36c7 commit ef9ee04

File tree

3 files changed

+19
-32
lines changed

3 files changed

+19
-32
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.intellij.lang.ASTNode;
55
import com.intellij.openapi.project.Project;
66
import com.intellij.openapi.util.Ref;
7-
import com.intellij.openapi.util.text.StringUtil;
87
import com.intellij.patterns.ElementPattern;
98
import com.intellij.patterns.PatternCondition;
109
import com.intellij.patterns.PlatformPatterns;
@@ -48,8 +47,8 @@
4847
import fr.adrienbrault.idea.symfony2plugin.dic.MethodReferenceBag;
4948
import fr.adrienbrault.idea.symfony2plugin.util.psi.PsiElementAssertUtil;
5049
import kotlin.Pair;
51-
import org.apache.commons.lang3.StringUtils;
5250
import org.apache.commons.lang3.ArrayUtils;
51+
import org.apache.commons.lang3.StringUtils;
5352
import org.jetbrains.annotations.NotNull;
5453
import org.jetbrains.annotations.Nullable;
5554

@@ -788,24 +787,17 @@ static public PhpClass getClassInterface(Project project, @NotNull String classN
788787
* @param expectedClass eg DateTimeInterface
789788
*/
790789
public static boolean isInstanceOf(@NotNull PhpClass subjectClass, @NotNull PhpClass expectedClass) {
791-
Ref<Boolean> result = new Ref<>(false);
792-
793-
PhpClassHierarchyUtils.processSupers(subjectClass, true, true, superClass -> {
794-
boolean b = StringUtil.equalsIgnoreCase(superClass.getFQN(), expectedClass.getFQN())
795-
|| StringUtil.equalsIgnoreCase(StringUtils.stripStart(superClass.getFQN(), "\\"), StringUtils.stripStart(expectedClass.getFQN(), "\\"));
796-
797-
if (b) {
798-
result.set(true);
799-
}
800-
801-
return !(Boolean)result.get();
802-
});
803-
804-
if (result.get()) {
790+
if (PhpLangUtil.equalsClassNames(subjectClass.getFQN(), expectedClass.getFQN())) {
805791
return true;
806792
}
807793

808-
return new PhpType().add(expectedClass).isConvertibleFrom(new PhpType().add(subjectClass), PhpIndex.getInstance(subjectClass.getProject()));
794+
Ref<Boolean> ref = new Ref<>(false);
795+
PhpClassHierarchyUtils.processSupers(subjectClass, false, true, curClass -> {
796+
ref.set(PhpLangUtil.equalsClassNames(curClass.getFQN(), expectedClass.getFQN()));
797+
return !(Boolean) ref.get();
798+
});
799+
800+
return ref.get();
809801
}
810802

811803
/**

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/PhpElementsUtilTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,29 @@ public void testIsInstanceOf() {
118118
Collection<String[]> providers = new ArrayList<>() {{
119119
add(new String[]{"\\Instance\\Of\\Foo", "\\Instance\\Of\\Bar"});
120120
add(new String[]{"\\Instance\\Of\\Foo", "\\Instance\\Of\\Cool"});
121+
add(new String[]{"\\Instance\\Of\\Foo", "\\Instance\\Of\\Apple"});
121122
add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Bar"});
122123
add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Foo"});
123124
add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Cool"});
125+
add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Car"});
124126

125127
// backslash
126128
add(new String[]{"Instance\\Of\\Car", "Instance\\Of\\Cool"});
127129
add(new String[]{"Instance\\Of\\Car", "\\Instance\\Of\\Cool"});
128130
add(new String[]{"\\Instance\\Of\\Car", "Instance\\Of\\Cool"});
129131

130-
// dups
131132
add(new String[]{"\\Instance\\Of\\Car", "Instance\\Of\\Apple"});
132133
add(new String[]{"\\Instance\\Of\\Foo", "Instance\\Of\\Apple"});
134+
add(new String[]{"\\Instance\\Of\\Apple", "Instance\\Of\\Apple"});
133135
}};
134136

135137
for (String[] provider : providers) {
136-
assertTrue(PhpElementsUtil.isInstanceOf(getProject(), provider[0], provider[1]));
137-
assertTrue(PhpElementsUtil.isInstanceOf(PhpElementsUtil.getClassInterface(getProject(), provider[0]), provider[1]));
138+
String errorMessage = "'%s' not instance of '%s'".formatted(provider[0], provider[1]);
138139

139-
assertTrue(PhpElementsUtil.isInstanceOf(
140+
assertTrue(errorMessage, PhpElementsUtil.isInstanceOf(getProject(), provider[0], provider[1]));
141+
assertTrue(errorMessage, PhpElementsUtil.isInstanceOf(PhpElementsUtil.getClassInterface(getProject(), provider[0]), provider[1]));
142+
143+
assertTrue(errorMessage, PhpElementsUtil.isInstanceOf(
140144
PhpElementsUtil.getClassInterface(getProject(), provider[0]),
141145
PhpElementsUtil.getClassInterface(getProject(), provider[1])
142146
));
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
<?php
22

33
namespace Instance\Of {
4-
//duplicates
5-
class Car implements \DateTime{}
6-
class Foo implements \DateTime{}
7-
8-
94
class Foo extends Cool implements Bar{}
10-
interface Bar {}
11-
class Car extends Foo{}
5+
interface Bar extends Apple {}
6+
class Car extends Foo implements Apple{}
127
class Cool{}
138

14-
//duplicates
15-
class Car implements Apple{}
16-
class Foo implements Apple{}
17-
189
interface Apple{}
1910
}

0 commit comments

Comments
 (0)