use of com.jetbrains.php.lang.psi.elements.UnaryExpression in project phpinspectionsea by kalessil.
the class NestedNotOperatorsInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpUnaryExpression(@NotNull UnaryExpression expression) {
/* process ony not operations */
if (!OpenapiTypesUtil.is(expression.getOperation(), PhpTokenTypes.opNOT)) {
return;
}
/* process only deepest not-operator: get contained expression */
final PsiElement value = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getValue());
if (null == value) {
return;
}
/* if contained expression is also inversion, do nothing -> to not report several times */
if (value instanceof UnaryExpression) {
if (OpenapiTypesUtil.is(((UnaryExpression) value).getOperation(), PhpTokenTypes.opNOT)) {
return;
}
}
/* check nesting level */
PsiElement target = null;
int nestingLevel = 1;
PsiElement parent = expression.getParent();
while (parent instanceof UnaryExpression || parent instanceof ParenthesizedExpression) {
if (!(parent instanceof ParenthesizedExpression)) {
expression = (UnaryExpression) parent;
if (OpenapiTypesUtil.is(expression.getOperation(), PhpTokenTypes.opNOT)) {
++nestingLevel;
target = parent;
}
}
parent = parent.getParent();
}
if (nestingLevel > 1) {
final String subject = String.format(value instanceof BinaryExpression ? "(%s)" : "%s", value.getText());
final String replacement = String.format(nestingLevel % 2 == 0 ? "(bool) %s" : "! %s", subject);
holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, replacement)), nestingLevel % 2 == 0 ? new UseCastingLocalFix(replacement) : new UseSingleNotLocalFix(replacement));
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.UnaryExpression in project phpinspectionsea by kalessil.
the class FilePutContentsMissUseInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
final String functionName = reference.getName();
if (functionName != null && functionName.equals("file_put_contents")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 2) {
/* inner call can be silenced, un-wrap it */
PsiElement innerCandidate = arguments[1];
if (innerCandidate instanceof UnaryExpression) {
final UnaryExpression unary = (UnaryExpression) innerCandidate;
if (OpenapiTypesUtil.is(unary.getOperation(), PhpTokenTypes.opSILENCE)) {
innerCandidate = unary.getValue();
}
}
/* analyze the call */
if (OpenapiTypesUtil.isFunctionReference(innerCandidate)) {
final FunctionReference innerReference = (FunctionReference) innerCandidate;
final String innerName = innerReference.getName();
if (innerName != null && innerName.equals("file_get_contents")) {
final PsiElement[] innerArguments = innerReference.getParameters();
if (innerArguments.length == 1) {
final String replacement = "copy(%s%, %d%)".replace("%s%", innerArguments[0].getText()).replace("%d%", arguments[0].getText());
holder.registerProblem(reference, String.format(messagePattern, replacement), ProblemHighlightType.GENERIC_ERROR, new UseCopyFix(replacement));
}
}
}
}
}
}
};
}
Aggregations