IntelliJ PHP自定义检查:如何调用visitPhpFunctionCall?

时间:2018-05-30 11:48:10

标签: php intellij-idea

我正在制作一个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实际上无法使用)。

1 个答案:

答案 0 :(得分:0)

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

我必须使用沙盒 IntelliJ(php.jar)中的C:\Users\212636336\.IntelliJIdea2018.1\system\plugins-sandbox,而不是我用于开发插件的IntelliJ。并且该jar必须位于此沙盒IntelliJ SDK的类路径中,而不是作为&#34;外部库&#34;添加。像我一样。