Support unionType param for GenerateModelIDEVisitor. (#6236)

This commit is contained in:
宣言就是Siam 2023-10-31 11:18:32 +08:00 committed by GitHub
parent 5183868c29
commit 667525bf3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 144 additions and 3 deletions

View File

@ -1,5 +1,9 @@
# v3.0.42 - TBD
## Added
- [#6236](https://github.com/hyperf/hyperf/pull/6236) Support unionType param for GenerateModelIDEVisitor.
## Optimized
- [#6239](https://github.com/hyperf/hyperf/pull/6239) Improve amqp, use methods instead of `$delivery_info` and optimize `BeforeConsume` event.

View File

@ -19,8 +19,10 @@ use Hyperf\Stringable\Str;
use PhpParser\BuilderFactory;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use ReflectionClass;
use ReflectionParameter;
use ReflectionUnionType;
class GenerateModelIDEVisitor extends AbstractVisitor
{
@ -106,10 +108,18 @@ class GenerateModelIDEVisitor extends AbstractVisitor
foreach ($call['arguments'] as $argument) {
$argName = new Node\Expr\Variable($argument->getName());
if ($argument->hasType()) {
if ($argument->getType()->allowsNull()) {
$argType = new Node\NullableType($argument->getType()->getName());
$argumentType = $argument->getType();
if ($argumentType instanceof ReflectionUnionType) {
$unionTypeIdentifier = [];
foreach ($argumentType->getTypes() as $type) {
$unionTypeIdentifier[] = new Identifier($type->getName());
}
$argType = new Node\UnionType($unionTypeIdentifier);
} else {
$argType = $argument->getType()->getName();
$argType = $argumentType->getName();
if ($argumentType->allowsNull()) {
$argType = new Node\NullableType($argType);
}
}
}
if ($argument->isDefaultValueAvailable()) {

View File

@ -0,0 +1,65 @@
<?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\Database;
use Hyperf\Database\Commands\Ast\GenerateModelIDEVisitor;
use Hyperf\Database\Commands\ModelData;
use Hyperf\Database\Commands\ModelOption;
use HyperfTest\Database\Stubs\Model\TestGenerateIdeModel;
use PhpParser\Lexer\Emulative;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ModelGenerateTest extends TestCase
{
public function setUp(): void
{
$this->lexer = new Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
],
]);
$this->astParser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7, $this->lexer);
$this->printer = new Standard();
}
public function testGenerateScope()
{
$code = file_get_contents(__DIR__ . '/Stubs/Model/TestGenerateIdeModel.php');
$option = new ModelOption();
$data = new ModelData(TestGenerateIdeModel::class, [
['column_name' => 'name'],
['column_name' => 'age'],
]);
$stmts = $this->astParser->parse($code);
$traverser = new NodeTraverser();
$traverser->addVisitor(new GenerateModelIDEVisitor($option, $data));
$stmts = $traverser->traverse($stmts);
$code = $this->printer->prettyPrintFile($stmts);
$this->assertNotFalse(strpos($code, 'public static function optionNull(?string $test)'));
$this->assertNotFalse(strpos($code, 'public static function string(string $test)'));
$this->assertNotFalse(strpos($code, 'public static function union(int $appId, string|int $uid)'));
$this->assertNotFalse(strpos($code, 'public static function unionOrNull(int $appId, string|int|null $uid)'));
$this->assertNotFalse(strpos($code, 'public static function singleOrNull(?string $test)'));
}
}

View File

@ -0,0 +1,62 @@
<?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\Database\Stubs\Model;
class TestGenerateIdeModel extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'test_generate_ide_models';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = ['id', 'user_id', 'title', 'created_at', 'updated_at'];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'user_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
public function image()
{
return $this->morphOne(Image::class, 'imageable');
}
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function scopeOptionNull($query, ?string $test)
{
}
public function scopeString($query, string $test)
{
}
public function scopeUnion($query, int $appId, string|int $uid)
{
}
public function scopeUnionOrNull($query, int $appId, string|int|null $uid)
{
}
public function scopeSingleOrNull($query, null|string $test)
{
}
// TODO PHP 8.2 起单独支持null类型
}