From c71ff7f38ebd80028c93cadda586e5191e35c0c8 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 12 Feb 2021 19:17:39 -0500 Subject: [PATCH] Fix more PHPStan issues --- src/AnimeClient/API/Kitsu/MutationTrait.php | 3 +- src/AnimeClient/Kitsu.php | 2 +- src/AnimeClient/Model/AnimeCollection.php | 48 +++++++++++++-------- src/AnimeClient/Model/Collection.php | 23 ++-------- src/AnimeClient/Model/MediaTrait.php | 8 +++- src/AnimeClient/Model/Settings.php | 4 ++ src/AnimeClient/UrlGenerator.php | 4 +- src/Ion/Type/ArrayType.php | 20 ++++----- 8 files changed, 58 insertions(+), 54 deletions(-) diff --git a/src/AnimeClient/API/Kitsu/MutationTrait.php b/src/AnimeClient/API/Kitsu/MutationTrait.php index 846b04a3..0fbc30b4 100644 --- a/src/AnimeClient/API/Kitsu/MutationTrait.php +++ b/src/AnimeClient/API/Kitsu/MutationTrait.php @@ -32,8 +32,7 @@ trait MutationTrait { * Create a list item * * @param array $data - * @return Request - * @throws InvalidArgumentException + * @return Request|null */ public function createListItem(array $data): ?Request { diff --git a/src/AnimeClient/Kitsu.php b/src/AnimeClient/Kitsu.php index a1d0d18a..ddd3c717 100644 --- a/src/AnimeClient/Kitsu.php +++ b/src/AnimeClient/Kitsu.php @@ -405,7 +405,7 @@ final class Kitsu { if (empty($parts)) { - return $last; + return ($last !== NULL) ? $last : ''; } return (count($parts) > 1) diff --git a/src/AnimeClient/Model/AnimeCollection.php b/src/AnimeClient/Model/AnimeCollection.php index e489dc5a..6802d0d8 100644 --- a/src/AnimeClient/Model/AnimeCollection.php +++ b/src/AnimeClient/Model/AnimeCollection.php @@ -50,6 +50,11 @@ final class AnimeCollection extends Collection { */ public function getCollection(): array { + if ($this->db === NULL) + { + return []; + } + $rawCollection = $this->getCollectionFromDatabase(); $collection = []; @@ -76,7 +81,7 @@ final class AnimeCollection extends Collection { */ public function getFlatCollection(): array { - if ( ! $this->validDatabase) + if ($this->db === NULL) { return []; } @@ -122,7 +127,7 @@ final class AnimeCollection extends Collection { */ public function getMediaTypeList(): array { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return []; } @@ -174,7 +179,7 @@ final class AnimeCollection extends Collection { */ public function add(mixed $data): void { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return; } @@ -213,7 +218,7 @@ final class AnimeCollection extends Collection { */ public function wasAdded(array $data): bool { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return FALSE; } @@ -231,7 +236,7 @@ final class AnimeCollection extends Collection { */ public function update(array $data): void { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return; } @@ -270,7 +275,7 @@ final class AnimeCollection extends Collection { */ public function wasUpdated(array $data): bool { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return FALSE; } @@ -301,7 +306,7 @@ final class AnimeCollection extends Collection { */ public function delete(array $data): void { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return; } @@ -332,7 +337,7 @@ final class AnimeCollection extends Collection { */ public function wasDeleted(array $data): bool { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return FALSE; } @@ -348,7 +353,7 @@ final class AnimeCollection extends Collection { */ public function get(int|string $kitsuId): array { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return []; } @@ -389,7 +394,7 @@ final class AnimeCollection extends Collection { */ public function has(int|string $kitsuId): bool { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return FALSE; } @@ -411,7 +416,7 @@ final class AnimeCollection extends Collection { */ public function getGenreList(array $filter = []): array { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return []; } @@ -479,7 +484,7 @@ final class AnimeCollection extends Collection { */ public function getMediaList(array $filter = []): array { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return []; } @@ -541,6 +546,11 @@ final class AnimeCollection extends Collection { private function updateMediaLink(string $animeId, array $media): void { + if ($this->db === NULL) + { + return; + } + $this->db->beginTransaction(); // Delete the old entries @@ -570,7 +580,7 @@ final class AnimeCollection extends Collection { */ private function updateGenres($animeId): void { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return; } @@ -604,13 +614,13 @@ final class AnimeCollection extends Collection { } } - if ( ! empty($linksToInsert)) + if ($this->db !== NULL && ! empty($linksToInsert)) { try { $this->db->insertBatch('anime_set_genre_link', $linksToInsert); } - catch (PDOException $e) {} + catch (PDOException) {} } } @@ -621,7 +631,7 @@ final class AnimeCollection extends Collection { */ private function addNewGenres(array $genres): void { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return; } @@ -663,7 +673,7 @@ final class AnimeCollection extends Collection { private function getExistingGenres(): array { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return []; } @@ -693,7 +703,7 @@ final class AnimeCollection extends Collection { private function getExistingGenreLinkEntries(): array { - if ($this->validDatabase === FALSE) + if ($this->db === NULL) { return []; } @@ -734,7 +744,7 @@ final class AnimeCollection extends Collection { */ private function getCollectionFromDatabase(): array { - if ( ! $this->validDatabase) + if ($this->db === NULL) { return []; } diff --git a/src/AnimeClient/Model/Collection.php b/src/AnimeClient/Model/Collection.php index ae63d8f2..d369af3f 100644 --- a/src/AnimeClient/Model/Collection.php +++ b/src/AnimeClient/Model/Collection.php @@ -29,15 +29,9 @@ class Collection extends DB { /** * The query builder object - * @var QueryBuilderInterface + * @var QueryBuilderInterface|null */ - protected QueryBuilderInterface $db; - - /** - * Whether the database is valid for querying - * @var boolean - */ - protected bool $validDatabase = FALSE; + protected ?QueryBuilderInterface $db; /** * Create a new collection object @@ -51,9 +45,8 @@ class Collection extends DB { try { $this->db = Query($this->dbConfig); - $this->validDatabase = TRUE; } - catch (PDOException $e) + catch (PDOException) { $this->db = Query([ 'type' => 'sqlite', @@ -71,16 +64,8 @@ class Collection extends DB { { $rawFile = file_get_contents($dbFileName); $dbFile = ($rawFile !== FALSE) ? $rawFile : ''; - $this->validDatabase = str_starts_with($dbFile, 'SQLite format 3'); + $this->db = (str_starts_with($dbFile, 'SQLite format 3')) ? $this->db : NULL; } - else - { - $this->validDatabase = FALSE; - } - } - else if ($this->db === NULL) - { - $this->validDatabase = FALSE; } } } diff --git a/src/AnimeClient/Model/MediaTrait.php b/src/AnimeClient/Model/MediaTrait.php index a844115c..14e2535c 100644 --- a/src/AnimeClient/Model/MediaTrait.php +++ b/src/AnimeClient/Model/MediaTrait.php @@ -100,7 +100,13 @@ trait MediaTrait { public function createLibraryItem(array $data): bool { $requester = new ParallelAPIRequest(); - $requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu'); + $kitsuRequest = $this->kitsuModel->createListItem($data); + if ($kitsuRequest === NULL) + { + return FALSE; + } + + $requester->addRequest($kitsuRequest, 'kitsu'); if ($this->anilistEnabled && $data['mal_id'] !== null) { diff --git a/src/AnimeClient/Model/Settings.php b/src/AnimeClient/Model/Settings.php index fc7e600b..13bacb76 100644 --- a/src/AnimeClient/Model/Settings.php +++ b/src/AnimeClient/Model/Settings.php @@ -124,6 +124,10 @@ final class Settings { public function validateSettings(array $settings): array { $cfg = Config::check($settings); + if ( ! is_iterable($cfg)) + { + return []; + } $looseConfig = []; $keyedConfig = []; diff --git a/src/AnimeClient/UrlGenerator.php b/src/AnimeClient/UrlGenerator.php index 77731bbf..19cb7262 100644 --- a/src/AnimeClient/UrlGenerator.php +++ b/src/AnimeClient/UrlGenerator.php @@ -72,7 +72,7 @@ class UrlGenerator extends RoutingBase { { $path = trim($path, '/'); - $path = preg_replace('`{/.*?}`i', '', $path); + $path = preg_replace('`{/.*?}`i', '', $path) ?? ""; // Remove any optional parameters from the route // and replace them with existing route parameters, if they exist @@ -87,7 +87,7 @@ class UrlGenerator extends RoutingBase { $segments[$i + 1] = ''; } - $pathSegments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $pathSegments[$i]); + $pathSegments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $pathSegments[$i] ?? ''); } $path = implode('/', $pathSegments); diff --git a/src/Ion/Type/ArrayType.php b/src/Ion/Type/ArrayType.php index cb3a9353..03050dbe 100644 --- a/src/Ion/Type/ArrayType.php +++ b/src/Ion/Type/ArrayType.php @@ -101,7 +101,7 @@ class ArrayType { * @return mixed * @throws InvalidArgumentException */ - public function __call(string $method, array $args) + public function __call(string $method, array $args): mixed { // Simple mapping for the majority of methods if (array_key_exists($method, $this->nativeMethods)) @@ -128,7 +128,7 @@ class ArrayType { * @param int|string|array $key * @return bool */ - public function hasKey($key): bool + public function hasKey(int|string|array $key): bool { if (\is_array($key)) { @@ -158,7 +158,7 @@ class ArrayType { * @param mixed $value * @return array */ - public function fill(int $start_index, int $num, $value): array + public function fill(int $start_index, int $num, mixed $value): array { return array_fill($start_index, $num, $value); } @@ -179,9 +179,9 @@ class ArrayType { * * @param mixed $value * @param bool $strict - * @return false|integer|string + * @return false|integer|string|null */ - public function search($value, bool $strict = TRUE) + public function search(mixed $value, bool $strict = TRUE): int|string|false|null { return array_search($value, $this->arr, $strict); } @@ -193,7 +193,7 @@ class ArrayType { * @param bool $strict * @return bool */ - public function has($value, bool $strict = TRUE): bool + public function has(mixed $value, bool $strict = TRUE): bool { return \in_array($value, $this->arr, $strict); } @@ -204,7 +204,7 @@ class ArrayType { * @param string|integer|null $key * @return mixed */ - public function &get($key = NULL) + public function &get(string|int|null $key = NULL): mixed { $value = NULL; if ($key === NULL) @@ -229,7 +229,7 @@ class ArrayType { * @param mixed $value * @return ArrayType */ - public function set($key, $value): ArrayType + public function set(mixed $key, mixed $value): ArrayType { $this->arr[$key] = $value; return $this; @@ -244,7 +244,7 @@ class ArrayType { * @param array $key An array of keys of the array * @return mixed */ - public function &getDeepKey(array $key) + public function &getDeepKey(array $key): mixed { $pos =& $this->arr; @@ -273,7 +273,7 @@ class ArrayType { * @param mixed $value * @return array */ - public function setDeepKey(array $key, $value): array + public function setDeepKey(array $key, mixed $value): array { $pos =& $this->arr;