Skip to content

Commit d42d358

Browse files
xificurkdg
authored andcommitted
improved type annotations (#290)
1 parent 7d5e2bc commit d42d358

File tree

7 files changed

+37
-17
lines changed

7 files changed

+37
-17
lines changed

src/Utils/ArrayHash.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
/**
1616
* Provides objects to work as array.
1717
* @template T
18+
* @implements \RecursiveArrayIterator<array-key, T>
19+
* @implements \ArrayAccess<array-key, T>
1820
*/
1921
class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \IteratorAggregate
2022
{
@@ -57,7 +59,7 @@ public function count(): int
5759

5860
/**
5961
* Replaces or appends a item.
60-
* @param string|int $key
62+
* @param array-key $key
6163
* @param T $value
6264
*/
6365
public function offsetSet($key, $value): void
@@ -72,7 +74,7 @@ public function offsetSet($key, $value): void
7274

7375
/**
7476
* Returns a item.
75-
* @param string|int $key
77+
* @param array-key $key
7678
* @return T
7779
*/
7880
#[\ReturnTypeWillChange]
@@ -84,7 +86,7 @@ public function offsetGet($key)
8486

8587
/**
8688
* Determines whether a item exists.
87-
* @param string|int $key
89+
* @param array-key $key
8890
*/
8991
public function offsetExists($key): bool
9092
{
@@ -94,7 +96,7 @@ public function offsetExists($key): bool
9496

9597
/**
9698
* Removes the element from this list.
97-
* @param string|int $key
99+
* @param array-key $key
98100
*/
99101
public function offsetUnset($key): void
100102
{

src/Utils/ArrayList.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
/**
1616
* Provides the base class for a generic list (items can be accessed by index).
1717
* @template T
18+
* @implements \IteratorAggregate<int, T>
19+
* @implements \ArrayAccess<int, T>
1820
*/
1921
class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
2022
{
@@ -26,7 +28,7 @@ class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
2628

2729
/**
2830
* Transforms array to ArrayList.
29-
* @param array<T> $array
31+
* @param list<T> $array
3032
* @return static
3133
*/
3234
public static function from(array $array)

src/Utils/Image.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@
9090
* @method void stringUp($font, $x, $y, string $s, $col)
9191
* @method void trueColorToPalette(bool $dither, $ncolors)
9292
* @method array ttfText($size, $angle, $x, $y, $color, string $fontfile, string $text)
93-
* @property-read int $width
94-
* @property-read int $height
93+
* @property-read positive-int $width
94+
* @property-read positive-int $height
9595
* @property-read resource|\GdImage $imageResource
9696
*/
9797
class Image
@@ -205,6 +205,8 @@ private static function invokeSafe(string $func, string $arg, string $message, s
205205

206206
/**
207207
* Creates a new true color image of the given dimensions. The default color is black.
208+
* @param positive-int $width
209+
* @param positive-int $height
208210
* @return static
209211
* @throws Nette\NotSupportedException if gd extension is not loaded
210212
*/
@@ -301,6 +303,7 @@ public function __construct($image)
301303

302304
/**
303305
* Returns image width.
306+
* @return positive-int
304307
*/
305308
public function getWidth(): int
306309
{
@@ -310,6 +313,7 @@ public function getWidth(): int
310313

311314
/**
312315
* Returns image height.
316+
* @return positive-int
313317
*/
314318
public function getHeight(): int
315319
{
@@ -536,7 +540,7 @@ public function sharpen()
536540
* Puts another image into this image.
537541
* @param int|string $left in pixels or percent
538542
* @param int|string $top in pixels or percent
539-
* @param int $opacity 0..100
543+
* @param int<0, 100> $opacity 0..100
540544
* @return static
541545
*/
542546
public function place(self $image, $left = 0, $top = 0, int $opacity = 100)

src/Utils/Paginator.php

+17-8
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
* @property int $page
1919
* @property-read int $firstPage
2020
* @property-read int|null $lastPage
21-
* @property-read int $firstItemOnPage
22-
* @property-read int $lastItemOnPage
21+
* @property-read int<0,max> $firstItemOnPage
22+
* @property-read int<0,max> $lastItemOnPage
2323
* @property int $base
2424
* @property-read bool $first
2525
* @property-read bool $last
26-
* @property-read int|null $pageCount
27-
* @property int $itemsPerPage
28-
* @property int|null $itemCount
29-
* @property-read int $offset
30-
* @property-read int|null $countdownOffset
31-
* @property-read int $length
26+
* @property-read int<0,max>|null $pageCount
27+
* @property positive-int $itemsPerPage
28+
* @property int<0,max>|null $itemCount
29+
* @property-read int<0,max> $offset
30+
* @property-read int<0,max>|null $countdownOffset
31+
* @property-read int<0,max> $length
3232
*/
3333
class Paginator
3434
{
@@ -89,6 +89,7 @@ public function getLastPage(): ?int
8989

9090
/**
9191
* Returns the sequence number of the first element on the page
92+
* @return int<0, max>
9293
*/
9394
public function getFirstItemOnPage(): int
9495
{
@@ -100,6 +101,7 @@ public function getFirstItemOnPage(): int
100101

101102
/**
102103
* Returns the sequence number of the last element on the page
104+
* @return int<0, max>
103105
*/
104106
public function getLastItemOnPage(): int
105107
{
@@ -129,6 +131,7 @@ public function getBase(): int
129131

130132
/**
131133
* Returns zero-based page number.
134+
* @return int<0, max>
132135
*/
133136
protected function getPageIndex(): int
134137
{
@@ -161,6 +164,7 @@ public function isLast(): bool
161164

162165
/**
163166
* Returns the total number of pages.
167+
* @return int<0, max>|null
164168
*/
165169
public function getPageCount(): ?int
166170
{
@@ -183,6 +187,7 @@ public function setItemsPerPage(int $itemsPerPage)
183187

184188
/**
185189
* Returns the number of items to display on a single page.
190+
* @return positive-int
186191
*/
187192
public function getItemsPerPage(): int
188193
{
@@ -203,6 +208,7 @@ public function setItemCount(?int $itemCount = null)
203208

204209
/**
205210
* Returns the total number of items.
211+
* @return int<0, max>|null
206212
*/
207213
public function getItemCount(): ?int
208214
{
@@ -212,6 +218,7 @@ public function getItemCount(): ?int
212218

213219
/**
214220
* Returns the absolute index of the first item on current page.
221+
* @return int<0, max>
215222
*/
216223
public function getOffset(): int
217224
{
@@ -221,6 +228,7 @@ public function getOffset(): int
221228

222229
/**
223230
* Returns the absolute index of the first item on current page in countdown paging.
231+
* @return int<0, max>|null
224232
*/
225233
public function getCountdownOffset(): ?int
226234
{
@@ -232,6 +240,7 @@ public function getCountdownOffset(): ?int
232240

233241
/**
234242
* Returns the number of items on current page.
243+
* @return int<0, max>
235244
*/
236245
public function getLength(): int
237246
{

src/Utils/Reflection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static function expandClassName(string $name, \ReflectionClass $context):
283283
}
284284

285285

286-
/** @return array of [alias => class] */
286+
/** @return array<string, class-string> of [alias => class] */
287287
public static function getUseStatements(\ReflectionClass $class): array
288288
{
289289
if ($class->isAnonymous()) {

src/Utils/Strings.php

+2
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ public static function trim(string $s, string $charlist = self::TRIM_CHARACTERS)
376376

377377
/**
378378
* Pads a UTF-8 string to given length by prepending the $pad string to the beginning.
379+
* @param non-empty-string $pad
379380
*/
380381
public static function padLeft(string $s, int $length, string $pad = ' '): string
381382
{
@@ -387,6 +388,7 @@ public static function padLeft(string $s, int $length, string $pad = ' '): strin
387388

388389
/**
389390
* Pads UTF-8 string to given length by appending the $pad string to the end.
391+
* @param non-empty-string $pad
390392
*/
391393
public static function padRight(string $s, int $length, string $pad = ' '): string
392394
{

src/Utils/Validators.php

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ public static function isMixed(): bool
284284
* Checks if a variable is a zero-based integer indexed array.
285285
* @param mixed $value
286286
* @deprecated use Nette\Utils\Arrays::isList
287+
* @return ($value is list ? true : false)
287288
*/
288289
public static function isList($value): bool
289290
{

0 commit comments

Comments
 (0)