From 9b4672969f8f3d7d06608d7245d8e30a88d11595 Mon Sep 17 00:00:00 2001 From: stefanScrumble Date: Fri, 8 Mar 2024 09:06:01 +0100 Subject: [PATCH] Fix collectino with template types WIP --- .phpactor.json | 5 ++ resources/views/helper.php | 20 +++--- src/Console/ModelsCommand.php | 25 +------ src/helpers/PhpDocTypeParser.php | 71 +++++++++++++++++++ .../Models/WithCollection.php | 48 +++++++++---- .../NonModels/CollectionModel.php | 8 +++ .../NonModels/NonModel.php | 8 +++ .../__snapshots__/Test__test__1.php | 52 +++++++++----- 8 files changed, 174 insertions(+), 63 deletions(-) create mode 100644 .phpactor.json create mode 100644 src/helpers/PhpDocTypeParser.php create mode 100644 tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php create mode 100644 tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/NonModel.php diff --git a/.phpactor.json b/.phpactor.json new file mode 100644 index 0000000..6888195 --- /dev/null +++ b/.phpactor.json @@ -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 +} \ No newline at end of file diff --git a/resources/views/helper.php b/resources/views/helper.php index ddf2922..ecbd055 100644 --- a/resources/views/helper.php +++ b/resources/views/helper.php @@ -25,12 +25,12 @@ -namespace { +namespace { - getDocComment(' ')) ?> + getDocComment(' ')) ?> getClassType() ?> getExtendsClass() ?> { getMethods() as $method) : ?> - getDocComment(' ')) ?> + getDocComment(' ')) ?> public static function getName() ?>(getParamsWithDefault() ?>) {getDeclaringClass() !== $method->getRoot()) : ?> //Method inherited from getDeclaringClass() ?> @@ -41,19 +41,19 @@ namespace { shouldReturn() ? 'return ' : '' ?>getRootMethodCall() ?>; } - + } - + } $aliases) : ?> -namespace { +namespace { getClassType() ?> getShortName() ?> extends getExtends() ?> {getExtendsNamespace() == '\Illuminate\Database\Eloquent') : ?> - getMethods() as $method) : ?> - getDocComment(' ')) ?> + getMethods() as $method) : ?> + getDocComment(' ')) ?> public static function getName() ?>(getParamsWithDefault() ?>) {getDeclaringClass() !== $method->getRoot()) : ?> //Method inherited from getDeclaringClass() ?> @@ -66,14 +66,14 @@ namespace { } } - + } namespace { - + } diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index e6b6db3..17bda64 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; @@ -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(); diff --git a/src/helpers/PhpDocTypeParser.php b/src/helpers/PhpDocTypeParser.php new file mode 100644 index 0000000..ed1aa7a --- /dev/null +++ b/src/helpers/PhpDocTypeParser.php @@ -0,0 +1,71 @@ +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; + } +} diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php index 997ae3b..d5ea285 100644 --- a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php @@ -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 +// */ +// 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 +// */ +// public function getCollectionWithNonModelTemplateAttribute(): Collection +// { +// return new Collection(); +// } +// /** - * @return Collection + * @return Collection>> */ - 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(); } } diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php b/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php new file mode 100644 index 0000000..9bbecf0 --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php @@ -0,0 +1,8 @@ + +// */ +// 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 +// */ +// public function getCollectionWithNonModelTemplateAttribute(): Collection +// { +// return new Collection(); +// } +// /** - * @return Collection + * @return Collection>> */ - 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(); } } $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()