Skip to content

Commit aa2dcd6

Browse files
authored
refactor: Improve types for phpstan (#9685)
* refactor: Improve types for phpstan * fix: Update phpstan baseline * fix: Remove phpDoc duplicate from parent
1 parent 769b679 commit aa2dcd6

13 files changed

+66
-253
lines changed

system/Common.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ function view_cell(string $library, $params = null, int $ttl = 0, ?string $cache
12261226
/**
12271227
* Get the class "basename" of the given object / class.
12281228
*
1229-
* @param object|string $class
1229+
* @param class-string|object $class
12301230
*
12311231
* @return string
12321232
*
@@ -1244,9 +1244,9 @@ function class_basename($class)
12441244
/**
12451245
* Returns all traits used by a class, its parent classes and trait of their traits.
12461246
*
1247-
* @param object|string $class
1247+
* @param class-string|object $class
12481248
*
1249-
* @return array
1249+
* @return array<class-string, class-string>
12501250
*
12511251
* @codeCoverageIgnore
12521252
*/
@@ -1270,9 +1270,9 @@ function class_uses_recursive($class)
12701270
/**
12711271
* Returns all traits used by a trait and its traits.
12721272
*
1273-
* @param string $trait
1273+
* @param class-string $trait
12741274
*
1275-
* @return array
1275+
* @return array<class-string, class-string>
12761276
*
12771277
* @codeCoverageIgnore
12781278
*/

system/Test/CIUnitTestCase.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CodeIgniter\Database\MigrationRunner;
2020
use CodeIgniter\Database\Seeder;
2121
use CodeIgniter\Events\Events;
22+
use CodeIgniter\HTTP\Header;
2223
use CodeIgniter\Router\RouteCollection;
2324
use CodeIgniter\Session\Handlers\ArrayHandler;
2425
use CodeIgniter\Test\Mock\MockCache;
@@ -72,6 +73,8 @@ abstract class CIUnitTestCase extends TestCase
7273

7374
/**
7475
* Store of identified traits.
76+
*
77+
* @var array<class-string, class-string>|null
7578
*/
7679
private ?array $traits = null;
7780

@@ -109,9 +112,9 @@ abstract class CIUnitTestCase extends TestCase
109112

110113
/**
111114
* The seed file(s) used for all tests within this test case.
112-
* Should be fully-namespaced or relative to $basePath
115+
* Should be fully-namespaced or relative to $basePath.
113116
*
114-
* @var class-string<Seeder>|list<class-string<Seeder>>
117+
* @var ''|class-string<Seeder>|list<class-string<Seeder>>
115118
*/
116119
protected $seed = '';
117120

@@ -127,9 +130,9 @@ abstract class CIUnitTestCase extends TestCase
127130
* The namespace(s) to help us find the migration classes.
128131
* `null` is equivalent to running `spark migrate --all`.
129132
* Note that running "all" runs migrations in date order,
130-
* but specifying namespaces runs them in namespace order (then date)
133+
* but specifying namespaces runs them in namespace order (then date).
131134
*
132-
* @var array|string|null
135+
* @var list<string>|string|null
133136
*/
134137
protected $namespace = 'Tests\Support';
135138

@@ -156,17 +159,17 @@ abstract class CIUnitTestCase extends TestCase
156159
protected $migrations;
157160

158161
/**
159-
* Seeder instance
162+
* Seeder instance.
160163
*
161-
* @var Seeder
164+
* @var Seeder|null
162165
*/
163166
protected $seeder;
164167

165168
/**
166169
* Stores information needed to remove any
167-
* rows inserted via $this->hasInDatabase();
170+
* rows inserted via $this->hasInDatabase().
168171
*
169-
* @var array
172+
* @var list<array<int|string, mixed>>
170173
*/
171174
protected $insertCache = [];
172175

@@ -186,27 +189,27 @@ abstract class CIUnitTestCase extends TestCase
186189
* Values to be set in the SESSION global
187190
* before running the test.
188191
*
189-
* @var array
192+
* @var array<int|string, mixed>
190193
*/
191194
protected $session = [];
192195

193196
/**
194-
* Enabled auto clean op buffer after request call
197+
* Enabled auto clean op buffer after request call.
195198
*
196199
* @var bool
197200
*/
198201
protected $clean = true;
199202

200203
/**
201-
* Custom request's headers
204+
* Custom request's headers.
202205
*
203-
* @var array
206+
* @var array<string, Header|list<Header>>
204207
*/
205208
protected $headers = [];
206209

207210
/**
208211
* Allows for formatting the request body to what
209-
* the controller is going to expect
212+
* the controller is going to expect.
210213
*
211214
* @var string
212215
*/
@@ -276,7 +279,7 @@ protected function tearDown(): void
276279
* Checks for traits with corresponding
277280
* methods for setUp or tearDown.
278281
*
279-
* @param string $stage 'setUp' or 'tearDown'
282+
* @param 'setUp'|'tearDown' $stage
280283
*/
281284
private function callTraitMethods(string $stage): void
282285
{
@@ -298,7 +301,7 @@ private function callTraitMethods(string $stage): void
298301
// --------------------------------------------------------------------
299302

300303
/**
301-
* Resets shared instanced for all Factories components
304+
* Resets shared instanced for all Factories components.
302305
*
303306
* @return void
304307
*/
@@ -308,7 +311,7 @@ protected function resetFactories()
308311
}
309312

310313
/**
311-
* Resets shared instanced for all Services
314+
* Resets shared instanced for all Services.
312315
*
313316
* @return void
314317
*/
@@ -318,7 +321,7 @@ protected function resetServices(bool $initAutoloader = true)
318321
}
319322

320323
/**
321-
* Injects the mock Cache driver to prevent filesystem collisions
324+
* Injects the mock Cache driver to prevent filesystem collisions.
322325
*
323326
* @return void
324327
*/
@@ -328,7 +331,7 @@ protected function mockCache()
328331
}
329332

