Skip to content

Commit

Permalink
Allow memoisation of ArrayAccess::offsetGet
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Apr 12, 2020
1 parent 5988149 commit 5bc9b09
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ private static function getValueForKey(
return Type::getMixed($inside_loop);
} elseif ($existing_key_type_part instanceof TString) {
$new_base_type_candidate = Type::getString();
} elseif ($existing_key_type_part instanceof Type\Atomic\TNamedObject) {
} elseif ($existing_key_type_part instanceof Type\Atomic\TNamedObject
&& ($has_isset || $has_inverted_isset)
) {
$has_object_array_access = true;
return null;
} elseif (!$existing_key_type_part instanceof Type\Atomic\ObjectLike) {
Expand Down
63 changes: 63 additions & 0 deletions tests/ArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,69 @@ function foo(SimpleXMLElement $s) : SimpleXMLElement {
return $s["a"];
}',
],
'assertOnArrayAccess' => [
'<?php
class A {
public function foo() : void {}
}
class C implements ArrayAccess
{
/**
* @var array
*/
protected $data = [];
/**
* @param string $name
* @return mixed
*/
public function offsetGet($name)
{
return $this->data[$name];
}
/**
* @param string $name
* @param mixed $value
*/
public function offsetSet($name, $value) : void
{
$this->data[$name] = $value;
}
public function __isset(string $name) : bool
{
return isset($this->data[$name]);
}
public function __unset(string $name) : void
{
unset($this->data[$name]);
}
/**
* @psalm-suppress MixedArgument
*/
public function offsetExists($offset) : bool
{
return $this->__isset($offset);
}
/**
* @psalm-suppress MixedArgument
*/
public function offsetUnset($offset) : void
{
$this->__unset($offset);
}
}
$container = new C();
if ($container["a"] instanceof A) {
$container["a"]->foo();
}'
],
];
}

Expand Down

0 comments on commit 5bc9b09

Please sign in to comment.