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");
}
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project phpinspectionsea by kalessil.
the class MultipleReturnStatementsInspector method buildVisitor.
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitPhpMethod(@NotNull Method method) {
final PhpClass clazz = method.getContainingClass();
final PsiElement nameIdentifier = NamedElementUtil.getNameIdentifier(method);
if (nameIdentifier != null && clazz != null && !clazz.isTrait()) {
final PhpExitPointInstruction exitPoint = method.getControlFlow().getExitPoint();
int returnsCount = 0;
for (final PhpInstruction instruction : OpenapiElementsUtil.getPredecessors(exitPoint)) {
if (instruction instanceof PhpReturnInstruction) {
++returnsCount;
}
}
if (returnsCount > 1) {
final ProblemHighlightType level = returnsCount > 3 ? ProblemHighlightType.GENERIC_ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
final String message = String.format(messagePattern, returnsCount);
holder.registerProblem(nameIdentifier, message, level);
}
}
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class ViewMissedPhpDocInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpFile(PhpFile PhpFile) {
Project project = PhpFile.getProject();
ViewResolve resolve = ViewUtil.resolveView(PhpFile.getVirtualFile(), project);
if (resolve == null) {
return;
}
Map<String, String> params = getVariables(PhpFile);
Map<String, String> declaredVariables = new HashMap<>();
Collection<PhpDocVariable> variableCollection = PsiTreeUtil.findChildrenOfType(PhpFile, PhpDocVariable.class);
for (PhpDocVariable variable : variableCollection) {
declaredVariables.put(variable.getName(), variable.getType().toString());
}
Map<String, String> missedVariables = new HashMap<>();
for (String variableName : params.keySet()) {
if (!declaredVariables.containsKey(variableName)) {
missedVariables.put(variableName, params.get(variableName));
}
}
if (missedVariables.isEmpty()) {
return;
}
String problemDescription = "Missed View variable declaration.";
ViewMissedPhpDocLocalQuickFix quickFix = new ViewMissedPhpDocLocalQuickFix(PhpFile, missedVariables);
problemsHolder.registerProblem(PhpFile, problemDescription, quickFix);
}
private Map<String, String> getVariables(PhpFile phpFile) {
Collection<PsiReference> references = ReferencesSearch.search(phpFile).findAll();
Map<String, String> result = new HashMap<>();
Map<String, PhpType> viewArgumentCollection = new LinkedHashMap<>();
for (PsiReference reference : references) {
MethodReference methodReference = PsiTreeUtil.getParentOfType(reference.getElement(), MethodReference.class);
if (methodReference == null) {
continue;
}
Map<String, PhpType> params = RenderUtil.getViewArguments(methodReference);
for (Map.Entry<String, PhpType> entry : params.entrySet()) {
if (viewArgumentCollection.containsKey(entry.getKey())) {
PhpType.PhpTypeBuilder typeBuilder = new PhpType.PhpTypeBuilder();
typeBuilder.add(viewArgumentCollection.get(entry.getKey()));
typeBuilder.add(entry.getValue());
viewArgumentCollection.replace(entry.getKey(), typeBuilder.build());
} else {
viewArgumentCollection.put(entry.getKey(), entry.getValue());
}
}
}
for (String key : viewArgumentCollection.keySet()) {
result.put(key, viewArgumentCollection.get(key).toString());
}
return result;
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class RequireParameterInspection 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;
}
final String name = reference.getName();
if (name == null || !ArrayUtil.contains(name, ViewUtil.renderMethods)) {
return;
}
final PsiElement[] renderParameters = reference.getParameters();
if (renderParameters.length == 0 || !(renderParameters[0] instanceof StringLiteralExpression)) {
return;
}
final ViewResolve resolve = ViewUtil.resolveView(renderParameters[0]);
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());
views.removeIf(viewInfo -> !application.equals(viewInfo.application));
if (views.size() == 0) {
return;
}
final Collection<String> viewParameters = new HashSet<>();
for (ViewInfo view : views) {
viewParameters.addAll(view.parameters);
}
if (viewParameters.size() == 0) {
return;
}
final Collection<String> existKeys;
if (renderParameters.length > 1) {
if (renderParameters[1] instanceof ArrayCreationExpression) {
existKeys = PhpUtil.getArrayKeys((ArrayCreationExpression) renderParameters[1]);
} else if (renderParameters[1] instanceof FunctionReference) {
FunctionReference function = (FunctionReference) renderParameters[1];
if (function.getName() != null && function.getName().equals("compact")) {
existKeys = new HashSet<>();
for (PsiElement element : function.getParameters()) {
if (element instanceof StringLiteralExpression) {
existKeys.add(((StringLiteralExpression) element).getContents());
}
}
} else {
return;
}
} else {
return;
}
} else {
existKeys = new HashSet<>();
}
viewParameters.removeIf(existKeys::contains);
if (viewParameters.size() == 0) {
return;
}
String description = "View " + renderParameters[0].getText() + " require ";
final Iterator<String> parameterIterator = viewParameters.iterator();
if (!isOnTheFly) {
while (parameterIterator.hasNext()) {
final String parameter = parameterIterator.next();
final String problemDescription = description + "\"" + parameter + "\" parameter.";
problemsHolder.registerProblem(reference, problemDescription, new RequireParameterLocalQuickFix(parameter));
}
return;
}
final Collection<LocalQuickFix> fixes = new HashSet<>();
if (viewParameters.size() > 1) {
fixes.add(new RequireParameterLocalQuickFix(viewParameters.toArray(new String[0])));
StringBuilder parameterString = new StringBuilder();
String parameter = parameterIterator.next();
while (parameterIterator.hasNext()) {
if (parameterString.length() > 0) {
parameterString.append(", ");
}
parameterString.append("\"").append(parameter).append("\"");
fixes.add(new RequireParameterLocalQuickFix(parameter));
parameter = parameterIterator.next();
}
parameterString.append(" and \"").append(parameter).append("\" parameters.");
description += parameterString.toString();
fixes.add(new RequireParameterLocalQuickFix(parameter));
} else {
String parameter = parameterIterator.next();
description += "\"" + parameter + "\" parameter.";
fixes.add(new RequireParameterLocalQuickFix(parameter));
}
problemsHolder.registerProblem(reference, description, fixes.toArray(new LocalQuickFix[0]));
}
};
}
Aggregations