Skip to content

Commit

Permalink
[9.x] Add extra type checks for model factories (#41305)
Browse files Browse the repository at this point in the history
* Add extra type checks for model factories

* Fix code style

* Bump minimum PHPStan version
  • Loading branch information
axlon committed Mar 3, 2022
1 parent 0d67602 commit 5b113da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"mockery/mockery": "^1.4.4",
"orchestra/testbench-core": "^7.1",
"pda/pheanstalk": "^4.0",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan": "^1.4.7",
"phpunit/phpunit": "^9.5.8",
"predis/predis": "^1.1.9",
"symfony/cache": "^6.0"
Expand Down
21 changes: 14 additions & 7 deletions types/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function definition()
assertType('UserFactory', $factory = UserFactory::new());
assertType('UserFactory', UserFactory::new(['string' => 'string']));
assertType('UserFactory', UserFactory::new(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
assertType('array<string, mixed>', $attributes);

return ['string' => 'string'];
}));

Expand All @@ -44,7 +45,8 @@ public function definition()
assertType('array<int|string, mixed>', $factory->raw());
assertType('array<int|string, mixed>', $factory->raw(['string' => 'string']));
assertType('array<int|string, mixed>', $factory->raw(function ($attributes) {
// assert('array<string, mixed>', $attributes);
assertType('array<string, mixed>', $attributes);

return ['string' => 'string'];
}));

Expand All @@ -53,7 +55,8 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne());
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOne(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
assertType('array<string, mixed>', $attributes);

return ['string' => 'string'];
}));

Expand All @@ -62,7 +65,8 @@ public function definition()
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly());
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly(['string' => 'string']));
assertType('Illuminate\Database\Eloquent\Model', $factory->createOneQuietly(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
assertType('array<string, mixed>', $attributes);

return ['string' => 'string'];
}));

Expand All @@ -85,7 +89,8 @@ public function definition()
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->create(function ($attributes) {
// assertType('array<string, mixed>', $attributes);
assertType('array<string, mixed>', $attributes);

return ['string' => 'string'];
}));

Expand Down Expand Up @@ -116,7 +121,8 @@ public function definition()
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Model', $factory->makeOne(function ($attributes) {
// assert('array<string, mixed>', $attributes);
assertType('array<string, mixed>', $attributes);

return ['string' => 'string'];
}));

Expand All @@ -129,7 +135,8 @@ public function definition()
'string' => 'string',
]));
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model', $factory->make(function ($attributes) {
// assert('array<string, mixed>', $attributes);
assertType('array<string, mixed>', $attributes);

return ['string' => 'string'];
}));

Expand Down

0 comments on commit 5b113da

Please sign in to comment.