use of com.jetbrains.php.lang.psi.elements.ParenthesizedExpression in project phpinspectionsea by kalessil.
the class SummerTimeUnsafeTimeManipulationInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
if (targetOperations.contains(expression.getOperationType())) {
final PsiElement left = expression.getLeftOperand();
final PsiElement right = expression.getRightOperand();
if (right != null && this.isTargetMagicNumber(right) && this.isTargetContext(right)) {
if (!this.isTestContext(expression)) {
holder.registerProblem(expression, message);
}
} else if (left != null && this.isTargetMagicNumber(left) && this.isTargetContext(left)) {
if (!this.isTestContext(expression)) {
holder.registerProblem(expression, message);
}
}
}
}
@Override
public void visitPhpSelfAssignmentExpression(@NotNull SelfAssignmentExpression expression) {
if (targetAssignments.contains(expression.getOperationType())) {
final PsiElement value = expression.getValue();
if (value != null && this.isTargetMagicNumber(value) && !this.isTestContext(expression)) {
holder.registerProblem(expression, message);
}
}
}
private boolean isTargetContext(@NotNull PsiElement magicNumber) {
boolean result = magicNumber.textMatches("86400");
if (!result) {
PsiElement expression = magicNumber.getParent();
while (expression instanceof ParenthesizedExpression || expression instanceof BinaryExpression) {
expression = expression.getParent();
}
result = PsiTreeUtil.findChildrenOfType(expression, PhpExpression.class).stream().filter(OpenapiTypesUtil::isNumber).anyMatch(candidate -> {
final String text = candidate.getText();
return text.equals("60") || text.endsWith("3600");
});
}
return result;
}
private boolean isTargetMagicNumber(@NotNull PsiElement candidate) {
boolean result = false;
if (OpenapiTypesUtil.isNumber(candidate)) {
result = candidate.textMatches("24") || candidate.textMatches("86400");
}
return result;
}
};
}
use of com.jetbrains.php.lang.psi.elements.ParenthesizedExpression in project phpinspectionsea by kalessil.
the class SuspiciousTernaryOperatorInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpTernaryExpression(@NotNull TernaryExpression expression) {
final PsiElement condition = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getCondition());
final PsiElement trueVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getTrueVariant());
final PsiElement falseVariant = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getFalseVariant());
/* Case 1: identical variants */
if (trueVariant != null && falseVariant != null && OpeanapiEquivalenceUtil.areEqual(trueVariant, falseVariant)) {
holder.registerProblem(expression, messageVariantsIdentical, ProblemHighlightType.GENERIC_ERROR);
}
/* Case 2: operations which might produce a value as not expected */
if (condition instanceof BinaryExpression && !(expression.getCondition() instanceof ParenthesizedExpression)) {
final IElementType operator = ((BinaryExpression) condition).getOperationType();
if (operator != null && !safeOperations.contains(operator)) {
holder.registerProblem(condition, messagePriorities, ProblemHighlightType.GENERIC_ERROR);
}
}
/* Case 3: literal operators priorities issue */
final PsiElement parent = expression.getParent();
if (parent instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) parent;
if (binary.getRightOperand() == expression && PhpTokenTypes.tsLIT_OPS.contains(binary.getOperationType())) {
holder.registerProblem(binary, messagePriorities, ProblemHighlightType.GENERIC_ERROR);
}
}
}
};
}
Aggregations