Skip to content

Commit d2227f8

Browse files
Improve wording of the NonexistentOffset, BooleanAndConstantConditionRule and BooleanOrConstantConditionRule
1 parent 948749c commit d2227f8

12 files changed

Lines changed: 776 additions & 22 deletions

‎conf/config.level4.neon‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ services:
4545
class: PHPStan\Rules\Comparison\BooleanAndConstantConditionRule
4646
arguments:
4747
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
48+
bleedingEdge: %featureToggles.bleedingEdge%
4849
tags:
4950
- phpstan.rules.rule
5051

5152
-
5253
class: PHPStan\Rules\Comparison\BooleanOrConstantConditionRule
5354
arguments:
5455
treatPhpDocTypesAsCertain: %treatPhpDocTypesAsCertain%
56+
bleedingEdge: %featureToggles.bleedingEdge%
5557
tags:
5658
- phpstan.rules.rule
5759

‎conf/config.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ services:
905905
class: PHPStan\Rules\Arrays\NonexistentOffsetInArrayDimFetchCheck
906906
arguments:
907907
reportMaybes: %reportMaybes%
908+
bleedingEdge: %featureToggles.bleedingEdge%
908909

909910
-
910911
class: PHPStan\Rules\ClassCaseSensitivityCheck

‎src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
class NonexistentOffsetInArrayDimFetchCheck
2020
{
2121

22-
public function __construct(private RuleLevelHelper $ruleLevelHelper, private bool $reportMaybes)
22+
public function __construct(
23+
private RuleLevelHelper $ruleLevelHelper,
24+
private bool $reportMaybes,
25+
private bool $bleedingEdge,
26+
)
2327
{
2428
}
2529

@@ -44,9 +48,19 @@ public function check(
4448
return $typeResult->getUnknownClassErrors();
4549
}
4650

47-
$report = $type->hasOffsetValueType($dimType)->no();
51+
if ($scope->isInExpressionAssign($var) || $scope->isUndefinedExpressionAllowed($var)) {
52+
return [];
53+
}
54+
55+
if ($type->hasOffsetValueType($dimType)->no()) {
56+
return [
57+
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))->build(),
58+
];
59+
}
60+
61+
if ($this->reportMaybes) {
62+
$report = false;
4863

49-
if (!$report && $this->reportMaybes) {
5064
if ($type instanceof BenevolentUnionType) {
5165
$flattenedTypes = [$type];
5266
} else {
@@ -67,16 +81,17 @@ public function check(
6781
}
6882
}
6983
}
70-
}
7184

72-
if ($report) {
73-
if ($scope->isInExpressionAssign($var) || $scope->isUndefinedExpressionAllowed($var)) {
74-
return [];
85+
if ($report) {
86+
if ($this->bleedingEdge) {
87+
return [
88+
RuleErrorBuilder::message(sprintf('Offset %s might not exist on %s.', $dimType->describe(VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))->build(),
89+
];
90+
}
91+
return [
92+
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))->build(),
93+
];
7594
}
76-
77-
return [
78-
RuleErrorBuilder::message(sprintf('Offset %s does not exist on %s.', $dimType->describe(VerbosityLevel::value()), $type->describe(VerbosityLevel::value())))->build(),
79-
];
8095
}
8196

8297
return [];

