diff --git a/README.md b/README.md index b28c0dd..bb9702f 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,9 @@ # IDE Helper Generator for Laravel -[![Tests](https://github.com/barryvdh/laravel-ide-helper/actions/workflows/run-tests.yml/badge.svg)](https://github.com/barryvdh/laravel-ide-helper/actions) +[![Tests](https://github.com/scrumble-nl/laravel-ide-helper/actions/workflows/run-tests.yml/badge.svg)](https://github.com/scrumble-nl/laravel-ide-helper/actions) [![Packagist License](https://poser.pugx.org/barryvdh/laravel-ide-helper/license.png)](http://choosealicense.com/licenses/mit/) [![Latest Stable Version](https://poser.pugx.org/barryvdh/laravel-ide-helper/version.png)](https://packagist.org/packages/barryvdh/laravel-ide-helper) [![Total Downloads](https://poser.pugx.org/barryvdh/laravel-ide-helper/d/total.png)](https://packagist.org/packages/barryvdh/laravel-ide-helper) -[![Fruitcake](https://img.shields.io/badge/Powered%20By-Fruitcake-b2bc35.svg)](https://fruitcake.nl/) **Complete PHPDocs, directly from the source** diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 9e29a5e..4ec323a 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -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; @@ -1239,7 +1240,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; } @@ -1249,28 +1251,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(); diff --git a/src/helpers/PhpDocTypeParser.php b/src/helpers/PhpDocTypeParser.php new file mode 100644 index 0000000..b7913d7 --- /dev/null +++ b/src/helpers/PhpDocTypeParser.php @@ -0,0 +1,80 @@ +typeAlias = $typeAlias; + $this->namespaceAliases = $namespaceAliases; + } + + /** + * @return string|null + */ + public function parse(): string|null + { + $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 ''; + } + + $type = ''; + $result = ''; + + foreach (str_split($template) as $char) { + $match = preg_match('/[A-z]/', $char); + + if (!$match) { + $type = $this->namespaceAliases[$type] ?? $type; + $result .= $type; + $result .= $char; + $type = ''; + + + continue; + } + + $type .= $char; + } + + return $result; + } +} diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php index 11bfb98..7f4a53d 100644 --- a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php @@ -4,8 +4,11 @@ declare(strict_types=1); namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\Models; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\CollectionModel; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\NonModel; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; +use Illuminate\Support\Collection as IntCollection; class WithCollection extends Model { @@ -29,4 +32,20 @@ class WithCollection extends Model { return new Collection(); } + + /** + * @return Collection + */ + public function getCollectionWithNonModelTemplateAttribute(): Collection + { + return new Collection(); + } + + /** + * @return Collection>> + */ + public function getCollectionWithNestedTemplateAttribute(): Collection + { + return new Collection(); + } } diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php b/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php new file mode 100644 index 0000000..aa8c82b --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php @@ -0,0 +1,9 @@ + + */ + public function getCollectionWithNonModelTemplateAttribute(): Collection + { + return new Collection(); + } + + /** + * @return Collection>> + */ + public function getCollectionWithNestedTemplateAttribute(): Collection + { + return new Collection(); + } } $collection + * @property-read \Illuminate\Support\Collection<\Illuminate\Support\Collection, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\CollectionModel<\Illuminate\Support\Collection, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\CollectionModel>> $collection_with_nested_template + * @property-read \Illuminate\Support\Collection $collection_with_non_model_template * @property-read \Illuminate\Support\Collection $collection_without_doc_block * @property-read \Illuminate\Support\Collection $collection_without_template * @method static \Illuminate\Database\Eloquent\Builder|WithCollection newModelQuery()