Skip to content

Commit 846dd52

Browse files
Generate tentative types when appropriate
1 parent a2ab47f commit 846dd52

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

src/ProxyManager/Generator/MethodGenerator.php

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
use Laminas\Code\Generator\DocBlockGenerator;
88
use Laminas\Code\Generator\MethodGenerator as LaminasMethodGenerator;
99
use Laminas\Code\Reflection\MethodReflection;
10+
use ReflectionException;
11+
use ReflectionMethod;
1012

1113
/**
1214
* Method generator that fixes minor quirks in ZF2's method generator
1315
*/
1416
class MethodGenerator extends LaminasMethodGenerator
1517
{
18+
protected $hasTentativeReturnType = false;
19+
1620
/**
1721
* @return static
1822
*/
@@ -24,15 +28,72 @@ public static function fromReflectionWithoutBodyAndDocBlock(MethodReflection $re
2428
$method->setInterface(false);
2529
$method->setBody('');
2630

31+
if (\PHP_VERSION_ID < 80100) {
32+
return $method;
33+
}
34+
35+
/** @var callable(ReflectionMethod) : ReflectionMethod $getPrototype */
36+
$getPrototype = \Closure::fromCallable([new ReflectionMethod(ReflectionMethod::class, 'getPrototype'), 'invoke']);
37+
38+
while (true) {
39+
if ($reflectionMethod->hasTentativeReturnType()) {
40+
$method->hasTentativeReturnType = true;
41+
break;
42+
}
43+
44+
if ($reflectionMethod->isAbstract()) {
45+
break;
46+
}
47+
48+
try {
49+
$reflectionMethod = $getPrototype($reflectionMethod);
50+
} catch (ReflectionException $e) {
51+
break;
52+
}
53+
}
54+
2755
return $method;
2856
}
2957

30-
/**
31-
* {@inheritDoc} override needed to specify type in more detail
32-
*/
3358
public function getDocBlock(): ?DocBlockGenerator
3459
{
35-
return parent::getDocBlock();
60+
$docBlock = parent::getDocBlock();
61+
62+
if (! $this->hasTentativeReturnType) {
63+
return $docBlock;
64+
}
65+
66+
if ($docBlock === null) {
67+
return new class ($this->getIndentation()) extends DocBlockGenerator {
68+
public function __construct(string $indentation)
69+
{
70+
$this->setIndentation($indentation);
71+
}
72+
73+
public function generate(): string
74+
{
75+
return $this->getIndentation() . '#[\ReturnTypeWillChange]' . self::LINE_FEED;
76+
}
77+
};
78+
}
79+
80+
return new class ($docBlock) extends DocBlockGenerator {
81+
public function __construct(DocBlockGenerator $docBlock)
82+
{
83+
$this->setShortDescription($docBlock->getShortDescription());
84+
$this->setLongDescription($docBlock->getLongDescription());
85+
$this->setTags($docBlock->getTags());
86+
$this->setWordWrap($docBlock->getWordWrap());
87+
$this->setSourceDirty($docBlock->isSourceDirty());
88+
$this->setIndentation($docBlock->getIndentation());
89+
$this->setSourceContent($docBlock->getSourceContent());
90+
}
91+
92+
public function generate(): string
93+
{
94+
return parent::generate() . $this->getIndentation() . '#[\ReturnTypeWillChange]' . self::LINE_FEED;
95+
}
96+
};
3697
}
3798

3899
/**

tests/language-feature-scripts/lazy-loading-value-holder-internal-php-classes.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class PharMock extends Phar
2424
{
2525
}
2626

27-
public function compress($compression_type, $file_ext = null)
27+
public function compress($compression_type, $file_ext = null): ?Phar
2828
{
2929
echo $compression_type;
30+
31+
return null;
3032
}
3133
}
3234

0 commit comments

Comments
 (0)