Search in sources :

Example 56 with PhpType

use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project idea-php-typo3-plugin by cedricziel.

the class CreateInjectorQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
    PhpDocComment docCommentElement = (PhpDocComment) element.getElement().getParent();
    PsiElement owner = docCommentElement.getOwner();
    if (!(owner instanceof Field)) {
        return;
    }
    Field field = (Field) owner;
    final String fieldName = field.getName();
    final PhpClass containingClass = PsiTreeUtil.getParentOfType(element.getElement(), PhpClass.class);
    if (containingClass == null) {
        return;
    }
    PhpType type = field.getType();
    if (type == PhpType.SCALAR) {
        // Scalar type - can't inject those, heh.
        return;
    }
    final String methodName = "inject" + StringUtils.capitalize(fieldName);
    Method injectorFunction = PhpPsiElementFactory.createMethod(project, "public function " + methodName + " (" + type + " $" + fieldName + ") {" + "  $this->" + fieldName + " = $" + fieldName + ";" + "}");
    final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, element.getContainingFile().getVirtualFile()), true);
    if (editor == null) {
        return;
    }
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    final int insertPos = CodeUtil.getMethodInsertPosition(containingClass, methodName);
    if (insertPos == -1) {
        return;
    }
    new WriteCommandAction(project) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            StringBuffer textBuf = new StringBuffer();
            textBuf.append("\n");
            textBuf.append(injectorFunction.getText());
            editor.getDocument().insertString(insertPos, textBuf);
            final int endPos = insertPos + textBuf.length();
            CodeStyleManager.getInstance(project).reformatText(containingClass.getContainingFile(), insertPos, endPos);
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            Method insertedMethod = containingClass.findMethodByName(methodName);
            if (insertedMethod != null) {
                editor.getCaretModel().moveToOffset(insertedMethod.getTextRange().getStartOffset());
                editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
                element.getElement().delete();
            }
        }

        @Override
        public String getGroupID() {
            return "Create Injection Method";
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PhpClass(com.jetbrains.php.lang.psi.elements.PhpClass) Method(com.jetbrains.php.lang.psi.elements.Method) PhpType(com.jetbrains.php.lang.psi.resolve.types.PhpType) Result(com.intellij.openapi.application.Result) Field(com.jetbrains.php.lang.psi.elements.Field) PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)56 PsiElement (com.intellij.psi.PsiElement)46 NotNull (org.jetbrains.annotations.NotNull)41 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)33 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)20 Project (com.intellij.openapi.project.Project)20 HashSet (java.util.HashSet)20 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)16 Nullable (org.jetbrains.annotations.Nullable)16 com.jetbrains.php.lang.psi.elements (com.jetbrains.php.lang.psi.elements)15 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)14 PhpIndex (com.jetbrains.php.PhpIndex)14 com.kalessil.phpStorm.phpInspectionsEA.utils (com.kalessil.phpStorm.phpInspectionsEA.utils)13 IElementType (com.intellij.psi.tree.IElementType)12 Set (java.util.Set)12 PhpTokenTypes (com.jetbrains.php.lang.lexer.PhpTokenTypes)10 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)9 PhpTypedElement (com.jetbrains.php.lang.psi.elements.PhpTypedElement)9 InterfacesExtractUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.hierarhy.InterfacesExtractUtil)8 Collectors (java.util.stream.Collectors)8