Skip to content

Commit be60a88

Browse files
staabmclxmstaab
authored andcommitted
Add reportPossiblyNonexistentStringOffset parameter
1 parent f9363fd commit be60a88

File tree

7 files changed

+20
-10
lines changed

7 files changed

+20
-10
lines changed

conf/bleedingEdge.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ parameters:
55
skipCheckGenericClasses!: []
66
stricterFunctionMap: true
77
reportPreciseLineForUnusedFunctionParameter: true
8+
reportPossiblyNonexistentStringOffset: true

conf/config.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ parameters:
6363
reportAnyTypeWideningInVarTag: false
6464
reportPossiblyNonexistentGeneralArrayOffset: false
6565
reportPossiblyNonexistentConstantArrayOffset: false
66+
reportPossiblyNonexistentStringOffset: false
6667
checkMissingOverrideMethodAttribute: false
6768
mixinExcludeClasses: []
6869
scanFiles: []
@@ -837,6 +838,7 @@ services:
837838
reportMaybes: %reportMaybes%
838839
reportPossiblyNonexistentGeneralArrayOffset: %reportPossiblyNonexistentGeneralArrayOffset%
839840
reportPossiblyNonexistentConstantArrayOffset: %reportPossiblyNonexistentConstantArrayOffset%
841+
reportPossiblyNonexistentStringOffset: %reportPossiblyNonexistentStringOffset%
840842

841843
-
842844
class: PHPStan\Rules\ClassNameCheck

conf/parametersSchema.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ parametersSchema:
7474
reportAnyTypeWideningInVarTag: bool()
7575
reportPossiblyNonexistentGeneralArrayOffset: bool()
7676
reportPossiblyNonexistentConstantArrayOffset: bool()
77+
reportPossiblyNonexistentStringOffset: bool()
7778
checkMissingOverrideMethodAttribute: bool()
7879
parallel: structure([
7980
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
}
@@ -782,6 +784,8 @@ public function testInternalClassesWithOverloadedOffsetAccessInvalid84(): void
782784

783785
public function testBug11946(): void
784786
{
787+
$this->reportPossiblyNonexistentStringOffset = true;
788+
785789
$this->analyse([__DIR__ . '/data/bug-11946.php'], [
786790
[
787791
'Offset -1 does not exist on string.',
@@ -805,23 +809,23 @@ public function testBug11946(): void
805809
],
806810
[
807811
'Offset int<-5, 5> might not exist on string.',
808-
45
812+
45,
809813
],
810814
[
811815
'Offset int<-5, 5> might not exist on numeric-string.',
812-
46
816+
46,
813817
],
814818
[
815819
'Offset int<-5, 5> might not exist on non-empty-string.',
816-
47
820+
47,
817821
],
818822
[
819823
'Offset int<-5, 5> might not exist on non-falsy-string.',
820-
48
824+
48,
821825
],
822826
[
823827
'Offset int<-5, 5> might not exist on string.',
824-
49
828+
49,
825829
],
826830
]);
827831
}

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)