Skip to content

Commit e1058cd

Browse files
authored
Fix oveerriding __debugInfo with covariant return type
1 parent 09dbdcf commit e1058cd

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/Reflection/Php/PhpMethodFromParserNodeReflection.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public function __construct(
8383
}
8484

8585
if ($name === '__debuginfo') {
86-
$realReturnType = TypeCombinator::addNull(new ArrayType(new MixedType(true), new MixedType(true)));
86+
$realReturnType = TypeCombinator::intersect(TypeCombinator::addNull(
87+
new ArrayType(new MixedType(true), new MixedType(true)),
88+
), $realReturnType);
8789
}
8890

8991
if ($name === '__unserialize') {

tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,4 +556,10 @@ public function testBug6104(): void
556556
$this->analyse([__DIR__ . '/data/bug-6104.php'], []);
557557
}
558558

559+
public function testBug9391(): void
560+
{
561+
$this->phpVersionId = PHP_VERSION_ID;
562+
$this->analyse([__DIR__ . '/data/bug-9391.php'], []);
563+
}
564+
559565
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bug9391;
6+
7+
class A
8+
{
9+
private $a;
10+
11+
/**
12+
* @return array<string, mixed>
13+
*/
14+
public function __debugInfo(): array
15+
{
16+
return [
17+
'a' => 1,
18+
];
19+
}
20+
21+
/**
22+
* @return array<int, string>
23+
*/
24+
public function __serialize(): array
25+
{
26+
return [
27+
'a' => $this->a
28+
];
29+
}
30+
}
31+
32+
class B extends A
33+
{
34+
private $b;
35+
36+
public function __debugInfo(): array
37+
{
38+
return [
39+
'b' => 2,
40+
];
41+
}
42+
43+
public function __serialize(): array
44+
{
45+
return [
46+
'b' => $this->b
47+
];
48+
}
49+
50+
}

0 commit comments

Comments
 (0)