Search in sources :

Example 1 with PhpDocPropertyTag

use of com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag 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 PhpDocPropertyTag

use of com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag 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)

Example 3 with PhpDocPropertyTag

use of com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag in project yii2support by nvlad.

the class DatabaseUtils method getUnusedProperties.

public static ArrayList<PhpDocPropertyTag> getUnusedProperties(String table, List<PhpDocPropertyTag> propertyTags, PhpClass phpClass) {
    ArrayList<PhpDocPropertyTag> unusedProperties = new ArrayList<>();
    ArrayList<String> columns = getColumnsByTable(table, phpClass.getProject());
    for (PhpDocPropertyTag tag : propertyTags) {
        PhpDocProperty property = tag.getProperty();
        if (!isPropertyUsed(property, columns, phpClass))
            unusedProperties.add(tag);
    }
    return unusedProperties;
}
Also used : PhpDocPropertyTag(com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag) ArrayList(java.util.ArrayList) PhpDocProperty(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty)

Aggregations

PhpDocPropertyTag (com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag)3 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 PhpDocProperty (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty)1 PhpElementVisitor (com.jetbrains.php.lang.psi.visitors.PhpElementVisitor)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1