Skip to content

Commit

Permalink
added PHP 8 typehints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 3, 2021
1 parent 23cbbaf commit eb3a987
Show file tree
Hide file tree
Showing 25 changed files with 125 additions and 258 deletions.
6 changes: 2 additions & 4 deletions src/Iterators/CachingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,17 @@ public function rewind(): void

/**
* Returns the next key.
* @return mixed
*/
public function getNextKey()
public function getNextKey(): mixed
{
return $this->getInnerIterator()->key();
}


/**
* Returns the next element.
* @return mixed
*/
public function getNextValue()
public function getNextValue(): mixed
{
return $this->getInnerIterator()->current();
}
Expand Down
10 changes: 3 additions & 7 deletions src/SmartObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ public static function __callStatic(string $name, array $args)


/**
* @return mixed
* @throws MemberAccessException if the property is not defined.
*/
public function &__get(string $name)
public function &__get(string $name): mixed
{
$class = static::class;

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


/**
* @param mixed $value
* @return void
* @throws MemberAccessException if the property is not defined or is read-only
*/
public function __set(string $name, $value)
public function __set(string $name, mixed $value): void
{
$class = static::class;

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


/**
* @return void
* @throws MemberAccessException
*/
public function __unset(string $name)
public function __unset(string $name): void
{
$class = static::class;
if (!ObjectHelpers::hasProperty($class, $name)) {
Expand Down
3 changes: 1 addition & 2 deletions src/StaticClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ final public function __construct()

/**
* Call to undefined static method.
* @return void
* @throws MemberAccessException
*/
public static function __callStatic(string $name, array $args)
public static function __callStatic(string $name, array $args): void
{
Utils\ObjectHelpers::strictStaticCall(static::class, $name);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ interface Translator
{
/**
* Translates the given string.
* @param mixed $message
* @param mixed ...$parameters
*/
function translate($message, ...$parameters): string;
function translate(mixed $message, mixed ...$parameters): string;
}


Expand Down
6 changes: 2 additions & 4 deletions src/Utils/ArrayHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ class ArrayHash extends \stdClass implements \ArrayAccess, \Countable, \Iterator
{
/**
* Transforms array to ArrayHash.
* @return static
*/
public static function from(array $array, bool $recursive = true)
public static function from(array $array, bool $recursive = true): static
{
$obj = new static;
foreach ($array as $key => $value) {
Expand Down Expand Up @@ -68,9 +67,8 @@ public function offsetSet($key, $value): void
/**
* Returns a item.
* @param string|int $key
* @return mixed
*/
public function offsetGet($key)
public function offsetGet($key): mixed
{
return $this->$key;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Utils/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ public function offsetSet($index, $value): void
/**
* Returns a item.
* @param int $index
* @return mixed
* @throws Nette\OutOfRangeException
*/
public function offsetGet($index)
public function offsetGet($index): mixed
{
if (!is_int($index) || $index < 0 || $index >= count($this->list)) {
throw new Nette\OutOfRangeException('Offset invalid or out of range');
Expand Down Expand Up @@ -101,9 +100,8 @@ public function offsetUnset($index): void

/**
* Prepends a item.
* @param mixed $value
*/
public function prepend($value): void
public function prepend(mixed $value): void
{
$first = array_slice($this->list, 0, 1);
$this->offsetSet(0, $value);
Expand Down
52 changes: 15 additions & 37 deletions src/Utils/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ class Arrays
/**
* Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
* @param string|int|array $key one or more keys
* @param mixed $default
* @return mixed
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
*/
public static function get(array $array, $key, $default = null)
public static function get(array $array, string|int|array $key, mixed $default = null): mixed
{
foreach (is_array($key) ? $key : [$key] as $k) {
if (is_array($array) && array_key_exists($k, $array)) {
Expand All @@ -46,10 +44,9 @@ public static function get(array $array, $key, $default = null)
/**
* Returns reference to array item. If the index does not exist, new one is created with value null.
* @param string|int|array $key one or more keys
* @return mixed
* @throws Nette\InvalidArgumentException if traversed item is not an array
*/
public static function &getRef(array &$array, $key)
public static function &getRef(array &$array, string|int|array $key): mixed
{
foreach (is_array($key) ? $key : [$key] as $k) {
if (is_array($array) || $array === null) {
Expand Down Expand Up @@ -81,10 +78,8 @@ public static function mergeTree(array $array1, array $array2): array

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

/**
* Tests an array for the presence of value.
* @param mixed $value
*/
public static function contains(array $array, $value): bool
public static function contains(array $array, mixed $value): bool
{
return in_array($value, $array, true);
}


/**
* Returns the first item from the array or null if array is empty.
* @return mixed
*/
public static function first(array $array)
public static function first(array $array): mixed
{
return count($array) ? reset($array) : null;
}


/**
* Returns the last item from the array or null if array is empty.
* @return mixed
*/
public static function last(array $array)
public static function last(array $array): mixed
{
return count($array) ? end($array) : null;
}
Expand All @@ -132,9 +124,8 @@ public static function last(array $array)
/**
* Inserts the contents of the $inserted array into the $array immediately after the $key.
* If $key is null (or does not exist), it is inserted at the beginning.
* @param string|int|null $key
*/
public static function insertBefore(array &$array, $key, array $inserted): void
public static function insertBefore(array &$array, string|int|null $key, array $inserted): void
{
$offset = $key === null ? 0 : (int) self::getKeyOffset($array, $key);
$array = array_slice($array, 0, $offset, true)
Expand All @@ -146,9 +137,8 @@ public static function insertBefore(array &$array, $key, array $inserted): void
/**
* Inserts the contents of the $inserted array into the $array before the $key.
* If $key is null (or does not exist), it is inserted at the end.
* @param string|int|null $key
*/
public static function insertAfter(array &$array, $key, array $inserted): void
public static function insertAfter(array &$array, string|int|null $key, array $inserted): void
{
if ($key === null || ($offset = self::getKeyOffset($array, $key)) === null) {
$offset = count($array) - 1;
Expand All @@ -161,10 +151,8 @@ public static function insertAfter(array &$array, $key, array $inserted): void

/**
* Renames key in array.
* @param string|int $oldKey
* @param string|int $newKey
*/
public static function renameKey(array &$array, $oldKey, $newKey): bool
public static function renameKey(array &$array, string|int $oldKey, string|int $newKey): bool
{
$offset = self::getKeyOffset($array, $oldKey);
if ($offset === null) {
Expand Down Expand Up @@ -205,9 +193,8 @@ public static function flatten(array $array, bool $preserveKeys = false): array

/**
* Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
* @param mixed $value
*/
public static function isList($value): bool
public static function isList(mixed $value): bool
{
return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
}
Expand All @@ -216,9 +203,8 @@ public static function isList($value): bool
/**
* Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
* @param string|string[] $path
* @return array|\stdClass
*/
public static function associate(array $array, $path)
public static function associate(array $array, $path): array|\stdClass
{
$parts = is_array($path)
? $path
Expand Down Expand Up @@ -271,9 +257,8 @@ public static function associate(array $array, $path)

/**
* Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
* @param mixed $filling
*/
public static function normalize(array $array, $filling = null): array
public static function normalize(array $array, mixed $filling = null): array
{
$res = [];
foreach ($array as $k => $v) {
Expand All @@ -286,12 +271,9 @@ public static function normalize(array $array, $filling = null): array
/**
* Returns and removes the value of an item from an array. If it does not exist, it throws an exception,
* or returns $default, if provided.
* @param string|int $key
* @param mixed $default
* @return mixed
* @throws Nette\InvalidArgumentException if item does not exist and default value is not provided
*/
public static function pick(array &$array, $key, $default = null)
public static function pick(array &$array, string|int $key, mixed $default = null): mixed
{
if (array_key_exists($key, $array)) {
$value = $array[$key];
Expand Down Expand Up @@ -381,10 +363,8 @@ public static function invokeMethod(iterable $objects, string $method, ...$args)

/**
* Copies the elements of the $array array to the $object object and then returns it.
* @param object $object
* @return object
*/
public static function toObject(iterable $array, $object)
public static function toObject(iterable $array, object $object): object
{
foreach ($array as $k => $v) {
$object->$k = $v;
Expand All @@ -395,10 +375,8 @@ public static function toObject(iterable $array, $object)

/**
* Converts value to array key.
* @param mixed $value
* @return int|string
*/
public static function toKey($value)
public static function toKey(mixed $value): int|string
{
return key([$value => null]);
}
Expand Down
10 changes: 3 additions & 7 deletions src/Utils/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ final class Callback

/**
* Invokes internal PHP function with own error handler.
* @return mixed
*/
public static function invokeSafe(string $function, array $args, callable $onError)
public static function invokeSafe(string $function, array $args, callable $onError): mixed
{
$prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function): ?bool {
if ($file === __FILE__) {
Expand All @@ -50,11 +49,10 @@ public static function invokeSafe(string $function, array $args, callable $onErr
/**
* Checks that $callable is valid PHP callback. Otherwise throws exception. If the $syntax is set to true, only verifies
* that $callable has a valid structure to be used as a callback, but does not verify if the class or method actually exists.
* @param mixed $callable
* @return callable
* @throws Nette\InvalidArgumentException
*/
public static function check($callable, bool $syntax = false)
public static function check(mixed $callable, bool $syntax = false)
{
if (!is_callable($callable, $syntax)) {
throw new Nette\InvalidArgumentException(
Expand All @@ -69,9 +67,8 @@ public static function check($callable, bool $syntax = false)

/**
* Converts PHP callback to textual form. Class or method may not exists.
* @param mixed $callable
*/
public static function toString($callable): string
public static function toString(mixed $callable): string
{
if ($callable instanceof \Closure) {
$inner = self::unwrap($callable);
Expand All @@ -88,7 +85,6 @@ public static function toString($callable): string
/**
* Returns reflection for method or function used in PHP callback.
* @param callable $callable type check is escalated to ReflectionException
* @return \ReflectionMethod|\ReflectionFunction
* @throws \ReflectionException if callback is not valid
*/
public static function toReflection($callable): \ReflectionFunctionAbstract
Expand Down
Loading

0 comments on commit eb3a987

Please sign in to comment.