From 74de5edd697b41feb5c6492183c2299f4f605fb7 Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Sun, 27 Nov 2022 15:15:21 +0100 Subject: [PATCH] Add parameter and return types --- phpstan.neon | 9 +++------ src/ArrayReader.php | 44 ++++++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 461b9cf..d3e46c9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,11 +2,8 @@ parameters: level: 8 paths: - src - - tests reportUnmatchedIgnoredErrors: false ignoreErrors: - - '#Cannot cast mixed to int.#' - - '#Cannot cast mixed to string.#' - - '#Cannot cast mixed to float.#' - - '#Parameter \#1 \$time of class Cake\\Chronos\\Chronos constructor expects DateTimeInterface\|int\|string\|null, mixed given.#' - - '#Cannot access offset string on mixed.#' + - '#Property (.*?) type has no value type specified in iterable type array#' + - '#Method (.*?) has parameter (.*?) with no value type specified in iterable type array#' + - '#Method (.*?) return type has no value type specified in iterable type array#' \ No newline at end of file diff --git a/src/ArrayReader.php b/src/ArrayReader.php index ee97370..eb214c8 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -11,14 +11,14 @@ final class ArrayReader { /** - * @var array The data + * @var array */ - private $data; + private array $data; /** * The constructor. * - * @param array $data Data + * @param array $data Data */ public function __construct(array $data = []) { @@ -28,7 +28,7 @@ public function __construct(array $data = []) /** * Crate instance from array. * - * @param array $data The data + * @param array $data The data * * @return self The new instance */ @@ -66,7 +66,7 @@ public function getInt(string $key, int $default = null): int * * @return int|null The value */ - public function findInt(string $key, int $default = null) + public function findInt(string $key, int $default = null): ?int { $value = $this->find($key, $default); @@ -102,11 +102,11 @@ public function getString(string $key, string $default = null): string * Get value as string or null. * * @param string $key The key - * @param string $default The default value + * @param string|null $default The default value * * @return string|null The value */ - public function findString(string $key, string $default = null) + public function findString(string $key, string $default = null): ?string { $value = $this->find($key, $default); @@ -121,11 +121,11 @@ public function findString(string $key, string $default = null) * Get value as array. * * @param string $key The key - * @param array|null $default The default value + * @param array|null $default The default value * * @throws InvalidArgumentException * - * @return array The value + * @return array The value */ public function getArray(string $key, array $default = null): array { @@ -142,11 +142,11 @@ public function getArray(string $key, array $default = null): array * Get value as array or null. * * @param string $key The key - * @param array $default The default value + * @param array|null $default The default value * - * @return array|null The value + * @return array|null The value */ - public function findArray(string $key, array $default = null) + public function findArray(string $key, array $default = null): ?array { $value = $this->find($key, $default); @@ -182,11 +182,11 @@ public function getFloat(string $key, float $default = null): float * Get value as float or null. * * @param string $key The key - * @param float $default The default value + * @param float|null $default The default value * * @return float|null The value */ - public function findFloat(string $key, float $default = null) + public function findFloat(string $key, float $default = null): ?float { $value = $this->find($key, $default); @@ -226,7 +226,7 @@ public function getBool(string $key, bool $default = null): bool * * @return bool|null The value */ - public function findBool(string $key, bool $default = null) + public function findBool(string $key, bool $default = null): ?bool { $value = $this->find($key, $default); @@ -266,11 +266,11 @@ public function getChronos(string $key, Chronos $default = null): Chronos * Get value as Chronos or null. * * @param string $key The key - * @param Chronos $default The default value + * @param Chronos|null $default The default value * * @return Chronos|null The value */ - public function findChronos(string $key, Chronos $default = null) + public function findChronos(string $key, Chronos $default = null): ?Chronos { $value = $this->find($key, $default); @@ -293,13 +293,13 @@ public function findChronos(string $key, Chronos $default = null) * * @return mixed|null The value */ - public function find(string $path, $default = null) + public function find(string $path, mixed $default = null): mixed { if (array_key_exists($path, $this->data)) { return $this->data[$path] ?? $default; } - if (strpos($path, '.') === false) { + if (!str_contains($path, '.')) { return $default; } @@ -320,7 +320,7 @@ public function find(string $path, $default = null) /** * Return all data as array. * - * @return array The data + * @return array The data */ public function all(): array { @@ -328,7 +328,7 @@ public function all(): array } /** - * Test whether or not a given path exists in $data. + * Test whether a given path exists in $data. * This method uses the same path syntax as Hash::extract(). * * Checking for paths that could target more than one element will @@ -373,7 +373,7 @@ public function isEmpty(string $path): bool * * @return bool The status */ - private function isNullOrBlank($value): bool + private function isNullOrBlank(mixed $value): bool { return $value === null || $value === ''; }