diff --git a/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java b/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java index f8ddfb205..004d26937 100644 --- a/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java @@ -7,23 +7,28 @@ import com.intellij.codeInspection.ProblemHighlightType; import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.php.lang.inspections.PhpInspection; +import com.jetbrains.php.lang.psi.elements.Method; +import com.jetbrains.php.lang.psi.elements.MethodReference; +import com.jetbrains.php.lang.psi.elements.PhpClass; import com.jetbrains.php.lang.psi.elements.StringLiteralExpression; import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor; import com.magento.idea.magento2plugin.bundles.InspectionBundle; import com.magento.idea.magento2plugin.inspections.php.fix.PhpModuleNameQuickFix; import com.magento.idea.magento2plugin.inspections.util.GetEditableModuleNameByRootFileUtil; import com.magento.idea.magento2plugin.magento.files.RegistrationPhp; +import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType; import com.magento.idea.magento2plugin.util.magento.IsFileInEditableModuleUtil; import org.jetbrains.annotations.NotNull; public class ModuleDeclarationInRegistrationPhpInspection extends PhpInspection { - @NotNull @Override - public PsiElementVisitor buildVisitor( + public @NotNull PsiElementVisitor buildVisitor( final @NotNull ProblemsHolder problemsHolder, final boolean isOnTheFly ) { @@ -33,19 +38,43 @@ public PsiElementVisitor buildVisitor( public void visitPhpStringLiteralExpression(final StringLiteralExpression expression) { final PsiFile file = expression.getContainingFile(); final String filename = file.getName(); - if (!filename.equals(RegistrationPhp.FILE_NAME)) { - return; + + if (!RegistrationPhp.FILE_NAME.equals(filename)) { + return; + } + final MethodReference callerReference = PsiTreeUtil.getParentOfType( + expression, + MethodReference.class + ); + + if (callerReference == null) { + return; + } + final PsiElement caller = callerReference.resolve(); + + if (!(caller instanceof Method)) { + return; } + final PhpClass callerOwner = ((Method) caller).getContainingClass(); + + if (callerOwner == null + || !FrameworkLibraryType.COMPONENT_REGISTRAR.getType().equals( + callerOwner.getPresentableFQN() + )) { + return; + } + if (!IsFileInEditableModuleUtil.execute(file)) { - return; + return; } final String expectedName = GetEditableModuleNameByRootFileUtil.execute(file); final String actualName = expression.getContents(); + if (actualName.equals(expectedName)) { - return; + return; } - final InspectionBundle inspectionBundle = new InspectionBundle(); + problemsHolder.registerProblem( expression, inspectionBundle.message( diff --git a/src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java b/src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java index 8387ba57f..5f60d6ef1 100644 --- a/src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java +++ b/src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java @@ -13,6 +13,7 @@ public enum FrameworkLibraryType { ABSTRACT_COLLECTION( "Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection" ), + COMPONENT_REGISTRAR("Magento\\Framework\\Component\\ComponentRegistrar"), COLLECTION_PROCESSOR("Magento\\Framework\\Api\\SearchCriteria\\CollectionProcessorInterface"), DATA_PERSISTOR("Magento\\Framework\\App\\Request\\DataPersistorInterface"), DATA_OBJECT("Magento\\Framework\\DataObject"), diff --git a/testData/project/magento2/vendor/magento/framework/Component/ComponentRegistrar.php b/testData/project/magento2/vendor/magento/framework/Component/ComponentRegistrar.php new file mode 100644 index 000000000..7fea9a4e9 --- /dev/null +++ b/testData/project/magento2/vendor/magento/framework/Component/ComponentRegistrar.php @@ -0,0 +1,87 @@ + [], + self::LIBRARY => [], + self::LANGUAGE => [], + self::THEME => [], + self::SETUP => [] + ]; + + /** + * Sets the location of a component. + * + * @param string $type component type + * @param string $componentName Fully-qualified component name + * @param string $path Absolute file path to the component + * @throws \LogicException + * @return void + */ + public static function register($type, $componentName, $path) + { + self::validateType($type); + if (isset(self::$paths[$type][$componentName])) { + throw new \LogicException( + ucfirst($type) . ' \'' . $componentName . '\' from \'' . $path . '\' ' + . 'has been already defined in \'' . self::$paths[$type][$componentName] . '\'.' + ); + } + self::$paths[$type][$componentName] = str_replace('\\', '/', $path); + } + + /** + * @inheritdoc + */ + public function getPaths($type) + { + self::validateType($type); + return self::$paths[$type]; + } + + /** + * @inheritdoc + */ + public function getPath($type, $componentName) + { + self::validateType($type); + return self::$paths[$type][$componentName] ?? null; + } + + /** + * Checks if type of component is valid + * + * @param string $type + * @return void + * @throws \LogicException + */ + private static function validateType($type) + { + if (!isset(self::$paths[$type])) { + throw new \LogicException('\'' . $type . '\' is not a valid component type'); + } + } +}