Fix collection attribute typing

This commit is contained in:
stefanScrumble
2024-01-04 11:19:16 +01:00
parent 1cce49c621
commit 9a4c79f822
4 changed files with 157 additions and 16 deletions
+43 -16
View File
@@ -196,7 +196,7 @@ class ModelsCommand extends Command
protected function getArguments()
{
return [
['model', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', []],
['model', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', []],
];
}
@@ -208,21 +208,21 @@ class ModelsCommand extends Command
protected function getOptions()
{
return [
['filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file'],
['dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'The model dir, supports glob patterns', [], ],
['write', 'W', InputOption::VALUE_NONE, 'Write to Model file'],
['write-mixin', 'M', InputOption::VALUE_NONE,
"Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings",
],
['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'],
['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'],
['smart-reset', 'r', InputOption::VALUE_NONE, 'Refresh the properties/methods list, but keep the text'],
['phpstorm-noinspections', 'p', InputOption::VALUE_NONE,
'Add PhpFullyQualifiedNameUsageInspection and PhpUnnecessaryFullyQualifiedNameInspection PHPStorm ' .
'noinspection tags',
],
['ignore', 'I', InputOption::VALUE_OPTIONAL, 'Which models to ignore', ''],
['filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file'],
['dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'The model dir, supports glob patterns', [],],
['write', 'W', InputOption::VALUE_NONE, 'Write to Model file'],
['write-mixin', 'M', InputOption::VALUE_NONE,
"Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings",
],
['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'],
['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'],
['smart-reset', 'r', InputOption::VALUE_NONE, 'Refresh the properties/methods list, but keep the text'],
['phpstorm-noinspections', 'p', InputOption::VALUE_NONE,
'Add PhpFullyQualifiedNameUsageInspection and PhpUnnecessaryFullyQualifiedNameInspection PHPStorm ' .
'noinspection tags',
],
['ignore', 'I', InputOption::VALUE_OPTIONAL, 'Which models to ignore', ''],
];
}
@@ -1260,12 +1260,39 @@ class ModelsCommand extends Command
$phpdoc = new DocBlock($reflection, $context);
if ($phpdoc->hasTag('return')) {
$returnTag = $phpdoc->getTagsByName('return')[0];
if ($typeAlias = $this->extractTypeAlias($returnTag->getContent(), $context->getNamespaceAliases())) {
return $typeAlias;
}
$type = $phpdoc->getTagsByName('return')[0]->getType();
}
return $type;
}
/**
* @param string $typeAlias
* @param array $namespaceAliases
* @return string|null
*/
private function extractTypeAlias(string $typeAlias, array $namespaceAliases): string|null {
$matches = [];
preg_match('/(\w+)(<.*>)/', $typeAlias, $matches);
$matchCount = count($matches);
if ($matchCount === 0 || $matchCount === 1) {
return null;
}
if (empty($namespaceAliases[$matches[1]])) {
return null;
}
return $namespaceAliases[$matches[1]].($matches[2] ?? '');
}
protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?string
{
$returnType = $reflection->getReturnType();