Skip to content

Commit eb3a987

Browse files
committed
added PHP 8 typehints
1 parent 23cbbaf commit eb3a987

25 files changed

+125
-258
lines changed

src/Iterators/CachingIterator.php

+2-4
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/SmartObject.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ public static function __callStatic(string $name, array $args)
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

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ final public function __construct()
2424

2525
/**
2626
* Call to undefined static method.
27-
* @return void
2827
* @throws MemberAccessException
2928
*/
30-
public static function __callStatic(string $name, array $args)
29+
public static function __callStatic(string $name, array $args): void
3130
{
3231
Utils\ObjectHelpers::strictStaticCall(static::class, $name);
3332
}

src/Translator.php

+1-3
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

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \Iterator
1919
{
2020
/**
2121
* Transforms array to ArrayHash.
22-
* @return static
2322
*/
24-
public static function from(array $array, bool $recursive = true)
23+
public static function from(array $array, bool $recursive = true): static
2524
{
2625
$obj = new static;
2726
foreach ($array as $key => $value) {
@@ -68,9 +67,8 @@ public function offsetSet($key, $value): void
6867
/**
6968
* Returns a item.
7069
* @param string|int $key
71-
* @return mixed
7270
*/
73-
public function offsetGet($key)
71+
public function offsetGet($key): mixed
7472
{
7573
return $this->$key;
7674
}

src/Utils/ArrayList.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,9 @@ public function offsetSet($index, $value): void
6363
/**
6464
* Returns a item.
6565
* @param int $index
66-
* @return mixed
6766
* @throws Nette\OutOfRangeException
6867
*/
69-
public function offsetGet($index)
68+
public function offsetGet($index): mixed
7069
{
7170
if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
7271
throw new Nette\OutOfRangeException('Offset invalid or out of range');
@@ -101,9 +100,8 @@ public function offsetUnset($index): void
101100

102101
/**
103102
* Prepends a item.
104-
* @param mixed $value
105103
*/
106-
public function prepend($value): void
104+
public function prepend(mixed $value): void
107105
{
108106
$first = array_slice($this->list, 0, 1);
109107
$this->offsetSet(0, $value);

src/Utils/Arrays.php

+15-37
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ class Arrays
2323
/**
2424
* Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
2525
* @param string|int|array $key one or more keys
26-
* @param mixed $default
27-
* @return mixed
2826
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
2927
*/
30-
public static function get(array $array, $key, $default = null)
28+
public static function get(array $array, string|int|array $key, mixed $default = null): mixed
3129
{
3230
foreach (is_array($key) ? $key : [$key] as $k) {
3331
if (is_array($array) && array_key_exists($k, $array)) {
@@ -46,10 +44,9 @@ public static function get(array $array, $key, $default = null)
4644
/**
4745
* Returns reference to array item. If the index does not exist, new one is created with value null.
4846
* @param string|int|array $key one or more keys
49-
* @return mixed
5047
* @throws Nette\InvalidArgumentException if traversed item is not an array
5148
*/
52-
public static function &getRef(array &$array, $key)
49+
public static function &getRef(array &$array, string|int|array $key): mixed
5350
{
5451
foreach (is_array($key) ? $key : [$key] as $k) {
5552
if (is_array($array) || $array === null) {
@@ -81,10 +78,8 @@ public static function mergeTree(array $array1, array $array2): array
8178

8279
/**
8380
* Returns zero-indexed position of given array key. Returns null if key is not found.
84-
* @param string|int $key
85-
* @return int|null offset if it is found, null otherwise
8681
*/
87-
public static function getKeyOffset(array $array, $key): ?int
82+
public static function getKeyOffset(array $array, string|int $key): ?int
8883
{
8984
return Helpers::falseToNull(array_search(self::toKey($key), array_keys($array), true));
9085
}
@@ -101,29 +96,26 @@ public static function searchKey(array $array, $key): ?int
10196

10297
/**
10398
* Tests an array for the presence of value.
104-
* @param mixed $value
10599
*/
106-
public static function contains(array $array, $value): bool
100+
public static function contains(array $array, mixed $value): bool
107101
{
108102
return in_array($value, $array, true);
109103
}
110104

111105

112106
/**
113107
* Returns the first item from the array or null if array is empty.
114-
* @return mixed
115108
*/
116-
public static function first(array $array)
109+
public static function first(array $array): mixed
117110
{
118111
return count($array) ? reset($array) : null;
119112
}
120113

121114

122115
/**
123116
* Returns the last item from the array or null if array is empty.
124-
* @return mixed
125117
*/
126-
public static function last(array $array)
118+
public static function last(array $array): mixed
127119
{
128120
return count($array) ? end($array) : null;
129121
}
@@ -132,9 +124,8 @@ public static function last(array $array)
132124
/**
133125
* Inserts the contents of the $inserted array into the $array immediately after the $key.
134126
* If $key is null (or does not exist), it is inserted at the beginning.
135-
* @param string|int|null $key
136127
*/
137-
public static function insertBefore(array &$array, $key, array $inserted): void
128+
public static function insertBefore(array &$array, string|int|null $key, array $inserted): void
138129
{
139130
$offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key);
140131
$array = array_slice($array, 0, $offset, true)
@@ -146,9 +137,8 @@ public static function insertBefore(array &$array, $key, array $inserted): void
146137
/**
147138
* Inserts the contents of the $inserted array into the $array before the $key.
148139
* If $key is null (or does not exist), it is inserted at the end.
149-
* @param string|int|null $key
150140
*/
151-
public static function insertAfter(array &$array, $key, array $inserted): void
141+
public static function insertAfter(array &$array, string|int|null $key, array $inserted): void
152142
{
153143
if ($key === null || ($offset = self::getKeyOffset($array, $key)) === null) {
154144
$offset = count($array) - 1;
@@ -161,10 +151,8 @@ public static function insertAfter(array &$array, $key, array $inserted): void
161151

162152
/**
163153
* Renames key in array.
164-
* @param string|int $oldKey
165-
* @param string|int $newKey
166154
*/
167-
public static function renameKey(array &$array, $oldKey, $newKey): bool
155+
public static function renameKey(array &$array, string|int $oldKey, string|int $newKey): bool
168156
{
169157
$offset = self::getKeyOffset($array, $oldKey);
170158
if ($offset === null) {
@@ -205,9 +193,8 @@ public static function flatten(array $array, bool $preserveKeys = false): array
205193

206194
/**
207195
* Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
208-
* @param mixed $value
209196
*/
210-
public static function isList($value): bool
197+
public static function isList(mixed $value): bool
211198
{
212199
return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
213200
}
@@ -216,9 +203,8 @@ public static function isList($value): bool
216203
/**
217204
* Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
218205
* @param string|string[] $path
219-
* @return array|\stdClass
220206
*/
221-
public static function associate(array $array, $path)
207+
public static function associate(array $array, $path): array|\stdClass
222208
{
223209
$parts = is_array($path)
224210
? $path
@@ -271,9 +257,8 @@ public static function associate(array $array, $path)
271257

272258
/**
273259
* Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
274-
* @param mixed $filling
275260
*/
276-
public static function normalize(array $array, $filling = null): array
261+
public static function normalize(array $array, mixed $filling = null): array
277262
{
278263
$res = [];
279264
foreach ($array as $k => $v) {
@@ -286,12 +271,9 @@ public static function normalize(array $array, $filling = null): array
286271
/**
287272
* Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
288273
* or returns $default, if provided.
289-
* @param string|int $key
290-
* @param mixed $default
291-
* @return mixed
292274
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
293275
*/
294-
public static function pick(array &$array, $key, $default = null)
276+
public static function pick(array &$array, string|int $key, mixed $default = null): mixed
295277
{
296278
if (array_key_exists($key, $array)) {
297279
$value = $array[$key];
@@ -381,10 +363,8 @@ public static function invokeMethod(iterable $objects, string $method, ...$args)
381363

382364
/**
383365
* Copies the elements of the $array array to the $object object and then returns it.
384-
* @param object $object
385-
* @return object
386366
*/
387-
public static function toObject(iterable $array, $object)
367+
public static function toObject(iterable $array, object $object): object
388368
{
389369
foreach ($array as $k => $v) {
390370
$object->$k = $v;
@@ -395,10 +375,8 @@ public static function toObject(iterable $array, $object)
395375

396376
/**
397377
* Converts value to array key.
398-
* @param mixed $value
399-
* @return int|string
400378
*/
401-
public static function toKey($value)
379+
public static function toKey(mixed $value): int|string
402380
{
403381
return key([$value => null]);
404382
}

src/Utils/Callback.php

+3-7
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)