use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class StrStartsWithCanBeUsedInspector 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();
if (operation == PhpTokenTypes.opNOT_IDENTICAL || operation == PhpTokenTypes.opIDENTICAL) {
final PsiElement second = OpenapiElementsUtil.getSecondOperand(binary, reference);
if (second != null && OpenapiTypesUtil.isNumber(second) && second.getText().equals("0")) {
final String replacement = String.format("%s%sstr_starts_with(%s, %s)", operation == PhpTokenTypes.opNOT_IDENTICAL ? "! " : "", reference.getImmediateNamespaceName(), arguments[0].getText(), arguments[1].getText());
holder.registerProblem(binary, String.format(MessagesPresentationUtil.prefixWithEa(message), replacement), new UseStrStartsWithFix(replacement));
}
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class StrTrUsageAsStrReplaceInspector 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("strtr")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 3) {
final StringLiteralExpression search = ExpressionSemanticUtil.resolveAsStringLiteral(arguments[1]);
if (search != null) {
final String content = search.getContents();
if (!content.isEmpty() && content.length() <= 2) {
final boolean isTarget;
if (search.isSingleQuote()) {
isTarget = signleQuoted.matcher(content).matches();
} else {
isTarget = doubleQuoted.matcher(content).matches();
}
if (isTarget) {
final String replacement = String.format("%sstr_replace(%s, %s, %s)", reference.getImmediateNamespaceName(), arguments[1].getText(), arguments[2].getText(), arguments[0].getText());
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), replacement), new UseStringReplaceFix(replacement));
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class AliasFunctionsUsageInspector 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) {
if (relevantAliases.containsKey(functionName) && this.isFromRootNamespace(reference)) {
final PsiElement target = NamedElementUtil.getNameIdentifier(reference);
if (target != null) {
final String original = relevantAliases.get(functionName);
holder.registerProblem(target, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), functionName, original), ProblemHighlightType.LIKE_DEPRECATED, new TheLocalFix(original));
}
} else if (deprecatedAliases.containsKey(functionName) && this.isFromRootNamespace(reference)) {
final PsiElement target = NamedElementUtil.getNameIdentifier(reference);
if (target != null) {
holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(deprecatedAliases.get(functionName)));
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class ArraySearchUsedAsInArrayInspector 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("array_search")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length >= 2) {
if (ExpressionSemanticUtil.isUsedAsLogicalOperand(reference)) {
/* case: used as (boolean) logical operand */
holder.registerProblem(reference, MessagesPresentationUtil.prefixWithEa(messageUseInArray), new TheLocalFix());
} else {
/* case: implicit booleans comparison */
final PsiElement parent = reference.getParent();
if (parent instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) parent;
final IElementType operation = binary.getOperationType();
if (operation == PhpTokenTypes.opIDENTICAL || operation == PhpTokenTypes.opNOT_IDENTICAL) {
final PsiElement secondOperand = OpenapiElementsUtil.getSecondOperand(binary, reference);
if (PhpLanguageUtil.isBoolean(secondOperand)) {
if (PhpLanguageUtil.isTrue(secondOperand)) {
holder.registerProblem(secondOperand, MessagesPresentationUtil.prefixWithEa(messageComparingWithTrue), ProblemHighlightType.GENERIC_ERROR);
} else {
holder.registerProblem(binary, MessagesPresentationUtil.prefixWithEa(messageUseInArray), new TheLocalFix());
}
}
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class GetTypeMissUseInspector 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("gettype")) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 1) {
final PsiElement parent = reference.getParent();
if (parent instanceof BinaryExpression) {
final BinaryExpression expression = (BinaryExpression) parent;
final IElementType operator = expression.getOperationType();
if (OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(operator)) {
final PsiElement candidate = OpenapiElementsUtil.getSecondOperand(expression, reference);
final StringLiteralExpression value = ExpressionSemanticUtil.resolveAsStringLiteral(candidate);
if (value != null) {
final String type = value.getContents();
if (!mapping.containsKey(type)) {
/* edge case: compared string is wrong xD - bug */
if (!type.equals("unknown type") && !type.equals("resource (closed)")) {
holder.registerProblem(value, String.format(MessagesPresentationUtil.prefixWithEa(messageInvalidPattern), type), ProblemHighlightType.GENERIC_ERROR);
}
} else {
final boolean isInverted = operator == PhpTokenTypes.opNOT_EQUAL || operator == PhpTokenTypes.opNOT_IDENTICAL;
final String replacement = String.format("%s%s(%s)", isInverted ? "!" : "", mapping.get(type), arguments[0].getText());
holder.registerProblem(parent, String.format(MessagesPresentationUtil.prefixWithEa(messageUseFunctionPattern), replacement), new UseSuggestedFunctionFix(replacement));
}
}
}
}
}
}
}
};
}
Aggregations