Skip to content

Commit d510a85

Browse files
authored
Merge pull request #159 from funivan/add-prohibited-extension-inspection
Add ProhibitedClassExtend inspection
2 parents 812168c + a6d050c commit d510a85

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ Check if parent property is deprecated.
8787
protected $name; // <-- Warn about deprecation
8888
}
8989
```
90+
#### ProhibitedClassExtend
91+
Classes marked with `@final` doc tag should not be extended
92+
```php
93+
/**
94+
* @final
95+
*/
96+
class User {};
97+
98+
class Admin extends User {}; // <- Prohibited extentions of @final class User.
99+
```
90100
#### PropertyAnnotation
91101
Properties that are not initialized in the constructor should be annotated as nullable.
92102
```php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Classes marked with `@final` doc tag should not be extended
2+
<pre>
3+
/**
4+
* @final
5+
*/
6+
class User {};
7+
8+
class Admin extends User {}; // <- Prohibited extentions of @final class User.
9+
</pre>
10+
<!-- main -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.funivan.idea.phpClean.inspections.classNameCollision
2+
3+
import com.funivan.idea.phpClean.inspections.missingParameterType.ClassesByFqn
4+
import com.funivan.idea.phpClean.inspections.missingParameterType.Implementations
5+
import com.funivan.idea.phpClean.spl.PhpCleanInspection
6+
import com.intellij.codeInspection.ProblemsHolder
7+
import com.intellij.psi.PsiElementVisitor
8+
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment
9+
import com.jetbrains.php.lang.psi.elements.PhpClass
10+
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor
11+
12+
class ProhibitedClassExtendInspection : PhpCleanInspection() {
13+
override fun getShortName() = "ProhibitedClassExtendInspection"
14+
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
15+
16+
return object : PhpElementVisitor() {
17+
private fun parent(phpClass: PhpClass) = ClassesByFqn(phpClass.project, Implementations(phpClass)).all()
18+
private fun hasFinalTag(phpClass: PhpClass): Boolean {
19+
val docComment = phpClass.docComment
20+
return when (docComment is PhpDocComment) {
21+
true -> docComment.getTagElementsByName("@final").isNotEmpty()
22+
false -> false
23+
}
24+
}
25+
26+
override fun visitPhpClass(phpClass: PhpClass) {
27+
phpClass.nameIdentifier?.let { name ->
28+
parent(phpClass).forEach { extendClass ->
29+
if (hasFinalTag(extendClass)) {
30+
holder.registerProblem(
31+
name,
32+
"Prohibited extentions of @final class ${extendClass.fqn}"
33+
)
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@
124124
language="PHP"
125125
implementationClass="com.funivan.idea.phpClean.inspections.parentPropertyDeprecated.ParentPropertyDeprecatedInspection"
126126
/>
127+
<localInspection
128+
groupPath="PHP"
129+
shortName="ProhibitedClassExtendInspection"
130+
displayName="Prohibited class extend"
131+
groupName="PhpClean"
132+
enabledByDefault="true"
133+
level="WARNING"
134+
language="PHP"
135+
implementationClass="com.funivan.idea.phpClean.inspections.prohibitedClassExtend.ProhibitedClassExtendInspection"
136+
/>
127137
<localInspection
128138
groupPath="PHP"
129139
shortName="MissingParameterTypeDeclarationInspection"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.funivan.idea.phpClean.inspections.prohibitedClassExtend
2+
3+
import com.funivan.idea.phpClean.BaseInspectionTest
4+
import com.funivan.idea.phpClean.inspections.classNameCollision.ProhibitedClassExtendInspection
5+
import kotlin.test.Test
6+
7+
internal class ProhibitedClassExtendInspectionTest : BaseInspectionTest() {
8+
@Test
9+
fun testProhibitExtend() {
10+
assert(
11+
ProhibitedClassExtendInspection(),
12+
"""<?php
13+
/**
14+
* @final
15+
*/
16+
class User{}
17+
class <warning descr="Prohibited extentions of @final class \User">Admin</warning> extends User{}
18+
"""
19+
)
20+
}
21+
22+
@Test
23+
fun testIgnoreCases() {
24+
assert(
25+
ProhibitedClassExtendInspection(),
26+
"""<?php
27+
/**
28+
* final
29+
*/
30+
class Fruit{}
31+
class Apple extends Fruit{}
32+
class Id implements Fa{}
33+
"""
34+
)
35+
}
36+
37+
}

0 commit comments

Comments
 (0)