mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-19 10:33:11 +00:00
Fix collectino with template types WIP
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
namespace Barryvdh\LaravelIdeHelper\Console;
|
||||
|
||||
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
|
||||
use Barryvdh\LaravelIdeHelper\helpers\PhpDocTypeParser;
|
||||
use Barryvdh\Reflection\DocBlock;
|
||||
use Barryvdh\Reflection\DocBlock\Context;
|
||||
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
||||
@@ -1262,7 +1263,8 @@ class ModelsCommand extends Command
|
||||
if ($phpdoc->hasTag('return')) {
|
||||
$returnTag = $phpdoc->getTagsByName('return')[0];
|
||||
|
||||
if ($typeAlias = $this->extractTypeAlias($returnTag->getContent(), $context->getNamespaceAliases())) {
|
||||
$typeParser = new PhpDocTypeParser($returnTag->getContent(), $context->getNamespaceAliases());
|
||||
if ($typeAlias = $typeParser->parse()) {
|
||||
return $typeAlias;
|
||||
}
|
||||
|
||||
@@ -1272,27 +1274,6 @@ class ModelsCommand extends Command
|
||||
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();
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Barryvdh\LaravelIdeHelper\helpers;
|
||||
|
||||
class PhpDocTypeParser
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $typeAlias;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $namespaceAliases;
|
||||
|
||||
/**
|
||||
* @param string $typeAlias
|
||||
* @param array $namespaceAliases
|
||||
*/
|
||||
public function __construct(string $typeAlias, array $namespaceAliases)
|
||||
{
|
||||
$this->typeAlias = $typeAlias;
|
||||
$this->namespaceAliases = $namespaceAliases;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function parse()
|
||||
{
|
||||
$matches = [];
|
||||
preg_match('/(\w+)(<.*>)/', $this->typeAlias, $matches);
|
||||
$matchCount = count($matches);
|
||||
|
||||
if ($matchCount === 0 || $matchCount === 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (empty($this->namespaceAliases[$matches[1]])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->namespaceAliases[$matches[1]] . $this->parseTemplate($matches[2] ?? null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $template
|
||||
* @return string
|
||||
*/
|
||||
private function parseTemplate($template): string
|
||||
{
|
||||
if (!$template || $template === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$matches = [];
|
||||
preg_match_all('/\w+/', $template, $matches);
|
||||
$types = array_unique($matches[0]);
|
||||
foreach ($types as $type) {
|
||||
$typeAlias = $this->namespaceAliases[$type] ?? $type;
|
||||
|
||||
dump($this->namespaceAliases, $typeAlias);
|
||||
$template = preg_replace("/\W$type\W/", $typeAlias, $template);
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user