Skip to content

Commit

Permalink
Merge pull request #8770 from kenjis/fix-type-FabricatorModel
Browse files Browse the repository at this point in the history
refactor: FabricatorModel
  • Loading branch information
kenjis authored Apr 12, 2024
2 parents 6834870 + ba4d372 commit 04394c6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
15 changes: 0 additions & 15 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -8601,16 +8601,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,
Expand Down Expand Up @@ -10386,11 +10376,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,
Expand Down
14 changes: 9 additions & 5 deletions system/Test/Interfaces/FabricatorModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Test\Interfaces;

use CodeIgniter\BaseModel;
use Faker\Generator;
use ReflectionException;

Expand All @@ -27,31 +28,34 @@
* @property string $returnType
* @property string $primaryKey
* @property string $dateFormat
*
* @phpstan-import-type row_array from BaseModel
*/
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<int|string>|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<row_array|object>)
*/
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
*
* @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
Expand Down
6 changes: 4 additions & 2 deletions tests/_support/Models/FabricatorModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 10 additions & 0 deletions user_guide_src/source/testing/fabricator/001.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

// ...
}

0 comments on commit 04394c6

Please sign in to comment.