Skip to content

Commit

Permalink
new inspection for declare(strict_types=1) (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio authored Dec 6, 2023
1 parent 89339a7 commit cd0e428
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 0 deletions.
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()
)
}
}
}
}
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)
}
}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
displayName="PHPDoc checks and simplification"
enabledByDefault="false" level="WEAK WARNING"
implementationClass="com.vk.kphpstorm.inspections.KphpDocInspection"/>

<localInspection language="PHP" groupName="KPHPStorm plugin" shortName="KphpStrictTypesEnableInspection"
displayName="Strict type enable checking"
enabledByDefault="false" level="WEAK WARNING"
implementationClass="com.vk.kphpstorm.inspections.KphpStrictTypesEnableInspection"/>

<localInspection language="PHP" groupName="KPHPStorm plugin" shortName="NoTypeDeclarationInspection"
displayName="No type declaration"
enabledByDefault="false" level="ERROR"
Expand Down
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(ticks=1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(strict_types=0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
/** @kphp-strict-types-enable */
declare(strict_types=1);
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
/** @kphp-strict-types-enable */
declare(strict_types=1);
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>
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);
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")
}
}

0 comments on commit cd0e428

Please sign in to comment.