Skip to content

Commit defd3fb

Browse files
committed
added PHP 8 typehints
1 parent dc06fca commit defd3fb

27 files changed

+133
-264
lines changed

src/Iterators/CachingIterator.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,17 @@ public function rewind(): void
146146

147147
/**
148148
* Returns the next key.
149-
* @return mixed
150149
*/
151-
public function getNextKey()
150+
public function getNextKey(): mixed
152151
{
153152
return $this->getInnerIterator()->key();
154153
}
155154

156155

157156
/**
158157
* Returns the next element.
159-
* @return mixed
160158
*/
161-
public function getNextValue()
159+
public function getNextValue(): mixed
162160
{
163161
return $this->getInnerIterator()->current();
164162
}

src/Iterators/Mapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public function __construct(\Traversable $iterator, callable $callback)
2727
}
2828

2929

30-
#[\ReturnTypeWillChange]
31-
public function current()
30+
public function current(): mixed
3231
{
3332
return ($this->callback)(parent::current(), parent::key());
3433
}

src/SmartObject.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait SmartObject
2424
/**
2525
* @throws MemberAccessException
2626
*/
27-
public function __call(string $name, array $args)
27+
public function __call(string $name, array $args): mixed
2828
{
2929
$class = static::class;
3030

@@ -37,27 +37,26 @@ public function __call(string $name, array $args)
3737
} elseif ($handlers !== null) {
3838
throw new UnexpectedValueException("Property $class::$$name must be iterable or null, " . gettype($handlers) . ' given.');
3939
}
40-
41-
} else {
42-
ObjectHelpers::strictCall($class, $name);
40+
return null;
4341
}
42+
43+
ObjectHelpers::strictCall($class, $name);
4444
}
4545

4646

4747
/**
4848
* @throws MemberAccessException
4949
*/
50-
public static function __callStatic(string $name, array $args)
50+
public static function __callStatic(string $name, array $args): mixed
5151
{
5252
ObjectHelpers::strictStaticCall(static::class, $name);
5353
}
5454

5555

