From 103f95c07bae410c89bf8ae21bc9fe509ab9eeb3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 10 Feb 2021 17:31:20 -0500 Subject: [PATCH] A few more PHPStan fixes --- phpstan.neon | 10 +++--- src/AnimeClient/Types/AbstractType.php | 47 ++++++++++++++------------ 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 7f460fb1..8390c43c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,16 +3,14 @@ parameters: checkMissingIterableValueType: false inferPrivatePropertyTypeFromConstructor: true level: 7 - autoload_files: - - %rootDir%/../../../tests/mocks.php paths: - src - ./console - index.php ignoreErrors: - - '#Access to an undefined property Aviat\\\Ion\\\Friend::\$[a-zA-Z0-9_]+#' - - '#Call to an undefined method Aviat\\\Ion\\\Friend::[a-zA-Z0-9_]+\(\)#' + - "#Offset 'fields' does not exist on array#" - '#Call to an undefined method Aura\\\Html\\\HelperLocator::[a-zA-Z0-9_]+\(\)#' - - '#Property Amp\\Artax\\Internal\\RequestCycle::\$[a-zA-Z0-9_]+#' + - '#Call to an undefined method Query\\QueryBuilderInterface::[a-zA-Z0-9_]+\(\)#' excludes_analyse: - - tests/mocks.php \ No newline at end of file + - tests/mocks.php + - vendor \ No newline at end of file diff --git a/src/AnimeClient/Types/AbstractType.php b/src/AnimeClient/Types/AbstractType.php index 13f6bd73..32766d2e 100644 --- a/src/AnimeClient/Types/AbstractType.php +++ b/src/AnimeClient/Types/AbstractType.php @@ -43,8 +43,7 @@ abstract class AbstractType implements ArrayAccess, Countable { if (get_parent_class($currentClass) !== FALSE) { - $output = static::class::from($data)->toArray(); - return (is_array($output)) ? $output : []; + return static::class::from($data)->toArray(); } return NULL; @@ -210,27 +209,12 @@ abstract class AbstractType implements ArrayAccess, Countable { * Returns early on primitive values to work recursively. * * @param mixed $parent - * @return float|bool|null|int|array|string + * @return array */ - final public function toArray(mixed $parent = null): float|null|bool|int|array|string + final public function toArray(mixed $parent = null): array { - $object = $parent ?? $this; - - if (is_scalar($object) || $object === NULL) - { - return $object; - } - - $output = []; - - foreach ($object as $key => $value) - { - $output[$key] = (is_scalar($value) || empty($value)) - ? $value - : $this->toArray((array) $value); - } - - return $output; + $fromObject = $this->fromObject($parent); + return (is_array($fromObject)) ? $fromObject : []; } /** @@ -251,4 +235,25 @@ abstract class AbstractType implements ArrayAccess, Countable { return TRUE; } + + final protected function fromObject(mixed $parent = null): float|null|bool|int|array|string + { + $object = $parent ?? $this; + + if (is_scalar($object) || $object === NULL) + { + return $object; + } + + $output = []; + + foreach ($object as $key => $value) + { + $output[$key] = (is_scalar($value) || empty($value)) + ? $value + : $this->fromObject((array) $value); + } + + return $output; + } }