Skip to content

Commit 4d1a978

Browse files
d8vjorkbarryvdh
authored andcommitted
Add support for enum default arguments using enum cases (barryvdh#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 <[email protected]> Co-authored-by: Barry vd. Heuvel <[email protected]>
1 parent af137f1 commit 4d1a978

File tree

8 files changed

+243
-1
lines changed

8 files changed

+243
-1
lines changed

.github/workflows/run-static-analysis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Setup PHP
1818
uses: shivammathur/setup-php@v2
1919
with:
20-
php-version: 8.0
20+
php-version: 8.1
2121
coverage: none
2222
extensions: pdo_sqlite
2323

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
88
### Changed
99
- Removed support for Laravel 8 and therefore for PHP < 8.0 [#1504 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1504)
1010

11+
12+
### Added
13+
- Add support for enum default arguments using enum cases. [#1464 / d8vjork](https://github.com/barryvdh/laravel-ide-helper/pull/1464)
14+
1115
2024-02-05, 2.14.0
1216
------------------
1317

psalm-baseline.xml

+5
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@
1616
<code>\Storage</code>
1717
</UndefinedClass>
1818
</file>
19+
<file src="src/Console/ModelsCommand.php">
20+
<UndefinedClass occurrences="1">
21+
<code>\UnitEnum</code>
22+
</UndefinedClass>
23+
</file>
1924
</files>

src/Console/ModelsCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,8 @@ public function getParameters($method)
11101110
$default = 'null';
11111111
} elseif (is_int($default)) {
11121112
//$default = $default;
1113+
} elseif ($default instanceof \UnitEnum) {
1114+
$default = '\\' . get_class($default) . '::' . $default->name;
11131115
} else {
11141116
$default = "'" . trim($default) . "'";
11151117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Enums;
4+
5+
enum PostStatus
6+
{
7+
case Published;
8+
9+
case Unpublished;
10+
11+
case Archived;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Enums\PostStatus;
10+
11+
class Post extends Model
12+
{
13+
public function scopeHasStatus(Builder $query, ?PostStatus $status = PostStatus::Published)
14+
{
15+
//
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
9+
10+
class Test extends AbstractModelsCommand
11+
{
12+
public function test(): void
13+
{
14+
if (!version_compare(PHP_VERSION, '8.1', '>=')) {
15+
$this->markTestSkipped(
16+
'This test only works in PHP >= 8.1'
17+
);
18+
}
19+
20+
$command = $this->app->make(ModelsCommand::class);
21+
22+
$tester = $this->runCommand($command, [
23+
'--write' => true,
24+
]);
25+
26+
$this->assertSame(0, $tester->getStatusCode());
27+
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
28+
$this->assertMatchesMockedSnapshot();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Models;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Enums\PostStatus;
10+
11+
/**
12+
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Models\Post
13+
*
14+
* @property integer $id
15+
* @property string|null $char_nullable
16+
* @property string $char_not_nullable
17+
* @property string|null $string_nullable
18+
* @property string $string_not_nullable
19+
* @property string|null $text_nullable
20+
* @property string $text_not_nullable
21+
* @property string|null $medium_text_nullable
22+
* @property string $medium_text_not_nullable
23+
* @property string|null $long_text_nullable
24+
* @property string $long_text_not_nullable
25+
* @property integer|null $integer_nullable
26+
* @property integer $integer_not_nullable
27+
* @property integer|null $tiny_integer_nullable
28+
* @property integer $tiny_integer_not_nullable
29+
* @property integer|null $small_integer_nullable
30+
* @property integer $small_integer_not_nullable
31+
* @property integer|null $medium_integer_nullable
32+
* @property integer $medium_integer_not_nullable
33+
* @property integer|null $big_integer_nullable
34+
* @property integer $big_integer_not_nullable
35+
* @property integer|null $unsigned_integer_nullable
36+
* @property integer $unsigned_integer_not_nullable
37+
* @property integer|null $unsigned_tiny_integer_nullable
38+
* @property integer $unsigned_tiny_integer_not_nullable
39+
* @property integer|null $unsigned_small_integer_nullable
40+
* @property integer $unsigned_small_integer_not_nullable
41+
* @property integer|null $unsigned_medium_integer_nullable
42+
* @property integer $unsigned_medium_integer_not_nullable
43+
* @property integer|null $unsigned_big_integer_nullable
44+
* @property integer $unsigned_big_integer_not_nullable
45+
* @property float|null $float_nullable
46+
* @property float $float_not_nullable
47+
* @property float|null $double_nullable
48+
* @property float $double_not_nullable
49+
* @property string|null $decimal_nullable
50+
* @property string $decimal_not_nullable
51+
* @property string|null $unsigned_decimal_nullable
52+
* @property string $unsigned_decimal_not_nullable
53+
* @property integer|null $boolean_nullable
54+
* @property integer $boolean_not_nullable
55+
* @property string|null $enum_nullable
56+
* @property string $enum_not_nullable
57+
* @property string|null $json_nullable
58+
* @property string $json_not_nullable
59+
* @property string|null $jsonb_nullable
60+
* @property string $jsonb_not_nullable
61+
* @property string|null $date_nullable
62+
* @property string $date_not_nullable
63+
* @property string|null $datetime_nullable
64+
* @property string $datetime_not_nullable
65+
* @property string|null $datetimetz_nullable
66+
* @property string $datetimetz_not_nullable
67+
* @property string|null $time_nullable
68+
* @property string $time_not_nullable
69+
* @property string|null $timetz_nullable
70+
* @property string $timetz_not_nullable
71+
* @property string|null $timestamp_nullable
72+
* @property string $timestamp_not_nullable
73+
* @property string|null $timestamptz_nullable
74+
* @property string $timestamptz_not_nullable
75+
* @property integer|null $year_nullable
76+
* @property integer $year_not_nullable
77+
* @property mixed|null $binary_nullable
78+
* @property mixed $binary_not_nullable
79+
* @property string|null $uuid_nullable
80+
* @property string $uuid_not_nullable
81+
* @property string|null $ipaddress_nullable
82+
* @property string $ipaddress_not_nullable
83+
* @property string|null $macaddress_nullable
84+
* @property string $macaddress_not_nullable
85+
* @property \Illuminate\Support\Carbon|null $created_at
86+
* @property \Illuminate\Support\Carbon|null $updated_at
87+
* @method static Builder|Post hasStatus(?\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Enums\PostStatus $status = \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GenerateBasicPhpDocWithEnumDefaults\Enums\PostStatus::Published)
88+
* @method static Builder|Post newModelQuery()
89+
* @method static Builder|Post newQuery()
90+
* @method static Builder|Post query()
91+
* @method static Builder|Post whereBigIntegerNotNullable($value)
92+
* @method static Builder|Post whereBigIntegerNullable($value)
93+
* @method static Builder|Post whereBinaryNotNullable($value)
94+
* @method static Builder|Post whereBinaryNullable($value)
95+
* @method static Builder|Post whereBooleanNotNullable($value)
96+
* @method static Builder|Post whereBooleanNullable($value)
97+
* @method static Builder|Post whereCharNotNullable($value)
98+
* @method static Builder|Post whereCharNullable($value)
99+
* @method static Builder|Post whereCreatedAt($value)
100+
* @method static Builder|Post whereDateNotNullable($value)
101+
* @method static Builder|Post whereDateNullable($value)
102+
* @method static Builder|Post whereDatetimeNotNullable($value)
103+
* @method static Builder|Post whereDatetimeNullable($value)
104+
* @method static Builder|Post whereDatetimetzNotNullable($value)
105+
* @method static Builder|Post whereDatetimetzNullable($value)
106+
* @method static Builder|Post whereDecimalNotNullable($value)
107+
* @method static Builder|Post whereDecimalNullable($value)
108+
* @method static Builder|Post whereDoubleNotNullable($value)
109+
* @method static Builder|Post whereDoubleNullable($value)
110+
* @method static Builder|Post whereEnumNotNullable($value)
111+
* @method static Builder|Post whereEnumNullable($value)
112+
* @method static Builder|Post whereFloatNotNullable($value)
113+
* @method static Builder|Post whereFloatNullable($value)
114+
* @method static Builder|Post whereId($value)
115+
* @method static Builder|Post whereIntegerNotNullable($value)
116+
* @method static Builder|Post whereIntegerNullable($value)
117+
* @method static Builder|Post whereIpaddressNotNullable($value)
118+
* @method static Builder|Post whereIpaddressNullable($value)
119+
* @method static Builder|Post whereJsonNotNullable($value)
120+
* @method static Builder|Post whereJsonNullable($value)
121+
* @method static Builder|Post whereJsonbNotNullable($value)
122+
* @method static Builder|Post whereJsonbNullable($value)
123+
* @method static Builder|Post whereLongTextNotNullable($value)
124+
* @method static Builder|Post whereLongTextNullable($value)
125+
* @method static Builder|Post whereMacaddressNotNullable($value)
126+
* @method static Builder|Post whereMacaddressNullable($value)
127+
* @method static Builder|Post whereMediumIntegerNotNullable($value)
128+
* @method static Builder|Post whereMediumIntegerNullable($value)
129+
* @method static Builder|Post whereMediumTextNotNullable($value)
130+
* @method static Builder|Post whereMediumTextNullable($value)
131+
* @method static Builder|Post whereSmallIntegerNotNullable($value)
132+
* @method static Builder|Post whereSmallIntegerNullable($value)
133+
* @method static Builder|Post whereStringNotNullable($value)
134+
* @method static Builder|Post whereStringNullable($value)
135+
* @method static Builder|Post whereTextNotNullable($value)
136+
* @method static Builder|Post whereTextNullable($value)
137+
* @method static Builder|Post whereTimeNotNullable($value)
138+
* @method static Builder|Post whereTimeNullable($value)
139+
* @method static Builder|Post whereTimestampNotNullable($value)
140+
* @method static Builder|Post whereTimestampNullable($value)
141+
* @method static Builder|Post whereTimestamptzNotNullable($value)
142+
* @method static Builder|Post whereTimestamptzNullable($value)
143+
* @method static Builder|Post whereTimetzNotNullable($value)
144+
* @method static Builder|Post whereTimetzNullable($value)
145+
* @method static Builder|Post whereTinyIntegerNotNullable($value)
146+
* @method static Builder|Post whereTinyIntegerNullable($value)
147+
* @method static Builder|Post whereUnsignedBigIntegerNotNullable($value)
148+
* @method static Builder|Post whereUnsignedBigIntegerNullable($value)
149+
* @method static Builder|Post whereUnsignedDecimalNotNullable($value)
150+
* @method static Builder|Post whereUnsignedDecimalNullable($value)
151+
* @method static Builder|Post whereUnsignedIntegerNotNullable($value)
152+
* @method static Builder|Post whereUnsignedIntegerNullable($value)
153+
* @method static Builder|Post whereUnsignedMediumIntegerNotNullable($value)
154+
* @method static Builder|Post whereUnsignedMediumIntegerNullable($value)
155+
* @method static Builder|Post whereUnsignedSmallIntegerNotNullable($value)
156+
* @method static Builder|Post whereUnsignedSmallIntegerNullable($value)
157+
* @method static Builder|Post whereUnsignedTinyIntegerNotNullable($value)
158+
* @method static Builder|Post whereUnsignedTinyIntegerNullable($value)
159+
* @method static Builder|Post whereUpdatedAt($value)
160+
* @method static Builder|Post whereUuidNotNullable($value)
161+
* @method static Builder|Post whereUuidNullable($value)
162+
* @method static Builder|Post whereYearNotNullable($value)
163+
* @method static Builder|Post whereYearNullable($value)
164+
* @mixin \Eloquent
165+
*/
166+
class Post extends Model
167+
{
168+
public function scopeHasStatus(Builder $query, ?PostStatus $status = PostStatus::Published)
169+
{
170+
//
171+
}
172+
}

0 commit comments

Comments
 (0)