Skip to content

Fix objectType::getOffsetValueType #4126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 21, 2025
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
8 changes: 4 additions & 4 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1228,14 +1228,14 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic

public function getOffsetValueType(Type $offsetType): Type
{
if (!$this->isExtraOffsetAccessibleClass()->no()) {
return new MixedType();
}

if ($this->isInstanceOf(ArrayAccess::class)->yes()) {
return RecursionGuard::run($this, fn (): Type => $this->getMethod('offsetGet', new OutOfClassScope())->getOnlyVariant()->getReturnType());
}

if (!$this->isExtraOffsetAccessibleClass()->no()) {
return new MixedType();
}

return new ErrorType();
}

Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12125.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace Bug12125;

use ArrayAccess;
use Exception;
use stdClass;

use function PHPStan\Testing\assertType;

/** @var ArrayAccess<array-key, stdClass>|array<array-key, stdClass> $bug */
$bug = [];

assertType('stdClass|null', $bug['key'] ?? null);

interface MyInterface {
/** @return array<string, string> | ArrayAccess<string, string> */
public function getStrings(): array | ArrayAccess;
}

function myFunction(MyInterface $container): string {
$strings = $container->getStrings();
assertType('array<string, string>|ArrayAccess<string, string>', $strings);
assertType('string|null', $strings['test']);
return $strings['test'];
}

function myOtherFunction(MyInterface $container): string {
$strings = $container->getStrings();
assertType('array<string, string>|ArrayAccess<string, string>', $strings);
if (isset($strings['test'])) {
assertType('string', $strings['test']);
return $strings['test'];
} else {
throw new Exception();
}
}
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13144.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug13144;

use ArrayObject;

use function PHPStan\Testing\assertType;

$arr = new ArrayObject(['a' => 1, 'b' => 2]);

assertType('ArrayObject<string, int>', $arr); // correctly inferred as `ArrayObject<string, int>`

$a = $arr['a']; // ok
$b = $arr['b']; // ok

assertType('int|null', $a); // correctly inferred as `int|null`
assertType('int|null', $b); // correctly inferred as `int|null`


['a' => $a, 'b' => $b] = $arr; // ok

assertType('int|null', $a); // incorrectly inferred as `mixed`
assertType('int|null', $b); // incorrectly inferred as `mixed`
71 changes: 71 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9456.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php // lint >= 8.0

namespace Bug9456;

use ArrayAccess;

use function PHPStan\Testing\assertType;

/**
* @template TKey of array-key
* @template TValue of mixed
*
* @implements ArrayAccess<TKey, TValue>
*/
class Collection implements ArrayAccess {
/**
* @param (callable(TValue, TKey): bool)|TValue|string $key
* @param TValue|string|null $operator
* @param TValue|null $value
* @return static<int<0, 1>, static<TKey, TValue>>
*/
public function partition($key, $operator = null, $value = null) {} // @phpstan-ignore-line

/**
* @param TKey $key
* @return TValue
*/
public function offsetGet($key): mixed { return null; } // @phpstan-ignore-line

/**
* @param TKey $key
* @return bool
*/
public function offsetExists($key): bool { return true; }

/**
* @param TKey|null $key
* @param TValue $value
* @return void
*/
public function offsetSet($key, $value): void {}

/**
* @param TKey $key
* @return void
*/
public function offsetUnset($key): void {}
}

class HelloWorld
{
/**
* @param Collection<int, string> $collection
*/
public function sayHello(Collection $collection): void
{
$result = $collection->partition('key');

assertType(
'Bug9456\Collection<int<0, 1>, Bug9456\Collection<int, string>>',
$result
);
assertType('Bug9456\Collection<int, string>', $result[0]);
assertType('Bug9456\Collection<int, string>', $result[1]);

[$one, $two] = $collection->partition('key');

assertType('Bug9456\Collection<int, string>', $one);
assertType('Bug9456\Collection<int, string>', $two);
}
}
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9575.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Bug9575;

use SimpleXMLElement;
use function PHPStan\Testing\assertType;

$string = <<<XML
<a>
<foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = new SimpleXMLElement($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}

assertType('(SimpleXMLElement|null)', $xml->foo);
assertType('(SimpleXMLElement|null)', $xml->foo[0]);
assertType('(SimpleXMLElement|null)', $xml->foobar);
assertType('(SimpleXMLElement|null)', $xml->foo->attributes());
Loading