Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2768,7 +2768,7 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
}
}

if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
if (($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) && !$type instanceof NeverType) {
$varType = TypeCombinator::intersect(
$varType,
new HasOffsetValueType($dimType, $type),
Expand Down
78 changes: 78 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14279.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types=1);

namespace Bug14279;

use function PHPStan\Testing\assertType;

/**
* @template TElement
* @implements \IteratorAggregate<array-key, TElement>
*/
abstract class Collection implements \IteratorAggregate, \Countable
{
/** @var array<array-key, TElement> */
protected array $elements = [];

/** @param iterable<TElement> $elements */
public function __construct(iterable $elements = [])
{
}

/**
* @param array-key $key
* @return TElement|null
*/
public function get($key)
{
return $this->elements[$key] ?? null;
}

/** @phpstan-impure */
public function count(): int
{
return \count($this->elements);
}

/** @return \Traversable<TElement> */
public function getIterator(): \Traversable
{
yield from $this->elements;
}

public function assignRecursive(array $options): static
{
return $this;
}
}

/**
* @template TElement
* @extends Collection<TElement>
*/
class TestCollection extends Collection
{
}

function test(): void
{
$data = [
null,
0,
'some-string',
];

$collection = (new TestCollection())->assignRecursive($data);

// assertSame($data[0], $collection->get(0)) narrows $data[0] via Identical
// $collection->get(0) returns null (TElement=*NEVER*)
// intersect(null, null) = null - no problem here
assert($data[0] === $collection->get(0));
assertType('null', $data[0]);
assertType("'some-string'", $data[2]);

// assertSame($data[1], $collection->get(1)) narrows $data[1] via Identical
// $collection->get(1) returns null, $data[1] is 0
// intersect(0, null) = *NEVER* - this must not poison the parent array $data
assert($data[1] === $collection->get(1));
assertType("'some-string'", $data[2]);
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-4099-php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function arrayHint(array $arr): void
assertNativeType('mixed', $arr['key']);

if (!array_key_exists('inner', $arr['key'])) {
assertType('*NEVER*', $arr);
assertType('array{key: array{inner: mixed}}', $arr);
assertNativeType('non-empty-array&hasOffset(\'key\')', $arr);
assertType('*NEVER*', $arr['key']);
assertNativeType("array<mixed~'inner', mixed>", $arr['key']);
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/has-offset-type-bug.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class AssignVsNarrow
public function doFoo(array $a)
{
if (is_int($a['a'])) {
assertType('*NEVER*', $a);
assertType('array{a: string}', $a);
}
}

Expand All @@ -144,7 +144,7 @@ public function doBar(array $a, int $i)
public function doFoo2(array $a)
{
if (is_int($a['a'])) {
assertType("non-empty-array<string, string>&hasOffsetValue('a', *NEVER*)", $a);
assertType("array<string, string>", $a);
}
}

Expand Down
Loading