From 19b6ebd58fe7c0e9aeeffb53691046f860955e87 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Thu, 9 Mar 2023 16:45:39 +0200 Subject: [PATCH 1/7] Add `AllowDynamicProperties` Attribute to cooperate with php8.2 deprecation --- src/Console/ModelsCommand.php | 6 ++++-- .../GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 9f9931f..2aecdf2 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1055,9 +1055,11 @@ class ModelsCommand extends Command } $classname = $this->write_mixin ? $mixinClassName : $classname; - $output = "namespace {$namespace}{\n{$docComment}\n\t{$keyword}class {$classname} "; - if (!$this->write_mixin) { + $allowDynamicAttributes = $this->write_mixin ? "#[\AllowDynamicProperties]\n\t" : ''; + $output = "namespace {$namespace}{\n{$docComment}\n\t{$keyword}{$allowDynamicAttributes}class {$classname} "; + + if (! $this->write_mixin) { $output .= "extends \Eloquent "; if ($interfaceNames) { diff --git a/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php index 88e7eb6..b8a826c 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php @@ -183,6 +183,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWi * @method static \Illuminate\Database\Eloquent\Builder|Post whereYearNullable($value) * @mixin \Eloquent */ - class IdeHelperPost {} + #[\AllowDynamicProperties] + class IdeHelperPost {} } From 6ac2a187c099b6e9c10b885cb097cb850bc78329 Mon Sep 17 00:00:00 2001 From: GeoSot Date: Thu, 9 Mar 2023 17:00:28 +0200 Subject: [PATCH 2/7] lint snapshot --- .../GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php index b8a826c..eb30200 100644 --- a/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/GeneratePhpdocWithMixin/__snapshots__/Test__test__1.php @@ -184,6 +184,6 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocWi * @mixin \Eloquent */ #[\AllowDynamicProperties] - class IdeHelperPost {} + class IdeHelperPost {} } From 9a4c79f822b2178af08658f31717bbdaa21af4b8 Mon Sep 17 00:00:00 2001 From: stefanScrumble Date: Thu, 4 Jan 2024 11:19:16 +0100 Subject: [PATCH 3/7] Fix collection attribute typing --- src/Console/ModelsCommand.php | 59 +++++++++++++----- .../Models/WithCollection.php | 30 +++++++++ .../GenerateMixinCollection/Test.php | 22 +++++++ .../__snapshots__/Test__test__1.php | 62 +++++++++++++++++++ 4 files changed, 157 insertions(+), 16 deletions(-) create mode 100644 tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php create mode 100644 tests/Console/ModelsCommand/GenerateMixinCollection/Test.php create mode 100644 tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index dab117e..e6b6db3 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -196,7 +196,7 @@ class ModelsCommand extends Command protected function getArguments() { return [ - ['model', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', []], + ['model', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Which models to include', []], ]; } @@ -208,21 +208,21 @@ class ModelsCommand extends Command protected function getOptions() { return [ - ['filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file'], - ['dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, - 'The model dir, supports glob patterns', [], ], - ['write', 'W', InputOption::VALUE_NONE, 'Write to Model file'], - ['write-mixin', 'M', InputOption::VALUE_NONE, - "Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings", - ], - ['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'], - ['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'], - ['smart-reset', 'r', InputOption::VALUE_NONE, 'Refresh the properties/methods list, but keep the text'], - ['phpstorm-noinspections', 'p', InputOption::VALUE_NONE, - 'Add PhpFullyQualifiedNameUsageInspection and PhpUnnecessaryFullyQualifiedNameInspection PHPStorm ' . - 'noinspection tags', - ], - ['ignore', 'I', InputOption::VALUE_OPTIONAL, 'Which models to ignore', ''], + ['filename', 'F', InputOption::VALUE_OPTIONAL, 'The path to the helper file'], + ['dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, + 'The model dir, supports glob patterns', [],], + ['write', 'W', InputOption::VALUE_NONE, 'Write to Model file'], + ['write-mixin', 'M', InputOption::VALUE_NONE, + "Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings", + ], + ['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'], + ['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'], + ['smart-reset', 'r', InputOption::VALUE_NONE, 'Refresh the properties/methods list, but keep the text'], + ['phpstorm-noinspections', 'p', InputOption::VALUE_NONE, + 'Add PhpFullyQualifiedNameUsageInspection and PhpUnnecessaryFullyQualifiedNameInspection PHPStorm ' . + 'noinspection tags', + ], + ['ignore', 'I', InputOption::VALUE_OPTIONAL, 'Which models to ignore', ''], ]; } @@ -1260,12 +1260,39 @@ class ModelsCommand extends Command $phpdoc = new DocBlock($reflection, $context); if ($phpdoc->hasTag('return')) { + $returnTag = $phpdoc->getTagsByName('return')[0]; + + if ($typeAlias = $this->extractTypeAlias($returnTag->getContent(), $context->getNamespaceAliases())) { + return $typeAlias; + } + $type = $phpdoc->getTagsByName('return')[0]->getType(); } 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/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php new file mode 100644 index 0000000..997ae3b --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/Models/WithCollection.php @@ -0,0 +1,30 @@ + + */ + public function getCollectionAttribute(): 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/Test.php b/tests/Console/ModelsCommand/GenerateMixinCollection/Test.php new file mode 100644 index 0000000..2231752 --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/Test.php @@ -0,0 +1,22 @@ +app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write-mixin' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php new file mode 100644 index 0000000..e3abb05 --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateMixinCollection/__snapshots__/Test__test__1.php @@ -0,0 +1,62 @@ + + */ + public function getCollectionAttribute(): Collection + { + return new Collection(); + } + + /** + * @return Collection + */ + public function getCollectionWithoutTemplateAttribute(): Collection + { + return new Collection(); + } + + public function getCollectionWithoutDocBlockAttribute(): Collection + { + return new Collection(); + } +} + + */ + + +namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\Models{ +/** + * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateMixinCollection\Models\WithCollection + * + * @property-read \Illuminate\Support\Collection $collection + * @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() + * @mixin \Eloquent + */ + #[\AllowDynamicProperties] + class IdeHelperWithCollection {} +} + From 1754dbb267de63ca0d7ce7535576b7087adf61c9 Mon Sep 17 00:00:00 2001 From: stefanScrumble <89080949+stefanScrumble@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:48:01 +0100 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70ed218..9bc1759 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,17 @@ All notable changes to this project will be documented in this file. ### Added - Add support for `immutable_date:*` and `immutable_datetime:*` casts. [#1380 / thekonz](https://github.com/barryvdh/laravel-ide-helper/pull/1380) +2024-01-04, v2.14.0 +------------------ + +### Fixes + +- Add AllowDynamicProperties when creating mixin: credit to GeoSot - https://github.com/GeoSot/laravel-ide-helper +- Fix for getSomethingAttribute functions which return a collection with type templating in the phpDoc. + +### Extra +This release contains all commits from the main laravel-ide-helper repo up to 04-01-2024. + 2023-02-04, 2.13.0 ------------------ From 456919de94ca8e12af0a60e6f2c527676d3af5c7 Mon Sep 17 00:00:00 2001 From: stefanScrumble <89080949+stefanScrumble@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:49:07 +0100 Subject: [PATCH 5/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bc1759..301458a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ All notable changes to this project will be documented in this file. ### Fixes - Add AllowDynamicProperties when creating mixin: credit to GeoSot - https://github.com/GeoSot/laravel-ide-helper -- Fix for getSomethingAttribute functions which return a collection with type templating in the phpDoc. +- Fix for getSomethingAttribute functions which return a collection with type templating in the phpDoc. https://github.com/scrumble-nl/laravel-ide-helper/pull/1 ### Extra This release contains all commits from the main laravel-ide-helper repo up to 04-01-2024. From 944fb325f47783a5b5da30a3eb62072afb261bfe Mon Sep 17 00:00:00 2001 From: stefanScrumble <89080949+stefanScrumble@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:50:25 +0100 Subject: [PATCH 6/7] Update CHANGELOG.md --- CHANGELOG.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 301458a..3983a3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,19 +2,6 @@ All notable changes to this project will be documented in this file. -[Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.13.0...master) --------------- - -### Changed -- Official support for Lumen has been dropped [#1425 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1425) -- Refactor resolving of null information for custom casted attribute types [#1330 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/1330) - -### Fixed -- Add support for attribute accessors marked as protected. [#1339 / pindab0ter](https://github.com/barryvdh/laravel-ide-helper/pull/1339) - -### Added -- Add support for `immutable_date:*` and `immutable_datetime:*` casts. [#1380 / thekonz](https://github.com/barryvdh/laravel-ide-helper/pull/1380) - 2024-01-04, v2.14.0 ------------------ From 7b65bdbc87533dd191d56924dee12562d46162ff Mon Sep 17 00:00:00 2001 From: stefanScrumble <89080949+stefanScrumble@users.noreply.github.com> Date: Fri, 5 Jan 2024 11:51:10 +0100 Subject: [PATCH 7/7] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2eecae2..bdea7a4 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "barryvdh/laravel-ide-helper", + "name": "scrumble-nl/laravel-ide-helper", "description": "Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.", "license": "MIT", "keywords": [