use of com.jetbrains.php.lang.psi.elements.ClassReference 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.elements.ClassReference in project phpinspectionsea by kalessil.
the class ThrowRawExceptionInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpThrowExpression(@NotNull PhpThrowExpression expression) {
final PsiElement argument = expression.getArgument();
if (argument instanceof NewExpression) {
final NewExpression newExpression = (NewExpression) argument;
final ClassReference classReference = newExpression.getClassReference();
final String classFqn = classReference == null ? null : classReference.getFQN();
if (classFqn != null) {
if (classFqn.equals("\\Exception")) {
holder.registerProblem(classReference, MessagesPresentationUtil.prefixWithEa(messageRawException), new TheLocalFix());
} else if (REPORT_MISSING_ARGUMENTS && newExpression.getParameters().length == 0) {
final PsiElement resolved = OpenapiResolveUtil.resolveReference(classReference);
if (resolved instanceof PhpClass && this.isTarget((PhpClass) resolved)) {
holder.registerProblem(newExpression, MessagesPresentationUtil.prefixWithEa(messageNoArguments));
}
}
}
}
}
private boolean isTarget(@NotNull PhpClass clazz) {
final Method constructor = clazz.getConstructor();
return constructor != null && constructor.getParameters().length == 3 && clazz.findOwnFieldByName("message", false) == null;
}
};
}
use of com.jetbrains.php.lang.psi.elements.ClassReference in project phpinspectionsea by kalessil.
the class DateIntervalSpecificationInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpNewExpression(@NotNull NewExpression expression) {
/* before inspecting check parameters amount */
final PsiElement[] params = expression.getParameters();
if (params.length == 1) {
final ClassReference classReference = expression.getClassReference();
final String classFQN = classReference == null ? null : classReference.getFQN();
if (classFQN == null || !classFQN.equals("\\DateInterval")) {
/* TODO: child classes support */
return;
}
/* now try getting string literal and test against valid patterns */
final StringLiteralExpression pattern = ExpressionSemanticUtil.resolveAsStringLiteral(params[0]);
if (pattern != null && pattern.getFirstPsiChild() == null) {
final String input = pattern.getContents();
if (!regexRegular.matcher(input).find() && !regexDateTimeAlike.matcher(input).find()) {
holder.registerProblem(pattern, MessagesPresentationUtil.prefixWithEa(message));
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.ClassReference in project phpinspectionsea by kalessil.
the class EfferentObjectCouplingInspector method buildVisitor.
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpClass(@NotNull PhpClass clazz) {
final PsiElement nameIdentifier = NamedElementUtil.getNameIdentifier(clazz);
if (nameIdentifier != null) {
final Set<String> references = PsiTreeUtil.findChildrenOfType(clazz, ClassReference.class).stream().map(ClassReference::getFQN).collect(Collectors.toSet());
final int count = references.size();
if (count >= optionCouplingLimit) {
holder.registerProblem(nameIdentifier, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), count));
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.ClassReference in project phpinspectionsea by kalessil.
the class IsCountableCanBeUsedInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
final String functionName = reference.getName();
if (functionName != null && functionName.equals("is_array")) {
final boolean isTargetVersion = PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP740);
if (isTargetVersion) {
final PsiElement[] arguments = reference.getParameters();
final PsiElement parent = reference.getParent();
if (parent instanceof BinaryExpression && arguments.length == 1) {
final BinaryExpression binary = (BinaryExpression) parent;
final IElementType operation = binary.getOperationType();
if (operation == PhpTokenTypes.opOR) {
/* find the high-level binary expression */
BinaryExpression context = binary;
while (context instanceof BinaryExpression) {
PsiElement up = context.getParent();
while (up instanceof ParenthesizedExpression) {
up = up.getParent();
}
if (up instanceof BinaryExpression && ((BinaryExpression) up).getOperationType() == PhpTokenTypes.opOR) {
context = (BinaryExpression) up;
} else {
break;
}
}
/* check the pattern */
final List<PsiElement> fragments = this.extract(context, PhpTokenTypes.opOR);
if (!fragments.isEmpty()) {
if (fragments.size() > 1) {
for (final PsiElement fragment : fragments) {
if (fragment != reference && fragment instanceof BinaryExpression) {
final BinaryExpression candidate = (BinaryExpression) fragment;
if (candidate.getOperationType() == PhpTokenTypes.kwINSTANCEOF) {
final PsiElement clazz = candidate.getRightOperand();
if (clazz instanceof ClassReference && "Countable".equals(((ClassReference) clazz).getName())) {
final PsiElement subject = candidate.getLeftOperand();
if (subject != null && OpenapiEquivalenceUtil.areEqual(subject, arguments[0])) {
final String argument = subject.getText();
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(message), argument, argument, argument));
break;
}
}
}
}
}
}
fragments.clear();
}
}
}
}
}
}
@NotNull
private List<PsiElement> extract(@NotNull BinaryExpression binary, @Nullable IElementType operator) {
final List<PsiElement> result = new ArrayList<>();
if (binary.getOperationType() == operator) {
Stream.of(binary.getLeftOperand(), binary.getRightOperand()).filter(Objects::nonNull).map(ExpressionSemanticUtil::getExpressionTroughParenthesis).forEach(expression -> {
if (expression instanceof BinaryExpression) {
result.addAll(this.extract((BinaryExpression) expression, operator));
} else {
result.add(expression);
}
});
} else {
result.add(binary);
}
return result;
}
};
}
Aggregations