mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-19 02:23:11 +00:00
Merge pull request #3 from scrumble-nl/@fix/collection-with-template-types
@fix/collection with template types
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
# IDE Helper Generator for Laravel
|
# IDE Helper Generator for Laravel
|
||||||
|
|
||||||
[](https://github.com/barryvdh/laravel-ide-helper/actions)
|
[](https://github.com/scrumble-nl/laravel-ide-helper/actions)
|
||||||
[](http://choosealicense.com/licenses/mit/)
|
[](http://choosealicense.com/licenses/mit/)
|
||||||
[](https://packagist.org/packages/barryvdh/laravel-ide-helper)
|
[](https://packagist.org/packages/barryvdh/laravel-ide-helper)
|
||||||
[](https://packagist.org/packages/barryvdh/laravel-ide-helper)
|
[](https://packagist.org/packages/barryvdh/laravel-ide-helper)
|
||||||
[](https://fruitcake.nl/)
|
|
||||||
|
|
||||||
**Complete PHPDocs, directly from the source**
|
**Complete PHPDocs, directly from the source**
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
namespace Barryvdh\LaravelIdeHelper\Console;
|
namespace Barryvdh\LaravelIdeHelper\Console;
|
||||||
|
|
||||||
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
|
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
|
||||||
|
use Barryvdh\LaravelIdeHelper\helpers\PhpDocTypeParser;
|
||||||
use Barryvdh\Reflection\DocBlock;
|
use Barryvdh\Reflection\DocBlock;
|
||||||
use Barryvdh\Reflection\DocBlock\Context;
|
use Barryvdh\Reflection\DocBlock\Context;
|
||||||
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
||||||
@@ -1239,7 +1240,8 @@ class ModelsCommand extends Command
|
|||||||
if ($phpdoc->hasTag('return')) {
|
if ($phpdoc->hasTag('return')) {
|
||||||
$returnTag = $phpdoc->getTagsByName('return')[0];
|
$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;
|
return $typeAlias;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1249,28 +1251,6 @@ class ModelsCommand extends Command
|
|||||||
return $type;
|
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
|
protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?string
|
||||||
{
|
{
|
||||||
$returnType = $reflection->getReturnType();
|
$returnType = $reflection->getReturnType();
|
||||||
|
|||||||
@@ -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;
|
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\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Collection as IntCollection;
|
||||||
|
|
||||||
class WithCollection extends Model
|
class WithCollection extends Model
|
||||||
{
|
{
|
||||||
@@ -29,4 +32,20 @@ class WithCollection extends Model
|
|||||||
{
|
{
|
||||||
return new Collection();
|
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;
|
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\Database\Eloquent\Model;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Collection as IntCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @mixin IdeHelperWithCollection
|
* @mixin IdeHelperWithCollection
|
||||||
@@ -32,6 +35,22 @@ class WithCollection extends Model
|
|||||||
{
|
{
|
||||||
return new Collection();
|
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
|
<?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<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_doc_block
|
||||||
* @property-read \Illuminate\Support\Collection $collection_without_template
|
* @property-read \Illuminate\Support\Collection $collection_without_template
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|WithCollection newModelQuery()
|
* @method static \Illuminate\Database\Eloquent\Builder|WithCollection newModelQuery()
|
||||||
|
|||||||
Reference in New Issue
Block a user