5656
/**
57-
* @return mixed
5857
* @throws MemberAccessException if the property is not defined.
5958
*/
60-
public function &__get(string $name)
59+
public function &__get(string $name): mixed
6160
{
6261
$class = static::class;
6362

@@ -79,11 +78,9 @@ public function &__get(string $name)
7978

8079

8180
/**
82-
* @param mixed $value
83-
* @return void
8481
* @throws MemberAccessException if the property is not defined or is read-only
8582
*/
86-
public function __set(string $name, $value)
83+
public function __set(string $name, mixed $value): void
8784
{
8885
$class = static::class;
8986

@@ -103,10 +100,9 @@ public function __set(string $name, $value)
103100

104101

105102
/**
106-
* @return void
107103
* @throws MemberAccessException
108104
*/
109-
public function __unset(string $name)
105+
public function __unset(string $name): void
110106
{
111107
$class = static::class;
112108
if (!ObjectHelpers::hasProperty($class, $name)) {

src/StaticClass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ final public function __construct()
2727

2828
/**
2929
* Call to undefined static method.
30-
* @return void
3130
* @throws MemberAccessException
3231
*/
33-
public static function __callStatic(string $name, array $args)
32+
public static function __callStatic(string $name, array $args): mixed
3433
{
3534
Utils\ObjectHelpers::strictStaticCall(static::class, $name);
3635
}

src/Translator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ interface Translator
1717
{
1818
/**
1919
* Translates the given string.
20-
* @param mixed $message
21-
* @param mixed ...$parameters
2220
*/
23-
function translate($message, ...$parameters): string;
21+
function translate(mixed $message, mixed ...$parameters): string;
2422
}
2523

2624

src/Utils/ArrayHash.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \Iterator
2121
/**
2222
* Transforms array to ArrayHash.
2323
* @param array<T> $array
24-
* @return static
2524
*/
26-
public static function from(array $array, bool $recursive = true)
25+
public static function from(array $array, bool $recursive = true): static
2726
{
2827
$obj = new static;
2928
foreach ($array as $key => $value) {
@@ -73,8 +72,7 @@ public function offsetSet($key, $value): void
7372
* @param string|int $key
7473
* @return T
7574
*/
76-
#[\ReturnTypeWillChange]
77-
public function offsetGet($key)
75+
public function offsetGet($key): mixed
7876
{
7977
return $this->$key;
8078
}

src/Utils/ArrayList.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ class ArrayList implements \ArrayAccess, \Countable, \IteratorAggregate
2626
/**
2727
* Transforms array to ArrayList.
2828
* @param array<T> $array
29-
* @return static
3029
*/
31-
public static function from(array $array)
30+
public static function from(array $array): static
3231
{
3332
if (!Arrays::isList($array)) {
3433
throw new Nette\InvalidArgumentException('Array is not valid list.');
@@ -84,8 +83,7 @@ public function offsetSet($index, $value): void
8483
* @return T
8584
* @throws Nette\OutOfRangeException
8685
*/
87-
#[\ReturnTypeWillChange]
88-
public function offsetGet($index)
86+
public function offsetGet($index): mixed
8987
{
9088
if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
9189
throw new Nette\OutOfRangeException('Offset invalid or out of range');
@@ -122,7 +120,7 @@ public function offsetUnset($index): void
122120
* Prepends a item.
123121
* @param T $value
124122
*/
125-
public function prepend($value): void
123+
public function prepend(mixed $value): void
126124
{
127125
$first = array_slice($this->list, 0, 1);
128126
$this->offsetSet(0, $value);

src/Utils/Arrays.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Arrays
2929
* @return ?T
3030
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
3131
*/
32-
public static function get(array $array, $key, $default = null)
32+
public static function get(array $array, string|int|array $key, mixed $default = null): mixed
3333
{
3434
foreach (is_array($key) ? $key : [$key] as $k) {
3535
if (is_array($array) && array_key_exists($k, $array)) {
@@ -53,7 +53,7 @@ public static function get(array $array, $key, $default = null)
5353
* @return ?T
5454
* @throws Nette\InvalidArgumentException if traversed item is not an array
5555
*/
56-
public static function &getRef(array &$array, $key)
56+
public static function &getRef(array &$array, string|int|array $key): mixed
5757
{
5858
foreach (is_array($key) ? $key : [$key] as $k) {
5959
if (is_array($array) || $array === null) {
@@ -90,10 +90,8 @@ public static function mergeTree(array $array1, array $array2): array
9090

9191
/**
9292
* Returns zero-indexed position of given array key. Returns null if key is not found.
93-
* @param array-key $key
94-
* @return int|null offset if it is found, null otherwise
9593
*/
96-
public static function getKeyOffset(array $array, $key): ?int
94+
public static function getKeyOffset(array $array, string|int $key): ?int
9795
{
9896
return Helpers::falseToNull(array_search(self::toKey($key), array_keys($array), true));
9997
}
@@ -110,9 +108,8 @@ public static function searchKey(array $array, $key): ?int
110108

111109
/**
112110
* Tests an array for the presence of value.
113-
* @param mixed $value
114111
*/
115-
public static function contains(array $array, $value): bool
112+
public static function contains(array $array, mixed $value): bool
116113
{
117114
return in_array($value, $array, true);
118115
}
@@ -124,7 +121,7 @@ public static function contains(array $array, $value): bool
124121
* @param array<T> $array
125122
* @return ?T
126123
*/
127-
public static function first(array $array)
124+
public static function first(array $array): mixed
128125
{
129126
return count($array) ? reset($array) : null;
130127
}
@@ -136,7 +133,7 @@ public static function first(array $array)
136133
* @param array<T> $array
137134
* @return ?T
138135
*/
139-
public static function last(array $array)
136+
public static function last(array $array): mixed
140137
{
141138
return count($array) ? end($array) : null;
142139
}
@@ -145,9 +142,8 @@ public static function last(array $array)
145142
/**
146143
* Inserts the contents of the $inserted array into the $array immediately after the $key.
147144
* If $key is null (or does not exist), it is inserted at the beginning.
148-
* @param array-key|null $key
149145
*/
150-
public static function insertBefore(array &$array, $key, array $inserted): void
146+
public static function insertBefore(array &$array, string|int|null $key, array $inserted): void
151147
{
152148
$offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key);
153149
$array = array_slice($array, 0, $offset, true)
@@ -159,9 +155,8 @@ public static function insertBefore(array &$array, $key, array $inserted): void
159155
/**
160156
* Inserts the contents of the $inserted array into the $array before the $key.
161157
* If $key is null (or does not exist), it is inserted at the end.
162-
* @param array-key|null $key
163158
*/
164-
public static function insertAfter(array &$array, $key, array $inserted): void
159+
public static function insertAfter(array &$array, string|int|null $key, array $inserted): void
165160
{
166161
if ($key === null || ($offset = self::getKeyOffset($array, $key)) === null) {
167162
$offset = count($array) - 1;
@@ -174,10 +169,8 @@ public static function insertAfter(array &$array, $key, array $inserted): void
174169

175170
/**
176171
* Renames key in array.
177-
* @param array-key $oldKey
178-
* @param array-key $newKey
179172
*/
180-
public static function renameKey(array &$array, $oldKey, $newKey): bool
173+
public static function renameKey(array &$array, string|int $oldKey, string|int $newKey): bool
181174
{
182175
$offset = self::getKeyOffset($array, $oldKey);
183176
if ($offset === null) {
@@ -219,9 +212,8 @@ public static function flatten(array $array, bool $preserveKeys = false): array
219212

220213
/**
221214
* Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
222-
* @param mixed $value
223215
*/
224-
public static function isList($value): bool
216+
public static function isList(mixed $value): bool
225217
{
226218
return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
227219
}
@@ -230,9 +222,8 @@ public static function isList($value): bool
230222
/**
231223
* Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
232224
* @param string|string[] $path
233-
* @return array|\stdClass
234225
*/
235-
public static function associate(array $array, $path)
226+
public static function associate(array $array, $path): array|\stdClass
236227
{
237228
$parts = is_array($path)
238229
? $path
@@ -285,9 +276,8 @@ public static function associate(array $array, $path)
285276

286277
/**
287278
* Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
288-
* @param mixed $filling
289279
*/
290-
public static function normalize(array $array, $filling = null): array
280+
public static function normalize(array $array, mixed $filling = null): array
291281
{
292282
$res = [];
293283
foreach ($array as $k => $v) {
@@ -302,12 +292,11 @@ public static function normalize(array $array, $filling = null): array
302292
* or returns $default, if provided.
303293
* @template T
304294
* @param array<T> $array
305-
* @param array-key $key
306295
* @param ?T $default
307296
* @return ?T
308297
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
309298
*/
310-
public static function pick(array &$array, $key, $default = null)
299+
public static function pick(array &$array, string|int $key, mixed $default = null): mixed
311300
{
312301
if (array_key_exists($key, $array)) {
313302
$value = $array[$key];
@@ -401,7 +390,7 @@ public static function invokeMethod(iterable $objects, string $method, ...$args)
401390
* @param T $object
402391
* @return T
403392
*/
404-
public static function toObject(iterable $array, $object)
393+
public static function toObject(iterable $array, object $object): object
405394
{
406395
foreach ($array as $k => $v) {
407396
$object->$k = $v;
@@ -412,10 +401,8 @@ public static function toObject(iterable $array, $object)
412401

413402
/**
414403
* Converts value to array key.
415-
* @param mixed $value
416-
* @return array-key
417404
*/
418-
public static function toKey($value)
405+
public static function toKey(mixed $value): int|string
419406
{
420407
return key([$value => null]);
421408
}

src/Utils/Callback.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ final class Callback
2222

2323
/**
2424
* Invokes internal PHP function with own error handler.
25-
* @return mixed
2625
*/
27-
public static function invokeSafe(string $function, array $args, callable $onError)
26+
public static function invokeSafe(string $function, array $args, callable $onError): mixed
2827
{
2928
$prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function): ?bool {
3029
if ($file === __FILE__) {
@@ -50,11 +49,10 @@ public static function invokeSafe(string $function, array $args, callable $onErr
5049
/**
5150
* Checks that $callable is valid PHP callback. Otherwise throws exception. If the $syntax is set to true, only verifies
5251
* that $callable has a valid structure to be used as a callback, but does not verify if the class or method actually exists.
53-
* @param mixed $callable
5452
* @return callable
5553
* @throws Nette\InvalidArgumentException
5654
*/
57-
public static function check($callable, bool $syntax = false)
55+
public static function check(mixed $callable, bool $syntax = false)
5856
{
5957
if (!is_callable($callable, $syntax)) {
6058
throw new Nette\InvalidArgumentException(
@@ -69,9 +67,8 @@ public static function check($callable, bool $syntax = false)
6967

7068
/**
7169
* Converts PHP callback to textual form. Class or method may not exists.
72-
* @param mixed $callable
7370
*/
74-
public static function toString($callable): string
71+
public static function toString(mixed $callable): string
7572
{
7673
if ($callable instanceof \Closure) {
7774
$inner = self::unwrap($callable);
@@ -88,7 +85,6 @@ public static function toString($callable): string
8885
/**
8986
* Returns reflection for method or function used in PHP callback.
9087
* @param callable $callable type check is escalated to ReflectionException
91-
* @return \ReflectionMethod|\ReflectionFunction
9288
* @throws \ReflectionException if callback is not valid
9389
*/
9490
public static function toReflection($callable): \ReflectionFunctionAbstract

0 commit comments

Comments
 (0)