Skip to content

Commit 834dbb2

Browse files
committed
refactor: fix phpstan errors in Test
1 parent 92008b3 commit 834dbb2

18 files changed

Lines changed: 82 additions & 358 deletions

system/Test/Constraints/SeeInDatabase.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ class SeeInDatabase extends Constraint
3434
/**
3535
* Data used to compare results against.
3636
*
37-
* @var array
37+
* @var array<string, mixed>
3838
*/
3939
protected $data;
4040

4141
/**
4242
* SeeInDatabase constructor.
43+
*
44+
* @param array<string, mixed> $data
4345
*/
4446
public function __construct(ConnectionInterface $db, array $data)
4547
{

system/Test/ControllerTestTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ trait ControllerTestTrait
7676
/**
7777
* URI of this request.
7878
*
79-
* @var string
79+
* @var string|URI
8080
*/
8181
protected $uri = 'http://example.com';
8282

@@ -144,7 +144,7 @@ public function controller(string $name)
144144
/**
145145
* Runs the specified method on the controller and returns the results.
146146
*
147-
* @param array $params
147+
* @param mixed ...$params
148148
*
149149
* @return TestResponse
150150
*

system/Test/DOMParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ public function dontSeeXPath(string $path): bool
198198
/**
199199
* Search the DOM using an XPath expression.
200200
*
201+
* @param list<string> $paths
202+
*
201203
* @return DOMNodeList|false
202204
*/
203205
protected function doXPath(?string $search, string $element, array $paths = [])

system/Test/Fabricator.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Fabricator
3737
/**
3838
* Array of counts for fabricated items
3939
*
40-
* @var array
40+
* @var array<string, int>
4141
*/
4242
protected static $tableCounts = [];
4343

@@ -65,28 +65,28 @@ class Fabricator
6565
/**
6666
* Map of properties and their formatter to use
6767
*
68-
* @var array|null
68+
* @var array<string, string>|null
6969
*/
7070
protected $formatters;
7171

7272
/**
7373
* Date fields present in the model
7474
*
75-
* @var array
75+
* @var list<string>
7676
*/
7777
protected $dateFields = [];
7878

7979
/**
8080
* Array of data to add or override faked versions
8181
*
82-
* @var array
82+
* @var array<string, mixed>
8383
*/
8484
protected $overrides = [];
8585

8686
/**
8787
* Array of single-use data to override faked versions
8888
*
89-
* @var array|null
89+
* @var array<string, mixed>|null
9090
*/
9191
protected $tempOverrides;
9292

@@ -111,9 +111,9 @@ class Fabricator
111111
/**
112112
* Store the model instance and initialize Faker to the locale.
113113
*
114-
* @param object|string $model Instance or classname of the model to use
115-
* @param array|null $formatters Array of property => formatter
116-
* @param string|null $locale Locale for Faker provider
114+
* @param object|string $model Instance or classname of the model to use
115+
* @param array<string, string>|null $formatters Array of property => formatter
116+
* @param string|null $locale Locale for Faker provider
117117
*
118118
* @throws InvalidArgumentException
119119
*/
@@ -238,6 +238,8 @@ public function getFaker(): Generator
238238

239239
/**
240240
* Return and reset tempOverrides
241+
*
242+
* @return array<string, mixed>
241243
*/
242244
public function getOverrides(): array
243245
{
@@ -251,8 +253,8 @@ public function getOverrides(): array
251253
/**
252254
* Set the overrides, once or persistent
253255
*
254-
* @param array $overrides Array of [field => value]
255-
* @param bool $persist Whether these overrides should persist through the next operation
256+
* @param array<string, mixed> $overrides Array of [field => value]
257+
* @param bool $persist Whether these overrides should persist through the next operation
256258
*/
257259
public function setOverrides(array $overrides = [], $persist = true): self
258260
{
@@ -307,6 +309,8 @@ public function setValid(string $field, ?Closure $validator = null, int $maxRetr
307309

308310
/**
309311
* Returns the current formatters
312+
*
313+
* @return array<string, string>|null
310314
*/
311315
public function getFormatters(): ?array
312316
{
@@ -316,7 +320,7 @@ public function getFormatters(): ?array
316320
/**
317321
* Set the formatters to use. Will attempt to autodetect if none are available.
318322
*
319-
* @param array|null $formatters Array of [field => formatter], or null to detect
323+
* @param array<string, string>|null $formatters Array of [field => formatter], or null to detect
320324
*/
321325
public function setFormatters(?array $formatters = null): self
322326
{
@@ -399,7 +403,7 @@ protected function guessFormatter($field): string
399403
*
400404
* @param int|null $count Optional number to create a collection
401405
*
402-
* @return array|object An array or object (based on returnType), or an array of returnTypes
406+
* @return array<string, mixed>|list<array<string, mixed>|object>|object An array or object (based on returnType), or an array of returnTypes
403407
*/
404408
public function make(?int $count = null)
405409
{
@@ -424,7 +428,7 @@ public function make(?int $count = null)
424428
/**
425429
* Generate an array of faked data
426430
*
427-
* @return array An array of faked data
431+
* @return array<string, mixed> An array of faked data
428432
*
429433
* @throws RuntimeException
430434
*/
@@ -534,7 +538,7 @@ public function makeObject(?string $className = null): object
534538
* @param int|null $count Optional number to create a collection
535539
* @param bool $mock Whether to execute or mock the insertion
536540
*
537-
* @return array|object An array or object (based on returnType), or an array of returnTypes
541+
* @return array<string, mixed>|list<array<string, mixed>|object>|object|null An array or object (based on returnType), or an array of returnTypes
538542
*
539543
* @throws FrameworkException
540544
*/
@@ -572,7 +576,7 @@ public function create(?int $count = null, bool $mock = false)
572576
*
573577
* @param int|null $count Optional number to create a collection
574578
*
575-
* @return array|object An array or object (based on returnType), or an array of returnTypes
579+
* @return array<string, mixed>|list<array<string, mixed>|object>|object An array or object (based on returnType), or an array of returnTypes
576580
*/
577581
protected function createMock(?int $count = null)
578582
{

system/Test/FeatureTestTrait.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ public function skipEvents()
179179
* Calls a single URI, executes it, and returns a TestResponse
180180
* instance that can be used to run many assertions against.
181181
*
182-
* @param string $method HTTP verb
182+
* @param string $method HTTP verb
183+
* @param array<array-key, mixed>|null $params
183184
*
184185
* @return TestResponse
185186
*/
@@ -242,7 +243,8 @@ public function call(string $method, string $path, ?array $params = null)
242243
/**
243244
* Performs a GET request.
244245
*
245-
* @param string $path URI path relative to baseURL. May include query.
246+
* @param string $path URI path relative to baseURL. May include query.
247+
* @param array<array-key, mixed>|null $params
246248
*
247249
* @return TestResponse
248250
*
@@ -257,6 +259,8 @@ public function get(string $path, ?array $params = null)
257259
/**
258260
* Performs a POST request.
259261
*
262+
* @param array<array-key, mixed>|null $params
263+
*
260264
* @return TestResponse
261265
*
262266
* @throws RedirectException
@@ -270,6 +274,8 @@ public function post(string $path, ?array $params = null)
270274
/**
271275
* Performs a PUT request
272276
*
277+
* @param array<array-key, mixed>|null $params
278+
*
273279
* @return TestResponse
274280
*
275281
* @throws RedirectException
@@ -283,6 +289,8 @@ public function put(string $path, ?array $params = null)
283289
/**
284290
* Performss a PATCH request
285291
*
292+
* @param array<array-key, mixed>|null $params
293+
*
286294
* @return TestResponse
287295
*
288296
* @throws RedirectException
@@ -296,6 +304,8 @@ public function patch(string $path, ?array $params = null)
296304
/**
297305
* Performs a DELETE request.
298306
*
307+
* @param array<array-key, mixed>|null $params
308+
*
299309
* @return TestResponse
300310
*
301311
* @throws RedirectException
@@ -309,6 +319,8 @@ public function delete(string $path, ?array $params = null)
309319
/**
310320
* Performs an OPTIONS request.
311321
*
322+
* @param array<array-key, mixed>|null $params
323+
*
312324
* @return TestResponse
313325
*
314326
* @throws RedirectException
@@ -379,10 +391,11 @@ protected function setupHeaders(IncomingRequest $request)
379391
*
380392
* Always populate the GET vars based on the URI.
381393
*
382-
* @param string $name Superglobal name (lowercase)
383-
* @param non-empty-array|null $params
394+
* @param string $name Superglobal name (lowercase)
395+
* @param IncomingRequest $request
396+
* @param non-empty-array<array-key, mixed>|null $params
384397
*
385-
* @return Request
398+
* @return IncomingRequest
386399
*
387400
* @throws ReflectionException
388401
*/
@@ -418,7 +431,10 @@ protected function populateGlobals(string $name, Request $request, ?array $param
418431
* This allows the body to be formatted in a way that the controller is going to
419432
* expect as in the case of testing a JSON or XML API.
420433
*
421-
* @param array|null $params The parameters to be formatted and put in the body.
434+
* @param IncomingRequest $request
435+
* @param array<array-key, mixed>|null $params The parameters to be formatted and put in the body.
436+
*
437+
* @return IncomingRequest
422438
*/
423439
protected function setRequestBody(Request $request, ?array $params = null): Request
424440
{

system/Test/IniTestTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515

1616
trait IniTestTrait
1717
{
18+
/**
19+
* @var array<string, false|string>
20+
*/
1821
private array $iniSettings = [];
1922

23+
/**
24+
* @param list<string> $keys
25+
*/
2026
private function backupIniValues(array $keys): void
2127
{
2228
foreach ($keys as $key) {

system/Test/TestResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ public function getJSON()
357357

358358
/**
359359
* Test that the response contains a matching JSON fragment.
360+
*
361+
* @param array<array-key, mixed> $fragment
360362
*/
361363
public function assertJSONFragment(array $fragment, bool $strict = false): void
362364
{
@@ -378,7 +380,7 @@ public function assertJSONFragment(array $fragment, bool $strict = false): void
378380
* Asserts that the JSON exactly matches the passed in data.
379381
* If the value being passed in is a string, it must be a json_encoded string.
380382
*
381-
* @param array|object|string $test
383+
* @param array<array-key, mixed>|object|string $test
382384
*/
383385
public function assertJSONExact($test): void
384386
{

tests/system/Test/ControllerTestTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function testFailsForward(): void
222222
->execute('index');
223223

224224
// won't fail, but doesn't do anything
225-
$this->assertNull($result->ohno('Hi'));
225+
$this->assertNull($result->ohno('Hi')); // @phpstan-ignore method.notFound (Testing TestResponse::__call() fallback)
226226
}
227227

228228
/**

tests/system/Test/DOMParserTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public function testSeeElement($text): void
144144
$this->assertTrue($dom->see($text, 'h1'));
145145
}
146146

147+
/**
148+
* @return iterable<string, array{string}>
149+
*/
147150
public static function provideText(): iterable
148151
{
149152
return [

tests/system/Test/FabricatorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class FabricatorTest extends CIUnitTestCase
3131
{
3232
/**
3333
* Default formatters to use for UserModel. Should match detected version.
34+
*
35+
* @var array<string, string>
3436
*/
3537
private array $formatters = [
3638
'name' => 'name',

0 commit comments

Comments
 (0)