-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new inspection for
declare(strict_types=1)
(#47)
- Loading branch information
Showing
12 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/com/vk/kphpstorm/inspections/KphpStrictTypesEnableInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.vk.kphpstorm.inspections | ||
|
||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil | ||
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment | ||
import com.jetbrains.php.lang.inspections.PhpInspection | ||
import com.jetbrains.php.lang.psi.elements.Declare | ||
import com.jetbrains.php.lang.psi.elements.PhpPsiElement | ||
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor | ||
import com.vk.kphpstorm.inspections.quickfixes.AddStrictTypesCommentQuickFix | ||
|
||
class KphpStrictTypesEnableInspection : PhpInspection() { | ||
val kphpTagStrictTypeEnable = "@kphp-strict-types-enable" | ||
|
||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : PhpElementVisitor() { | ||
override fun visitPhpElement(element: PhpPsiElement) { | ||
if (element !is Declare) { | ||
return | ||
} | ||
|
||
if (PhpCodeInsightUtil.getStrictTypeDirectiveValue(element)?.text != "1") { | ||
return | ||
} | ||
|
||
val docComment = element.prevPsiSibling | ||
if (docComment is PhpDocComment) { | ||
if (docComment.getTagElementsByName(kphpTagStrictTypeEnable).isNotEmpty()) { | ||
return | ||
} | ||
} | ||
|
||
holder.registerProblem( | ||
element, | ||
"Missing $kphpTagStrictTypeEnable tag", | ||
ProblemHighlightType.WEAK_WARNING, | ||
AddStrictTypesCommentQuickFix() | ||
) | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/vk/kphpstorm/inspections/quickfixes/AddStrictTypesCommentQuickFix.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.vk.kphpstorm.inspections.quickfixes | ||
|
||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.openapi.project.Project | ||
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes | ||
import com.jetbrains.php.lang.psi.PhpPsiElementFactory | ||
|
||
class AddStrictTypesCommentQuickFix : LocalQuickFix { | ||
override fun getFamilyName() = "Add @kphp-strict-types-enable tag" | ||
|
||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
val declareElement = descriptor.startElement | ||
val docComment = PhpPsiElementFactory.createFromText( | ||
project, PhpDocElementTypes.DOC_COMMENT, | ||
"/** @kphp-strict-types-enable */" | ||
) | ||
|
||
declareElement.parent.addBefore(docComment, declareElement) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/resources/inspectionDescriptions/KphpStrictTypesEnableInspection.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<html lang="en"> | ||
<body> | ||
Reports missing @kphp-strict-types-enable comment above the strict_types=1 declaration in a PHP file. This inspection ensures that the necessary KPHP annotation is in place when strict typing is enabled. | ||
|
||
<p> | ||
In KPHP, it's a best practice to annotate the strict typing feature with a @kphp-strict-types-enable comment. This provides clarity for developers, helping them understand that strict typing is actively enforced in the current file. | ||
</p> | ||
|
||
<p> | ||
For instance, a correct setup would look like: | ||
</p> | ||
<pre><code> | ||
/** @kphp-strict-types-enable */ | ||
declare(strict_types=1); | ||
</code></pre> | ||
|
||
<!— tooltip end —> | ||
|
||
<p>This inspection will highlight the <code>declare(strict_types=1)</code> directive if the corresponding KPHP comment is missing. A quick fix option will be provided to automatically insert the necessary comment.</p> | ||
|
||
</body> | ||
</html> |
3 changes: 3 additions & 0 deletions
3
...ures/kphpdoc_inspection/kphp_strict_type_enable/declare_another_argument.good.fixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
declare(ticks=1); |
3 changes: 3 additions & 0 deletions
3
...doc_inspection/kphp_strict_type_enable/declare_strict_type_another_value.good.fixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
declare(strict_types=0); |
3 changes: 3 additions & 0 deletions
3
...es/kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable.good.fixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
/** @kphp-strict-types-enable */ | ||
declare(strict_types=1); |
2 changes: 2 additions & 0 deletions
2
...es/kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable_miss.fixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
<weak_warning descr="Missing @kphp-strict-types-enable tag">declare(strict_types=1);</weak_warning> |
3 changes: 3 additions & 0 deletions
3
...ixtures/kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable_miss.qf.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
/** @kphp-strict-types-enable */ | ||
declare(strict_types=1); |
4 changes: 4 additions & 0 deletions
4
...phpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable_not_near.fixture.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
/** @kphp-strict-types-enable */ | ||
/** @test-tag*/ | ||
<weak_warning descr="Missing @kphp-strict-types-enable tag">declare(strict_types=1);</weak_warning> |
5 changes: 5 additions & 0 deletions
5
...res/kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable_not_near.qf.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
/** @kphp-strict-types-enable */ | ||
/** @test-tag*/ | ||
/** @kphp-strict-types-enable */ | ||
declare(strict_types=1); |
26 changes: 26 additions & 0 deletions
26
src/test/kotlin/com/vk/kphpstorm/testing/tests/KphpStrictTypeEnableTagTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.vk.kphpstorm.testing.tests | ||
|
||
import com.vk.kphpstorm.inspections.KphpStrictTypesEnableInspection | ||
import com.vk.kphpstorm.testing.infrastructure.InspectionTestBase | ||
|
||
class KphpStrictTypeEnableTagTest : InspectionTestBase(KphpStrictTypesEnableInspection()) { | ||
fun `test strict type with tag`() { | ||
runFixture("kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable.good.fixture.php") | ||
} | ||
|
||
fun `test declare another argument`(){ | ||
runFixture("kphpdoc_inspection/kphp_strict_type_enable/declare_another_argument.good.fixture.php") | ||
} | ||
|
||
fun `test strict type another value`(){ | ||
runFixture("kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_another_value.good.fixture.php") | ||
} | ||
|
||
fun `test strict type without tag`() { | ||
runFixture("kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable_miss.fixture.php") | ||
} | ||
|
||
fun `test strict type tag not near above declare`() { | ||
runFixture("kphpdoc_inspection/kphp_strict_type_enable/declare_strict_type_enable_not_near.fixture.php") | ||
} | ||
} |