Skip to content

Commit

Permalink
Fix SimpleXMLElement issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 24, 2022
1 parent ee4e929 commit 707f57a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ public function getIterableKeyType(): Type
}
}

if ($this->isInstanceOf(Traversable::class)->yes()) {
if ($this->isInstanceOf(Traversable::class)->yes() && !$this->isExtraOffsetAccessibleClass()->yes()) {
$isTraversable = true;
$tKey = GenericTypeVariableResolver::getType($this, Traversable::class, 'TKey');
if ($tKey !== null) {
Expand Down Expand Up @@ -732,7 +732,7 @@ public function getIterableValueType(): Type
}
}

if ($this->isInstanceOf(Traversable::class)->yes()) {
if ($this->isInstanceOf(Traversable::class)->yes() && !$this->isExtraOffsetAccessibleClass()->yes()) {
$isTraversable = true;
$tValue = GenericTypeVariableResolver::getType($this, Traversable::class, 'TValue');
if ($tValue !== null) {
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-search-type-specifying.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/array-replace.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6889.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/simplexml.php');
}

/**
Expand Down
69 changes: 69 additions & 0 deletions tests/PHPStan/Analyser/data/simplexml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace SimpleXMLIteratorBug;

use SimpleXMLElement;
use function PHPStan\Testing\assertType;

class Foo
{

public function getAddressByGps()
{
/** @var SimpleXMLElement|null $data */
$data = doFoo();

if ($data === null) {
return;
}

assertType('(SimpleXMLElement|null)', $data->item);
foreach ($data->item as $item) {
assertType('SimpleXMLElement', $item);
assertType('SimpleXMLElement|null', $item['name']);
}
}

}

class Bar extends SimpleXMLElement
{

public function getAddressByGps()
{
/** @var self|null $data */
$data = doFoo();

if ($data === null) {
return;
}

assertType('(SimpleXMLIteratorBug\Bar|null)', $data->item);
foreach ($data->item as $item) {
assertType(self::class, $item);
assertType('SimpleXMLIteratorBug\Bar|null', $item['name']);
}
}

}

class Baz
{

public function getAddressByGps()
{
/** @var Bar|null $data */
$data = doFoo();

if ($data === null) {
return;
}

assertType('(SimpleXMLIteratorBug\Bar|null)', $data->item);
foreach ($data->item as $item) {
assertType(Bar::class, $item);
assertType('SimpleXMLIteratorBug\Bar|null', $item['name']);
}
}

}

0 comments on commit 707f57a

Please sign in to comment.