‎src/Rules/Comparison/BooleanAndConstantConditionRule.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class BooleanAndConstantConditionRule implements Rule
2020
public function __construct(
2121
private ConstantConditionRuleHelper $helper,
2222
private bool $treatPhpDocTypesAsCertain,
23+
private bool $bleedingEdge,
2324
)
2425
{
2526
}
@@ -36,6 +37,7 @@ public function processNode(
3637
{
3738
$errors = [];
3839
$originalNode = $node->getOriginalNode();
40+
$nodeText = $this->bleedingEdge ? $originalNode->getOperatorSigil() : '&&';
3941
$leftType = $this->helper->getBooleanType($scope, $originalNode->left);
4042
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
4143
if ($leftType instanceof ConstantBooleanType) {
@@ -52,7 +54,8 @@ public function processNode(
5254
return $ruleErrorBuilder->tip($tipText);
5355
};
5456
$errors[] = $addTipLeft(RuleErrorBuilder::message(sprintf(
55-
'Left side of && is always %s.',
57+
'Left side of %s is always %s.',
58+
$nodeText,
5659
$leftType->getValue() ? 'true' : 'false',
5760
)))->line($originalNode->left->getLine())->build();
5861
}
@@ -81,7 +84,8 @@ public function processNode(
8184

8285
if (!$scope->isInFirstLevelStatement()) {
8386
$errors[] = $addTipRight(RuleErrorBuilder::message(sprintf(
84-
'Right side of && is always %s.',
87+
'Right side of %s is always %s.',
88+
$nodeText,
8589
$rightType->getValue() ? 'true' : 'false',
8690
)))->line($originalNode->right->getLine())->build();
8791
}
@@ -105,7 +109,8 @@ public function processNode(
105109

106110
if (!$scope->isInFirstLevelStatement()) {
107111
$errors[] = $addTip(RuleErrorBuilder::message(sprintf(
108-
'Result of && is always %s.',
112+
'Result of %s is always %s.',
113+
$nodeText,
109114
$nodeType->getValue() ? 'true' : 'false',
110115
)))->build();
111116
}

‎src/Rules/Comparison/BooleanOrConstantConditionRule.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class BooleanOrConstantConditionRule implements Rule
2020
public function __construct(
2121
private ConstantConditionRuleHelper $helper,
2222
private bool $treatPhpDocTypesAsCertain,
23+
private bool $bleedingEdge,
2324
)
2425
{
2526
}
@@ -35,6 +36,7 @@ public function processNode(
3536
): array
3637
{
3738
$originalNode = $node->getOriginalNode();
39+
$nodeText = $this->bleedingEdge ? $originalNode->getOperatorSigil() : '||';
3840
$messages = [];
3941
$leftType = $this->helper->getBooleanType($scope, $originalNode->left);
4042
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
@@ -52,7 +54,8 @@ public function processNode(
5254
return $ruleErrorBuilder->tip($tipText);
5355
};
5456
$messages[] = $addTipLeft(RuleErrorBuilder::message(sprintf(
55-
'Left side of || is always %s.',
57+
'Left side of %s is always %s.',
58+
$nodeText,
5659
$leftType->getValue() ? 'true' : 'false',
5760
)))->line($originalNode->left->getLine())->build();
5861
}
@@ -81,7 +84,8 @@ public function processNode(
8184

8285
if (!$scope->isInFirstLevelStatement()) {
8386
$messages[] = $addTipRight(RuleErrorBuilder::message(sprintf(
84-
'Right side of || is always %s.',
87+
'Right side of %s is always %s.',
88+
$nodeText,
8589
$rightType->getValue() ? 'true' : 'false',
8690
)))->line($originalNode->right->getLine())->build();
8791
}
@@ -105,7 +109,8 @@ public function processNode(
105109

106110
if (!$scope->isInFirstLevelStatement()) {
107111
$messages[] = $addTip(RuleErrorBuilder::message(sprintf(
108-
'Result of || is always %s.',
112+
'Result of %s is always %s.',
113+
$nodeText,
109114
$nodeType->getValue() ? 'true' : 'false',
110115
)))->build();
111116
}

‎tests/PHPStan/Levels/data/stringOffsetAccess-7.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[
22
{
3-
"message": "Offset 'foo' does not exist on array|string.",
3+
"message": "Offset 'foo' might not exist on array|string.",
44
"line": 27,
55
"ignorable": true
66
},
77
{
8-
"message": "Offset 12.34 does not exist on array|string.",
8+
"message": "Offset 12.34 might not exist on array|string.",
99
"line": 31,
1010
"ignorable": true
1111
},
@@ -19,4 +19,4 @@
1919
"line": 55,
2020
"ignorable": true
2121
}
22-
]
22+
]

‎tests/PHPStan/Rules/Arrays/ArrayDestructuringRuleTest.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
class ArrayDestructuringRuleTest extends RuleTestCase
1414
{
1515

16+
private bool $bleedingEdge = false;
17+
1618
protected function getRule(): Rule
1719
{
1820
$ruleLevelHelper = new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false, false, true);
1921

2022
return new ArrayDestructuringRule(
2123
$ruleLevelHelper,
22-
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true),
24+
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true, $this->bleedingEdge),
2325
);
2426
}
2527

‎tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php‎

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ class NonexistentOffsetInArrayDimFetchRuleTest extends RuleTestCase
1515

1616
private bool $checkExplicitMixed = false;
1717

18+
private bool $bleedingEdge = false;
19+
1820
protected function getRule(): Rule
1921
{
2022
$ruleLevelHelper = new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, false, true);
2123

2224
return new NonexistentOffsetInArrayDimFetchRule(
2325
$ruleLevelHelper,
24-
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true),
26+
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true, $this->bleedingEdge),
2527
true,
2628
);
2729
}
@@ -170,6 +172,151 @@ public function testRule(): void
170172
]);
171173
}
172174

