Merge pull request #3 from scrumble-nl/@fix/collection-with-template-types

@fix/collection with template types
This commit is contained in:
stefanScrumble
2024-03-08 13:37:52 +01:00
committed by GitHub
7 changed files with 142 additions and 25 deletions
+1 -2
View File
@@ -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**
+3 -23
View File
@@ -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();
+80
View File
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\helpers;
class PhpDocTypeParser
{
/**
* @var string
*/
private string $typeAlias;
/**
* @var array
*/
private array $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(): 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;
}
}
@@ -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<int, NonModel>
*/
public function getCollectionWithNonModelTemplateAttribute(): Collection
{
return new Collection();
}
/**
* @return Collection<Collection, CollectionModel<IntCollection, CollectionModel<int, NonModel>>>
*/
public function getCollectionWithNestedTemplateAttribute(): Collection
{
return new Collection();
}
}
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels;
class CollectionModel
{
}
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels;
class NonModel
{
}
@@ -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;
/**
* @mixin IdeHelperWithCollection
@@ -32,6 +35,22 @@ class WithCollection extends Model
{
return new Collection();
}
/**
* @return Collection<int, NonModel>
*/
public function getCollectionWithNonModelTemplateAttribute(): Collection
{
return new Collection();
}
/**
* @return Collection<Collection, CollectionModel<IntCollection, CollectionModel<int, NonModel>>>
*/
public function getCollectionWithNestedTemplateAttribute(): Collection
{
return new Collection();
}
}
<?php
@@ -51,6 +70,8 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCol
*
*
* @property-read \Illuminate\Support\Collection<int, string> $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<int, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\NonModel>>> $collection_with_nested_template
* @property-read \Illuminate\Support\Collection<int, \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\NonModel> $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()