330333
/**
331-
* Injects the mock email driver so no emails really send
334+
* Injects the mock email driver so no emails really send.
332335
*
333336
* @return void
334337
*/
@@ -338,7 +341,7 @@ protected function mockEmail()
338341
}
339342

340343
/**
341-
* Injects the mock session driver into Services
344+
* Injects the mock session driver into Services.
342345
*
343346
* @return void
344347
*/

system/Test/DatabaseTestTrait.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
namespace CodeIgniter\Test;
1515

1616
use CodeIgniter\Database\BaseBuilder;
17+
use CodeIgniter\Database\BaseConnection;
1718
use CodeIgniter\Database\Exceptions\DatabaseException;
19+
use CodeIgniter\Database\MigrationRunner;
20+
use CodeIgniter\Database\Seeder;
1821
use CodeIgniter\Test\Constraints\SeeInDatabase;
1922
use Config\Database;
2023
use Config\Migrations;
21-
use Config\Services;
2224
use PHPUnit\Framework\Attributes\AfterClass;
2325

2426
/**
@@ -27,6 +29,12 @@
2729
* Provides functionality for refreshing/seeding
2830
* the database during testing.
2931
*
32+
* @property BaseConnection $db
33+
* @property list<array<int|string, mixed>> $insertCache
34+
* @property Seeder|null $seeder
35+
* @property MigrationRunner|null $migrations
36+
* @property list<string>|string|null $namespace
37+
*
3038
* @mixin CIUnitTestCase
3139
*/
3240
trait DatabaseTestTrait
@@ -88,7 +96,7 @@ public function loadDependencies()
8896
$config = new Migrations();
8997
$config->enabled = true;
9098

91-
$this->migrations = Services::migrations($config, $this->db, false);
99+
$this->migrations = service('migrations', $config, $this->db, false);
92100
$this->migrations->setSilent(false);
93101
}
94102

system/Test/FeatureTestTrait.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313

1414
namespace CodeIgniter\Test;
1515

16+
use Closure;
1617
use CodeIgniter\Events\Events;
1718
use CodeIgniter\HTTP\Exceptions\RedirectException;
19+
use CodeIgniter\HTTP\Header;
1820
use CodeIgniter\HTTP\IncomingRequest;
1921
use CodeIgniter\HTTP\Method;
2022
use CodeIgniter\HTTP\Request;
23+
use CodeIgniter\HTTP\ResponseInterface;
2124
use CodeIgniter\HTTP\SiteURI;
2225
use CodeIgniter\HTTP\URI;
26+
use CodeIgniter\Router\RouteCollection;
2327
use Config\App;
2428
use Config\Services;
2529
use Exception;
@@ -30,6 +34,12 @@
3034
*
3135
* Provides additional utilities for doing full HTTP testing
3236
* against your application in trait format.
37+
*
38+
* @property array<int|string, mixed> $session
39+
* @property array<string, Header|list<Header>> $headers
40+
* @property RouteCollection|null $routes
41+
*
42+
* @mixin CIUnitTestCase
3343
*/
3444
trait FeatureTestTrait
3545
{
@@ -42,7 +52,12 @@ trait FeatureTestTrait
4252
* ['GET', 'home', 'Home::index'],
4353
* ]
4454
*
45-
* @param array|null $routes Array to set routes
55+
* @param array<int, array{
56+
* 0: string,
57+
* 1: string,
58+
* 2: ((Closure(mixed...): (ResponseInterface|string|void)))|string,
59+
* 3?: array<string, mixed>
60+
* }>|null $routes Array to set routes
4661
*
4762
* @return $this
4863
*/
@@ -84,7 +99,7 @@ protected function withRoutes(?array $routes = null)
8499
/**
85100
* Sets any values that should exist during this session.
86101
*
87-
* @param array|null $values Array of values, or null to use the current $_SESSION
102+
* @param array<int|string, mixed>|null $values Array of values, or null to use the current $_SESSION
88103
*
89104
* @return $this
90105
*/
@@ -103,7 +118,7 @@ public function withSession(?array $values = null)
103118
* 'Authorization' => 'Token'
104119
* ])
105120
*
106-
* @param array $headers Array of headers
121+
* @param array<string, Header|list<Header>> $headers Array of headers
107122
*
108123
* @return $this
109124
*/

