From 9ae7f43a8180457c85c562f913f2e958ed4486fe Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 12 Apr 2024 16:13:13 +0900 Subject: [PATCH 1/5] refactor: update param name --- system/Test/Interfaces/FabricatorModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/Interfaces/FabricatorModel.php b/system/Test/Interfaces/FabricatorModel.php index d4a4c270919c..186f1d4cc468 100644 --- a/system/Test/Interfaces/FabricatorModel.php +++ b/system/Test/Interfaces/FabricatorModel.php @@ -51,7 +51,7 @@ public function find($id = null); * * @throws ReflectionException */ - public function insert($data = null, bool $returnID = true); + public function insert($row = null, bool $returnID = true); /** * The following properties and methods are optional, but if present should From c02ce684fba57bd17499eea951bcc88853f5e528 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 12 Apr 2024 16:15:15 +0900 Subject: [PATCH 2/5] docs: make array type more precise --- system/Test/Interfaces/FabricatorModel.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/system/Test/Interfaces/FabricatorModel.php b/system/Test/Interfaces/FabricatorModel.php index 186f1d4cc468..a5860e22fb83 100644 --- a/system/Test/Interfaces/FabricatorModel.php +++ b/system/Test/Interfaces/FabricatorModel.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Test\Interfaces; +use CodeIgniter\BaseModel; use Faker\Generator; use ReflectionException; @@ -27,6 +28,8 @@ * @property string $returnType * @property string $primaryKey * @property string $dateFormat + * + * @phpstan-import-type row_array from BaseModel */ interface FabricatorModel { @@ -34,9 +37,9 @@ interface FabricatorModel * Fetches the row of database from $this->table with a primary key * matching $id. * - * @param array|mixed|null $id One primary key or an array of primary keys + * @param int|list|string|null $id One primary key or an array of primary keys * - * @return array|object|null The resulting row of data, or null. + * @phpstan-return ($id is int|string ? row_array|object|null : list) */ public function find($id = null); @@ -44,8 +47,9 @@ public function find($id = null); * Inserts data into the current table. If an object is provided, * it will attempt to convert it to an array. * - * @param array|object $data - * @param bool $returnID Whether insert ID should be returned or not. + * @param array|object|null $row + * @phpstan-param row_array|object|null $row + * @param bool $returnID Whether insert ID should be returned or not. * * @return bool|int|string * From 3cd7572f0dbcd5397f2ef7d9c6b9fdb52e7d0f79 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 12 Apr 2024 16:15:57 +0900 Subject: [PATCH 3/5] refactor: add return type --- tests/_support/Models/FabricatorModel.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/_support/Models/FabricatorModel.php b/tests/_support/Models/FabricatorModel.php index 2903b5036402..1cae9394b9a3 100644 --- a/tests/_support/Models/FabricatorModel.php +++ b/tests/_support/Models/FabricatorModel.php @@ -28,8 +28,10 @@ class FabricatorModel extends Model 'description', ]; - // Return a faked entity - public function fake(Generator &$faker) + /** + * Return a faked entity + */ + public function fake(Generator &$faker): object { return (object) [ 'name' => $faker->ipv4(), From b7443388303801737cbbafb157f2bb5415821eaf Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 12 Apr 2024 16:16:18 +0900 Subject: [PATCH 4/5] chore: update phpstan-baseline --- phpstan-baseline.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index dd91befad54a..5734ae93e1ef 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -8616,16 +8616,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/Fabricator.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Method CodeIgniter\\\\Test\\\\Interfaces\\\\FabricatorModel\\:\\:find\\(\\) return type has no value type specified in iterable type array\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Test/Interfaces/FabricatorModel.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Method CodeIgniter\\\\Test\\\\Interfaces\\\\FabricatorModel\\:\\:insert\\(\\) has parameter \\$data with no value type specified in iterable type array\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/system/Test/Interfaces/FabricatorModel.php', -]; $ignoreErrors[] = [ 'message' => '#^Property CodeIgniter\\\\Test\\\\Mock\\\\MockBuilder\\:\\:\\$supportedIgnoreStatements type has no value type specified in iterable type array\\.$#', 'count' => 1, @@ -10401,11 +10391,6 @@ 'count' => 1, 'path' => __DIR__ . '/tests/_support/Models/EventModel.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Method Tests\\\\Support\\\\Models\\\\FabricatorModel\\:\\:fake\\(\\) has no return type specified\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/_support/Models/FabricatorModel.php', -]; $ignoreErrors[] = [ 'message' => '#^Property Tests\\\\Support\\\\Models\\\\JobModel\\:\\:\\$description has no type specified\\.$#', 'count' => 1, From ba4d372b52c3f8d9bebae84a8d6dad2616b21f17 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 12 Apr 2024 16:26:48 +0900 Subject: [PATCH 5/5] docs: add method stubs to sample Model code --- user_guide_src/source/testing/fabricator/001.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/user_guide_src/source/testing/fabricator/001.php b/user_guide_src/source/testing/fabricator/001.php index 8fbf136032ca..f237d56b1324 100644 --- a/user_guide_src/source/testing/fabricator/001.php +++ b/user_guide_src/source/testing/fabricator/001.php @@ -6,5 +6,15 @@ class MyModel implements FabricatorModel { + public function find($id = null) + { + // TODO: Implement find() method. + } + + public function insert($row = null, bool $returnID = true) + { + // TODO: Implement insert() method. + } + // ... }