Fix collectino with template types WIP

This commit is contained in:
stefanScrumble
2024-03-08 09:06:01 +01:00
parent 9a4c79f822
commit 9b4672969f
8 changed files with 174 additions and 63 deletions
+5
View File
@@ -0,0 +1,5 @@
{
"$schema": "/Users/stefan/.local/share/nvim/mason/packages/phpactor/phpactor.schema.json",
"language_server_psalm.enabled": true,
"language_server_php_cs_fixer.enabled": true
}
+3 -22
View File
@@ -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;
@@ -1262,7 +1263,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;
} }
@@ -1272,27 +1274,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();
+71
View File
@@ -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;
}
}
@@ -2,29 +2,49 @@
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
{ {
// /**
// * @return Collection<int, string>
// */
// public function getCollectionAttribute(): Collection
// {
// return new Collection();
// }
//
// /**
// * @return Collection
// */
// public function getCollectionWithoutTemplateAttribute(): Collection
// {
// return new Collection();
// }
//
// public function getCollectionWithoutDocBlockAttribute(): Collection
// {
// return new Collection();
// }
//
// /**
// * @return Collection<int, NonModel>
// */
// public function getCollectionWithNonModelTemplateAttribute(): Collection
// {
// return new Collection();
// }
//
/** /**
* @return Collection<int, string> * @return Collection<Collection, CollectionModel<IntCollection, CollectionModel<int, NonModel>>>
*/ */
public function getCollectionAttribute(): Collection public function getCollectionWithNestedTemplateAttribute(): Collection
{ {
return new Collection(); return new Collection();
}
/**
* @return Collection
*/
public function getCollectionWithoutTemplateAttribute(): Collection
{
return new Collection();
}
public function getCollectionWithoutDocBlockAttribute(): Collection
{
return new Collection();
} }
} }
@@ -0,0 +1,8 @@
<?php
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels;
class CollectionModel
{
}
@@ -0,0 +1,8 @@
<?php
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels;
class NonModel
{
}
@@ -2,33 +2,53 @@
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
*/ */
class WithCollection extends Model class WithCollection extends Model
{ {
// /**
// * @return Collection<int, string>
// */
// public function getCollectionAttribute(): Collection
// {
// return new Collection();
// }
//
// /**
// * @return Collection
// */
// public function getCollectionWithoutTemplateAttribute(): Collection
// {
// return new Collection();
// }
//
// public function getCollectionWithoutDocBlockAttribute(): Collection
// {
// return new Collection();
// }
//
// /**
// * @return Collection<int, NonModel>
// */
// public function getCollectionWithNonModelTemplateAttribute(): Collection
// {
// return new Collection();
// }
//
/** /**
* @return Collection<int, string> * @return Collection<Collection, CollectionModel<IntCollection, CollectionModel<int, NonModel>>>
*/ */
public function getCollectionAttribute(): Collection public function getCollectionWithNestedTemplateAttribute(): Collection
{ {
return new Collection(); return new Collection();
}
/**
* @return Collection
*/
public function getCollectionWithoutTemplateAttribute(): Collection
{
return new Collection();
}
public function getCollectionWithoutDocBlockAttribute(): Collection
{
return new Collection();
} }
} }
<?php <?php
@@ -48,9 +68,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCol
/** /**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\Models\WithCollection * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\Models\WithCollection
* *
* @property-read \Illuminate\Support\Collection<int, string> $collection * @property-read \Illuminate\Support\Collection\Illuminate\Support\Collection\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\CollectionModelIntCollection,\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\CollectionModelint,\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\NonModels\NonModel>> $collection_with_nested_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() * @method static \Illuminate\Database\Eloquent\Builder|WithCollection newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|WithCollection newQuery() * @method static \Illuminate\Database\Eloquent\Builder|WithCollection newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|WithCollection query() * @method static \Illuminate\Database\Eloquent\Builder|WithCollection query()