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
}
+10 -10
View File
@@ -25,12 +25,12 @@
<?php if ($namespace == '\Illuminate\Database\Eloquent') :
continue;
endif; ?>
namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach ($aliases as $alias) : ?>
<?= trim($alias->getDocComment(' ')) ?>
<?= trim($alias->getDocComment(' ')) ?>
<?= $alias->getClassType() ?> <?= $alias->getExtendsClass() ?> {
<?php foreach ($alias->getMethods() as $method) : ?>
<?= trim($method->getDocComment(' ')) ?>
<?= trim($method->getDocComment(' ')) ?>
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
{<?php if ($method->getDeclaringClass() !== $method->getRoot()) : ?>
//Method inherited from <?= $method->getDeclaringClass() ?>
@@ -41,19 +41,19 @@ namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?php endif?>
<?= $method->shouldReturn() ? 'return ' : '' ?><?= $method->getRootMethodCall() ?>;
}
<?php endforeach; ?>
<?php endforeach; ?>
}
<?php endforeach; ?>
<?php endforeach; ?>
}
<?php endforeach; ?>
<?php foreach ($namespaces_by_alias_ns as $namespace => $aliases) : ?>
namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach ($aliases as $alias) : ?>
<?= $alias->getClassType() ?> <?= $alias->getShortName() ?> extends <?= $alias->getExtends() ?> {<?php if ($alias->getExtendsNamespace() == '\Illuminate\Database\Eloquent') : ?>
<?php foreach ($alias->getMethods() as $method) : ?>
<?= trim($method->getDocComment(' ')) ?>
<?php foreach ($alias->getMethods() as $method) : ?>
<?= trim($method->getDocComment(' ')) ?>
public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefault() ?>)
{<?php if ($method->getDeclaringClass() !== $method->getRoot()) : ?>
//Method inherited from <?= $method->getDeclaringClass() ?>
@@ -66,14 +66,14 @@ namespace <?= $namespace == '__root' ? '' : trim($namespace, '\\') ?> {
}
<?php endforeach; ?>
<?php endif; ?>}
<?php endforeach; ?>
<?php endforeach; ?>
}
<?php endforeach; ?>
<?php if ($helpers) : ?>
namespace {
<?= $helpers ?>
<?= $helpers ?>
}
<?php endif; ?>
+3 -22
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;
@@ -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();
+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;
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
{
// /**
// * @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 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;
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
*/
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 Collection
*/
public function getCollectionWithoutTemplateAttribute(): Collection
{
return new Collection();
}
public function getCollectionWithoutDocBlockAttribute(): Collection
{
return new Collection();
}
}
<?php
@@ -48,9 +68,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCol
/**
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\Models\WithCollection
*
* @property-read \Illuminate\Support\Collection<int, string> $collection
* @property-read \Illuminate\Support\Collection $collection_without_doc_block
* @property-read \Illuminate\Support\Collection $collection_without_template
* @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
* @method static \Illuminate\Database\Eloquent\Builder|WithCollection newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|WithCollection newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|WithCollection query()