Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ public function getOffsetValueType(Type $offsetType): Type

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
$stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);

if ($stringOffset instanceof ErrorType) {
return $stringOffset;
}

if ($valueType->isLiteralString()->yes()) {
return $this;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ public function getOffsetValueType(Type $offsetType): Type

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
$stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);

if ($stringOffset instanceof ErrorType) {
return $stringOffset;
}

return $this;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ public function getOffsetValueType(Type $offsetType): Type

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
$stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);

if ($stringOffset instanceof ErrorType) {
return $stringOffset;
}

if ($valueType->isNonFalsyString()->yes()) {
return $this;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ public function getOffsetValueType(Type $offsetType): Type

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
$stringOffset = (new StringType())->setOffsetValueType($offsetType, $valueType, $unionValues);

if ($stringOffset instanceof ErrorType) {
return $stringOffset;
}

return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,4 +864,18 @@ public function testBug10997(): void
]);
}

public function testBug11572(): void
{
$this->analyse([__DIR__ . '/data/bug-11572.php'], [
[
'Cannot access an offset on int.',
45,
],
[
'Cannot access an offset on int<3, 4>.',
46,
],
]);
}

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/Arrays/OffsetAccessAssignmentRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,43 @@ public function testBug8015(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-8015.php'], []);
}

public function testBug11572(): void
{
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-11572.php'], [
[
'Cannot assign new offset to string.',
15,
],
[
'Cannot assign new offset to string.',
16,
],
[
'Cannot assign new offset to string.',
17,
],
[
'Cannot assign new offset to string.',
18,
],
[
'Cannot assign new offset to string.',
19,
],
[
'Cannot assign new offset to string.',
20,
],
[
'Cannot assign new offset to string.',
24,
],
[
'Cannot assign new offset to string.',
36,
],
]);
}

}
47 changes: 47 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-11572.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace bug11572;

/**
* @param string $s
* @param numeric-string $numericS
* @param literal-string $literalS
* @param non-empty-string $nonEmptyS
* @param non-falsy-string $nonFalsyS
* @param non-falsy-string&literal-string $intersectedS
*/
function doFoo($s, $numericS, $literalS, $nonEmptyS, $nonFalsyS, $intersectedS): void
{
$s[] = 'foo';
$numericS[] = 'foo';
$literalS[] = 'foo';
$nonEmptyS[] = 'foo';
$nonFalsyS[] = 'foo';
$intersectedS[] = 'foo';
}

$string = returnString() . ' bar';
$string[] = 'foo';

function returnString(): string
{
return 'baz';
}

class X {
const XY ='ABC';

function doFoo() {
$s = X::XY;
$s[] = 'foo';
}
}

/**
* @param int<3,4> $range
*/
function doInt(int $i, $range): void
{
$i[] = 1;
$range[] = 1;
}
Loading