Resolve remaining PHPStan issues

This commit is contained in:
Timothy Warren 2021-02-16 14:43:51 -05:00
parent 73ee1a41e1
commit b393c695a5
15 changed files with 89 additions and 63 deletions

View File

@ -60,7 +60,7 @@ $checkedConfig = ConfigType::check($configArray);
// First look in app config, then PHP config, and at last // First look in app config, then PHP config, and at last
// resort, just set to UTC. // resort, just set to UTC.
$timezone = ini_get('date.timezone'); $timezone = ini_get('date.timezone');
if (array_key_exists('timezone', $checkedConfig) && ! empty($checkedConfig['timezone'])) if (is_array($checkedConfig) && array_key_exists('timezone', $checkedConfig) && ! empty($checkedConfig['timezone']))
{ {
date_default_timezone_set($checkedConfig['timezone']); date_default_timezone_set($checkedConfig['timezone']);
} }

View File

@ -12,6 +12,7 @@ parameters:
- '#Function imagepalletetotruecolor not found#' - '#Function imagepalletetotruecolor not found#'
- '#Call to an undefined method Aura\\\Html\\\HelperLocator::[a-zA-Z0-9_]+\(\)#' - '#Call to an undefined method Aura\\\Html\\\HelperLocator::[a-zA-Z0-9_]+\(\)#'
- '#Call to an undefined method Query\\QueryBuilderInterface::[a-zA-Z0-9_]+\(\)#' - '#Call to an undefined method Query\\QueryBuilderInterface::[a-zA-Z0-9_]+\(\)#'
- '#Unable to resolve the template type TValue#'
excludes_analyse: excludes_analyse:
- tests/mocks.php - tests/mocks.php
- vendor - vendor

View File

