Search in sources :

Example 1 with VirtualProperty

use of com.nvlad.yii2support.common.VirtualProperty in project yii2support by nvlad.

the class MissingPropertiesQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
    List<PhpDocPropertyTag> propertyTags = this.comment.getPropertyTags();
    Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (editor == null)
        return;
    Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
    TemplateManager templateManager = TemplateManager.getInstance(project);
    Template template = templateManager.createTemplate("", "");
    template.setToReformat(true);
    for (VirtualProperty missingProperty : this.missingProperties) {
        String propertyText = "* @property " + (missingProperty.getType() != null ? missingProperty.getType() : "") + " $" + missingProperty.getName();
        if (missingProperty.getComment() != null) {
            propertyText += " " + missingProperty.getComment();
        }
        template.addTextSegment("\n" + propertyText);
    }
    template.addTextSegment("\n");
    int offset = comment.getLastChild().getTextOffset();
    if (propertyTags.size() > 0) {
        PhpDocPropertyTag phpDocPropertyTag = propertyTags.get(comment.getPropertyTags().size() - 1);
        offset = phpDocPropertyTag.getTextOffset() + phpDocPropertyTag.getTextLength();
    }
    editor.getCaretModel().moveToOffset(offset);
    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
    templateManager.startTemplate(editor, template);
}
Also used : PhpDocPropertyTag(com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag) VirtualProperty(com.nvlad.yii2support.common.VirtualProperty) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) Template(com.intellij.codeInsight.template.Template)

Example 2 with VirtualProperty

use of com.nvlad.yii2support.common.VirtualProperty in project yii2support by nvlad.

the class PropertiesInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean isOnTheFly) {
    return new PhpElementVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof PhpDocComment && DatabaseUtils.HasConnections(element.getProject())) {
                PhpDocComment docComment = (PhpDocComment) element;
                PhpIndex index = PhpIndex.getInstance(element.getProject());
                PhpClass phpClass = DatabaseUtils.getClassByClassPhpDoc(docComment);
                if (phpClass != null && ClassUtils.isClassInheritsOrEqual(phpClass, ClassUtils.getClass(index, "\\yii\\db\\BaseActiveRecord"))) {
                    Collection<Field> fields = phpClass.getFields();
                    String table = DatabaseUtils.getTableByActiveRecordClass(phpClass);
                    ArrayList<VirtualProperty> notDeclaredColumns = DatabaseUtils.getNotDeclaredColumns(table, fields, element.getProject());
                    if (notDeclaredColumns.size() > 0) {
                        MissingPropertiesQuickFix qFix = new MissingPropertiesQuickFix(notDeclaredColumns, docComment);
                        String str1 = notDeclaredColumns.size() > 1 ? "properties" : "property";
                        problemsHolder.registerProblem(docComment, "Class " + phpClass.getFQN() + " is missing " + notDeclaredColumns.size() + " " + str1 + " that corresponds to database columns", ProblemHighlightType.WEAK_WARNING, qFix);
                    }
                    ArrayList<PhpDocPropertyTag> unusedProperties = DatabaseUtils.getUnusedProperties(table, docComment.getPropertyTags(), phpClass);
                    if (unusedProperties.size() > 0) {
                        for (PhpDocPropertyTag tag : unusedProperties) {
                            problemsHolder.registerProblem(tag, "Property is unused in class " + phpClass.getFQN(), ProblemHighlightType.LIKE_UNUSED_SYMBOL);
                        }
                    }
                }
            }
            super.visitElement(element);
        }
    };
}
Also used : PhpElementVisitor(com.jetbrains.php.lang.psi.visitors.PhpElementVisitor) VirtualProperty(com.nvlad.yii2support.common.VirtualProperty) PhpIndex(com.jetbrains.php.PhpIndex) PhpDocPropertyTag(com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag) PhpDocComment(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PhpDocPropertyTag (com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag)2 VirtualProperty (com.nvlad.yii2support.common.VirtualProperty)2 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 PsiElement (com.intellij.psi.PsiElement)1 PhpIndex (com.jetbrains.php.PhpIndex)1 PhpDocComment (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment)1 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1