use of com.jetbrains.php.lang.psi.elements.ParameterList 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.ParameterList 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.ParameterList in project idea-php-typo3-plugin by cedricziel.
the class ControllerActionLookupElement method handleInsert.
@Override
public void handleInsert(InsertionContext context) {
PsiElement elementAt = context.getFile().findElementAt(context.getStartOffset());
if (elementAt != null) {
MethodReference methodReference = PhpLangUtil.getMethodReference(elementAt);
if (methodReference != null) {
ParameterList parameters = methodReference.getParameterList();
PsiElement[] children = parameters.getChildren();
while (parameters.getFirstChild() != null) {
parameters.getFirstChild().delete();
}
String shortActionName = controllerAction.getName().replace("Action", "");
parameters.add(PhpPsiElementFactory.createPhpPsiFromText(context.getProject(), StringLiteralExpression.class, "'" + shortActionName + "'"));
parameters.add(PhpPsiElementFactory.createComma(context.getProject()));
String shortControllerName = controllerAction.getControllerName().replace("Controller", "");
parameters.add(PhpPsiElementFactory.createPhpPsiFromText(context.getProject(), StringLiteralExpression.class, "'" + shortControllerName + "'"));
parameters.add(PhpPsiElementFactory.createComma(context.getProject()));
String extensionName = controllerAction.getExtensionName();
parameters.add(PhpPsiElementFactory.createPhpPsiFromText(context.getProject(), StringLiteralExpression.class, "'" + extensionName + "'"));
}
}
super.handleInsert(context);
}
use of com.jetbrains.php.lang.psi.elements.ParameterList in project phpinspectionsea by kalessil.
the class SubStrUsedAsStrPosInspector 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("substr") && !functionName.equals("mb_substr"))) {
return;
}
final PsiElement[] arguments = reference.getParameters();
if (arguments.length != 3 && arguments.length != 4) {
return;
}
/* checking 2nd and 3rd arguments is not needed/simplified:
* - 2nd re-used as it is (should be a positive number!)
* - 3rd is not important, as we'll rely on parent comparison operand instead
*/
final String index = arguments[1].getText();
if (!OpenapiTypesUtil.isNumber(arguments[1]) || !index.equals("0")) {
return;
}
/* prepare variables, so we could properly process polymorphic pattern */
PsiElement highLevelCall = reference;
PsiElement parentExpression = reference.getParent();
if (parentExpression instanceof ParameterList) {
parentExpression = parentExpression.getParent();
}
/* if the call wrapped with case manipulation, propose to use stripos */
boolean caseManipulated = false;
if (OpenapiTypesUtil.isFunctionReference(parentExpression)) {
final FunctionReference parentCall = (FunctionReference) parentExpression;
final PsiElement[] parentArguments = parentCall.getParameters();
final String parentName = parentCall.getName();
if (parentName != null && parentArguments.length == 1 && (parentName.equals("strtoupper") || parentName.equals("strtolower"))) {
caseManipulated = true;
highLevelCall = parentExpression;
parentExpression = parentExpression.getParent();
}
}
/* check parent expression, to ensure pattern matched */
if (parentExpression instanceof BinaryExpression) {
final BinaryExpression parent = (BinaryExpression) parentExpression;
if (OpenapiTypesUtil.tsCOMPARE_EQUALITY_OPS.contains(parent.getOperationType())) {
/* get second operand */
final PsiElement secondOperand = OpenapiElementsUtil.getSecondOperand(parent, highLevelCall);
final PsiElement operationNode = parent.getOperation();
if (secondOperand != null && operationNode != null) {
final String operator = operationNode.getText();
final boolean isMbFunction = functionName.equals("mb_substr");
final boolean hasEncoding = isMbFunction && 4 == arguments.length;
final String replacement = "%i% %o% %f%(%s%, %p%%e%)".replace("%e%", hasEncoding ? (", " + arguments[3].getText()) : "").replace("%p%", secondOperand.getText()).replace("%s%", arguments[0].getText()).replace("%f%", (isMbFunction ? "mb_" : "") + (caseManipulated ? "stripos" : "strpos")).replace("%o%", operator.length() == 2 ? (operator + '=') : operator).replace("%i%", index);
final String message = messagePattern.replace("%r%", replacement);
holder.registerProblem(parentExpression, message, new UseStringSearchFix(replacement));
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.ParameterList in project idea-php-typo3-plugin by cedricziel.
the class MethodArgumentDroppedMatcherInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpElement(PhpPsiElement element) {
if (!PlatformPatterns.psiElement(PhpElementTypes.METHOD_REFERENCE).accepts(element)) {
return;
}
MethodReference methodReference = (MethodReference) element;
ParameterList parameterList = methodReference.getParameterList();
PhpExpression classReference = methodReference.getClassReference();
if (classReference != null) {
PhpType inferredType = classReference.getInferredType();
String compiledClassMethodKey = inferredType.toString() + "->" + methodReference.getName();
if (ExtensionScannerUtil.classMethodHasDroppedArguments(element.getProject(), compiledClassMethodKey)) {
Integer maximumNumberOfArguments = ExtensionScannerUtil.getMaximumNumberOfArguments(element.getProject(), compiledClassMethodKey);
if (parameterList != null && maximumNumberOfArguments != -1 && parameterList.getParameters().length != maximumNumberOfArguments) {
problemsHolder.registerProblem(element, "Number of arguments changed with TYPO3 9, consider refactoring");
}
}
}
}
};
}
Aggregations