@ -193,12 +193,11 @@ abstract class APIRequestBuilder {
* Set the request body * Set the request body
* *
* @param mixed $body * @param mixed $body
* @throws \TypeError
* @return self * @return self
*/ */
public function setJsonBody($body): self public function setJsonBody(mixed $body): self
{ {
$requestBody = ( ! is_scalar($body)) $requestBody = ( ! is_string($body))
? Json::encode($body) ? Json::encode($body)
: $body; : $body;

View File

@ -38,7 +38,7 @@ final class ListItem extends AbstractListItem {
public function create(array $data): Request public function create(array $data): Request
{ {
$checkedData = Types\MediaListEntry::check($data); $checkedData = Types\MediaListEntry::check($data);
return $this->requestBuilder->mutateRequest('CreateMediaListEntry', $checkedData); return $this->requestBuilder->mutateRequest('CreateMediaListEntry', $checkedData ?? []);
} }
/** /**
@ -50,7 +50,7 @@ final class ListItem extends AbstractListItem {
public function createFull(array $data): Request public function createFull(array $data): Request
{ {
$checkedData = Types\MediaListEntry::check($data); $checkedData = Types\MediaListEntry::check($data);
return $this->requestBuilder->mutateRequest('CreateFullMediaListEntry', $checkedData); return $this->requestBuilder->mutateRequest('CreateFullMediaListEntry', $checkedData ?? []);
} }
/** /**
@ -90,7 +90,7 @@ final class ListItem extends AbstractListItem {
'progress' => $data->progress, 'progress' => $data->progress,
]); ]);
return $this->requestBuilder->mutateRequest('IncrementMediaListEntry', $checkedData); return $this->requestBuilder->mutateRequest('IncrementMediaListEntry', $checkedData ?? []);
} }
/** /**
@ -120,6 +120,6 @@ final class ListItem extends AbstractListItem {
'notes' => $notes, 'notes' => $notes,
]); ]);
return $this->requestBuilder->mutateRequest('UpdateMediaListEntry', $updateData); return $this->requestBuilder->mutateRequest('UpdateMediaListEntry', $updateData ?? []);
} }
} }

View File

@ -190,6 +190,10 @@ final class Model
public function getListItem(string $malId, string $type): array public function getListItem(string $malId, string $type): array
{ {
$id = $this->getListIdFromMalId($malId, $type); $id = $this->getListIdFromMalId($malId, $type);
if ($id === NULL)
{
return [];
}
$data = $this->listItem->get($id)['data']; $data = $this->listItem->get($id)['data'];

View File

@ -205,13 +205,16 @@ final class RequestBuilder extends APIRequestBuilder {
$request = $this->setUpRequest($url, $options); $request = $this->setUpRequest($url, $options);
$response = getResponse($request); $response = getResponse($request);
$logger->debug('Anilist response', [ if ($logger !== NULL)
'status' => $response->getStatus(), {
'reason' => $response->getReason(), $logger->debug('Anilist response', [
'body' => $response->getBody(), 'status' => $response->getStatus(),
'headers' => $response->getHeaders(), 'reason' => $response->getReason(),
'requestHeaders' => $request->getHeaders(), 'body' => $response->getBody(),
]); 'headers' => $response->getHeaders(),
'requestHeaders' => $request->getHeaders(),
]);
}
return $response; return $response;
} }
@ -227,13 +230,16 @@ final class RequestBuilder extends APIRequestBuilder {
$response = getResponse($request); $response = getResponse($request);
$logger->debug('Anilist response', [ if ($logger !== NULL)
'status' => $response->getStatus(), {
'reason' => $response->getReason(), $logger->debug('Anilist response', [
'body' => $response->getBody(), 'status' => $response->getStatus(),
'headers' => $response->getHeaders(), 'reason' => $response->getReason(),
'requestHeaders' => $request->getHeaders(), 'body' => $response->getBody(),
]); 'headers' => $response->getHeaders(),
'requestHeaders' => $request->getHeaders(),
]);
}
return $response; return $response;
} }
@ -251,17 +257,24 @@ final class RequestBuilder extends APIRequestBuilder {
$validResponseCodes = [200, 201]; $validResponseCodes = [200, 201];
$logger = $this->container->getLogger('anilist-request'); $logger = $this->container->getLogger('anilist-request');
$logger->debug('Anilist response', [ if ($logger !== NULL)
'status' => $response->getStatus(), {
'reason' => $response->getReason(), $logger->debug('Anilist response', [
'body' => $response->getBody(), 'status' => $response->getStatus(),
'headers' => $response->getHeaders(), 'reason' => $response->getReason(),
//'requestHeaders' => $request->getHeaders(), 'body' => $response->getBody(),
]); 'headers' => $response->getHeaders(),
//'requestHeaders' => $request->getHeaders(),
]);
}
if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE)) if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE))
{ {
$logger->warning('Non 200 response for POST api call', (array)$response->getBody()); if ($logger !== NULL)
{
$logger->warning('Non 200 response for POST api call', (array)$response->getBody());
}
} }
$rawBody = wait($response->getBody()->buffer()); $rawBody = wait($response->getBody()->buffer());

View File

@ -62,10 +62,11 @@ trait CacheTrait {
*/ */
public function getCached(string $key, callable $primer, ?array $primeArgs = []): mixed public function getCached(string $key, callable $primer, ?array $primeArgs = []): mixed
{ {
$value = $this->cache->get($key, NULL); $value = $this->cache->get($key);
if ($value === NULL) if ($value === NULL)
{ {
$primeArgs ??= [];
$value = $primer(...$primeArgs); $value = $primer(...$primeArgs);
if ($value === NULL) if ($value === NULL)
{ {

View File

@ -166,11 +166,6 @@ final class ListItem extends AbstractListItem {
return $this->requestBuilder->mutateRequest('UpdateLibraryItem', $updateData); return $this->requestBuilder->mutateRequest('UpdateLibraryItem', $updateData);
} }
/**
* @return bool|string
* @throws ContainerException
* @throws NotFoundException
*/
private function getAuthHeader(): ?string private function getAuthHeader(): ?string
{ {
$auth = $this->getContainer()->get('auth'); $auth = $this->getContainer()->get('auth');

View File

@ -89,9 +89,9 @@ final class Model {
* *
* @param string $username * @param string $username
* @param string $password * @param string $password
* @return bool|array * @return array|false
*/ */
public function authenticate(string $username, string $password) public function authenticate(string $username, string $password): array|false
{ {
// K::AUTH_URL // K::AUTH_URL
$response = $this->requestBuilder->getResponse('POST', K::AUTH_URL, [ $response = $this->requestBuilder->getResponse('POST', K::AUTH_URL, [
@ -131,9 +131,9 @@ final class Model {
* Extend the current session with a refresh token * Extend the current session with a refresh token
* *
* @param string $token * @param string $token
* @return bool|array * @return array|false
*/ */
public function reAuthenticate(string $token) public function reAuthenticate(string $token): array|false
{ {
$response = $this->requestBuilder->getResponse('POST', K::AUTH_URL, [ $response = $this->requestBuilder->getResponse('POST', K::AUTH_URL, [
'headers' => [ 'headers' => [
@ -617,6 +617,7 @@ final class Model {
} }
/** /**
*
* Get the data to sync Kitsu anime/manga list with another API * Get the data to sync Kitsu anime/manga list with another API
* *
* @param string $type * @param string $type
@ -708,11 +709,9 @@ final class Model {
$page = $data['pageInfo']; $page = $data['pageInfo'];
if (empty($data)) if (empty($data))
{ {
// @TODO Proper Error logging
dump($rawData); dump($rawData);
die(); die();
// @TODO Error logging
break;
} }
$cursor = $page['endCursor']; $cursor = $page['endCursor'];
@ -812,7 +811,7 @@ final class Model {
}); });
} }
private function getPages(callable $method, mixed ...$args): ?Generator private function getPages(callable $method, mixed ...$args): Generator
{ {
$items = $method(...$args); $items = $method(...$args);

View File

@ -144,7 +144,10 @@ final class RequestBuilder extends APIRequestBuilder {
if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE)) if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE))
{ {
$logger = $this->container->getLogger('kitsu-graphql'); $logger = $this->container->getLogger('kitsu-graphql');
$logger->warning('Non 200 response for GraphQL call', (array)$response->getBody()); if ($logger !== NULL)
{
$logger->warning('Non 200 response for GraphQL call', (array)$response->getBody());
}
} }
return Json::decode(wait($response->getBody()->buffer())); return Json::decode(wait($response->getBody()->buffer()));
@ -166,7 +169,10 @@ final class RequestBuilder extends APIRequestBuilder {
if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE)) if ( ! \in_array($response->getStatus(), $validResponseCodes, TRUE))
{ {
$logger = $this->container->getLogger('kitsu-graphql'); $logger = $this->container->getLogger('kitsu-graphql');
$logger->warning('Non 200 response for GraphQL call', (array)$response->getBody()); if ($logger !== NULL)
{
$logger->warning('Non 200 response for GraphQL call', (array)$response->getBody());
}
} }
return Json::decode(wait($response->getBody()->buffer())); return Json::decode(wait($response->getBody()->buffer()));
@ -186,13 +192,16 @@ final class RequestBuilder extends APIRequestBuilder {
$request = $this->setUpRequest($type, $url, $options); $request = $this->setUpRequest($type, $url, $options);
$response = getResponse($request); $response = getResponse($request);
$logger->debug('Kitsu API Response', [ if ($logger !== NULL)
'status' => $response->getStatus(), {
'reason' => $response->getReason(), $logger->debug('Kitsu API Response', [
'body' => $response->getBody(), 'status' => $response->getStatus(),
'headers' => $response->getHeaders(), 'reason' => $response->getReason(),
'requestHeaders' => $request->getHeaders(), 'body' => $response->getBody(),
]); 'headers' => $response->getHeaders(),
'requestHeaders' => $request->getHeaders(),
]);
}
return $response; return $response;
} }
@ -289,7 +298,10 @@ final class RequestBuilder extends APIRequestBuilder {
// Any other type of failed request // Any other type of failed request
if ($statusCode > 299 || $statusCode < 200) if ($statusCode > 299 || $statusCode < 200)
{ {
$logger->warning('Non 2xx response for api call', (array)$response); if ($logger !== NULL)
{
$logger->warning('Non 2xx response for api call', (array)$response);
}
} }
try try

View File

@ -43,7 +43,7 @@ final class AnimeTransformer extends AbstractTransformer {
sort($genres); sort($genres);
$title = $base['titles']['canonical']; $title = $base['titles']['canonical'] ?? '';
$titles = Kitsu::getTitles($base['titles']); $titles = Kitsu::getTitles($base['titles']);
$titles_more = Kitsu::filterLocalizedTitles($base['titles']); $titles_more = Kitsu::filterLocalizedTitles($base['titles']);

View File

@ -727,7 +727,7 @@ final class SyncLists extends BaseCommand {
$this->echoWarning("Skipped creating Kitsu {$type} due to missing id ¯\_(ツ)_/¯"); $this->echoWarning("Skipped creating Kitsu {$type} due to missing id ¯\_(ツ)_/¯");
continue; continue;
} }
$requester->addRequest($this->kitsuModel->createListItem($item)); $requester->addRequest($maybeRequest);
} }
} }
@ -785,9 +785,11 @@ final class SyncLists extends BaseCommand {
{ {
if ($action === SyncAction::UPDATE) if ($action === SyncAction::UPDATE)
{ {
$requester->addRequest( $maybeRequest = $this->anilistModel->updateListItem(FormItem::from($item), $type);
$this->anilistModel->updateListItem(FormItem::from($item), $type) if ($maybeRequest !== NULL)
); {
$requester->addRequest($maybeRequest);
}
} }
else if ($action === SyncAction::CREATE) else if ($action === SyncAction::CREATE)
{ {

View File

@ -329,7 +329,7 @@ final class Anime extends BaseController {
'title' => $this->formatTitle( 'title' => $this->formatTitle(
$this->config->get('whose_list') . "'s Anime List", $this->config->get('whose_list') . "'s Anime List",
'Anime', 'Anime',
$data->title $data->title ?? ''
), ),
'data' => $data, 'data' => $data,
]); ]);
@ -367,7 +367,7 @@ final class Anime extends BaseController {
'title' => $this->formatTitle( 'title' => $this->formatTitle(
$this->config->get('whose_list') . "'s Anime List", $this->config->get('whose_list') . "'s Anime List",
'Anime', 'Anime',
$data->title $data->title ?? ''
), ),
'data' => $data, 'data' => $data,
]); ]);

View File

@ -139,7 +139,7 @@ final class AnimeCollection extends BaseController {
$action $action
), ),
'media_items' => $this->animeCollectionModel->getMediaTypeList(), 'media_items' => $this->animeCollectionModel->getMediaTypeList(),
'item' => ($action === 'Edit') ? $this->animeCollectionModel->get($id) : [] 'item' => ($action === 'Edit' && $id !== NULL) ? $this->animeCollectionModel->get($id) : []
]); ]);
} }

View File

@ -166,7 +166,7 @@ final class Images extends BaseController {
{ {
$contentType = ($ext === 'webp') $contentType = ($ext === 'webp')
? 'image/webp' ? 'image/webp'
: $response->getHeader('content-type')[0]; : $response->getHeader('content-type')[0] ?? 'image/jpeg';
$outputFile = (str_contains($file, '-original')) $outputFile = (str_contains($file, '-original'))
? "{$filePrefix}-original.{$ext}" ? "{$filePrefix}-original.{$ext}"