use of com.jetbrains.php.lang.psi.elements.BinaryExpression in project phpinspectionsea by kalessil.
the class SubStrShortHandUsageInspector 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 && substringFunctions.contains(functionName)) {
final PsiElement[] arguments = reference.getParameters();
if ((arguments.length == 3 || arguments.length == 4) && arguments[2] instanceof BinaryExpression) {
/* check if 3rd argument is "strlen($search) - strlen(...)": "strlen($search)" is not needed */
final BinaryExpression binary = (BinaryExpression) arguments[2];
if (binary.getOperationType() == PhpTokenTypes.opMINUS) {
final PsiElement left = binary.getLeftOperand();
final PsiElement right = binary.getRightOperand();
if (right != null && OpenapiTypesUtil.isFunctionReference(left)) {
final FunctionReference leftCall = (FunctionReference) left;
final String leftName = leftCall.getName();
if (leftName != null && lengthFunctions.contains(leftName)) {
final PsiElement[] leftArguments = leftCall.getParameters();
if (leftArguments.length == 1 && OpenapiEquivalenceUtil.areEqual(leftArguments[0], arguments[0])) {
final PsiElement startOffset = arguments[1];
if (OpenapiEquivalenceUtil.areEqual(right, startOffset)) {
/* case: third parameter is not needed at all */
holder.registerProblem(arguments[2], String.format(MessagesPresentationUtil.prefixWithEa(patternDropLength), arguments[2].getText()), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DropThirdParameterFix(holder.getProject(), reference));
} else if (OpenapiTypesUtil.isNumber(startOffset) && OpenapiTypesUtil.isNumber(right)) {
try {
int offset = Integer.parseInt(startOffset.getText()) - Integer.parseInt(right.getText());
if (offset < 0) {
/* case: third parameter can be simplified */
holder.registerProblem(binary, String.format(MessagesPresentationUtil.prefixWithEa(patternSimplifyLength), offset), new SimplifyFix(String.valueOf(offset)));
} else {
/* case: third parameter is not needed at all */
holder.registerProblem(arguments[2], String.format(MessagesPresentationUtil.prefixWithEa(patternDropLength), arguments[2].getText()), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DropThirdParameterFix(holder.getProject(), reference));
}
} catch (final NumberFormatException expected) {
// return;
}
}
}
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.BinaryExpression in project phpinspectionsea by kalessil.
the class ArrayIsListCanBeUsedInspector 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("array_keys") || functionName.equals("array_values"))) {
final boolean isTargetVersion = PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP800);
if (isTargetVersion) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 1) {
final PsiElement context = reference.getParent();
if (context instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) context;
final IElementType operator = binary.getOperationType();
if (OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator)) {
final PsiElement second = OpenapiElementsUtil.getSecondOperand(binary, reference);
if (functionName.equals("array_values")) {
if (second != null && OpenapiEquivalenceUtil.areEqual(arguments[0], second)) {
this.report(reference, operator, arguments[0]);
}
} else if (functionName.equals("array_keys")) {
if (OpenapiTypesUtil.isFunctionReference(second)) {
final FunctionReference rightReference = (FunctionReference) second;
final String rightFunctionName = rightReference.getName();
final PsiElement[] rightArguments = rightReference.getParameters();
if ((rightFunctionName != null && rightFunctionName.equals("range")) && rightArguments.length == 2) {
final boolean rangeFromZero = OpenapiTypesUtil.isNumber(rightArguments[0]) && rightArguments[0].getText().equals("0");
if (rangeFromZero && rightArguments[1] instanceof BinaryExpression) {
final BinaryExpression rangeToBinary = (BinaryExpression) rightArguments[1];
if (rangeToBinary.getOperationType() == PhpTokenTypes.opMINUS) {
final PsiElement right = rangeToBinary.getRightOperand();
final PsiElement left = rangeToBinary.getLeftOperand();
if (OpenapiTypesUtil.isNumber(right) && right.getText().equals("1") && OpenapiTypesUtil.isFunctionReference(left)) {
final FunctionReference leftReference = (FunctionReference) left;
final String leftName = leftReference.getName();
final PsiElement[] leftArguments = leftReference.getParameters();
if (leftName != null && leftName.equals("count") && leftArguments.length == 1) {
if (OpenapiEquivalenceUtil.areEqual(arguments[0], leftArguments[0])) {
this.report(reference, operator, arguments[0]);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
private void report(@NotNull FunctionReference reference, @NotNull IElementType operator, @NotNull PsiElement argument) {
final boolean listExpected = operator == PhpTokenTypes.opIDENTICAL || operator == PhpTokenTypes.opEQUAL;
final String replacement = String.format("%s%sarray_is_list(%s)", listExpected ? "" : "!", reference.getImmediateNamespaceName(), argument.getText());
holder.registerProblem(reference.getParent(), String.format(MessagesPresentationUtil.prefixWithEa(message), replacement), new UseArrayIsListFix(replacement));
}
};
}
use of com.jetbrains.php.lang.psi.elements.BinaryExpression in project phpinspectionsea by kalessil.
the class StrContainsCanBeUsedInspector 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("strpos") || functionName.equals("mb_strpos"))) {
final boolean isTargetVersion = PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP800);
if (isTargetVersion) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 2) {
final PsiElement context = reference.getParent();
if (context instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) context;
final IElementType operation = binary.getOperationType();
final boolean isTarget = (operation == PhpTokenTypes.opNOT_IDENTICAL || operation == PhpTokenTypes.opIDENTICAL) && PhpLanguageUtil.isFalse(OpenapiElementsUtil.getSecondOperand(binary, reference));
if (isTarget) {
final String replacement = String.format("%s%sstr_contains(%s, %s)", operation == PhpTokenTypes.opIDENTICAL ? "! " : "", reference.getImmediateNamespaceName(), arguments[0].getText(), arguments[1].getText());
holder.registerProblem(binary, String.format(MessagesPresentationUtil.prefixWithEa(message), replacement), new UseStrContainsFix(replacement));
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.BinaryExpression in project phpinspectionsea by kalessil.
the class ComparisonOperandsOrderInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder problemsHolder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
final IElementType operator = expression.getOperationType();
if (operator != null && OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator)) {
final PsiElement left = expression.getLeftOperand();
final PsiElement right = expression.getRightOperand();
if (left != null && right != null) {
final boolean isLeftConstant = left instanceof StringLiteralExpression || left instanceof ConstantReference || OpenapiTypesUtil.isNumber(left);
final boolean isRightConstant = right instanceof StringLiteralExpression || right instanceof ConstantReference || OpenapiTypesUtil.isNumber(right);
if (isLeftConstant != isRightConstant) {
final boolean isRegular = ComparisonStyle.isRegular();
if (isRightConstant && !isRegular) {
problemsHolder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(messageUseYoda), new TheLocalFix());
}
if (isLeftConstant && isRegular) {
problemsHolder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(messageUseRegular), new TheLocalFix());
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.BinaryExpression 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