use of com.jetbrains.php.lang.psi.elements.UnaryExpression in project phpinspectionsea by kalessil.
the class PlainApiUseCheckStrategy method getPregMatchContext.
private static PsiElement getPregMatchContext(@NotNull FunctionReference reference) {
PsiElement result = reference;
final PsiElement parent = reference.getParent();
if (ExpressionSemanticUtil.isUsedAsLogicalOperand(reference)) {
if (parent instanceof UnaryExpression) {
final UnaryExpression unary = (UnaryExpression) parent;
if (OpenapiTypesUtil.is(unary.getOperation(), PhpTokenTypes.opNOT)) {
result = parent;
}
}
} else if (parent instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) parent;
final IElementType operator = binary.getOperationType();
final boolean isTargetOperator = OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator) || PhpTokenTypes.tsCOMPARE_ORDER_OPS.contains(operator);
if (isTargetOperator) {
final PsiElement second = OpenapiElementsUtil.getSecondOperand(binary, reference);
if (OpenapiTypesUtil.isNumber(second)) {
result = parent;
}
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.UnaryExpression in project phpinspectionsea by kalessil.
the class IssetConstructsCanBeMergedInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
final IElementType operator = expression.getOperationType();
if (operator != null && (operator == PhpTokenTypes.opAND || operator == PhpTokenTypes.opOR)) {
/* false-positives: part of another condition */
final PsiElement parent = expression.getParent();
final PsiElement context = parent instanceof ParenthesizedExpression ? parent.getParent() : parent;
if (context instanceof BinaryExpression && ((BinaryExpression) context).getOperationType() == operator) {
return;
}
final List<PsiElement> fragments = this.extract(expression, operator);
if (fragments.size() > 1) {
if (operator == PhpTokenTypes.opAND) {
/* handle isset && isset ... */
PsiElement firstHit = null;
int hitsCount = 0;
for (final PsiElement fragment : fragments) {
if (fragment instanceof PhpIsset) {
if (++hitsCount > 1 && firstHit != null) {
fragments.remove(firstHit);
fragments.remove(fragment);
holder.registerProblem(fragment, MessagesPresentationUtil.prefixWithEa(messageIsset), new MergeConstructsFix(holder.getProject(), expression, fragments, (PhpIsset) firstHit, (PhpIsset) fragment, operator));
break;
}
firstHit = firstHit == null ? fragment : firstHit;
}
}
} else {
/* handle !isset || !isset ... */
PsiElement firstHit = null;
int hitsCount = 0;
for (final PsiElement fragment : fragments) {
if (fragment instanceof UnaryExpression) {
final PsiElement candidate = ((UnaryExpression) fragment).getValue();
if (candidate instanceof PhpIsset) {
if (++hitsCount > 1 && firstHit != null) {
fragments.remove(firstHit.getParent());
fragments.remove(fragment);
holder.registerProblem(candidate, MessagesPresentationUtil.prefixWithEa(messageIvertedIsset), new MergeConstructsFix(holder.getProject(), expression, fragments, (PhpIsset) firstHit, (PhpIsset) candidate, operator));
break;
}
firstHit = firstHit == null ? candidate : firstHit;
}
}
}
}
}
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;
}
};
}
use of com.jetbrains.php.lang.psi.elements.UnaryExpression in project phpinspectionsea by kalessil.
the class SuspiciousOperatorFormattingStrategy method apply.
public static void apply(@NotNull AssignmentExpression expression, @NotNull ProblemsHolder holder) {
final PhpPsiElement value = expression.getValue();
if (value instanceof UnaryExpression) {
/* previous should be "...=[!+-] ..." */
final PsiElement previous = value.getPrevSibling();
if (null == previous || PhpTokenTypes.opASGN != previous.getNode().getElementType()) {
return;
}
final PsiElement valueOperator = ((UnaryExpression) value).getOperation();
if (null == valueOperator || !(valueOperator.getNextSibling() instanceof PsiWhiteSpace)) {
return;
}
/* analyze statement */
final IElementType valueOperation = valueOperator.getNode().getElementType();
if (mapping.containsKey(valueOperation)) {
holder.registerProblem(expression, MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%o%", mapping.get(valueOperation))));
}
}
}
use of com.jetbrains.php.lang.psi.elements.UnaryExpression in project phpinspectionsea by kalessil.
the class NullableArgumentComparisonStrategy method apply.
public static boolean apply(@NotNull BinaryExpression expression, @NotNull ProblemsHolder holder) {
boolean result = false;
final IElementType operator = expression.getOperationType();
if (mapping.containsKey(operator)) {
PsiElement parent = expression.getParent();
while (parent instanceof ParenthesizedExpression) {
parent = parent.getParent();
}
final PsiElement argument = expression.getLeftOperand();
final PsiElement value = expression.getRightOperand();
if (parent instanceof UnaryExpression && argument instanceof PhpTypedElement && value != null) {
final UnaryExpression target = (UnaryExpression) parent;
if (OpenapiTypesUtil.is(target.getOperation(), PhpTokenTypes.opNOT)) {
final PhpType type = OpenapiResolveUtil.resolveType((PhpTypedElement) argument, holder.getProject());
if (type != null && !type.hasUnknown()) {
final Set<String> types = new HashSet<>();
type.getTypes().forEach(t -> types.add(Types.getType(t)));
if (types.contains(Types.strNull) || types.contains(Types.strBoolean)) {
final String replacement = String.format("%s %s %s", argument.getText(), mapping.get(operator), value.getText());
holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(String.format(messagePattern, replacement)), new NullableArgumentComparisonFix(replacement));
result = true;
}
types.clear();
}
}
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.UnaryExpression in project phpinspectionsea by kalessil.
the class NullCoalescingOperatorCorrectnessStrategy method apply.
public static boolean apply(@NotNull BinaryExpression expression, @NotNull ProblemsHolder holder) {
boolean result = false;
if (expression.getOperationType() == PhpTokenTypes.opCOALESCE) {
final PsiElement left = ExpressionSemanticUtil.getExpressionTroughParenthesis(expression.getLeftOperand());
final PsiElement operation = left instanceof UnaryExpression ? ((UnaryExpression) left).getOperation() : null;
if (operation != null) {
final IElementType operator = operation.getNode().getElementType();
if (result = (operator == PhpTokenTypes.opNOT || PhpTokenTypes.tsCAST_OPS.contains(operator))) {
holder.registerProblem(left, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), left.getText()));
}
}
}
return result;
}
Aggregations