Skip to content

Commit

Permalink
Fix resolving tentative return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 11, 2024
1 parent 2da94f8 commit 753fc4d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/Native/NativeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getPrototype(): ClassMemberReflection

$tentativeReturnType = null;
if ($prototypeMethod->getTentativeReturnType() !== null) {
$tentativeReturnType = TypehintHelper::decideTypeFromReflection($prototypeMethod->getTentativeReturnType());
$tentativeReturnType = TypehintHelper::decideTypeFromReflection($prototypeMethod->getTentativeReturnType(), null, $prototypeDeclaringClass);
}

return new MethodPrototypeReflection(
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getPrototype(): ClassMemberReflection

$tentativeReturnType = null;
if ($prototypeMethod->getTentativeReturnType() !== null) {
$tentativeReturnType = TypehintHelper::decideTypeFromReflection($prototypeMethod->getTentativeReturnType());
$tentativeReturnType = TypehintHelper::decideTypeFromReflection($prototypeMethod->getTentativeReturnType(), null, $prototypeDeclaringClass);
}

return new MethodPrototypeReflection(
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,4 +812,10 @@ public function testBug9524(): void
$this->analyse([__DIR__ . '/data/bug-9524.php'], []);
}

public function testSimpleXmlElementChildClass(): void
{
$this->phpVersionId = PHP_VERSION_ID;
$this->analyse([__DIR__ . '/data/simple-xml-element-child.php'], []);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/simple-xml-element-child.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace SimpleXmlElementChild;

class CustomXMLElement extends \SimpleXMLElement
{
#[\Override]
public function addChild(string $qualifiedName, ?string $value = null, ?string $namespace = null): ?static
{
return parent::addChild($qualifiedName, $this->escapeInput($value), $namespace);
}

private function escapeInput(?string $value): ?string
{
if ($value === null) {
return null;
}
return htmlspecialchars((string) normalizer_normalize($value), ENT_XML1, 'UTF-8');
}
}

0 comments on commit 753fc4d

Please sign in to comment.