use of com.jetbrains.php.lang.psi.elements.NewExpression 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.NewExpression 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));
}
}
}
}
};
}
Aggregations