Skip to content

Commit

Permalink
update: 支持移除注解构造函数冗余方法
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Nov 1, 2023
1 parent ddbee5a commit de2425d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/Visitor/AttributeRewriteVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ protected function migrationConstruct(Node\Stmt\ClassMethod $node): Node\Stmt\Cl
$newParams[] = $param;
}

$codeBlock = $this->generator->getPrinter()->prettyPrint($node->getStmts());

if ($codeBlock === 'parent::__construct(...\func_get_args());')
{
// 移除构造方法冗余代码
$node->stmts = [];
$isModified = true;
}
else
{
// 方法存在代码块,请检查
$this->logger->warning("Method {$this->currentClass->name}::{$node->name} has code block, please check");
}

$classCommentDoc = Helper::arrayValueLast($this->currentClass->getComments());
$classComments = $classCommentDoc?->getText();

Expand Down Expand Up @@ -360,9 +374,10 @@ protected function refactorCommentsProperty(array $comments, array $props): ?str
elseif (($prop->type && str_contains($propRawType = $this->generator->getPrinter()->prettyPrint([$prop->type]), 'callable')) || str_contains($meta['type'] ?? '', 'callable'))
{
// callable 不能作为属性类型
$propRawType = $propRawType ?: $meta['type'];
$propRawType = empty($propRawType) ? $meta['type'] : $propRawType;
$methodComments[] = "* @var {$propRawType}";
$prop->type = null;
unset($propRawType);
}
if (!empty($methodComments))
{
Expand Down
43 changes: 41 additions & 2 deletions tests/AttributeRewriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getLogs(): array
echo $log . \PHP_EOL;
}

// echo $handle->execPrintStmt(), PHP_EOL;
// echo $handle->execPrintStmt(), PHP_EOL;

$this->assertTrue(true);
$this->assertEquals(
Expand Down Expand Up @@ -115,7 +115,46 @@ public function __construct(
public \$call2 = null
)
{
parent::__construct(...\\func_get_args());
}
}
PHP,
];

yield 'class_construct_warning' => [
__DIR__ . '/StubAttribute/TestConstruct.php',
<<<PHP
<?php
declare (strict_types=1);
namespace Imiphp\Tests\StubAttribute;
use Imi\Bean\Annotation\Base;
/**
* 回调注解.
*
* @Annotation
*
* @Target({"PROPERTY", "ANNOTATION"})
*
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class TestConstruct extends Base
{
public function __construct(
/**
* 类名,或者传入对象
* @var string|object
*/
public \$class = null,
/**
* 方法名
*/
public string \$method = ''
)
{
// parent::__construct(...\\func_get_args());
\$this->method = \$class . \$method;
}
}
PHP,
Expand Down
30 changes: 30 additions & 0 deletions tests/StubAttribute/TestConstruct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Imiphp\Tests\StubAttribute;

use Imi\Bean\Annotation\Base;

/**
* 回调注解.
*
* @Annotation
*
* @Target({"PROPERTY", "ANNOTATION"})
*
* @property string|object $class 类名,或者传入对象
* @property string $method 方法名
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class TestConstruct extends Base
{
/**
* @param string|object $class
*/
public function __construct(?array $__data = null, $class = null, string $method = '')
{
// parent::__construct(...\func_get_args());
$this->method = $class . $method;
}
}

0 comments on commit de2425d

Please sign in to comment.