Skip to content

Fix SimpleXMLElement::offsetSet #4196

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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions stubs/iterable.stub
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class SimpleXMLElement implements Traversable, ArrayAccess, Iterator, RecursiveI
*/
public function saveXML(?string $filename = null) { }

/**
* @param int|string|null $key
* @param self|string|int|float|bool $value
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept consistency with

public function getWritableType(): Type
{
return TypeCombinator::union(
$this->type,
new IntegerType(),
new FloatType(),
new StringType(),
new BooleanType(),
);
}

* @return void
*/
public function offsetSet($key, $value) {}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function testRuleWithNullsafeVariant(): void
]);
}

public function testBug8236(): void
{
$this->analyse([__DIR__ . '/data/bug-8236.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug5655b(): void
{
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-8236.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug8236;

class HelloWorld
{
public static function sayHello(): void
{
$xml = new \SimpleXmlElement('<root><a></a></root>');
$string = 'But isn\'t 7 > 5 & 9 < 11?';
$node = $xml->a->addChild('formula1');
if ($node !== null) {
$node[0] = $string;
}
echo $xml->asXML();
}
}
HelloWorld::sayHello();
Loading