Skip to content

Commit 07f342d

Browse files
authored
Merge pull request #3 from mzur/laravel-9-patch-1
Fix failing tests for firstOrCreate, firstOrNew and PHP 8.1
2 parents e50f7f0 + 5fba336 commit 07f342d

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
"codeception/module-filesystem": "^3.0",
3434
"codeception/module-phpbrowser": "*@dev",
3535
"slevomat/coding-standard": "^6.2",
36-
"squizlabs/php_codesniffer": "*"
36+
"squizlabs/php_codesniffer": "*",
37+
"phpoption/phpoption": "^1.8.0",
38+
"symfony/http-foundation": "^5.3.7 || ^6.0",
39+
"ramsey/collection": "^1.2.0"
3740
},
3841
"autoload": {
3942
"psr-4": {

stubs/EloquentBuilder.stubphp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ class Builder
167167
*/
168168
public function firstOr($columns = ['*'], Closure $callback = null) { }
169169

170+
/**
171+
* @param array $attributes
172+
* @param array $values
173+
* @return TModel|self<TModel>
174+
*/
175+
public function firstOrNew(array $attributes = [], array $values = []) { }
176+
177+
/**
178+
* @param array $attributes
179+
* @param array $values
180+
* @return TModel|self<TModel>
181+
*/
182+
public function firstOrCreate(array $attributes = [], array $values = []) { }
183+
170184
/**
171185
* @param string $column
172186
* @return mixed

stubs/HasOneOrMany.stubphp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,20 @@ abstract class HasOneOrMany extends Relation
6666
* @psalm-return \Illuminate\Database\Eloquent\Collection<TRelatedModel>
6767
*/
6868
public function createMany(array $records) { }
69+
70+
/**
71+
* @param array $attributes
72+
* @param array $values
73+
* @return \Illuminate\Database\Eloquent\Model
74+
* @psalm-return TRelatedModel
75+
*/
76+
public function firstOrNew(array $attributes = [], array $values = []) { }
77+
78+
/**
79+
* @param array $attributes
80+
* @param array $values
81+
* @return \Illuminate\Database\Eloquent\Model
82+
* @psalm-return TRelatedModel
83+
*/
84+
public function firstOrCreate(array $attributes = [], array $values = []) { }
6985
}

tests/acceptance/EloquentBuilderTypes.feature

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Feature: Eloquent Builder types
178178
Then I see no errors
179179

180180
Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 8.x
181-
Given I have the "laravel/framework" package satisfying the ">= 8.0"
181+
Given I have the "laravel/framework" package satisfying the "^8.0"
182182
And I have the following code
183183
"""
184184
/**
@@ -200,6 +200,29 @@ Feature: Eloquent Builder types
200200
When I run Psalm
201201
Then I see no errors
202202

203+
Scenario: can call firstOrNew and firstOrCreate without parameters in Laravel 9.x
204+
Given I have the "laravel/framework" package satisfying the "^9.0"
205+
And I have the following code
206+
"""
207+
/**
208+
* @psalm-param Builder<User> $builder
209+
* @psalm-return Builder<User>|User
210+
*/
211+
function test_firstOrCreate(Builder $builder): Builder|User {
212+
return $builder->firstOrCreate();
213+
}
214+
215+
/**
216+
* @psalm-param Builder<User> $builder
217+
* @psalm-return Builder<User>|User
218+
*/
219+
function test_firstOrNew(Builder $builder): Builder|User {
220+
return $builder->firstOrNew();
221+
}
222+
"""
223+
When I run Psalm
224+
Then I see no errors
225+
203226
Scenario: can call whereDate with \DateTimeInterface|string|null
204227
Given I have the following code
205228
"""

0 commit comments

Comments
 (0)