tests/system/Database/DatabaseTestCase/DatabaseTestCaseMigrationOnce1Test.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,9 @@ final class DatabaseTestCaseMigrationOnce1Test extends CIUnitTestCase
2929
{
3030
use DatabaseTestTrait;
3131

32-
/**
33-
* Should run db migration only once?
34-
*
35-
* @var bool
36-
*/
3732
protected $migrateOnce = true;
38-
39-
/**
40-
* Should the db be refreshed before test?
41-
*
42-
* @var bool
43-
*/
44-
protected $refresh = true;
45-
46-
/**
47-
* The namespace(s) to help us find the migration classes.
48-
* Empty is equivalent to running `spark migrate -all`.
49-
* Note that running "all" runs migrations in date order,
50-
* but specifying namespaces runs them in namespace order (then date)
51-
*
52-
* @var array|string|null
53-
*/
54-
protected $namespace = [
33+
protected $refresh = true;
34+
protected $namespace = [
5535
'Tests\Support\MigrationTestMigrations',
5636
];
5737

tests/system/Database/DatabaseTestCase/DatabaseTestCaseMigrationOnce2Test.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,9 @@ final class DatabaseTestCaseMigrationOnce2Test extends CIUnitTestCase
2929
{
3030
use DatabaseTestTrait;
3131

32-
/**
33-
* Should run db migration only once?
34-
*
35-
* @var bool
36-
*/
3732
protected $migrateOnce = true;
38-
39-
/**
40-
* Should the db be refreshed before test?
41-
*
42-
* @var bool
43-
*/
44-
protected $refresh = true;
45-
46-
/**
47-
* The namespace(s) to help us find the migration classes.
48-
* Empty is equivalent to running `spark migrate -all`.
49-
* Note that running "all" runs migrations in date order,
50-
* but specifying namespaces runs them in namespace order (then date)
51-
*
52-
* @var array|string|null
53-
*/
54-
protected $namespace = [
33+
protected $refresh = true;
34+
protected $namespace = [
5535
'Tests\Support\MigrationTestMigrations',
5636
];
5737

tests/system/Database/DatabaseTestCaseTest.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,11 @@ final class DatabaseTestCaseTest extends CIUnitTestCase
2828
{
2929
use DatabaseTestTrait;
3030

31-
/**
32-
* Should the db be refreshed before
33-
* each test?
34-
*
35-
* @var bool
36-
*/
3731
protected $refresh = true;
38-
39-
/**
40-
* The seed file(s) used for all tests within this test case.
41-
* Should be fully-namespaced or relative to $basePath
42-
*
43-
* @var array|string
44-
*/
45-
protected $seed = [
32+
protected $seed = [
4633
CITestSeeder::class,
4734
AnotherSeeder::class,
4835
];
49-
50-
/**
51-
* The namespace(s) to help us find the migration classes.
52-
* Empty is equivalent to running `spark migrate -all`.
53-
* Note that running "all" runs migrations in date order,
54-
* but specifying namespaces runs them in namespace order (then date)
55-
*
56-
* @var array|string|null
57-
*/
5836
protected $namespace = [
5937
'Tests\Support',
6038
'Tests\Support\MigrationTestMigrations',

tests/system/Database/Live/MetadataTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ final class MetadataTest extends CIUnitTestCase
2727
{
2828
use DatabaseTestTrait;
2929

30-
/**
31-
* The seed file used for all tests within this test case.
32-
*
33-
* @var string
34-
*/
35-
protected $seed = CITestSeeder::class;
36-
30+
protected $seed = CITestSeeder::class;
3731
private array $expectedTables = [];
3832

3933
protected function setUp(): void

0 commit comments

Comments
 (0)