Skip to content

Commit

Permalink
Merge branch 'master' into 3.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/crontab/src/Event/Event.php
#	src/crontab/src/Event/FailToExecute.php
  • Loading branch information
limingxinleo committed Nov 1, 2023
2 parents 381b62e + 2a43741 commit 0ee75ac
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Commands/Ast/GenerateModelIDEVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use PhpParser\BuilderFactory;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use ReflectionClass;
use ReflectionParameter;
use ReflectionUnionType;

class GenerateModelIDEVisitor extends AbstractVisitor
{
Expand Down Expand Up @@ -106,10 +108,18 @@ public function afterTraverse(array $nodes)
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()) {
Expand Down
65 changes: 65 additions & 0 deletions tests/ModelGenerateTest.php
Original file line number Diff line number Diff line change
@@ -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)'));
}
}
62 changes: 62 additions & 0 deletions tests/Stubs/Model/TestGenerateIdeModel.php
Original file line number Diff line number Diff line change
@@ -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类型
}

0 comments on commit 0ee75ac

Please sign in to comment.