【问题标题】:IntelliJ PHP custom inspection: how to have visitPhpFunctionCall being called?IntelliJ PHP 自定义检查:如何调用 visitPhpFunctionCall?
【发布时间】:2018-05-30 11:48:10
【问题描述】:

我正在制作一个 IntelliJ 插件来为 PHP 语言添加一些检查。在plugin.xml,我已经声明了我的检查:

<extensions defaultExtensionNs="com.intellij">
    <localInspection
            language="PHP"
            groupPath="PHP,Php Inspections (MTA)"
            shortName="UnsafeCallToHeaderInspection"
            displayName="Unsafe call to 'header()' function"
            groupName="Security"
            enabledByDefault="true"
            level="ERROR"
            implementationClass="com.ge.sdc.intellij.mtaplugin.security.php.UnsafeCallToHeaderInspection"/>

</extensions>

<application-components>
    <component>
        <implementation-class>com.ge.sdc.intellij.mtaplugin.MtaApplicationComponent</implementation-class>
    </component>
</application-components>

在我的检查课程中,我扩展了 PhpInspection 并且正在构建一个 PhpElementVisitor

package com.ge.sdc.intellij.mtaplugin.security.php;

import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.jetbrains.php.lang.inspections.PhpInspection;
import com.jetbrains.php.lang.psi.elements.FunctionReference;
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class UnsafeCallToHeaderInspection extends PhpInspection {
    private Logger log = Logger.getInstance(UnsafeCallToHeaderInspection.class);

    @Nullable
    @Override
    public String getStaticDescription() {
        return "Calls to 'header()' function must only use constant strings or safe-known patterns." +
                " Otherwise, this could allow arbitrary data to be passed in HTTP headers, and would then alter the behavior of the browser (or client)." +
                "Ie: Inserting a custom Content-Security-Policy or a custom Content-Type can break several securities and be a breach.";
    }

    @NotNull
    @Override
    public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder problemsHolder, final boolean isOnTheFly) {
        return new PhpElementVisitor() {
            @Override
            public void visitPhpFunctionCall(FunctionReference reference) {
                log.debug("visitPhpFunctionCall called");
                super.visitPhpFunctionCall(reference);
            }

            @Override
            public void visitElement(PsiElement element) {
                log.debug("visitElement called");
                super.visitElement(element);
            }
        };
    }
}

但是当我调试或运行插件时,打开一个像这样的 PHP 文件:

<?php
header($headerName);

我只看到调试窗口中调用了“visitElement”,没有调用“visitPhpFunctionCall”。

如何让 IntelliJ 调用正确的访问者方法,以便我可以在该访问者方法中操作 FunctionReference 而不是抽象的 PsiElement


到目前为止我已经尝试过:

我查看了php.jar 库,并以PhpSillyAssignmentInspection 为例,但我认为这里的代码没有区别。我还尝试模仿https://github.com/kalessil/phpinspectionsea/,但仍然无法调用visitPhpFunctionCall。最后,我尝试加载 https://github.com/kalessil/phpinspectionsea/ 插件并运行它,但我也没有看到任何检查...

尽管插件似乎正确加载并且检查似乎也被识别,因为当我进入“设置/检查”时,我在“PHP”下看到新添加的检查。但似乎我不能让 IntelliJ 调用 visitPhpFunctionCall 而不是 visitElement (这太通用了 IMO,实际上无法使用)。

【问题讨论】:

    标签: php intellij-idea


    【解决方案1】:

    在再次关注https://intellij-support.jetbrains.com/hc/en-us/community/posts/206752935-com-jetbrains-php-classes-availability 的提示后,我发现我在错误的位置添加了错误的php.jar

    我必须使用 sandboxed IntelliJ (C:\Users\212636336\.IntelliJIdea2018.1\system\plugins-sandbox) 中的 php.jar,而不是我用于开发插件的 IntelliJ。并且那个 jar 必须在这个沙盒 IntelliJ SDK 的类路径中,而不是像我一样添加为“外部库”。

    【讨论】:

      猜你喜欢
      • 2020-07-03
      • 2021-12-13
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      相关资源
      最近更新 更多