From 9d30b65cc11c005e97febe85b915b098b9e68b77 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 24 Dec 2021 09:56:36 +0200 Subject: [PATCH 1/3] 834: Code refactoring --- .../ModuleDeclarationInRegistrationPhpInspection.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java b/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java index f8ddfb205..943ea3acc 100644 --- a/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java @@ -21,9 +21,8 @@ public class ModuleDeclarationInRegistrationPhpInspection extends PhpInspection { - @NotNull @Override - public PsiElementVisitor buildVisitor( + public @NotNull PsiElementVisitor buildVisitor( final @NotNull ProblemsHolder problemsHolder, final boolean isOnTheFly ) { @@ -33,19 +32,22 @@ 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)) { + + if (!RegistrationPhp.FILE_NAME.equals(filename)) { return; } + if (!IsFileInEditableModuleUtil.execute(file)) { return; } final String expectedName = GetEditableModuleNameByRootFileUtil.execute(file); final String actualName = expression.getContents(); + if (actualName.equals(expectedName)) { return; } - final InspectionBundle inspectionBundle = new InspectionBundle(); + problemsHolder.registerProblem( expression, inspectionBundle.message( From 1d84101ff9d5be6c019c61e2694375a64ca758b5 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 24 Dec 2021 10:25:01 +0200 Subject: [PATCH 2/3] 834: Fixed bug - wrong inspection for module name in registration.php --- ...eclarationInRegistrationPhpInspection.java | 33 +++++++++++++++++-- .../packages/code/FrameworkLibraryType.java | 1 + 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java b/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java index 943ea3acc..004d26937 100644 --- a/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java +++ b/src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java @@ -7,15 +7,21 @@ 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; @@ -34,17 +40,38 @@ public void visitPhpStringLiteralExpression(final StringLiteralExpression expres final String filename = file.getName(); if (!RegistrationPhp.FILE_NAME.equals(filename)) { - return; + 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(); 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"), From 674522574291a877b00498863e60e39d378fd357 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 24 Dec 2021 14:22:03 +0200 Subject: [PATCH 3/3] 834: Added component registrar to the testing data to make it available for the reference resolving --- .../Component/ComponentRegistrar.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 testData/project/magento2/vendor/magento/framework/Component/ComponentRegistrar.php 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'); + } + } +}