Skip to content

Commit 340fb99

Browse files
authored
Fix bug that model with trait method comments cannot be created by gen:model (#7279)
1 parent 81953c4 commit 340fb99

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/PhpParser.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,52 @@ public function getExprFromValue($value): Node\Expr
140140
/**
141141
* @return Node\Stmt\ClassMethod[]
142142
*/
143-
public function getAllMethodsFromStmts(array $stmts): array
143+
public function getAllMethodsFromStmts(array $stmts, bool $withTrait = false): array
144144
{
145145
$methods = [];
146146
foreach ($stmts as $namespace) {
147147
if (! $namespace instanceof Node\Stmt\Namespace_) {
148148
continue;
149149
}
150150

151+
/** @var string[] $uses */
152+
$uses = [];
153+
151154
foreach ($namespace->stmts as $class) {
152-
if (! $class instanceof Node\Stmt\Class_ && ! $class instanceof Node\Stmt\Interface_) {
155+
if ($class instanceof Node\Stmt\Use_) {
156+
foreach ($class->uses as $use) {
157+
$uses[$use->name->getLast()] = $use->name->toString();
158+
}
159+
continue;
160+
}
161+
162+
if (! $class instanceof Node\Stmt\Class_ && ! $class instanceof Node\Stmt\Interface_ && ! $class instanceof Node\Stmt\Trait_) {
153163
continue;
154164
}
155165

156166
foreach ($class->getMethods() as $method) {
157167
$methods[] = $method;
158168
}
169+
170+
if ($withTrait) {
171+
foreach ($class->stmts as $stmt) {
172+
if ($stmt instanceof Node\Stmt\TraitUse) {
173+
foreach ($stmt->traits as $trait) {
174+
if (isset($uses[$trait->getFirst()])) {
175+
$traitName = $uses[$trait->getFirst()] . substr($trait->toString(), strlen($trait->getFirst()));
176+
} else {
177+
if (count($trait->getParts()) == 1) {
178+
$traitName = $namespace->name->toString() . '\\' . $trait->toString();
179+
} else {
180+
$traitName = $trait->toString();
181+
}
182+
}
183+
$traitNodes = $this->parser->parse(file_get_contents((new ReflectionClass($traitName))->getFileName()));
184+
$methods = array_merge($methods, $this->getAllMethodsFromStmts($traitNodes, true));
185+
}
186+
}
187+
}
188+
}
159189
}
160190
}
161191

0 commit comments

Comments
 (0)