use of com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil 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.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil in project phpinspectionsea by kalessil.
the class SlowArrayOperationsInLoopInspector 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 && greedyFunctions.contains(functionName)) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length > 1 && !(arguments[0] instanceof ArrayAccessExpression)) {
PsiElement parent = reference.getParent();
if (parent instanceof AssignmentExpression) {
/* false-positives: return/break as last group statement expression */
boolean canLoop = true;
if (OpenapiTypesUtil.isStatementImpl(parent = parent.getParent())) {
final PsiElement grandParent = parent.getParent();
if (grandParent instanceof GroupStatement) {
final PsiElement last = ExpressionSemanticUtil.getLastStatement((GroupStatement) grandParent);
canLoop = !(last instanceof PhpBreak) && !(last instanceof PhpReturn);
}
}
while (canLoop && parent != null && !(parent instanceof PhpFile) && !(parent instanceof Function)) {
if (OpenapiTypesUtil.isLoop(parent)) {
final PsiElement container = ((AssignmentExpression) reference.getParent()).getVariable();
if (container != null) {
for (final PsiElement argument : arguments) {
if (OpenapiEquivalenceUtil.areEqual(container, argument)) {
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messageGreedyPattern), functionName));
return;
}
}
}
}
parent = parent.getParent();
}
}
}
}
}
@Override
public void visitPhpFor(@NotNull For forStatement) {
final Set<FunctionReference> references = new HashSet<>();
Arrays.stream(forStatement.getConditionalExpressions()).forEach(c -> {
if (c instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) c;
Stream.of(binary.getLeftOperand(), binary.getRightOperand()).filter(p -> p instanceof FunctionReference).forEach(p -> references.add((FunctionReference) p));
}
});
references.stream().filter(OpenapiTypesUtil::isFunctionReference).forEach(r -> {
final String functionName = r.getName();
if (functionName != null && slowFunctions.contains(functionName)) {
final BinaryExpression condition = (BinaryExpression) r.getParent();
holder.registerProblem(condition, String.format(MessagesPresentationUtil.prefixWithEa(messageSlowPattern), functionName), ProblemHighlightType.GENERIC_ERROR, new ReduceRepetitiveCallsInForFix(holder.getProject(), forStatement, condition));
}
});
references.clear();
}
};
}
Aggregations