Skip to content

Commit ec0ea0e

Browse files
committed
Add type in iterable type array
1 parent cb67605 commit ec0ea0e

4 files changed

Lines changed: 108 additions & 50 deletions

File tree

phpstan-baseline.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@ parameters:
33
- # To be fixed in v2.0
44
message: '#__construct\(\).+but should not return anything#'
55
path: src/AbstractPaginator.php
6-
-
7-
message: '#no value type specified in iterable type array#'
8-
path: tests/

tests/AbtractPaginatorTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function testBadPageOption(mixed $page, int $expectedPage): void
3838
$this->assertFalse($paginator->pageExists());
3939
}
4040

41+
/**
42+
* @return array<array{mixed, int}>
43+
*/
4144
public static function getTestBadPageOptionProvider(): array
4245
{
4346
return [
@@ -186,6 +189,9 @@ public function testGetFirstIndice(mixed $page, int $maxPerPage, \ArrayIterator
186189
$this->assertSame($expectedResult, $paginator->getFirstIndice());
187190
}
188191

192+
/**
193+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, int}>
194+
*/
189195
public static function getTestGetFirstIndiceProvider(): array
190196
{
191197
return [
@@ -220,6 +226,9 @@ public function testGetLastIndice(mixed $page, int $maxPerPage, \ArrayIterator $
220226
$this->assertSame($expectedResult, $paginator->getLastIndice());
221227
}
222228

229+
/**
230+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, int}>
231+
*/
223232
public static function getTestGetLastIndiceProvider(): array
224233
{
225234
return [
@@ -254,6 +263,9 @@ public function testGetFirstPage(mixed $page, int $maxPerPage, \ArrayIterator $i
254263
$this->assertSame($expectedResult, $paginator->getFirstPage());
255264
}
256265

266+
/**
267+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, int}>
268+
*/
257269
public static function getTestGetFirstPageProvider(): array
258270
{
259271
return [
@@ -288,6 +300,9 @@ public function testGetPreviousPage(mixed $page, int $maxPerPage, \ArrayIterator
288300
$this->assertSame($expectedResult, $paginator->getPreviousPage());
289301
}
290302

303+
/**
304+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, ?int}>
305+
*/
291306
public static function getTestGetPreviousPageProvider(): array
292307
{
293308
return [
@@ -322,6 +337,9 @@ public function testGetPage(mixed $page, int $maxPerPage, \ArrayIterator $iterat
322337
$this->assertSame($expectedResult, $paginator->getPage());
323338
}
324339

340+
/**
341+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, int}>
342+
*/
325343
public static function getTestGetPageProvider(): array
326344
{
327345
return [
@@ -346,6 +364,9 @@ public function testPageExists(mixed $page, int $maxPerPage, \ArrayIterator $ite
346364
$this->assertSame($expectedResult, $paginator->pageExists());
347365
}
348366

367+
/**
368+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, bool}>
369+
*/
349370
public static function getTestPageExistsProdiver(): array
350371
{
351372
return [
@@ -370,6 +391,9 @@ public function testGetNextPage(mixed $page, int $maxPerPage, \ArrayIterator $it
370391
$this->assertSame($expectedResult, $paginator->getNextPage());
371392
}
372393

394+
/**
395+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, ?int}>
396+
*/
373397
public static function getTestGetNextPageProvider(): array
374398
{
375399
return [
@@ -404,6 +428,9 @@ public function testGetLastPage(mixed $page, int $maxPerPage, \ArrayIterator $it
404428
$this->assertSame($expectedResult, $paginator->getLastPage());
405429
}
406430

431+
/**
432+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, int}>
433+
*/
407434
public static function getTestGetLastPageProvider(): array
408435
{
409436
return [
@@ -438,6 +465,9 @@ public function testIsFirstPage(mixed $page, int $maxPerPage, \ArrayIterator $it
438465
$this->assertSame($expectedResult, $paginator->isFirstPage());
439466
}
440467

468+
/**
469+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, bool}>
470+
*/
441471
public static function getTestIsFirstPageProvider(): array
442472
{
443473
return [
@@ -472,6 +502,9 @@ public function testIsLastPage(mixed $page, int $maxPerPage, \ArrayIterator $ite
472502
$this->assertSame($expectedResult, $paginator->isLastPage());
473503
}
474504

505+
/**
506+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>, bool}>
507+
*/
475508
public static function getTestIsLastPageProvider(): array
476509
{
477510
return [
@@ -514,6 +547,9 @@ public function testGetIterator(mixed $page, int $maxPerPage, \ArrayIterator $it
514547
$this->assertSame($iterator, $paginator->getIterator());
515548
}
516549

550+
/**
551+
* @return array<array{mixed, int, \ArrayIterator<int|string, mixed>}>
552+
*/
517553
public static function getTestGetIteratorProvider(): array
518554
{
519555
return [

tests/ArrayPaginatorTest.php

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ArrayPaginatorTest extends TestCase
2525
{
2626
use BuildArrayIteratorTrait;
2727

28+
/** @var ?array<mixed> */
2829
protected static ?array $defaultArray = null;
2930

3031
public function testMissingDataOption(): void
@@ -70,8 +71,8 @@ public function testBadNumberCountOption(): void
7071
/**
7172
* @dataProvider getTestCountProvider
7273
*
73-
* @param int<1, max> $maxPerPage
74-
* @param \ArrayIterator<int|string, mixed>|array $data
74+
* @param int<1, max> $maxPerPage
75+
* @param \ArrayIterator<int|string, mixed>|array<mixed> $data
7576
*/
7677
public function testCount(mixed $page, int $maxPerPage, \ArrayIterator|array $data, int $expectedValue): void
7778
{
@@ -81,31 +82,34 @@ public function testCount(mixed $page, int $maxPerPage, \ArrayIterator|array $da
8182
$this->assertCount($expectedValue, $paginator);
8283
}
8384

85+
/**
86+
* @return array<array{mixed, int<1, max>, \ArrayIterator<int|string, mixed>|array<mixed>, int}>
87+
*/
8488
public static function getTestCountProvider(): array
8589
{
8690
return [
87-
[1, 5, static::getDefaultArray(), 52],
88-
[3, 5, static::getDefaultArray(), 52],
89-
[11, 5, static::getDefaultArray(), 52],
91+
[1, 5, static::getDefaultArray(), 52],
92+
[3, 5, static::getDefaultArray(), 52],
93+
[11, 5, static::getDefaultArray(), 52],
9094
[1, 5, [], 0], // No data
91-
['page', 5, static::getDefaultArray(), 52], // Bad page
92-
[1000, 5, static::getDefaultArray(), 52], // Page too high
95+
['page', 5, static::getDefaultArray(), 52], // Bad page
96+
[1000, 5, static::getDefaultArray(), 52], // Page too high
9397

94-
[1, 5, static::getDefaultIterator(), 52],
95-
[3, 5, static::getDefaultIterator(), 52],
96-
[11, 5, static::getDefaultIterator(), 52],
98+
[1, 5, static::getDefaultIterator(), 52],
99+
[3, 5, static::getDefaultIterator(), 52],
100+
[11, 5, static::getDefaultIterator(), 52],
97101
[1, 5, static::createIterator([]), 0], // No data
98-
['page', 5, static::getDefaultIterator(), 52], // Bad page
99-
[1000, 5, static::getDefaultIterator(), 52], // Page too high
102+
['page', 5, static::getDefaultIterator(), 52], // Bad page
103+
[1000, 5, static::getDefaultIterator(), 52], // Page too high
100104
];
101105
}
102106

103107
/**
104108
* @dataProvider getTestGetIteratorProvider
105109
*
106-
* @param int<1, max> $maxPerPage
107-
* @param \ArrayIterator<int|string, mixed>|array $data
108-
* @param \ArrayIterator<int|string, mixed> $expectedValue
110+
* @param int<1, max> $maxPerPage
111+
* @param \ArrayIterator<int|string, mixed>|array<mixed> $data
112+
* @param \ArrayIterator<int|string, mixed> $expectedValue
109113
*/
110114
public function testGetIterator(mixed $page, int $maxPerPage, \ArrayIterator|array $data, \ArrayIterator $expectedValue): void
111115
{
@@ -115,32 +119,35 @@ public function testGetIterator(mixed $page, int $maxPerPage, \ArrayIterator|arr
115119
$this->assertEquals($expectedValue, $paginator->getIterator());
116120
}
117121

122+
/**
123+
* @return array<array{mixed, int<1, max>, \ArrayIterator<int|string, mixed>|array<mixed>, \ArrayIterator<int|string, mixed>}>
124+
*/
118125
public static function getTestGetIteratorProvider(): array
119126
{
120127
return [
121-
[1, 5, static::getDefaultArray(), new \ArrayIterator([0, 1, 2, 3, 4])],
122-
[3, 5, static::getDefaultArray(), new \ArrayIterator([10, 11, 12, 13, 14])],
123-
[11, 5, static::getDefaultArray(), new \ArrayIterator([50, 51])],
124-
[1, 5, [], new \ArrayIterator()], // No data
125-
['page', 5, static::getDefaultArray(), new \ArrayIterator([0, 1, 2, 3, 4])], // Bad page
126-
[1000, 5, static::getDefaultArray(), new \ArrayIterator([50, 51])], // Page too high
127-
128-
[1, 5, static::getDefaultIterator(), new \ArrayIterator([0, 1, 2, 3, 4])],
129-
[3, 5, static::getDefaultIterator(), new \ArrayIterator([10, 11, 12, 13, 14])],
130-
[11, 5, static::getDefaultIterator(), new \ArrayIterator([50, 51])],
131-
[1, 5, static::createIterator([]), new \ArrayIterator()], // No data
132-
['page', 5, static::getDefaultIterator(), new \ArrayIterator([0, 1, 2, 3, 4])], // Bad page
133-
[1000, 5, static::getDefaultIterator(), new \ArrayIterator([50, 51])], // Page too high
128+
[1, 5, static::getDefaultArray(), static::createIterator([0, 1, 2, 3, 4])],
129+
[3, 5, static::getDefaultArray(), static::createIterator([10, 11, 12, 13, 14])],
130+
[11, 5, static::getDefaultArray(), static::createIterator([50, 51])],
131+
[1, 5, [], static::createIterator()], // No data
132+
['page', 5, static::getDefaultArray(), static::createIterator([0, 1, 2, 3, 4])], // Bad page
133+
[1000, 5, static::getDefaultArray(), static::createIterator([50, 51])], // Page too high
134+
135+
[1, 5, static::getDefaultIterator(), static::createIterator([0, 1, 2, 3, 4])],
136+
[3, 5, static::getDefaultIterator(), static::createIterator([10, 11, 12, 13, 14])],
137+
[11, 5, static::getDefaultIterator(), static::createIterator([50, 51])],
138+
[1, 5, static::createIterator([]), static::createIterator()], // No data
139+
['page', 5, static::getDefaultIterator(), static::createIterator([0, 1, 2, 3, 4])], // Bad page
140+
[1000, 5, static::getDefaultIterator(), static::createIterator([50, 51])], // Page too high
134141
];
135142
}
136143

137144
/**
138145
* @dataProvider getTestCountWithCountProvider
139146
*
140-
* @param int<1, max> $maxPerPage
141-
* @param \ArrayIterator<int|string, mixed>|array $data
142-
* @param \ArrayIterator<int|string, mixed> $expectedIterator
143-
* @param int<0, max> $count
147+
* @param int<1, max> $maxPerPage
148+
* @param \ArrayIterator<int|string, mixed>|array<mixed> $data
149+
* @param \ArrayIterator<int|string, mixed> $expectedIterator
150+
* @param int<0, max> $count
144151
*/
145152
public function testWithCount(mixed $page, int $maxPerPage, \ArrayIterator|array $data, int $count, int $expectedCountPages, \ArrayIterator $expectedIterator): void
146153
{
@@ -153,28 +160,41 @@ public function testWithCount(mixed $page, int $maxPerPage, \ArrayIterator|array
153160
$this->assertEquals($expectedIterator, $paginator->getIterator());
154161
}
155162

163+
/**
164+
* @return array<array{mixed, int<1, max>, \ArrayIterator<int|string, mixed>|array<mixed>, int<0, max>, int, \ArrayIterator<int|string, mixed>}>
165+
*/
156166
public static function getTestCountWithCountProvider(): array
157167
{
158168
return [
159-
[1, 5, static::getDefaultArray(), 202, 41, new \ArrayIterator(range(0, 51))],
160-
[3, 5, static::getDefaultArray(), 202, 41, new \ArrayIterator(range(0, 51))],
161-
[11, 5, static::getDefaultArray(), 202, 41, new \ArrayIterator(range(0, 51))],
162-
[1, 5, [], 0, 1, new \ArrayIterator()], // No data
163-
['page', 5, static::getDefaultArray(), 202, 41, new \ArrayIterator(range(0, 51))], // Bad page
164-
[1000, 5, static::getDefaultArray(), 202, 41, new \ArrayIterator(range(0, 51))], // Page too high
165-
166-
[1, 5, static::getDefaultIterator(), 202, 41, new \ArrayIterator(range(0, 51))],
167-
[3, 5, static::getDefaultIterator(), 202, 41, new \ArrayIterator(range(0, 51))],
168-
[11, 5, static::getDefaultIterator(), 202, 41, new \ArrayIterator(range(0, 51))],
169-
[1, 5, static::createIterator([]), 0, 1, new \ArrayIterator()], // No data
170-
['page', 5, static::getDefaultIterator(), 202, 41, new \ArrayIterator(range(0, 51))], // Bad page
171-
[1000, 5, static::getDefaultIterator(), 202, 41, new \ArrayIterator(range(0, 51))], // Page too high
169+
[1, 5, static::getDefaultArray(), 202, 41, static::createIterator(range(0, 51))],
170+
[3, 5, static::getDefaultArray(), 202, 41, static::createIterator(range(0, 51))],
171+
[11, 5, static::getDefaultArray(), 202, 41, static::createIterator(range(0, 51))],
172+
[1, 5, [], 0, 1, static::createIterator()], // No data
173+
['page', 5, static::getDefaultArray(), 202, 41, static::createIterator(range(0, 51))], // Bad page
174+
[1000, 5, static::getDefaultArray(), 202, 41, static::createIterator(range(0, 51))], // Page too high
175+
176+
[1, 5, static::getDefaultIterator(), 202, 41, static::createIterator(range(0, 51))],
177+
[3, 5, static::getDefaultIterator(), 202, 41, static::createIterator(range(0, 51))],
178+
[11, 5, static::getDefaultIterator(), 202, 41, static::createIterator(range(0, 51))],
179+
[1, 5, static::createIterator([]), 0, 1, static::createIterator()], // No data
180+
['page', 5, static::getDefaultIterator(), 202, 41, static::createIterator(range(0, 51))], // Bad page
181+
[1000, 5, static::getDefaultIterator(), 202, 41, static::createIterator(range(0, 51))], // Page too high
172182
];
173183
}
174184

175185
/**
176-
* @param int<1, max> $perPage
177-
* @param \ArrayIterator<int|string, mixed>|array $data
186+
* @param array<mixed> $data
187+
*
188+
* @return \ArrayIterator<int|string, mixed>
189+
*/
190+
public static function createIterator(array $data = []): \ArrayIterator
191+
{
192+
return new \ArrayIterator($data);
193+
}
194+
195+
/**
196+
* @param int<1, max> $perPage
197+
* @param \ArrayIterator<int|string, mixed>|array<mixed> $data
178198
*
179199
* @return PaginatorOptions
180200
*/
@@ -201,6 +221,9 @@ protected function createPaginator(array $options): ArrayPaginator
201221
return new ArrayPaginator($options);
202222
}
203223

224+
/**
225+
* @return array<mixed>
226+
*/
204227
protected static function getDefaultArray(): array
205228
{
206229
if (null === static::$defaultArray) {

tests/BuildArrayIteratorTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ trait BuildArrayIteratorTrait
1919
protected static ?\ArrayIterator $defaultIterator = null;
2020

2121
/**
22+
* @param array<mixed> $data
23+
*
2224
* @return \ArrayIterator<int|string, mixed>
2325
*/
2426
protected static function createIterator(array $data): \ArrayIterator

0 commit comments

Comments
 (0)