Skip to content

Commit bba183b

Browse files
authored
fixes #162 Skip "use assert" qf before variable declaration (#178)
* fixes #162 Skip "use assert" qf before variable declaration * Fix changelog release version * Specify fix section
1 parent f5962af commit bba183b

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<!-- Keep a Changelog guide -> https://keepachangelog.com -->
22

33
# PhpClean Changelog
4+
## [2023.02.30]
5+
### Fixed
6+
- #162 Skip "use assert" qf before variable declaration
7+
48
## [2022.08.30]
59
### Changed
610
- #163 Skip "final check" for anonymous classes

src/main/kotlin/com/funivan/idea/phpClean/inspections/virtualTypeCheck/UseAssertQF.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ class UseAssertQF(
3232
}
3333
}
3434
}
35-
}
35+
}

src/main/kotlin/com/funivan/idea/phpClean/inspections/virtualTypeCheck/VirtualTypeCheckInspection.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import com.funivan.idea.phpClean.spl.Pointer
55
import com.intellij.codeInspection.ProblemsHolder
66
import com.intellij.psi.PsiElementVisitor
77
import com.jetbrains.php.PhpIndex
8+
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment
89
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocType
910
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocVariable
1011
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag
12+
import com.jetbrains.php.lang.psi.elements.AssignmentExpression
1113
import com.jetbrains.php.lang.psi.resolve.types.PhpType
1214
import com.jetbrains.php.lang.psi.visitors.PhpElementVisitor
1315

@@ -26,16 +28,26 @@ class VirtualTypeCheckInspection : PhpCleanInspection() {
2628
val plain = type.toStringResolved()
2729
if (!PhpType.isPluralType(plain) && !PhpType.isNotExtendablePrimitiveType(plain)) {
2830
val index = PhpIndex.getInstance(variableType.project)
29-
if (index.getClassesByFQN(plain).isNotEmpty() || index.getInterfacesByFQN(plain).isNotEmpty()) {
30-
holder.registerProblem(
31+
if (index.getClassesByFQN(plain).isNotEmpty() || index.getInterfacesByFQN(plain)
32+
.isNotEmpty()
33+
) {
34+
val nextPsiSibling = (tag.parent as? PhpDocComment)?.nextPsiSibling?.firstChild
35+
if (nextPsiSibling is AssignmentExpression && nextPsiSibling.variable?.name == variable.name) {
36+
holder.registerProblem(
37+
variableType,
38+
"Use assert to check variable type"
39+
)
40+
} else {
41+
holder.registerProblem(
3142
variableType,
3243
"Use assert to check variable type",
3344
UseAssertQF(
34-
Pointer(tag.parent).create(),
35-
Pointer(variable).create(),
36-
Pointer(variableType).create()
45+
Pointer(tag.parent).create(),
46+
Pointer(variable).create(),
47+
Pointer(variableType).create()
3748
)
38-
)
49+
)
50+
}
3951
}
4052
}
4153
}

src/test/kotlin/com/funivan/idea/phpClean/inspections/virtualTypeCheck/VirtualTypeCheckInspectionTest.kt

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ class VirtualTypeCheckInspectionTest : BaseInspectionTest() {
99
assert(
1010
VirtualTypeCheckInspection(),
1111
"""<?php
12-
class User{}
13-
/** @var ${'$'}user <warning descr="Use assert to check variable type">User</warning> */;
14-
assert(${'$'}user instanceof User); // Valid
15-
"""
12+
class User{}
13+
/** @var <warning descr="Use assert to check variable type">User</warning> ${'$'}user */
14+
assert(${'$'}user instanceof User); // Valid
15+
""",
16+
"""<?php
17+
class User{}
18+
19+
assert(${'$'}user instanceof User);
20+
assert(${'$'}user instanceof User); // Valid
21+
""",
1622
)
1723
}
1824

@@ -22,7 +28,7 @@ class VirtualTypeCheckInspectionTest : BaseInspectionTest() {
2228
VirtualTypeCheckInspection(),
2329
"""<?php
2430
interface User{}
25-
/** @var ${'$'}user <warning descr="Use assert to check variable type">User</warning> */;
31+
/** @var <warning descr="Use assert to check variable type">User</warning> ${'$'}user */
2632
assert(${'$'}user instanceof User); // Valid
2733
"""
2834
)
@@ -33,8 +39,8 @@ class VirtualTypeCheckInspectionTest : BaseInspectionTest() {
3339
assert(
3440
VirtualTypeCheckInspection(),
3541
"""<?php
36-
/** @var ${'$'}user[] user */;
37-
/** @var ${'$'}user stdClass[] */;
42+
/** @var ${'$'}user[] user */
43+
/** @var ${'$'}user stdClass[] */
3844
"""
3945
)
4046
}
@@ -44,12 +50,12 @@ class VirtualTypeCheckInspectionTest : BaseInspectionTest() {
4450
assert(
4551
VirtualTypeCheckInspection(),
4652
"""<?php
47-
/** @var ${'$'}user string */;
48-
/** @var ${'$'}user int */;
49-
/** @var ${'$'}user resource */;
50-
/** @var ${'$'}user iterable */;
51-
/** @var ${'$'}user array */;
52-
/** @var ${'$'}user mixed */;
53+
/** @var ${'$'}user string */
54+
/** @var ${'$'}user int */
55+
/** @var ${'$'}user resource */
56+
/** @var ${'$'}user iterable */
57+
/** @var ${'$'}user array */
58+
/** @var ${'$'}user mixed */
5359
"""
5460
)
5561
}
@@ -106,4 +112,20 @@ class Options{};
106112
)
107113
}
108114

115+
@Test
116+
fun testSkipQFOnUninitializedVariable() {
117+
assert(
118+
VirtualTypeCheckInspection(),
119+
"""<?php
120+
class Letter{}
121+
/** @var <warning descr="Use assert to check variable type">Letter</warning> ${'$'}letter */
122+
${'$'}letter = new Letter();
123+
""",
124+
"""<?php
125+
class Letter{}
126+
/** @var Letter ${'$'}letter */
127+
${'$'}letter = new Letter();
128+
"""
129+
)
130+
}
109131
}

0 commit comments

Comments
 (0)