use of com.jetbrains.php.lang.psi.elements.SelfAssignmentExpression in project phpinspectionsea by kalessil.
the class AdditionOperationOnArraysInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
final PsiElement operation = expression.getOperation();
if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS)) {
/* do not check nested operations */
final boolean isNestedBinary = expression.getParent() instanceof BinaryExpression;
if (!isNestedBinary) {
/* do not report ' ... + []' and '[] + ...' */
final PsiElement right = expression.getRightOperand();
PsiElement left = expression.getLeftOperand();
while (left instanceof BinaryExpression) {
left = ((BinaryExpression) left).getLeftOperand();
}
if (left != null && right != null) {
final boolean addsImplicitArray = left instanceof ArrayCreationExpression || right instanceof ArrayCreationExpression;
if (!addsImplicitArray) {
this.inspectExpression(operation, expression);
}
}
}
}
}
@Override
public void visitPhpSelfAssignmentExpression(@NotNull SelfAssignmentExpression expression) {
final PsiElement operation = expression.getOperation();
if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS_ASGN)) {
/* do not report '... += []' */
final boolean addsImplicitArray = expression.getValue() instanceof ArrayCreationExpression;
if (!addsImplicitArray) {
this.inspectExpression(operation, expression);
}
}
}
/* inspection itself */
private void inspectExpression(@NotNull PsiElement operation, @NotNull PsiElement expression) {
if (expression instanceof PhpTypedElement) {
final Set<String> types = new HashSet<>();
final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) expression, holder.getProject());
if (resolved != null) {
resolved.filterUnknown().getTypes().forEach(t -> types.add(Types.getType(t)));
}
if (types.size() == 1 && types.contains(Types.strArray)) {
holder.registerProblem(operation, message);
}
types.clear();
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.SelfAssignmentExpression 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;
}
};
}
Aggregations