use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project phpinspectionsea by kalessil.
the class StrtotimeUsageInspector 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 || !functionName.equals("strtotime")) {
return;
}
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 0 || arguments.length > 2) {
return;
}
/* handle case: strtotime("now") -> time() */
if (arguments.length == 1) {
if (arguments[0] instanceof StringLiteralExpression) {
final StringLiteralExpression pattern = (StringLiteralExpression) arguments[0];
if (pattern.getContents().equalsIgnoreCase("now")) {
holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageUseTime), new UseTimeFunctionLocalFix());
}
}
} else /* handle case: strtotime(..., time()) -> date(...) */
if (arguments.length == 2) {
if (OpenapiTypesUtil.isFunctionReference(arguments[1])) {
final FunctionReference candidate = (FunctionReference) arguments[1];
final String candidateName = candidate.getName();
if (candidateName != null && candidateName.equals("time") && this.isFromRootNamespace(candidate)) {
final String replacement = "strtotime(%a%)".replace("%a%", arguments[0].getText());
holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageDropTime), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DropTimeFunctionCallLocalFix(replacement));
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project phpinspectionsea by kalessil.
the class DynamicCallsToScopeIntrospectionInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpFunctionCall(@NotNull FunctionReference reference) {
if (PhpLanguageLevel.get(holder.getProject()).atLeast(PhpLanguageLevel.PHP710)) {
final String functionName = reference.getName();
if (functionName != null) {
/* discover target element */
final PsiElement target;
if (functionName.isEmpty()) {
final PsiElement[] children = reference.getChildren();
target = children.length == 2 ? children[0] : null;
} else if (callbacksPositions.containsKey(functionName)) {
final int callbackPosition = callbacksPositions.get(functionName);
final PsiElement[] arguments = reference.getParameters();
target = arguments.length >= callbackPosition + 1 ? arguments[callbackPosition] : null;
} else {
target = null;
}
/* discover the target function */
if (target != null) {
final StringLiteralExpression literal = ExpressionSemanticUtil.resolveAsStringLiteral(target);
if (literal != null) {
final String raw = PhpStringUtil.unescapeText(literal.getContents(), literal.isSingleQuote());
final String callback = raw.startsWith("\\") ? raw.substring(1) : raw;
if (targetCalls.containsKey(callback)) {
holder.registerProblem(target, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), callback));
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project phpinspectionsea by kalessil.
the class FopenBinaryUnsafeUsageInspector 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 && functionName.equals("fopen")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length >= 2) {
/* verify if mode provided and has no 'b' already */
final StringLiteralExpression mode = ExpressionSemanticUtil.resolveAsStringLiteral(arguments[1]);
final String modeText = mode == null ? null : mode.getContents();
if (modeText != null && !modeText.isEmpty()) {
if (modeText.indexOf('b') != -1) {
final boolean isCorrectlyPlaced = modeText.endsWith("b") || modeText.endsWith("b+");
if (!isCorrectlyPlaced) {
holder.registerProblem(arguments[1], MessagesPresentationUtil.prefixWithEa(messageMisplacedBinaryMode), ProblemHighlightType.GENERIC_ERROR, new TheLocalFix(holder.getProject(), mode));
}
} else if (modeText.indexOf('t') != -1) {
if (ENFORCE_BINARY_MODIFIER_USAGE) {
holder.registerProblem(arguments[1], MessagesPresentationUtil.prefixWithEa(messageReplaceWithBinaryMode), new TheLocalFix(holder.getProject(), mode));
}
} else {
if (ENFORCE_BINARY_MODIFIER_USAGE) {
holder.registerProblem(arguments[1], MessagesPresentationUtil.prefixWithEa(messageUseBinaryMode), new TheLocalFix(holder.getProject(), mode));
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression in project phpinspectionsea by kalessil.
the class CaseInsensitiveStringFunctionsMissUseInspector 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 || !mapping.containsKey(functionName)) {
return;
}
final PsiElement[] arguments = reference.getParameters();
if (arguments.length != 2 && arguments.length != 3) {
return;
}
// resolve second parameter
final StringLiteralExpression pattern = ExpressionSemanticUtil.resolveAsStringLiteral(arguments[1]);
// might be not available - in another file (PhpStorm limitations)
if (pattern == null || pattern.getContainingFile() != arguments[1].getContainingFile()) {
return;
}
final String patternString = pattern.getContents();
if (!StringUtils.isEmpty(patternString) && !patternString.matches(".*\\p{L}.*")) {
final String replacementFunctionName = mapping.get(functionName);
holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messagePattern.replace("%f%", replacementFunctionName)), ProblemHighlightType.WEAK_WARNING, new TheLocalFix(replacementFunctionName));
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.StringLiteralExpression 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());
}
}
}
}
}
};
}
Aggregations