use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class MissedViewInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitPhpMethodReference(MethodReference reference) {
if (!ViewUtil.isValidRenderMethod(reference)) {
return;
}
if (ArrayUtil.contains(reference.getName(), ViewUtil.renderMethods)) {
if (reference.getParameters().length > 0) {
final PsiElement pathParameter = reference.getParameters()[0];
final ViewResolve resolve = ViewUtil.resolveView(pathParameter);
if (resolve == null) {
return;
}
String key = resolve.key;
if (Files.getFileExtension(key).isEmpty()) {
key = key + '.' + Yii2SupportSettings.getInstance(reference.getProject()).defaultViewExtension;
}
final Project project = reference.getProject();
final Collection<ViewInfo> views = FileBasedIndex.getInstance().getValues(ViewFileIndex.identity, key, GlobalSearchScope.projectScope(project));
final String application = YiiApplicationUtils.getApplicationName(reference.getContainingFile());
final boolean localViewSearch;
final String value = PhpUtil.getValue(pathParameter);
if (resolve.from == ViewResolveFrom.View) {
localViewSearch = !value.startsWith("@") && !value.startsWith("//");
} else {
localViewSearch = false;
}
views.removeIf(view -> {
if (!application.equals(view.application)) {
return true;
}
return localViewSearch && !resolve.theme.equals(view.theme);
});
if (views.size() != 0) {
return;
}
if (pathParameter instanceof StringLiteralExpression) {
Collection<String> paths = ViewUtil.viewResolveToPaths(resolve, project);
if (!paths.iterator().hasNext()) {
return;
}
VirtualFile yiiRoot = YiiApplicationUtils.getYiiRootVirtualFile(project);
if (yiiRoot == null) {
return;
}
int projectUrlLength = project.getBaseDir().getUrl().length();
String yiiRootUrl = yiiRoot.getUrl();
String path;
if (projectUrlLength > yiiRootUrl.length()) {
path = paths.iterator().next();
} else {
path = yiiRootUrl.substring(projectUrlLength) + paths.iterator().next();
}
final String viewNotFoundMessage = "View file for \"" + value + "\" not found in \"" + path + "\".";
final MissedViewLocalQuickFix quickFix = new MissedViewLocalQuickFix(value, path, RenderUtil.getViewArguments(reference));
final PsiElement stringPart = pathParameter.findElementAt(1);
if (stringPart != null) {
problemsHolder.registerProblem(stringPart, viewNotFoundMessage, quickFix);
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class MissedParamInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitPhpMethodReference(MethodReference reference) {
if (reference != null && reference.getParameters().length > 0) {
Method method = (Method) reference.resolve();
if (method == null)
return;
int paramParameterIndex = ClassUtils.getParamIndex(method, "params");
int conditionParameterIndex = ClassUtils.getParamIndex(method, new String[] { "condition", "expression", "sql" });
if (paramParameterIndex > -1 && conditionParameterIndex > -1 && reference.getParameters().length > conditionParameterIndex && conditionParameterIndex == paramParameterIndex - 1) {
PsiElement element = reference.getParameters()[conditionParameterIndex];
String condition = ClassUtils.getStringByElement(element);
String[] conditionParams = DatabaseUtils.extractParamsFromCondition(condition);
String[] conditionParamsWithoutColon = DatabaseUtils.extractParamsFromCondition(condition, false);
if (conditionParams.length > 0) {
if (reference.getParameters().length > paramParameterIndex) {
PsiElement paramParam = reference.getParameters()[paramParameterIndex];
if (paramParam instanceof ArrayCreationExpression) {
ArrayCreationExpression array = (ArrayCreationExpression) paramParam;
ArrayList<String> paramString = new ArrayList<>();
for (ArrayHashElement elem : array.getHashElements()) {
if (elem.getKey() != null && elem.getKey().getText() != null)
paramString.add(ClassUtils.removeQuotes(elem.getKey().getText()).trim());
}
if (!Arrays.equals(paramString.toArray(), conditionParams) && !Arrays.equals(paramString.toArray(), conditionParamsWithoutColon)) {
MissedParamQuickFix qFix = new MissedParamQuickFix(reference);
problemsHolder.registerProblem(reference.getParameters()[paramParameterIndex], "Condition parameters do not conform to the condition", qFix);
}
}
} else {
MissedParamQuickFix qFix = new MissedParamQuickFix(reference);
problemsHolder.registerProblem(reference.getParameters()[conditionParameterIndex], "Condition parameters must be defined", qFix);
}
}
}
}
super.visitPhpMethodReference(reference);
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class MissingActiveRecordInActiveQueryInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitPhpClass(PhpClass clazz) {
if (clazz.getSuperClass() != null && clazz.getSuperClass().getFQN().equals("\\yii\\db\\ActiveQuery")) {
PhpIndex index = PhpIndex.getInstance(clazz.getProject());
PhpClass activeRecordClass = ClassUtils.findClassInSeeTags(index, clazz, "\\yii\\db\\BaseActiveRecord");
if (activeRecordClass == null) {
problemsHolder.registerProblem(clazz.getFirstChild(), "Can not find connected ActiveRecord class.\nYou should add @see tag with linked ActiveRecord", ProblemHighlightType.WEAK_WARNING);
}
}
super.visitPhpClass(clazz);
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project idea-php-typo3-plugin by cedricziel.
the class ClassNameMatcherInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpElement(PhpPsiElement element) {
if (!PlatformPatterns.psiElement(PhpElementTypes.CLASS_REFERENCE).accepts(element)) {
return;
}
Set<String> constants = getDeprecatedClasses(element);
ClassReference classReference = (ClassReference) element;
if (constants.contains(classReference.getFQN())) {
problemsHolder.registerProblem(element, "Class removed with TYPO3 9, consider using an alternative");
}
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project idea-php-typo3-plugin by cedricziel.
the class ConstantMatcherInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpElement(PhpPsiElement element) {
if (!PlatformPatterns.psiElement(PhpElementTypes.CONSTANT_REF).accepts(element)) {
return;
}
ConstantReference constantReference = (ConstantReference) element;
Set<String> constants = getRemovedConstantsFQNs(constantReference);
if (constants.contains(constantReference.getFQN())) {
problemsHolder.registerProblem(element, "Constant removed with TYPO3 9, consider using an alternative");
}
}
};
}
Aggregations