|
| 1 | +package com.vk.kphpstorm.intentions |
| 2 | + |
| 3 | +import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction |
| 4 | +import com.intellij.openapi.editor.Editor |
| 5 | +import com.intellij.openapi.project.Project |
| 6 | +import com.intellij.psi.PsiElement |
| 7 | +import com.intellij.psi.search.LocalSearchScope |
| 8 | +import com.intellij.psi.search.searches.ReferencesSearch |
| 9 | +import com.intellij.psi.util.findParentInFile |
| 10 | +import com.jetbrains.php.lang.psi.elements.AssignmentExpression |
| 11 | +import com.jetbrains.php.lang.psi.elements.PhpClass |
| 12 | +import com.vk.kphpstorm.inspections.helpers.PhpDocPsiBuilder |
| 13 | +import com.vk.kphpstorm.kphptags.KphpImmutableClassDocTag |
| 14 | + |
| 15 | +class AddImmutableClassAnnotationIntention : PsiElementBaseIntentionAction() { |
| 16 | + override fun getText(): String = "Add @kphp-immutable-class" |
| 17 | + override fun getFamilyName(): String = getText() |
| 18 | + |
| 19 | + override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean { |
| 20 | + if (!element.isClassNameNode()) { |
| 21 | + return false |
| 22 | + } |
| 23 | + |
| 24 | + val klass = element.parent as PhpClass |
| 25 | + if (klass.isAbstract || klass.isInterface || klass.isTrait || klass.isAnonymous) { |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + val klassDocNode = klass.docComment |
| 30 | + |
| 31 | + // do not suggest if already present |
| 32 | + if (klassDocNode != null && KphpImmutableClassDocTag.existsInDocComment(klassDocNode)) { |
| 33 | + return false |
| 34 | + } |
| 35 | + |
| 36 | + return !isClassLocallyImmutable(klass) |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Simple local class mutability check. If there is any field mutation in class, |
| 41 | + * the class is mutable. The only exception is the class constructor |
| 42 | + */ |
| 43 | + private fun isClassLocallyImmutable(klass: PhpClass): Boolean { |
| 44 | + val searchScope = LocalSearchScope(klass) |
| 45 | + for (field in klass.fields) { |
| 46 | + val hasAnyMutation = ReferencesSearch.search(field, searchScope).any { ref -> |
| 47 | + val element = ref.element |
| 48 | + |
| 49 | + isMutatingOp(element) && !isInClassConstructor(klass, element) |
| 50 | + } |
| 51 | + |
| 52 | + if (hasAnyMutation) { |
| 53 | + return true |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + private fun isMutatingOp(psiElement: PsiElement): Boolean { |
| 61 | + val parent = psiElement.parent |
| 62 | + |
| 63 | + return parent is AssignmentExpression && parent.variable == psiElement |
| 64 | + } |
| 65 | + |
| 66 | + private fun isInClassConstructor(klass: PhpClass, psiElement: PsiElement): Boolean { |
| 67 | + val classConstructor = klass.constructor |
| 68 | + return classConstructor != null && psiElement.findParentInFile { e -> e == classConstructor } != null |
| 69 | + } |
| 70 | + |
| 71 | + override fun invoke(project: Project, editor: Editor?, element: PsiElement) { |
| 72 | + val klass = element.parent as PhpClass |
| 73 | + PhpDocPsiBuilder.addTagToClass(klass, KphpImmutableClassDocTag) |
| 74 | + } |
| 75 | + |
| 76 | + private fun PsiElement.isClassNameNode(): Boolean { |
| 77 | + val klass = this.parent as? PhpClass ?: return false |
| 78 | + return klass.nameIdentifier == this |
| 79 | + } |
| 80 | +} |
0 commit comments