use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project phpinspectionsea by kalessil.
the class SenselessCommaInArrayDefinitionInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpArrayCreationExpression(@NotNull ArrayCreationExpression expression) {
final PsiElement last = expression.getLastChild().getPrevSibling();
final PsiElement candidate = last instanceof PsiWhiteSpace ? last.getPrevSibling() : last;
if (OpenapiTypesUtil.is(candidate, PhpTokenTypes.opCOMMA)) {
holder.registerProblem(candidate, message, new TheLocalFix());
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project phpinspectionsea by kalessil.
the class AdditionOperationOnArraysInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpBinaryExpression(@NotNull BinaryExpression expression) {
final PsiElement operation = expression.getOperation();
if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS)) {
/* do not check nested operations */
final boolean isNestedBinary = expression.getParent() instanceof BinaryExpression;
if (!isNestedBinary) {
/* do not report ' ... + []' and '[] + ...' */
final PsiElement right = expression.getRightOperand();
PsiElement left = expression.getLeftOperand();
while (left instanceof BinaryExpression) {
left = ((BinaryExpression) left).getLeftOperand();
}
if (left != null && right != null) {
final boolean addsImplicitArray = left instanceof ArrayCreationExpression || right instanceof ArrayCreationExpression;
if (!addsImplicitArray) {
this.inspectExpression(operation, expression);
}
}
}
}
}
@Override
public void visitPhpSelfAssignmentExpression(@NotNull SelfAssignmentExpression expression) {
final PsiElement operation = expression.getOperation();
if (OpenapiTypesUtil.is(operation, PhpTokenTypes.opPLUS_ASGN)) {
/* do not report '... += []' */
final boolean addsImplicitArray = expression.getValue() instanceof ArrayCreationExpression;
if (!addsImplicitArray) {
this.inspectExpression(operation, expression);
}
}
}
/* inspection itself */
private void inspectExpression(@NotNull PsiElement operation, @NotNull PsiElement expression) {
if (expression instanceof PhpTypedElement) {
final Set<String> types = new HashSet<>();
final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) expression, holder.getProject());
if (resolved != null) {
resolved.filterUnknown().getTypes().forEach(t -> types.add(Types.getType(t)));
}
if (types.size() == 1 && types.contains(Types.strArray)) {
holder.registerProblem(operation, message);
}
types.clear();
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression in project yii2support by nvlad.
the class PsiUtil method getArrayCreation.
public static ArrayCreationExpression getArrayCreation(PsiElement element) {
int limit = 10;
PsiElement curElement = element;
while (limit > 0) {
if (curElement instanceof ArrayCreationExpression) {
return (ArrayCreationExpression) curElement;
} else {
curElement = curElement.getParent();
}
limit--;
}
return null;
}
use of com.jetbrains.php.lang.psi.elements.ArrayCreationExpression 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.ArrayCreationExpression 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);
}
}
}
}
Aggregations