Skip to content

834: module name declaration inspection flags valid code #898

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

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 @@ -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
) {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Component;

/**
* Provides ability to statically register components.
*
* @api
* @since 100.0.2
*/
class ComponentRegistrar implements ComponentRegistrarInterface
{
/**#@+
* Different types of components
*/
const MODULE = 'module';
const LIBRARY = 'library';
const THEME = 'theme';
const LANGUAGE = 'language';
const SETUP = 'setup';
/**#@- */

/**#@- */
private static $paths = [
self::MODULE => [],
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');
}
}
}