From ef9ee04c0e50d56a75c2a42a0196a1521bcde62b Mon Sep 17 00:00:00 2001 From: Daniel Espendiller Date: Mon, 29 Apr 2024 20:29:13 +0200 Subject: [PATCH] replace "isConvertibleFrom" instance of check with PhpClassHierarchyUtils.processSupers --- .../symfony2plugin/util/PhpElementsUtil.java | 26 +++++++------------ .../tests/util/PhpElementsUtilTest.java | 12 ++++++--- .../tests/util/fixtures/InstanceOf.php | 13 ++-------- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java index 01472469d..84556f351 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java @@ -4,7 +4,6 @@ import com.intellij.lang.ASTNode; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Ref; -import com.intellij.openapi.util.text.StringUtil; import com.intellij.patterns.ElementPattern; import com.intellij.patterns.PatternCondition; import com.intellij.patterns.PlatformPatterns; @@ -48,8 +47,8 @@ import fr.adrienbrault.idea.symfony2plugin.dic.MethodReferenceBag; import fr.adrienbrault.idea.symfony2plugin.util.psi.PsiElementAssertUtil; import kotlin.Pair; -import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -788,24 +787,17 @@ static public PhpClass getClassInterface(Project project, @NotNull String classN * @param expectedClass eg DateTimeInterface */ public static boolean isInstanceOf(@NotNull PhpClass subjectClass, @NotNull PhpClass expectedClass) { - Ref result = new Ref<>(false); - - PhpClassHierarchyUtils.processSupers(subjectClass, true, true, superClass -> { - boolean b = StringUtil.equalsIgnoreCase(superClass.getFQN(), expectedClass.getFQN()) - || StringUtil.equalsIgnoreCase(StringUtils.stripStart(superClass.getFQN(), "\\"), StringUtils.stripStart(expectedClass.getFQN(), "\\")); - - if (b) { - result.set(true); - } - - return !(Boolean)result.get(); - }); - - if (result.get()) { + if (PhpLangUtil.equalsClassNames(subjectClass.getFQN(), expectedClass.getFQN())) { return true; } - return new PhpType().add(expectedClass).isConvertibleFrom(new PhpType().add(subjectClass), PhpIndex.getInstance(subjectClass.getProject())); + Ref ref = new Ref<>(false); + PhpClassHierarchyUtils.processSupers(subjectClass, false, true, curClass -> { + ref.set(PhpLangUtil.equalsClassNames(curClass.getFQN(), expectedClass.getFQN())); + return !(Boolean) ref.get(); + }); + + return ref.get(); } /** diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/PhpElementsUtilTest.java b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/PhpElementsUtilTest.java index c76176d7f..8491cbb5e 100644 --- a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/PhpElementsUtilTest.java +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/PhpElementsUtilTest.java @@ -118,25 +118,29 @@ public void testIsInstanceOf() { Collection providers = new ArrayList<>() {{ add(new String[]{"\\Instance\\Of\\Foo", "\\Instance\\Of\\Bar"}); add(new String[]{"\\Instance\\Of\\Foo", "\\Instance\\Of\\Cool"}); + add(new String[]{"\\Instance\\Of\\Foo", "\\Instance\\Of\\Apple"}); add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Bar"}); add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Foo"}); add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Cool"}); + add(new String[]{"\\Instance\\Of\\Car", "\\Instance\\Of\\Car"}); // backslash add(new String[]{"Instance\\Of\\Car", "Instance\\Of\\Cool"}); add(new String[]{"Instance\\Of\\Car", "\\Instance\\Of\\Cool"}); add(new String[]{"\\Instance\\Of\\Car", "Instance\\Of\\Cool"}); - // dups add(new String[]{"\\Instance\\Of\\Car", "Instance\\Of\\Apple"}); add(new String[]{"\\Instance\\Of\\Foo", "Instance\\Of\\Apple"}); + add(new String[]{"\\Instance\\Of\\Apple", "Instance\\Of\\Apple"}); }}; for (String[] provider : providers) { - assertTrue(PhpElementsUtil.isInstanceOf(getProject(), provider[0], provider[1])); - assertTrue(PhpElementsUtil.isInstanceOf(PhpElementsUtil.getClassInterface(getProject(), provider[0]), provider[1])); + String errorMessage = "'%s' not instance of '%s'".formatted(provider[0], provider[1]); - assertTrue(PhpElementsUtil.isInstanceOf( + assertTrue(errorMessage, PhpElementsUtil.isInstanceOf(getProject(), provider[0], provider[1])); + assertTrue(errorMessage, PhpElementsUtil.isInstanceOf(PhpElementsUtil.getClassInterface(getProject(), provider[0]), provider[1])); + + assertTrue(errorMessage, PhpElementsUtil.isInstanceOf( PhpElementsUtil.getClassInterface(getProject(), provider[0]), PhpElementsUtil.getClassInterface(getProject(), provider[1]) )); diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/fixtures/InstanceOf.php b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/fixtures/InstanceOf.php index bda35fe90..221eb6803 100644 --- a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/fixtures/InstanceOf.php +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/fixtures/InstanceOf.php @@ -1,19 +1,10 @@