Я создаю плагин 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
(что является слишком универсальным ИМО для фактического использования).