use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class RandomApiMigrationInspector 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) {
final PhpLanguageLevel php = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
String suggestion = getMapping(php).get(functionName);
if (suggestion != null) {
/* random_int needs 2 parameters always, so check if mt_rand can be suggested */
if (reference.getParameters().length != 2 && suggestion.equals("random_int")) {
if (functionName.equals("rand")) {
suggestion = "mt_rand";
} else {
return;
}
}
final String message = messagePattern.replace("%o%", functionName).replace("%n%", suggestion);
holder.registerProblem(reference, message, new TheLocalFix(suggestion));
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project yii2support by nvlad.
the class RequireParameterLocalQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
ParameterList parameterList = ((MethodReference) descriptor.getPsiElement()).getParameterList();
if (parameterList == null) {
return;
}
PsiElement[] parameters = parameterList.getParameters();
if (parameters.length == 1 || parameters[1] instanceof PsiErrorElement) {
ArrayCreationExpression params = PhpPsiElementFactory.createFromText(project, ArrayCreationExpression.class, "[]");
if (params == null) {
return;
}
if (parameters.length == 1) {
parameterList.add(PhpPsiElementFactory.createComma(project));
} else {
parameterList.deleteChildRange(parameters[1], parameters[1]);
}
parameterList.add(params);
parameters = parameterList.getParameters();
}
TemplateManager templateManager = TemplateManager.getInstance(project);
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor == null) {
return;
}
if (parameters[1] instanceof FunctionReference) {
FunctionReference functionReference = (FunctionReference) parameters[1];
if (functionReference.getName() == null || !functionReference.getName().equals("compact")) {
return;
}
Template template = templateManager.createTemplate("", "");
template.setToReformat(true);
Boolean firstElement = functionReference.getParameters().length == 0;
for (String variable : myVariables) {
template.addTextSegment(firstElement ? "'" : ", '");
String var = "$" + variable.toUpperCase() + "$";
template.addVariable(var, "", "\"" + variable + "\"", true);
template.addVariableSegment(var);
template.addTextSegment("'");
firstElement = false;
}
PsiElement psiElement = functionReference.getParameterList();
if (psiElement != null) {
editor.getCaretModel().moveToOffset(psiElement.getTextRange().getEndOffset());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
templateManager.startTemplate(editor, template);
}
return;
}
if (!(parameters[1] instanceof ArrayCreationExpression)) {
return;
}
ArrayCreationExpression params = (ArrayCreationExpression) parameters[1];
Template template = templateManager.createTemplate("", "");
template.setToReformat(true);
Boolean addComma = params.getHashElements().iterator().hasNext();
Boolean newLined = false;
PsiElement psiElement = params.getLastChild();
while (psiElement instanceof PsiWhiteSpace || psiElement.getText().equals("]")) {
if (psiElement instanceof PsiWhiteSpace && psiElement.getText().contains("\n")) {
newLined = true;
}
psiElement = psiElement.getPrevSibling();
}
if (psiElement.getText().equals(",")) {
addComma = false;
}
for (String variable : myVariables) {
if (addComma) {
template.addTextSegment(",");
}
template.addTextSegment((newLined ? "\n" : " "));
String templateVariable = "$" + variable.toUpperCase() + "$";
template.addVariable(templateVariable, "", "\"$" + variable + "\"", true);
template.addTextSegment("'" + variable + "' => ");
template.addVariableSegment(templateVariable);
addComma = true;
}
template.addTextSegment(newLined ? "," : " ");
editor.getCaretModel().moveToOffset(psiElement.getTextRange().getEndOffset());
PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
templateManager.startTemplate(editor, template);
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project yii2support by nvlad.
the class UnusedParameterLocalQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement item = descriptor.getPsiElement();
PsiElement context = item.getContext();
if (context instanceof ArrayCreationExpression) {
ArrayCreationExpression params = (ArrayCreationExpression) item.getParent();
PsiUtil.deleteArrayElement(item);
if (!params.getHashElements().iterator().hasNext()) {
if (params.getPrevSibling() instanceof PsiWhiteSpace) {
params.getPrevSibling().delete();
}
params.getPrevSibling().delete();
params.delete();
}
}
if (context instanceof ParameterList && context.getParent() instanceof FunctionReference) {
FunctionReference functionReference = (FunctionReference) context.getParent();
if (functionReference.getName() != null && functionReference.getName().equals("compact")) {
PsiUtil.deleteFunctionParam(item);
if (functionReference.getParameters().length == 0) {
PsiUtil.deleteFunctionParam(functionReference);
}
}
}
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class FixedTimeStartWithInspector 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)) {
final PsiElement parent = reference.getParent();
if (parent instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) parent;
final IElementType operator = binary.getOperationType();
if (operator == PhpTokenTypes.opIDENTICAL || operator == PhpTokenTypes.opNOT_IDENTICAL) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 2 && arguments[1] instanceof StringLiteralExpression) {
final PsiElement zeroCandidate = OpenapiElementsUtil.getSecondOperand(binary, reference);
if (zeroCandidate != null && zeroCandidate.getText().equals("0")) {
final StringLiteralExpression literal = (StringLiteralExpression) arguments[1];
if (literal.getFirstPsiChild() == null) {
final String replacement = String.format("%s(%s, %s, %s)", mapping.get(functionName), arguments[0].getText(), arguments[1].getText(), PhpStringUtil.unescapeText(literal.getContents(), literal.isSingleQuote()).length());
holder.registerProblem(reference, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), replacement), new UseFirstCharactersCompareFix(replacement));
}
}
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.FunctionReference in project phpinspectionsea by kalessil.
the class StrEndsWithCanBeUsedInspector 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("substr") || functionName.equals("mb_substr"))) {
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 limitArgument = this.extractLimitArgument(arguments[1]);
final PsiElement second = OpenapiElementsUtil.getSecondOperand(binary, reference);
if (second != null && limitArgument != null && OpenapiEquivalenceUtil.areEqual(second, limitArgument)) {
final String replacement = String.format("%s%sstr_ends_with(%s, %s)", operation == PhpTokenTypes.opNOT_IDENTICAL ? "! " : "", reference.getImmediateNamespaceName(), arguments[0].getText(), second.getText());
holder.registerProblem(binary, String.format(MessagesPresentationUtil.prefixWithEa(message), replacement), new UseStrEndsWithFix(replacement));
}
}
}
}
}
}
}
@Nullable
private PsiElement extractLimitArgument(@Nullable PsiElement expression) {
if (expression instanceof UnaryExpression) {
final UnaryExpression unary = (UnaryExpression) expression;
if (OpenapiTypesUtil.is(unary.getOperation(), PhpTokenTypes.opMINUS)) {
final PsiElement argument = unary.getValue();
if (OpenapiTypesUtil.isFunctionReference(argument)) {
final FunctionReference reference = (FunctionReference) argument;
final String functionName = reference.getName();
if (functionName != null && (functionName.equals("strlen") || functionName.equals("mb_strlen"))) {
final PsiElement[] arguments = reference.getParameters();
if (arguments.length == 1) {
return arguments[0];
}
}
}
}
}
return null;
}
};
}
Aggregations