Skip to content

Commit 6607841

Browse files
authored
Merge pull request #898 from bohdan-harniuk/834-module-name-declaration-inspection-flags-valid-code
834: module name declaration inspection flags valid code
2 parents 6061963 + 6745225 commit 6607841

File tree

3 files changed

+124
-7
lines changed

3 files changed

+124
-7
lines changed

src/com/magento/idea/magento2plugin/inspections/php/ModuleDeclarationInRegistrationPhpInspection.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@
77

88
import com.intellij.codeInspection.ProblemHighlightType;
99
import com.intellij.codeInspection.ProblemsHolder;
10+
import com.intellij.psi.PsiElement;
1011
import com.intellij.psi.PsiElementVisitor;
1112
import com.intellij.psi.PsiFile;
13+
import com.intellij.psi.util.PsiTreeUtil;
1214
import com.jetbrains.php.lang.inspections.PhpInspection;
15+
import com.jetbrains.php.lang.psi.elements.Method;
16+
import com.jetbrains.php.lang.psi.elements.MethodReference;
17+
import com.jetbrains.php.lang.psi.elements.PhpClass;
1318
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
1419
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
1520
import com.magento.idea.magento2plugin.bundles.InspectionBundle;
1621
import com.magento.idea.magento2plugin.inspections.php.fix.PhpModuleNameQuickFix;
1722
import com.magento.idea.magento2plugin.inspections.util.GetEditableModuleNameByRootFileUtil;
1823
import com.magento.idea.magento2plugin.magento.files.RegistrationPhp;
24+
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
1925
import com.magento.idea.magento2plugin.util.magento.IsFileInEditableModuleUtil;
2026
import org.jetbrains.annotations.NotNull;
2127

2228
public class ModuleDeclarationInRegistrationPhpInspection extends PhpInspection {
2329

24-
@NotNull
2530
@Override
26-
public PsiElementVisitor buildVisitor(
31+
public @NotNull PsiElementVisitor buildVisitor(
2732
final @NotNull ProblemsHolder problemsHolder,
2833
final boolean isOnTheFly
2934
) {
@@ -33,19 +38,43 @@ public PsiElementVisitor buildVisitor(
3338
public void visitPhpStringLiteralExpression(final StringLiteralExpression expression) {
3439
final PsiFile file = expression.getContainingFile();
3540
final String filename = file.getName();
36-
if (!filename.equals(RegistrationPhp.FILE_NAME)) {
37-
return;
41+
42+
if (!RegistrationPhp.FILE_NAME.equals(filename)) {
43+
return;
44+
}
45+
final MethodReference callerReference = PsiTreeUtil.getParentOfType(
46+
expression,
47+
MethodReference.class
48+
);
49+
50+
if (callerReference == null) {
51+
return;
52+
}
53+
final PsiElement caller = callerReference.resolve();
54+
55+
if (!(caller instanceof Method)) {
56+
return;
3857
}
58+
final PhpClass callerOwner = ((Method) caller).getContainingClass();
59+
60+
if (callerOwner == null
61+
|| !FrameworkLibraryType.COMPONENT_REGISTRAR.getType().equals(
62+
callerOwner.getPresentableFQN()
63+
)) {
64+
return;
65+
}
66+
3967
if (!IsFileInEditableModuleUtil.execute(file)) {
40-
return;
68+
return;
4169
}
4270
final String expectedName = GetEditableModuleNameByRootFileUtil.execute(file);
4371
final String actualName = expression.getContents();
72+
4473
if (actualName.equals(expectedName)) {
45-
return;
74+
return;
4675
}
47-
4876
final InspectionBundle inspectionBundle = new InspectionBundle();
77+
4978
problemsHolder.registerProblem(
5079
expression,
5180
inspectionBundle.message(

src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum FrameworkLibraryType {
1313
ABSTRACT_COLLECTION(
1414
"Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection"
1515
),
16+
COMPONENT_REGISTRAR("Magento\\Framework\\Component\\ComponentRegistrar"),
1617
COLLECTION_PROCESSOR("Magento\\Framework\\Api\\SearchCriteria\\CollectionProcessorInterface"),
1718
DATA_PERSISTOR("Magento\\Framework\\App\\Request\\DataPersistorInterface"),
1819
DATA_OBJECT("Magento\\Framework\\DataObject"),
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Component;
7+
8+
/**
9+
* Provides ability to statically register components.
10+
*
11+
* @api
12+
* @since 100.0.2
13+
*/
14+
class ComponentRegistrar implements ComponentRegistrarInterface
15+
{
16+
/**#@+
17+
* Different types of components
18+
*/
19+
const MODULE = 'module';
20+
const LIBRARY = 'library';
21+
const THEME = 'theme';
22+
const LANGUAGE = 'language';
23+
const SETUP = 'setup';
24+
/**#@- */
25+
26+
/**#@- */
27+
private static $paths = [
28+
self::MODULE => [],
29+
self::LIBRARY => [],
30+
self::LANGUAGE => [],
31+
self::THEME => [],
32+
self::SETUP => []
33+
];
34+
35+
/**
36+
* Sets the location of a component.
37+
*
38+
* @param string $type component type
39+
* @param string $componentName Fully-qualified component name
40+
* @param string $path Absolute file path to the component
41+
* @throws \LogicException
42+
* @return void
43+
*/
44+
public static function register($type, $componentName, $path)
45+
{
46+
self::validateType($type);
47+
if (isset(self::$paths[$type][$componentName])) {
48+
throw new \LogicException(
49+
ucfirst($type) . ' \'' . $componentName . '\' from \'' . $path . '\' '
50+
. 'has been already defined in \'' . self::$paths[$type][$componentName] . '\'.'
51+
);
52+
}
53+
self::$paths[$type][$componentName] = str_replace('\\', '/', $path);
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function getPaths($type)
60+
{
61+
self::validateType($type);
62+
return self::$paths[$type];
63+
}
64+
65+
/**
66+
* @inheritdoc
67+
*/
68+
public function getPath($type, $componentName)
69+
{
70+
self::validateType($type);
71+
return self::$paths[$type][$componentName] ?? null;
72+
}
73+
74+
/**
75+
* Checks if type of component is valid
76+
*
77+
* @param string $type
78+
* @return void
79+
* @throws \LogicException
80+
*/
81+
private static function validateType($type)
82+
{
83+
if (!isset(self::$paths[$type])) {
84+
throw new \LogicException('\'' . $type . '\' is not a valid component type');
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)