Skip to content

Commit 2a1c9e0

Browse files
committed
Add reportPossiblyNonexistentStringOffset parameter
1 parent 7b266fc commit 2a1c9e0

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

conf/bleedingEdge.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ parameters:
33
bleedingEdge: true
44
skipCheckGenericClasses!: []
55
stricterFunctionMap: true
6+
7+
reportPossiblyNonexistentStringOffset: true

conf/config.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ parameters:
6060
reportAnyTypeWideningInVarTag: false
6161
reportPossiblyNonexistentGeneralArrayOffset: false
6262
reportPossiblyNonexistentConstantArrayOffset: false
63+
reportPossiblyNonexistentStringOffset: false
6364
checkMissingOverrideMethodAttribute: false
6465
mixinExcludeClasses: []
6566
scanFiles: []
@@ -824,6 +825,7 @@ services:
824825
reportMaybes: %reportMaybes%
825826
reportPossiblyNonexistentGeneralArrayOffset: %reportPossiblyNonexistentGeneralArrayOffset%
826827
reportPossiblyNonexistentConstantArrayOffset: %reportPossiblyNonexistentConstantArrayOffset%
828+
reportPossiblyNonexistentStringOffset: %reportPossiblyNonexistentStringOffset%
827829

828830
-
829831
class: PHPStan\Rules\ClassNameCheck

conf/parametersSchema.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ parametersSchema:
7171
reportAnyTypeWideningInVarTag: bool()
7272
reportPossiblyNonexistentGeneralArrayOffset: bool()
7373
reportPossiblyNonexistentConstantArrayOffset: bool()
74+
reportPossiblyNonexistentStringOffset: bool()
7475
checkMissingOverrideMethodAttribute: bool()
7576
parallel: structure([
7677
jobSize: int(),

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct(
2626
private bool $reportMaybes,
2727
private bool $reportPossiblyNonexistentGeneralArrayOffset,
2828
private bool $reportPossiblyNonexistentConstantArrayOffset,
29+
private bool $reportPossiblyNonexistentStringOffset,
2930
)
3031
{
3132
}
@@ -68,8 +69,9 @@ public function check(
6869

6970
$zeroOrMore = IntegerRangeType::fromInterval(0, null);
7071
if (
71-
$type->isString()->yes()
72-
&& $dimType->isInteger()->yes()
72+
$this->reportPossiblyNonexistentStringOffset
73+
&& $type->isString()->yes()
74+
&& $dimType instanceof IntegerRangeType
7375
&& !$zeroOrMore->isSuperTypeOf($dimType)->yes()
7476
) {
7577
$report = true;

tests/PHPStan/Rules/Arrays/ArrayDestructuringRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function getRule(): Rule
1919

2020
return new ArrayDestructuringRule(
2121
$ruleLevelHelper,
22-
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true, false, false),
22+
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true, false, false, false),
2323
);
2424
}
2525

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class NonexistentOffsetInArrayDimFetchRuleTest extends RuleTestCase
2121

2222
private bool $reportPossiblyNonexistentConstantArrayOffset = false;
2323

24+
private bool $reportPossiblyNonexistentStringOffset = false;
25+
2426
protected function getRule(): Rule
2527
{
2628
$ruleLevelHelper = new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false);
2729

2830
return new NonexistentOffsetInArrayDimFetchRule(
2931
$ruleLevelHelper,
30-
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true, $this->reportPossiblyNonexistentGeneralArrayOffset, $this->reportPossiblyNonexistentConstantArrayOffset),
32+
new NonexistentOffsetInArrayDimFetchCheck($ruleLevelHelper, true, $this->reportPossiblyNonexistentGeneralArrayOffset, $this->reportPossiblyNonexistentConstantArrayOffset, $this->reportPossiblyNonexistentStringOffset),
3133
true,
3234
);
3335
}
@@ -756,6 +758,8 @@ public function testBug2634(): void
756758

757759
public function testBug11946(): void
758760
{
761+
$this->reportPossiblyNonexistentStringOffset = true;
762+
759763
$this->analyse([__DIR__ . '/data/bug-11946.php'], [
760764
[
761765
'Offset -1 does not exist on string.',
@@ -779,23 +783,23 @@ public function testBug11946(): void
779783
],
780784
[
781785
'Offset int<-5, 5> might not exist on string.',
782-
45
786+
45,
783787
],
784788
[
785789
'Offset int<-5, 5> might not exist on numeric-string.',
786-
46
790+
46,
787791
],
788792
[
789793
'Offset int<-5, 5> might not exist on non-empty-string.',
790-
47
794+
47,
791795
],
792796
[
793797
'Offset int<-5, 5> might not exist on non-falsy-string.',
794-
48
798+
48,
795799
],
796800
[
797801
'Offset int<-5, 5> might not exist on string.',
798-
49
802+
49,
799803
],
800804
]);
801805
}

tests/PHPStan/Rules/Arrays/data/bug-11946.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function nonExistentStringOffset(
1515
string $numericS,
1616
string $nonEmpty,
1717
string $nonFalsy,
18-
string $lowerCase,
18+
string $lowerCase
1919
)
2020
{
2121
echo $s[-1];

0 commit comments

Comments
 (0)