use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class UnusedParameterInspection 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 < 2 || !(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) {
if (renderParameters[1] instanceof ArrayCreationExpression || renderParameters[1] instanceof FunctionReference) {
String errorUnusedParameters = "This View does not use parameters";
UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix();
problemsHolder.registerProblem(renderParameters[1], errorUnusedParameters, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
if (isOnTheFly) {
problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
}
}
return;
}
final String errorUnusedParameter = "View " + renderParameters[0].getText() + " not use \"%parameter%\" parameter";
final Set<String> unusedParameters = new HashSet<>();
BiConsumer<String, PsiElement> processParameter = (String arrayKey, PsiElement element) -> {
if (!viewParameters.contains(arrayKey)) {
UnusedParameterLocalQuickFix fix = new UnusedParameterLocalQuickFix(arrayKey);
String description = errorUnusedParameter.replace("%parameter%", arrayKey);
problemsHolder.registerProblem(element, description, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
unusedParameters.add(arrayKey);
}
};
if (renderParameters[1] instanceof ArrayCreationExpression) {
for (ArrayHashElement element : ((ArrayCreationExpression) renderParameters[1]).getHashElements()) {
if (element.getKey() != null && element.getKey() instanceof StringLiteralExpression) {
final String arrayKey = ((StringLiteralExpression) element.getKey()).getContents();
processParameter.accept(arrayKey, element);
}
}
}
if (renderParameters[1] instanceof FunctionReference) {
FunctionReference function = ((FunctionReference) renderParameters[1]);
if (function.getName() != null && function.getName().contains("compact")) {
for (PsiElement element : function.getParameters()) {
if (element instanceof StringLiteralExpression) {
String arrayKey = ((StringLiteralExpression) element).getContents();
processParameter.accept(arrayKey, element);
}
}
}
}
if (unusedParameters.size() > 0 && isOnTheFly) {
if (viewParameters.containsAll(unusedParameters)) {
String errorUnusedParameters = "This View does not use parameters";
UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix();
problemsHolder.registerProblem(renderParameters[1], errorUnusedParameters, ProblemHighlightType.LIKE_UNUSED_SYMBOL, fix);
problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
} else {
String errorUnusedParameters = "This View have unused parameters";
UnusedParametersLocalQuickFix fix = new UnusedParametersLocalQuickFix(unusedParameters);
problemsHolder.registerProblem(reference, errorUnusedParameters, ProblemHighlightType.INFORMATION, fix);
}
}
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class UndetectableTableInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpClass(PhpClass clazz) {
PhpIndex index = PhpIndex.getInstance(problemsHolder.getProject());
if (DatabaseUtils.HasConnections(problemsHolder.getProject()) && ClassUtils.isClassInheritsOrEqual(clazz, ClassUtils.getClass(index, "\\yii\\db\\ActiveRecord"))) {
String table = DatabaseUtils.getTableByActiveRecordClass(clazz);
if (table == null) {
problemsHolder.registerProblem(clazz.getFirstChild(), "Can not detect database table for class " + clazz.getFQN(), ProblemHighlightType.WEAK_WARNING);
} else if (!DatabaseUtils.isTableExists(table, problemsHolder.getProject())) {
problemsHolder.registerProblem(clazz.getFirstChild(), "Table '" + table + "' not found in database connections", ProblemHighlightType.WEAK_WARNING);
}
}
super.visitPhpClass(clazz);
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class PropertiesInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof PhpDocComment && DatabaseUtils.HasConnections(element.getProject())) {
PhpDocComment docComment = (PhpDocComment) element;
PhpIndex index = PhpIndex.getInstance(element.getProject());
PhpClass phpClass = DatabaseUtils.getClassByClassPhpDoc(docComment);
if (phpClass != null && ClassUtils.isClassInheritsOrEqual(phpClass, ClassUtils.getClass(index, "\\yii\\db\\BaseActiveRecord"))) {
Collection<Field> fields = phpClass.getFields();
String table = DatabaseUtils.getTableByActiveRecordClass(phpClass);
ArrayList<VirtualProperty> notDeclaredColumns = DatabaseUtils.getNotDeclaredColumns(table, fields, element.getProject());
if (notDeclaredColumns.size() > 0) {
MissingPropertiesQuickFix qFix = new MissingPropertiesQuickFix(notDeclaredColumns, docComment);
String str1 = notDeclaredColumns.size() > 1 ? "properties" : "property";
problemsHolder.registerProblem(docComment, "Class " + phpClass.getFQN() + " is missing " + notDeclaredColumns.size() + " " + str1 + " that corresponds to database columns", ProblemHighlightType.WEAK_WARNING, qFix);
}
ArrayList<PhpDocPropertyTag> unusedProperties = DatabaseUtils.getUnusedProperties(table, docComment.getPropertyTags(), phpClass);
if (unusedProperties.size() > 0) {
for (PhpDocPropertyTag tag : unusedProperties) {
problemsHolder.registerProblem(tag, "Property is unused in class " + phpClass.getFQN(), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
}
}
}
}
super.visitElement(element);
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project yii2support by nvlad.
the class ObjectFactoryMissedFieldInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
return new PhpElementVisitor() {
@Override
public void visitPhpArrayCreationExpression(ArrayCreationExpression expression) {
PsiDirectory dir = expression.getContainingFile().getContainingDirectory();
PhpClass phpClass = ObjectFactoryUtils.findClassByArrayCreation(expression, dir);
if (phpClass != null && !phpClass.getFQN().equals("\\" + phpClass.getName())) {
// Avoid System Classes: \Closure, \ArrayAccess
for (ArrayHashElement elem : expression.getHashElements()) {
PsiElement key = elem.getKey();
if (key != null) {
String keyName = (key instanceof ClassConstantReference || key instanceof ConstantReference) ? ClassUtils.getConstantValue(key) : key.getText();
if (keyName != null) {
keyName = ClassUtils.removeQuotes(keyName);
if (keyName != null && !keyName.equals("class") && !keyName.startsWith("as ") && !keyName.startsWith("on ") && ClassUtils.findWritableField(phpClass, keyName) == null) {
final String descriptionTemplate = "Field '" + keyName + "' not exists in referenced class " + phpClass.getFQN();
problemsHolder.registerProblem(elem, descriptionTemplate);
}
}
}
}
}
super.visitPhpArrayCreationExpression(expression);
}
};
}
use of com.jetbrains.php.lang.psi.visitors.PhpElementVisitor in project idea-php-typo3-plugin by cedricziel.
the class ClassConstantMatcherInspection 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_CONSTANT_REFERENCE).accepts(element)) {
return;
}
Set<String> constants = getDeprecatedClassConstants(element);
ClassConstantReference classConstantReference = (ClassConstantReference) element;
if (constants.contains(classConstantReference.getText())) {
problemsHolder.registerProblem(element, "Deprecated class constant");
}
}
};
}
Aggregations