From 9b4672969f8f3d7d06608d7245d8e30a88d11595 Mon Sep 17 00:00:00 2001 From: stefanScrumble Date: Fri, 8 Mar 2024 09:06:01 +0100 Subject: [PATCH 1/4] 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() From 5d6b655271ccc2f337644d26ac20dd23c6a79a0a Mon Sep 17 00:00:00 2001 From: laravel-ide-helper Date: Fri, 8 Mar 2024 10:50:58 +0000 Subject: [PATCH 2/4] composer fix-style --- .../Models/WithCollection.php | 59 +++++++++---------- .../NonModels/CollectionModel.php | 3 +- .../NonModels/NonModel.php | 3 +- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php index 4537f64..e0b5882 100644 --- a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php @@ -12,41 +12,40 @@ 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 + // */ + // 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>> */ public function getCollectionWithNestedTemplateAttribute(): Collection { return new Collection(); - } } diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php b/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php index 9bbecf0..aa8c82b 100644 --- a/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/NonModels/CollectionModel.php @@ -1,8 +1,9 @@ Date: Fri, 8 Mar 2024 13:09:43 +0100 Subject: [PATCH 3/4] Fix collectino with complex template types{ --- README.md | 3 +- src/helpers/PhpDocTypeParser.php | 31 +++++---- .../Models/WithCollection.php | 58 ++++++++--------- .../__snapshots__/Test__test__1.php | 65 ++++++++++--------- 4 files changed, 84 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index b28c0dd..bb9702f 100644 --- a/README.md +++ b/README.md @@ -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** diff --git a/src/helpers/PhpDocTypeParser.php b/src/helpers/PhpDocTypeParser.php index ed1aa7a..b7913d7 100644 --- a/src/helpers/PhpDocTypeParser.php +++ b/src/helpers/PhpDocTypeParser.php @@ -9,12 +9,12 @@ class PhpDocTypeParser /** * @var string */ - private $typeAlias; + private string $typeAlias; /** * @var array */ - private $namespaceAliases; + private array $namespaceAliases; /** * @param string $typeAlias @@ -29,7 +29,7 @@ class PhpDocTypeParser /** * @return string|null */ - public function parse() + public function parse(): string|null { $matches = []; preg_match('/(\w+)(<.*>)/', $this->typeAlias, $matches); @@ -56,16 +56,25 @@ class PhpDocTypeParser return ''; } - $matches = []; - preg_match_all('/\w+/', $template, $matches); - $types = array_unique($matches[0]); - foreach ($types as $type) { - $typeAlias = $this->namespaceAliases[$type] ?? $type; + $type = ''; + $result = ''; - dump($this->namespaceAliases, $typeAlias); - $template = preg_replace("/\W$type\W/", $typeAlias, $template); + 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 $template; + return $result; } } diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php index e0b5882..7f4a53d 100644 --- a/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php @@ -12,35 +12,35 @@ 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 + */ + 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>> */ diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php index 37c065a..d7d64f4 100644 --- a/tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php @@ -15,42 +15,41 @@ 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 + */ + 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>> */ public function getCollectionWithNestedTemplateAttribute(): Collection { return new Collection(); - } } > $collection_with_nested_template + * @property-read \Illuminate\Support\Collection $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>> $collection_with_nested_template + * @property-read \Illuminate\Support\Collection $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() * @method static \Illuminate\Database\Eloquent\Builder|WithCollection newQuery() * @method static \Illuminate\Database\Eloquent\Builder|WithCollection query() From 1bedabf0f17fc1fc689123f692871dd84cb35cee Mon Sep 17 00:00:00 2001 From: stefanScrumble Date: Fri, 8 Mar 2024 13:34:31 +0100 Subject: [PATCH 4/4] Delete phpactor --- .phpactor.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .phpactor.json diff --git a/.phpactor.json b/.phpactor.json deleted file mode 100644 index 6888195..0000000 --- a/.phpactor.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "$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