Fix more PHPStan issues
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2021-02-12 19:17:39 -05:00
parent c03bd4c040
commit c71ff7f38e
8 changed files with 58 additions and 54 deletions

View File

@ -32,8 +32,7 @@ trait MutationTrait {
* Create a list item * Create a list item
* *
* @param array $data * @param array $data
* @return Request * @return Request|null
* @throws InvalidArgumentException
*/ */
public function createListItem(array $data): ?Request public function createListItem(array $data): ?Request
{ {

View File

@ -405,7 +405,7 @@ final class Kitsu {
if (empty($parts)) if (empty($parts))
{ {
return $last; return ($last !== NULL) ? $last : '';
} }
return (count($parts) > 1) return (count($parts) > 1)

View File

@ -50,6 +50,11 @@ final class AnimeCollection extends Collection {
*/ */
public function getCollection(): array public function getCollection(): array
{ {
if ($this->db === NULL)
{
return [];
}
$rawCollection = $this->getCollectionFromDatabase(); $rawCollection = $this->getCollectionFromDatabase();
$collection = []; $collection = [];
@ -76,7 +81,7 @@ final class AnimeCollection extends Collection {
*/ */
public function getFlatCollection(): array public function getFlatCollection(): array
{ {
if ( ! $this->validDatabase) if ($this->db === NULL)
{ {
return []; return [];
} }
@ -122,7 +127,7 @@ final class AnimeCollection extends Collection {
*/ */
public function getMediaTypeList(): array public function getMediaTypeList(): array
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return []; return [];
} }
@ -174,7 +179,7 @@ final class AnimeCollection extends Collection {
*/ */
public function add(mixed $data): void public function add(mixed $data): void
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return; return;
} }
@ -213,7 +218,7 @@ final class AnimeCollection extends Collection {
*/ */
public function wasAdded(array $data): bool public function wasAdded(array $data): bool
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return FALSE; return FALSE;
} }
@ -231,7 +236,7 @@ final class AnimeCollection extends Collection {
*/ */
public function update(array $data): void public function update(array $data): void
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return; return;
} }
@ -270,7 +275,7 @@ final class AnimeCollection extends Collection {
*/ */
public function wasUpdated(array $data): bool public function wasUpdated(array $data): bool
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return FALSE; return FALSE;
} }
@ -301,7 +306,7 @@ final class AnimeCollection extends Collection {
*/ */
public function delete(array $data): void public function delete(array $data): void
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return; return;
} }
@ -332,7 +337,7 @@ final class AnimeCollection extends Collection {
*/ */
public function wasDeleted(array $data): bool public function wasDeleted(array $data): bool
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return FALSE; return FALSE;
} }
@ -348,7 +353,7 @@ final class AnimeCollection extends Collection {
*/ */
public function get(int|string $kitsuId): array public function get(int|string $kitsuId): array
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return []; return [];
} }
@ -389,7 +394,7 @@ final class AnimeCollection extends Collection {
*/ */
public function has(int|string $kitsuId): bool public function has(int|string $kitsuId): bool
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return FALSE; return FALSE;
} }
@ -411,7 +416,7 @@ final class AnimeCollection extends Collection {
*/ */
public function getGenreList(array $filter = []): array public function getGenreList(array $filter = []): array
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return []; return [];
} }
@ -479,7 +484,7 @@ final class AnimeCollection extends Collection {
*/ */
public function getMediaList(array $filter = []): array public function getMediaList(array $filter = []): array
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return []; return [];
} }
@ -541,6 +546,11 @@ final class AnimeCollection extends Collection {
private function updateMediaLink(string $animeId, array $media): void private function updateMediaLink(string $animeId, array $media): void
{ {
if ($this->db === NULL)
{
return;
}
$this->db->beginTransaction(); $this->db->beginTransaction();
// Delete the old entries // Delete the old entries
@ -570,7 +580,7 @@ final class AnimeCollection extends Collection {
*/ */
private function updateGenres($animeId): void private function updateGenres($animeId): void
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return; return;
} }
@ -604,13 +614,13 @@ final class AnimeCollection extends Collection {
} }
} }
if ( ! empty($linksToInsert)) if ($this->db !== NULL && ! empty($linksToInsert))
{ {
try try
{ {
$this->db->insertBatch('anime_set_genre_link', $linksToInsert); $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 private function addNewGenres(array $genres): void
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return; return;
} }
@ -663,7 +673,7 @@ final class AnimeCollection extends Collection {
private function getExistingGenres(): array private function getExistingGenres(): array
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return []; return [];
} }
@ -693,7 +703,7 @@ final class AnimeCollection extends Collection {
private function getExistingGenreLinkEntries(): array private function getExistingGenreLinkEntries(): array
{ {
if ($this->validDatabase === FALSE) if ($this->db === NULL)
{ {
return []; return [];
} }
@ -734,7 +744,7 @@ final class AnimeCollection extends Collection {
*/ */
private function getCollectionFromDatabase(): array private function getCollectionFromDatabase(): array
{ {
if ( ! $this->validDatabase) if ($this->db === NULL)
{ {
return []; return [];
} }

View File

@ -29,15 +29,9 @@ class Collection extends DB {
/** /**
* The query builder object * The query builder object
* @var QueryBuilderInterface * @var QueryBuilderInterface|null
*/ */
protected QueryBuilderInterface $db; protected ?QueryBuilderInterface $db;
/**
* Whether the database is valid for querying
* @var boolean
*/
protected bool $validDatabase = FALSE;
/** /**
* Create a new collection object * Create a new collection object
@ -51,9 +45,8 @@ class Collection extends DB {
try try
{ {
$this->db = Query($this->dbConfig); $this->db = Query($this->dbConfig);
$this->validDatabase = TRUE;
} }
catch (PDOException $e) catch (PDOException)
{ {
$this->db = Query([ $this->db = Query([
'type' => 'sqlite', 'type' => 'sqlite',
@ -71,16 +64,8 @@ class Collection extends DB {
{ {
$rawFile = file_get_contents($dbFileName); $rawFile = file_get_contents($dbFileName);
$dbFile = ($rawFile !== FALSE) ? $rawFile : ''; $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;
} }
} }
} }

View File

@ -100,7 +100,13 @@ trait MediaTrait {
public function createLibraryItem(array $data): bool public function createLibraryItem(array $data): bool
{ {
$requester = new ParallelAPIRequest(); $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) if ($this->anilistEnabled && $data['mal_id'] !== null)
{ {

View File

@ -124,6 +124,10 @@ final class Settings {
public function validateSettings(array $settings): array public function validateSettings(array $settings): array
{ {
$cfg = Config::check($settings); $cfg = Config::check($settings);
if ( ! is_iterable($cfg))
{
return [];
}
$looseConfig = []; $looseConfig = [];
$keyedConfig = []; $keyedConfig = [];

View File

@ -72,7 +72,7 @@ class UrlGenerator extends RoutingBase {
{ {
$path = trim($path, '/'); $path = trim($path, '/');
$path = preg_replace('`{/.*?}`i', '', $path); $path = preg_replace('`{/.*?}`i', '', $path) ?? "";
// Remove any optional parameters from the route // Remove any optional parameters from the route
// and replace them with existing route parameters, if they exist // and replace them with existing route parameters, if they exist
@ -87,7 +87,7 @@ class UrlGenerator extends RoutingBase {
$segments[$i + 1] = ''; $segments[$i + 1] = '';
} }
$pathSegments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $pathSegments[$i]); $pathSegments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $pathSegments[$i] ?? '');
} }
$path = implode('/', $pathSegments); $path = implode('/', $pathSegments);

View File

@ -101,7 +101,7 @@ class ArrayType {
* @return mixed * @return mixed
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function __call(string $method, array $args) public function __call(string $method, array $args): mixed
{ {
// Simple mapping for the majority of methods // Simple mapping for the majority of methods
if (array_key_exists($method, $this->nativeMethods)) if (array_key_exists($method, $this->nativeMethods))
@ -128,7 +128,7 @@ class ArrayType {
* @param int|string|array $key * @param int|string|array $key
* @return bool * @return bool
*/ */
public function hasKey($key): bool public function hasKey(int|string|array $key): bool
{ {
if (\is_array($key)) if (\is_array($key))
{ {
@ -158,7 +158,7 @@ class ArrayType {
* @param mixed $value * @param mixed $value
* @return array * @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); return array_fill($start_index, $num, $value);
} }
@ -179,9 +179,9 @@ class ArrayType {
* *
* @param mixed $value * @param mixed $value
* @param bool $strict * @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); return array_search($value, $this->arr, $strict);
} }
@ -193,7 +193,7 @@ class ArrayType {
* @param bool $strict * @param bool $strict
* @return bool * @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); return \in_array($value, $this->arr, $strict);
} }
@ -204,7 +204,7 @@ class ArrayType {
* @param string|integer|null $key * @param string|integer|null $key
* @return mixed * @return mixed
*/ */
public function &get($key = NULL) public function &get(string|int|null $key = NULL): mixed
{ {
$value = NULL; $value = NULL;
if ($key === NULL) if ($key === NULL)
@ -229,7 +229,7 @@ class ArrayType {
* @param mixed $value * @param mixed $value
* @return ArrayType * @return ArrayType
*/ */
public function set($key, $value): ArrayType public function set(mixed $key, mixed $value): ArrayType
{ {
$this->arr[$key] = $value; $this->arr[$key] = $value;
return $this; return $this;
@ -244,7 +244,7 @@ class ArrayType {
* @param array $key An array of keys of the array * @param array $key An array of keys of the array
* @return mixed * @return mixed
*/ */
public function &getDeepKey(array $key) public function &getDeepKey(array $key): mixed
{ {
$pos =& $this->arr; $pos =& $this->arr;
@ -273,7 +273,7 @@ class ArrayType {
* @param mixed $value * @param mixed $value
* @return array * @return array
*/ */
public function setDeepKey(array $key, $value): array public function setDeepKey(array $key, mixed $value): array
{ {
$pos =& $this->arr; $pos =& $this->arr;