mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Fixed bug that generate proxy class failed when using union type. (#4158)
Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
parent
4f57ed7208
commit
d4840c5bc5
@ -11,6 +11,7 @@
|
||||
|
||||
- [#4130](https://github.com/hyperf/hyperf/pull/4130) Fixed bug that generate model failed when using option `--with-ide` and `scope` methods.
|
||||
- [#4141](https://github.com/hyperf/hyperf/pull/4141) Fixed bug that validator factory does not support other validators.
|
||||
- [#4158](https://github.com/hyperf/hyperf/pull/4158) Fixed bug that generate proxy class failed when using union type.
|
||||
|
||||
## Optimized
|
||||
|
||||
|
@ -75,11 +75,26 @@ class PhpParser
|
||||
}
|
||||
|
||||
if ($parameter->hasType()) {
|
||||
$type = $parameter->getType()->getName();
|
||||
/** @var \ReflectionNamedType|\ReflectionUnionType $reflection */
|
||||
$reflection = $parameter->getType();
|
||||
if ($reflection instanceof \ReflectionUnionType) {
|
||||
$unionType = [];
|
||||
foreach ($reflection->getTypes() as $objType) {
|
||||
$type = $objType->getName();
|
||||
if (! in_array($type, static::TYPES)) {
|
||||
$unionType[] = new Node\Name('\\' . $type);
|
||||
} else {
|
||||
$unionType[] = new Node\Identifier($type);
|
||||
}
|
||||
}
|
||||
$result->type = new Node\UnionType($unionType);
|
||||
} else {
|
||||
$type = $reflection->getName();
|
||||
if (! in_array($type, static::TYPES)) {
|
||||
$result->type = new Node\Name('\\' . $type);
|
||||
} else {
|
||||
$result->type = new Node\Identifier($parameter->getType()->getName());
|
||||
$result->type = new Node\Identifier($type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ namespace HyperfTest\Utils\CodeGen;
|
||||
|
||||
use Hyperf\Utils\CodeGen\PhpParser;
|
||||
use HyperfTest\Utils\Stub\Bar;
|
||||
use HyperfTest\Utils\Stub\UnionTypeFoo;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\ParserFactory;
|
||||
@ -27,9 +28,9 @@ class PhpParserTest extends TestCase
|
||||
public function testGetAstFromReflectionParameter()
|
||||
{
|
||||
$parserFactory = new ParserFactory();
|
||||
$parser = $parserFactory->create(ParserFactory::ONLY_PHP7);
|
||||
$parser7 = $parserFactory->create(ParserFactory::ONLY_PHP7);
|
||||
|
||||
$stmts = $parser->parse(file_get_contents(__DIR__ . '/../Stub/Bar.php'));
|
||||
$stmts = $parser7->parse(file_get_contents(__DIR__ . '/../Stub/Bar.php'));
|
||||
/** @var ClassMethod $classMethod */
|
||||
$classMethod = $stmts[1]->stmts[0]->stmts[0];
|
||||
$name = $classMethod->getParams()[0];
|
||||
@ -42,6 +43,17 @@ class PhpParserTest extends TestCase
|
||||
$this->assertNodeParam($foo, $foo2 = $parser->getNodeFromReflectionParameter($parameters[1]));
|
||||
$this->assertSame(['', 'HyperfTest', 'Utils', 'Stub', 'Foo'], $foo2->type->parts);
|
||||
$this->assertNodeParam($extra, $parser->getNodeFromReflectionParameter($parameters[2]));
|
||||
|
||||
if (PHP_VERSION_ID > 80000) {
|
||||
$stmts = $parser7->parse(file_get_contents(__DIR__ . '/../Stub/UnionTypeFoo.php'));
|
||||
/** @var ClassMethod $classMethod */
|
||||
$classMethod = $stmts[1]->stmts[0]->stmts[0];
|
||||
$name = $classMethod->getParams()[0];
|
||||
|
||||
$foo = new \ReflectionClass(UnionTypeFoo::class);
|
||||
$parameters = $foo->getMethod('__construct')->getParameters();
|
||||
$this->assertNodeParam($name, $parser->getNodeFromReflectionParameter($parameters[0]));
|
||||
}
|
||||
}
|
||||
|
||||
protected function assertNodeParam(Node\Param $param, Node\Param $param2)
|
||||
|
19
src/utils/tests/Stub/UnionTypeFoo.php
Normal file
19
src/utils/tests/Stub/UnionTypeFoo.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace HyperfTest\Utils\Stub;
|
||||
|
||||
class UnionTypeFoo
|
||||
{
|
||||
public function __construct(Bar|Foo|string $attr)
|
||||
{
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user