Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only allow valid class "use" annotating missing import #190 #193

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,7 @@ private void visitAnnotationDocTag(@NotNull PhpDocTag phpDocTag, @NotNull Proble
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new ImportUseForAnnotationQuickFix(phpDocTag, collect)
);

return;
}

holder.registerProblem(
firstChild,
MESSAGE,
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,29 +447,35 @@ public static Map<String, String> getPossibleImportClasses(@NotNull PhpDocTag ph

Map<String, String> phpClasses = new HashMap<>();

for (PhpClass annotationClass: AnnotationUtil.getAnnotationsClasses(phpDocTag.getProject())) {
if (annotationClass.getName().equals(className)) {
phpClasses.put(annotationClass.getFQN(), null);
// "@Test\Bar" can be ignored here
// "@Bar" We only search for a direct class
if (!className.contains("\\")) {
for (PhpClass annotationClass: AnnotationUtil.getAnnotationsClasses(phpDocTag.getProject())) {
if (annotationClass.getName().equals(className)) {
phpClasses.put(annotationClass.getFQN(), null);
}
}
}

String[] split = className.split("\\\\");
if (split.length > 1) {
String aliasStart = split[0];

for (UseAliasOption useAliasOption : AnnotationUtil.getActiveImportsAliasesFromSettings()) {
String alias = useAliasOption.getAlias();
// "@ORM\Entity" is search for configured alias which can also provide a valid "use" path
if (!className.startsWith("\\")) {
String[] split = className.split("\\\\");
if (split.length > 1) {
String aliasStart = split[0];

if (!aliasStart.equals(alias)) {
continue;
}
for (UseAliasOption useAliasOption : AnnotationUtil.getActiveImportsAliasesFromSettings()) {
String alias = useAliasOption.getAlias();
if (!aliasStart.equals(alias)) {
continue;
}

String clazz = useAliasOption.getClassName() + "\\" + StringUtils.join(Arrays.copyOfRange(split, 1, split.length), "\\");
PhpClass phpClass = PhpElementsUtil.getClassInterface(phpDocTag.getProject(), clazz);
String clazz = useAliasOption.getClassName() + "\\" + StringUtils.join(Arrays.copyOfRange(split, 1, split.length), "\\");
PhpClass phpClass = PhpElementsUtil.getClassInterface(phpDocTag.getProject(), clazz);

if (phpClass != null && isAnnotationClass(phpClass)) {
// user input for class name
phpClasses.put("\\" + StringUtils.stripStart(useAliasOption.getClassName(), "\\"), alias);
if (phpClass != null && isAnnotationClass(phpClass)) {
// user input for class name
phpClasses.put("\\" + StringUtils.stripStart(useAliasOption.getClassName(), "\\"), alias);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ public void testThatInspectionIsDisplayedForAnnotationClasses() {
AnnotationMissingUseInspection.MESSAGE
);

assertLocalInspectionContains("test.php", "<?php\n" +
assertLocalInspectionContainsNotContains("test.php", "<?php\n" +
"use Foo\\Entity;\n" +
"\n" +
"/**\n" +
" * @Entity()\n" +
" * @Foo()\n" +
" * @Fo<caret>obar()\n" +
" * @E<caret>ntity()\n" +
" */\n" +
"class Foo\n" +
"{\n" +
Expand All @@ -45,21 +43,25 @@ public void testThatInspectionIsDisplayedForAnnotationClasses() {
);

assertLocalInspectionContainsNotContains("test.php", "<?php\n" +
"use Foo\\Entity;\n" +
"\n" +
"/**\n" +
" * @E<caret>ntity()\n" +
" * @\\E<caret>ntity()\n" +
" */\n" +
"class Foo\n" +
"{\n" +
"}",
AnnotationMissingUseInspection.MESSAGE
);
}

public void testThatInspectionIsNotDisplayedForClassesWhichDoesNotHaveAValidImportPath() {
assertLocalInspectionContainsNotContains("test.php", "<?php\n" +
"use Foo\\Entity;\n" +
"\n" +
"/**\n" +
" * @\\E<caret>ntity()\n" +
" * @Entity()\n" +
" * @Foo()\n" +
" * @Fo<caret>obar()\n" +
" */\n" +
"class Foo\n" +
"{\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ public void testThatImportForClassIsSuggestedForAliasImportClass() {
assertEquals("ORM", possibleImportClasses.get("\\Doctrine\\ORM\\Mapping"));
}

public void testThatAlreadyFqnNameMustNotSuggestAnyImport() {
myFixture.copyFileToProject("doctrine.php");

PhpDocTag phpDocTag = PhpPsiElementFactory.createFromText(getProject(), PhpDocTag.class, "<?php\n" +
"/**\n" +
"* @\\ORM\\Entity()\n" +
"*/\n" +
"class Foo {}\n"
);

Map<String, String> possibleImportClasses = AnnotationUtil.getPossibleImportClasses(phpDocTag);
assertEquals(0, possibleImportClasses.size());
}

public void testAttributeVisitingForAnnotationClass() {
myFixture.copyFileToProject("doctrine.php");

Expand Down