From f64ff9c0b1fa12dff9d79fadb118019b062a512e Mon Sep 17 00:00:00 2001 From: Ruben Robles Date: Wed, 7 Feb 2024 21:27:30 +0100 Subject: [PATCH] Add support for enum default arguments using enum cases (#1464) * add case when default value is of type enum * Update CHANGELOG.md * add tests for enum arguments default values * ignore psalm issue (built-in `\UnitEnum` class does not exists in PHP7) * Update run-static-analysis.yml to use PHP8.1 --------- Co-authored-by: Barry vd. Heuvel Co-authored-by: Barry vd. Heuvel --- .github/workflows/run-static-analysis.yml | 2 +- CHANGELOG.md | 4 + psalm-baseline.xml | 5 + src/Console/ModelsCommand.php | 2 + .../Enums/PostStatus.php | 12 ++ .../Models/Post.php | 17 ++ .../Test.php | 30 +++ .../__snapshots__/Test__test__1.php | 172 ++++++++++++++++++ 8 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/Enums/PostStatus.php create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/Models/Post.php create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/Test.php create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/__snapshots__/Test__test__1.php diff --git a/.github/workflows/run-static-analysis.yml b/.github/workflows/run-static-analysis.yml index 1085dfe..c8c29bb 100644 --- a/.github/workflows/run-static-analysis.yml +++ b/.github/workflows/run-static-analysis.yml @@ -17,7 +17,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.0 + php-version: 8.1 coverage: none extensions: pdo_sqlite diff --git a/CHANGELOG.md b/CHANGELOG.md index 16fa904..d638d2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file. ### Changed - Removed support for Laravel 8 and therefore for PHP < 8.0 [#1504 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1504) + +### Added +- Add support for enum default arguments using enum cases. [#1464 / d8vjork](https://github.com/barryvdh/laravel-ide-helper/pull/1464) + 2024-02-05, 2.14.0 ------------------ diff --git a/psalm-baseline.xml b/psalm-baseline.xml index f6a5b14..84de223 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -16,4 +16,9 @@ \Storage + + + \UnitEnum + + diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index c2ce4a6..13735c2 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1099,6 +1099,8 @@ class ModelsCommand extends Command $default = 'null'; } elseif (is_int($default)) { //$default = $default; + } elseif ($default instanceof \UnitEnum) { + $default = '\\' . get_class($default) . '::' . $default->name; } else { $default = "'" . trim($default) . "'"; } diff --git a/tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/Enums/PostStatus.php b/tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/Enums/PostStatus.php new file mode 100644 index 0000000..afe6b9b --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/Enums/PostStatus.php @@ -0,0 +1,12 @@ +=')) { + $this->markTestSkipped( + 'This test only works in PHP >= 8.1' + ); + } + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/__snapshots__/Test__test__1.php new file mode 100644 index 0000000..b4229da --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateBasicPhpDocWithEnumDefaults/__snapshots__/Test__test__1.php @@ -0,0 +1,172 @@ +