175+
public function testRuleBleedingEdge(): void
176+
{
177+
$this->bleedingEdge = true;
178+
$this->analyse([__DIR__ . '/data/nonexistent-offset.php'], [
179+
[
180+
'Offset \'b\' does not exist on array{a: stdClass, 0: 2}.',
181+
17,
182+
],
183+
[
184+
'Offset 1 does not exist on array{a: stdClass, 0: 2}.',
185+
18,
186+
],
187+
[
188+
'Offset \'a\' does not exist on array{b: 1}.',
189+
55,
190+
],
191+
[
192+
'Access to offset \'bar\' on an unknown class NonexistentOffset\Bar.',
193+
101,
194+
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
195+
],
196+
[
197+
'Access to an offset on an unknown class NonexistentOffset\Bar.',
198+
102,
199+
'Learn more at https://phpstan.org/user-guide/discovering-symbols',
200+
],
201+
[
202+
'Offset 0 does not exist on array<string, string>.',
203+
111,
204+
],
205+
[
206+
'Offset \'0\' does not exist on array<string, string>.',
207+
112,
208+
],
209+
[
210+
'Offset int does not exist on array<string, string>.',
211+
114,
212+
],
213+
[
214+
'Offset \'test\' does not exist on null.',
215+
126,
216+
],
217+
[
218+
'Cannot access offset 42 on int.',
219+
142,
220+
],
221+
[
222+
'Cannot access offset 42 on float.',
223+
143,
224+
],
225+
[
226+
'Cannot access offset 42 on bool.',
227+
144,
228+
],
229+
[
230+
'Cannot access offset 42 on resource.',
231+
145,
232+
],
233+
[
234+
'Offset \'c\' might not exist on array{c: false}|array{c: true}|array{e: true}.',
235+
171,
236+
],
237+
[
238+
'Offset int might not exist on array{}|array{1: 1, 2: 2}|array{3: 3, 4: 4}.',
239+
190,
240+
],
241+
[
242+
'Offset int might not exist on array{}|array{1: 1, 2: 2}|array{3: 3, 4: 4}.',
243+
193,
244+
],
245+
[
246+
'Offset \'b\' does not exist on array{a: \'blabla\'}.',
247+
225,
248+
],
249+
[
250+
'Offset \'b\' does not exist on array{a: \'blabla\'}.',
251+
228,
252+
],
253+
[
254+
'Offset string does not exist on array<int, mixed>.',
255+
240,
256+
],
257+
[
258+
'Cannot access offset \'a\' on Closure(): void.',
259+
253,
260+
],
261+
[
262+
'Offset string does not exist on array<int, string>.',
263+
308,
264+
],
265+
[
266+
'Offset null does not exist on array<int, string>.',
267+
310,
268+
],
269+
[
270+
'Offset int does not exist on array<string, string>.',
271+
312,
272+
],
273+
[
274+
'Offset \'baz\' might not exist on array{bar: 1, baz?: 2}.',
275+
344,
276+
],
277+
[
278+
'Offset \'foo\' does not exist on ArrayAccess<int, stdClass>.',
279+
411,
280+
],
281+
[
282+
'Cannot access offset \'foo\' on stdClass.',
283+
423,
284+
],
285+
[
286+
'Cannot access offset \'foo\' on true.',
287+
426,
288+
],
289+
[
290+
'Cannot access offset \'foo\' on false.',
291+
429,
292+
],
293+
[
294+
'Cannot access offset \'foo\' on resource.',
295+
433,
296+
],
297+
[
298+
'Cannot access offset \'foo\' on 42.',
299+
436,
300+
],
301+
[
302+
'Cannot access offset \'foo\' on 4.141.',
303+
439,
304+
],
305+
[
306+
'Cannot access offset \'foo\' on array|int.',
307+
443,
308+
],
309+
[
310+
'Offset \'feature_pretty…\' might not exist on array{version: non-falsy-string, commit: string|null, pretty_version: string|null, feature_version: non-falsy-string, feature_pretty_version?: string|null}.',
311+
504,
312+
],
313+
[
314+
"Cannot access offset 'foo' on bool.",
315+
517,
316+
],
317+
]);
318+
}
319+
173320
public function testStrings(): void
174321
{
175322
$this->analyse([__DIR__ . '/data/strings-offset-access.php'], [

0 commit comments

Comments
 (0)