Skip to content

Commit 3b958a5

Browse files
[#1954] InvertedIfElseConstructsInspector: resolved false positives (operands types are not considered)
1 parent 83ae086 commit 3b958a5

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

src/main/java/com/kalessil/phpStorm/phpInspectionsEA/inspectors/ifs/InvertedIfElseConstructsInspector.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
1212
import com.jetbrains.php.lang.psi.PhpPsiElementFactory;
1313
import com.jetbrains.php.lang.psi.elements.*;
14+
import com.jetbrains.php.lang.psi.resolve.types.PhpType;
1415
import com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor;
1516
import com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection;
16-
import com.kalessil.phpStorm.phpInspectionsEA.utils.ExpressionSemanticUtil;
17-
import com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil;
18-
import com.kalessil.phpStorm.phpInspectionsEA.utils.OpenapiTypesUtil;
19-
import com.kalessil.phpStorm.phpInspectionsEA.utils.PhpLanguageUtil;
17+
import com.kalessil.phpStorm.phpInspectionsEA.utils.*;
2018
import org.jetbrains.annotations.NotNull;
2119

2220
/*
@@ -92,11 +90,20 @@ public void visitPhpElse(@NotNull Else elseStatement) {
9290
}
9391
/* if managed to extract condition, then proceed with reporting */
9492
if (extractedCondition != null) {
93+
final Project project = holder.getProject();
94+
/* false-positive: variable return types or failing to resolve types */
95+
if (extractedCondition instanceof PhpTypedElement) {
96+
final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) extractedCondition, project);
97+
if (resolved == null || resolved.size() != 1) {
98+
return;
99+
}
100+
}
101+
95102
final String newCondition = String.format("%s !== %s", left.getText(), right.getText());
96103
holder.registerProblem(
97104
elseStatement.getFirstChild(),
98105
MessagesPresentationUtil.prefixWithEa(message),
99-
new NormalizeWorkflowFix(holder.getProject(), (GroupStatement) ifBody, (GroupStatement) elseBody, extractedCondition, newCondition)
106+
new NormalizeWorkflowFix(project, (GroupStatement) ifBody, (GroupStatement) elseBody, extractedCondition, newCondition)
100107
);
101108
}
102109
}

testData/fixtures/ifs/if-inverted-condition-else-normalization.fixed.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
22

3-
if (expr()) { c(); d(); }
3+
function returnsBool(): bool { return true; }
4+
function returnsNullableBool(): ?bool { return true; }
5+
6+
if (returnsBool()) { c(); d(); }
47
else { a(); b(); }
58

6-
if (expr()) { c(); d(); }
9+
if (returnsBool()) { c(); d(); }
710
else { a(); b(); }
811

912
if (!true)
@@ -19,7 +22,7 @@
1922
if (true === true) { c(); d(); }
2023
else { a(); b(); }
2124

22-
if (false !== expr()) { c(); d(); }
25+
if (false !== returnsBool()) { c(); d(); }
2326
else { a(); b(); }
2427

2528
/* not supported: must follow PSR and use `{}` */
@@ -35,4 +38,9 @@
3538
else {}
3639

3740
if (!empty([])) {}
38-
else {}
41+
else {}
42+
43+
if (false === returnsNullableBool())
44+
{ a(); b(); }
45+
else
46+
{ c(); d(); }

testData/fixtures/ifs/if-inverted-condition-else-normalization.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<?php
22

3-
if (!expr())
3+
function returnsBool(): bool { return true; }
4+
function returnsNullableBool(): ?bool { return true; }
5+
6+
if (!returnsBool())
47
{ a(); b(); }
58
<weak_warning descr="[EA] The if-else workflow is driven by inverted conditions, consider avoiding invertions.">else</weak_warning>
69
{ c(); d(); }
710
8-
if ( ! expr() )
11+
if ( ! returnsBool() )
912
{ a(); b(); }
1013
<weak_warning descr="[EA] The if-else workflow is driven by inverted conditions, consider avoiding invertions.">else</weak_warning>
1114
{ c(); d(); }
@@ -29,7 +32,7 @@
2932
<weak_warning descr="[EA] The if-else workflow is driven by inverted conditions, consider avoiding invertions.">else</weak_warning>
3033
{ c(); d(); }
3134
32-
if (false === expr())
35+
if (false === returnsBool())
3336
{ a(); b(); }
3437
<weak_warning descr="[EA] The if-else workflow is driven by inverted conditions, consider avoiding invertions.">else</weak_warning>
3538
{ c(); d(); }
@@ -47,4 +50,9 @@
4750
else {}
4851

4952
if (!empty([])) {}
50-
else {}
53+
else {}
54+
55+
if (false === returnsNullableBool())
56+
{ a(); b(); }
57+
else
58+
{ c(); d(); }

0 commit comments

Comments
 (0)