Skip to content

Commit d6168fe

Browse files
xificurkdg
authored andcommitted
improved type annotations (#290)
1 parent 878a9f6 commit d6168fe

File tree

7 files changed

+50
-19
lines changed

7 files changed

+50
-19
lines changed

src/Utils/ArrayHash.php

+7-5
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 \IteratorAggregate<array-key, T>
19+
* @implements \ArrayAccess<array-key, T>
1820
*/
1921
class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \IteratorAggregate
2022
{
@@ -37,7 +39,7 @@ public static function from(array $array, bool $recursive = true): static
3739

3840
/**
3941
* Returns an iterator over all items.
40-
* @return \Iterator<int|string, T>
42+
* @return \Iterator<array-key, T>
4143
*/
4244
public function &getIterator(): \Iterator
4345
{
@@ -58,7 +60,7 @@ public function count(): int
5860

5961
/**
6062
* Replaces or appends a item.
61-
* @param string|int $key
63+
* @param array-key $key
6264
* @param T $value
6365
*/
6466
public function offsetSet($key, $value): void
@@ -73,7 +75,7 @@ public function offsetSet($key, $value): void
7375

7476
/**
7577
* Returns a item.
76-
* @param string|int $key
78+
* @param array-key $key
7779
* @return T
7880
*/
7981
#[\ReturnTypeWillChange]
@@ -85,7 +87,7 @@ public function offsetGet($key)
8587

8688
/**
8789
* Determines whether a item exists.
88-
* @param string|int $key
90+
* @param array-key $key
8991
*/
9092
public function offsetExists($key): bool
9193
{
@@ -95,7 +97,7 @@ public function offsetExists($key): bool
9597

9698
/**
9799
* Removes the element from this list.
98-
* @param string|int $key
100+
* @param array-key $key
99101
*/
100102
public function offsetUnset($key): void
101103
{

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
{
@@ -25,7 +27,7 @@ class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
2527

2628
/**
2729
* Transforms array to ArrayList.
28-
* @param array<T> $array
30+
* @param list<T> $array
2931
*/
3032
public static function from(array $array): static
3133
{

src/Utils/Arrays.php

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ public static function flatten(array $array, bool $preserveKeys = false): array
225225

226226
/**
227227
* Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
228+
* @return ($value is list ? true : false)
228229
*/
229230
public static function isList(mixed $value): bool
230231
{

src/Utils/Image.php

+10-5
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 \GdImage $imageResource
9696
*/
9797
class Image
@@ -220,6 +220,8 @@ private static function invokeSafe(string $func, string $arg, string $message, s
220220

221221
/**
222222
* Creates a new true color image of the given dimensions. The default color is black.
223+
* @param positive-int $width
224+
* @param positive-int $height
223225
* @throws Nette\NotSupportedException if gd extension is not loaded
224226
*/
225227
public static function fromBlank(int $width, int $height, ?array $color = null): static
@@ -267,6 +269,7 @@ public static function detectTypeFromString(string $s, &$width = null, &$height
267269

268270
/**
269271
* Returns the file extension for the given `Image::XXX` constant.
272+
* @return value-of<self::Formats>
270273
*/
271274
public static function typeToExtension(int $type): string
272275
{
@@ -314,6 +317,7 @@ public function __construct(\GdImage $image)
314317

315318
/**
316319
* Returns image width.
320+
* @return positive-int
317321
*/
318322
public function getWidth(): int
319323
{
@@ -323,6 +327,7 @@ public function getWidth(): int
323327

324328
/**
325329
* Returns image height.
330+
* @return positive-int
326331
*/
327332
public function getHeight(): int
328333
{
@@ -351,7 +356,7 @@ public function getImageResource(): \GdImage
351356

352357
/**
353358
* Scales an image. Width and height accept pixels or percent.
354-
* @param self::OrSmaller|self::OrBigger|self::Stretch|self::Cover|self::ShrinkOnly $mode
359+
* @param int-mask-of<self::OrSmaller|self::OrBigger|self::Stretch|self::Cover|self::ShrinkOnly> $mode
355360
*/
356361
public function resize(int|string|null $width, int|string|null $height, int $mode = self::OrSmaller): static
357362
{
@@ -388,7 +393,7 @@ public function resize(int|string|null $width, int|string|null $height, int $mod
388393

389394
/**
390395
* Calculates dimensions of resized image. Width and height accept pixels or percent.
391-
* @param self::OrSmaller|self::OrBigger|self::Stretch|self::Cover|self::ShrinkOnly $mode
396+
* @param int-mask-of<self::OrSmaller|self::OrBigger|self::Stretch|self::Cover|self::ShrinkOnly> $mode
392397
*/
393398
public static function calculateSize(
394399
int $srcWidth,
@@ -534,7 +539,7 @@ public function sharpen(): static
534539

535540
/**
536541
* Puts another image into this image. Left and top accepts pixels or percent.
537-
* @param int $opacity 0..100
542+
* @param int<0, 100> $opacity 0..100
538543
*/
539544
public function place(self $image, int|string $left = 0, int|string $top = 0, int $opacity = 100): static
540545
{

src/Utils/Paginator.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@
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
{
3535
use Nette\SmartObject;
3636

3737
private int $base = 1;
38+
39+
/** @var positive-int */
3840
private int $itemsPerPage = 1;
41+
3942
private int $page = 1;
43+
44+
/** @var int<0, max>|null */
4045
private ?int $itemCount = null;
4146

4247

@@ -81,6 +86,7 @@ public function getLastPage(): ?int
8186

8287
/**
8388
* Returns the sequence number of the first element on the page
89+
* @return int<0, max>
8490
*/
8591
public function getFirstItemOnPage(): int
8692
{
@@ -92,6 +98,7 @@ public function getFirstItemOnPage(): int
9298

9399
/**
94100
* Returns the sequence number of the last element on the page
101+
* @return int<0, max>
95102
*/
96103
public function getLastItemOnPage(): int
97104
{
@@ -120,6 +127,7 @@ public function getBase(): int
120127

121128
/**
122129
* Returns zero-based page number.
130+
* @return int<0, max>
123131
*/
124132
protected function getPageIndex(): int
125133
{
@@ -152,6 +160,7 @@ public function isLast(): bool
152160

153161
/**
154162
* Returns the total number of pages.
163+
* @return int<0, max>|null
155164
*/
156165
public function getPageCount(): ?int
157166
{
@@ -173,6 +182,7 @@ public function setItemsPerPage(int $itemsPerPage): static
173182

174183
/**
175184
* Returns the number of items to display on a single page.
185+
* @return positive-int
176186
*/
177187
public function getItemsPerPage(): int
178188
{
@@ -192,6 +202,7 @@ public function setItemCount(?int $itemCount = null): static
192202

193203
/**
194204
* Returns the total number of items.
205+
* @return int<0, max>|null
195206
*/
196207
public function getItemCount(): ?int
197208
{
@@ -201,6 +212,7 @@ public function getItemCount(): ?int
201212

202213
/**
203214
* Returns the absolute index of the first item on current page.
215+
* @return int<0, max>
204216
*/
205217
public function getOffset(): int
206218
{
@@ -210,6 +222,7 @@ public function getOffset(): int
210222

211223
/**
212224
* Returns the absolute index of the first item on current page in countdown paging.
225+
* @return int<0, max>|null
213226
*/
214227
public function getCountdownOffset(): ?int
215228
{
@@ -221,6 +234,7 @@ public function getCountdownOffset(): ?int
221234

222235
/**
223236
* Returns the number of items on current page.
237+
* @return int<0, max>
224238
*/
225239
public function getLength(): int
226240
{

src/Utils/Strings.php

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

416416
/**
417417
* Pads a UTF-8 string to given length by prepending the $pad string to the beginning.
418+
* @param non-empty-string $pad
418419
*/
419420
public static function padLeft(string $s, int $length, string $pad = ' '): string
420421
{
@@ -426,6 +427,7 @@ public static function padLeft(string $s, int $length, string $pad = ' '): strin
426427

427428
/**
428429
* Pads UTF-8 string to given length by appending the $pad string to the end.
430+
* @param non-empty-string $pad
429431
*/
430432
public static function padRight(string $s, int $length, string $pad = ' '): string
431433
{

src/Utils/Validators.php

+5
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ public static function everyIs(iterable $values, string $expected): bool
212212

213213
/**
214214
* Checks if the value is an integer or a float.
215+
* @return ($value is int|float ? true : false)
215216
*/
216217
public static function isNumber(mixed $value): bool
217218
{
@@ -221,6 +222,7 @@ public static function isNumber(mixed $value): bool
221222

222223
/**
223224
* Checks if the value is an integer or a integer written in a string.
225+
* @return ($value is non-empty-string ? bool : ($value is int ? true : false))
224226
*/
225227
public static function isNumericInt(mixed $value): bool
226228
{
@@ -230,6 +232,7 @@ public static function isNumericInt(mixed $value): bool
230232

231233
/**
232234
* Checks if the value is a number or a number written in a string.
235+
* @return ($value is non-empty-string ? bool : ($value is int|float ? true : false))
233236
*/
234237
public static function isNumeric(mixed $value): bool
235238
{
@@ -257,6 +260,7 @@ public static function isUnicode(mixed $value): bool
257260

258261
/**
259262
* Checks if the value is 0, '', false or null.
263+
* @return ($value is 0|''|false|null ? true : false)
260264
*/
261265
public static function isNone(mixed $value): bool
262266
{
@@ -274,6 +278,7 @@ public static function isMixed(): bool
274278
/**
275279
* Checks if a variable is a zero-based integer indexed array.
276280
* @deprecated use Nette\Utils\Arrays::isList
281+
* @return ($value is list ? true : false)
277282
*/
278283
public static function isList(mixed $value): bool
279284
{

0 commit comments

Comments
 (0)