From dc9d7ab4e1df97806bd7548e99c0ba6191c59d66 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 18 Jan 2018 16:21:45 -0500 Subject: [PATCH] Lots of style fixes and minor logic tweaks --- app/appConf/routes.php | 5 +- composer.json | 14 +++- console | 35 +++++----- index.php | 2 +- phpstan.neon | 10 +-- src/API/APIRequestBuilder.php | 2 +- src/API/CacheTrait.php | 21 +++--- src/API/HummingbirdClient.php | 6 +- src/API/JsonAPI.php | 2 +- src/API/Kitsu/KitsuTrait.php | 4 +- .../Transformer/AnimeListTransformer.php | 8 +-- .../Transformer/MangaListTransformer.php | 8 +-- src/AnimeClient.php | 15 ----- src/Controller.php | 2 +- src/Controller/Anime.php | 4 +- src/Controller/Manga.php | 6 -- src/Dispatcher.php | 66 ++++++++++++------- src/Model/API.php | 5 +- src/Model/AnimeCollection.php | 35 +++++----- src/Model/Collection.php | 15 ++--- src/Model/DB.php | 7 +- src/Model/MangaCollection.php | 35 +++++----- .../AbstractModel.php => constants.php} | 18 ++--- tests/ControllerTest.php | 12 ++-- tests/mocks.php | 6 +- 25 files changed, 170 insertions(+), 173 deletions(-) rename src/{Model/AbstractModel.php => constants.php} (56%) diff --git a/app/appConf/routes.php b/app/appConf/routes.php index 08e32bd0..387c74c8 100644 --- a/app/appConf/routes.php +++ b/app/appConf/routes.php @@ -16,17 +16,14 @@ use const Aviat\AnimeClient\{ - DEFAULT_CONTROLLER_NAMESPACE, DEFAULT_CONTROLLER_METHOD, DEFAULT_CONTROLLER }; -use Aviat\AnimeClient\AnimeClient; - // ------------------------------------------------------------------------- // Routing Config // -// Maps paths to controlers and methods +// Maps paths to controllers and methods // ------------------------------------------------------------------------- return [ // --------------------------------------------------------------------- diff --git a/composer.json b/composer.json index f7b6eb70..82279651 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,7 @@ "license": "MIT", "autoload": { "files": [ + "src/constants.php", "src/AnimeClient.php" ], "psr-4": { @@ -22,7 +23,7 @@ "aura/router": "^3.0", "aura/session": "^2.0", "aviat/banker": "^1.0.0", - "aviat/ion": "^2.2.0", + "aviat/ion": "^2.3.0", "maximebf/consolekit": "^1.0", "monolog/monolog": "^1.0", "psr/http-message": "~1.0", @@ -51,9 +52,16 @@ "build:css": "cd public && npm run build && cd ..", "clean": "vendor/bin/robo clean", "coverage": "phpdbg -qrr -- vendor/bin/phpunit -c build", - "docs": "vendor/bin/phpdox", - "phpstan": "phpstan analyse -l 3 ./phpstan.neon src tests", + "phpstan": "phpstan analyse -l 4 -c phpstan.neon src tests ./console index.php", "watch:css": "cd public && npm run watch", "test": "vendor/bin/phpunit" + }, + "scripts-descriptions": { + "build": "Generate the api docs", + "build:css": "Generate browser css", + "clean": "Remove documentation generation files and folders", + "coverage": "Generate a test coverage report", + "phpstan": "Run PHP Static analysis", + "test": "Run the unit tests" } } diff --git a/console b/console index 674a96e5..6869e202 100755 --- a/console +++ b/console @@ -2,30 +2,29 @@ Command\CachePrime::class, - 'cache-clear' => Command\CacheClear::class, - 'clear-cache' => Command\CacheClear::class, - 'sync-lists' => Command\SyncKitsuWithMal::class, -]); +try +{ + (new \ConsoleKit\Console([ + 'cache:clear' => Command\CacheClear::class, + 'cache:prime' => Command\CachePrime::class, + 'lists:sync' => Command\SyncKitsuWithMal::class, + 'cache-prime' => Command\CachePrime::class, + 'cache-clear' => Command\CacheClear::class, + 'clear-cache' => Command\CacheClear::class, + 'sync-lists' => Command\SyncKitsuWithMal::class, + ]))->run(); +} +catch (\Exception $e) +{ + +} -$console->run(); \ No newline at end of file diff --git a/index.php b/index.php index 6edaeba5..b13fc9ac 100644 --- a/index.php +++ b/index.php @@ -41,7 +41,7 @@ $CONF_DIR = _dir($APP_DIR, 'config'); // Dependency Injection setup // ----------------------------------------------------------------------------- $base_config = require $APPCONF_DIR . '/base_config.php'; -$di = require_once $APP_DIR . '/bootstrap.php'; +$di = require $APP_DIR . '/bootstrap.php'; $config = loadToml($CONF_DIR); $config_array = array_merge($base_config, $config); diff --git a/phpstan.neon b/phpstan.neon index 7d68366f..964eb301 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,9 @@ parameters: + autoload_files: + - %rootDir%/../../../tests/mocks.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_]+\(\)#' - - '#Call to an undefined method Aura\\\Html\\\HelperLocator:[a-zA-Z0-9_]+\(\)#' - excludes_analyze: - - %rootDir%/test_views/* \ No newline at end of file + - '#Call to an undefined method Aviat\\\Ion\\\Friend::[a-zA-Z0-9_]+\(\)#' + - '#Call to an undefined method Aura\\\Html\\\HelperLocator::[a-zA-Z0-9_]+\(\)#' + - '#Undefined variable: \$var#' + - '#Property Amp\\Artax\\Internal\\RequestCycle::\$[a-zA-Z0-9_]+#' diff --git a/src/API/APIRequestBuilder.php b/src/API/APIRequestBuilder.php index 5a0fe9b1..3fb2a7b5 100644 --- a/src/API/APIRequestBuilder.php +++ b/src/API/APIRequestBuilder.php @@ -55,7 +55,7 @@ class APIRequestBuilder { protected $defaultHeaders = []; /** - * Valid HTTP request methos + * Valid HTTP request methods * @var array */ protected $validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; diff --git a/src/API/CacheTrait.php b/src/API/CacheTrait.php index 6a00270d..de3ff71a 100644 --- a/src/API/CacheTrait.php +++ b/src/API/CacheTrait.php @@ -17,18 +17,17 @@ namespace Aviat\AnimeClient\API; use Aviat\Banker\Pool; -use Aviat\Ion\Di\ContainerAware; /** * Helper methods for dealing with the Cache */ trait CacheTrait { - + /** - * @var Aviat\Banker\Pool + * @var Pool */ protected $cache; - + /** * Inject the cache object * @@ -40,34 +39,32 @@ trait CacheTrait { $this->cache = $cache; return $this; } - + /** * Get the cache object if it exists * * @return Pool */ - public function getCache() + public function getCache(): Pool { return $this->cache; } - + /** * Generate a hash as a cache key from the current method call * - * @param object $object + * @param mixed $object * @param string $method * @param array $args * @return string */ public function getHashForMethodCall($object, string $method, array $args = []): string { - $classname = get_class($object); $keyObj = [ - 'class' => $classname, + 'class' => \get_class($object), 'method' => $method, 'args' => $args, ]; - $hash = sha1(json_encode($keyObj)); - return $hash; + return sha1(json_encode($keyObj)); } } \ No newline at end of file diff --git a/src/API/HummingbirdClient.php b/src/API/HummingbirdClient.php index 7cf46123..6f72e536 100644 --- a/src/API/HummingbirdClient.php +++ b/src/API/HummingbirdClient.php @@ -63,7 +63,7 @@ use function Amp\{ * @see Client */ final class HummingbirdClient implements Client { - const DEFAULT_USER_AGENT = 'Mozilla/5.0 (compatible; Artax)'; + const DEFAULT_USER_AGENT = 'Hummingbird Anime Client/5.0'; private $cookieJar; private $socketPool; @@ -71,7 +71,7 @@ final class HummingbirdClient implements Client { private $hasZlib; private $options = [ self::OP_AUTO_ENCODING => true, - self::OP_TRANSFER_TIMEOUT => 15000, + self::OP_TRANSFER_TIMEOUT => 60000, self::OP_MAX_REDIRECTS => 5, self::OP_AUTO_REFERER => true, self::OP_DISCARD_BODY => false, @@ -89,7 +89,7 @@ final class HummingbirdClient implements Client { $this->cookieJar = $cookieJar ?? new NullCookieJar; $this->tlsContext = $tlsContext ?? new ClientTlsContext; $this->socketPool = $socketPool ?? new HttpSocketPool; - $this->hasZlib = extension_loaded('zlib'); + $this->hasZlib = \extension_loaded('zlib'); } /** @inheritdoc */ diff --git a/src/API/JsonAPI.php b/src/API/JsonAPI.php index 05ac78ea..c90c4b85 100644 --- a/src/API/JsonAPI.php +++ b/src/API/JsonAPI.php @@ -313,7 +313,7 @@ class JsonAPI { foreach ($data['data'] as $item) { - if (is_array($item) && array_key_exists('id', $item)) + if (\is_array($item) && array_key_exists('id', $item)) { $organized[$key][] = $item['id']; } diff --git a/src/API/Kitsu/KitsuTrait.php b/src/API/Kitsu/KitsuTrait.php index 3c0bf48c..efe3a930 100644 --- a/src/API/Kitsu/KitsuTrait.php +++ b/src/API/Kitsu/KitsuTrait.php @@ -177,7 +177,7 @@ trait KitsuTrait { try { - return Json::decode(wait($response->getBody()), TRUE); + return Json::decode(wait($response->getBody())); } catch (JsonException $e) { @@ -226,7 +226,7 @@ trait KitsuTrait { $response = $this->getResponse('POST', ...$args); $validResponseCodes = [200, 201]; - if ( ! in_array((int) $response->getStatus(), $validResponseCodes)) + if ( ! \in_array((int) $response->getStatus(), $validResponseCodes, TRUE)) { if ($logger) { diff --git a/src/API/Kitsu/Transformer/AnimeListTransformer.php b/src/API/Kitsu/Transformer/AnimeListTransformer.php index 539f01b4..cd692caf 100644 --- a/src/API/Kitsu/Transformer/AnimeListTransformer.php +++ b/src/API/Kitsu/Transformer/AnimeListTransformer.php @@ -31,7 +31,7 @@ class AnimeListTransformer extends AbstractTransformer { * @param array $item API library item * @return array */ - public function transform($item) + public function transform($item): array { $included = $item['included']; $animeId = $item['relationships']['media']['data']['id']; @@ -41,7 +41,7 @@ class AnimeListTransformer extends AbstractTransformer { sort($genres); $rating = (int) $item['attributes']['rating'] !== 0 - ? (int) 2 * $item['attributes']['rating'] + ? 2 * $item['attributes']['rating'] : '-'; $total_episodes = array_key_exists('episodeCount', $anime) && (int) $anime['episodeCount'] !== 0 @@ -97,7 +97,7 @@ class AnimeListTransformer extends AbstractTransformer { 'rewatching' => (bool) $item['attributes']['reconsuming'], 'rewatched' => (int) $item['attributes']['reconsumeCount'], 'user_rating' => $rating, - 'private' => (bool) $item['attributes']['private'] ?? FALSE, + 'private' => $item['attributes']['private'] ?? FALSE, ]; } @@ -108,7 +108,7 @@ class AnimeListTransformer extends AbstractTransformer { * @param array $item Transformed library item * @return array API library item */ - public function untransform($item) + public function untransform($item): array { $privacy = (array_key_exists('private', $item) && $item['private']); $rewatching = (array_key_exists('rewatching', $item) && $item['rewatching']); diff --git a/src/API/Kitsu/Transformer/MangaListTransformer.php b/src/API/Kitsu/Transformer/MangaListTransformer.php index 0e143632..b243bcac 100644 --- a/src/API/Kitsu/Transformer/MangaListTransformer.php +++ b/src/API/Kitsu/Transformer/MangaListTransformer.php @@ -33,7 +33,7 @@ class MangaListTransformer extends AbstractTransformer { * @param array $item manga entry item * @return array */ - public function transform($item) + public function transform($item): array { $included = $item['included']; $mangaId = $item['relationships']['media']['data']['id']; @@ -43,7 +43,7 @@ class MangaListTransformer extends AbstractTransformer { sort($genres); $rating = (int) $item['attributes']['rating'] !== 0 - ? (int) 2 * $item['attributes']['rating'] + ? 2 * $item['attributes']['rating'] : '-'; $totalChapters = ((int) $manga['chapterCount'] !== 0) @@ -109,9 +109,9 @@ class MangaListTransformer extends AbstractTransformer { * @param array $item * @return array */ - public function untransform($item) + public function untransform($item): array { - $rereading = (array_key_exists('rereading', $item)) && (bool)$item['rereading']; + $rereading = array_key_exists('rereading', $item) && (bool)$item['rereading']; $map = [ 'id' => $item['id'], diff --git a/src/AnimeClient.php b/src/AnimeClient.php index 0e15f4c1..5383dabb 100644 --- a/src/AnimeClient.php +++ b/src/AnimeClient.php @@ -18,21 +18,6 @@ namespace Aviat\AnimeClient; use Yosymfony\Toml\Toml; -if ( ! \defined('SRC_DIR')) -{ - \define('SRC_DIR', \realpath(__DIR__)); -} - -const SESSION_SEGMENT = 'Aviat\AnimeClient\Auth'; -const DEFAULT_CONTROLLER = Controller\Index::class; -const DEFAULT_CONTROLLER_NAMESPACE = Controller::class; -const DEFAULT_LIST_CONTROLLER = Controller\Anime::class; -const DEFAULT_CONTROLLER_METHOD = 'index'; -const NOT_FOUND_METHOD = 'notFound'; -const ERROR_MESSAGE_METHOD = 'errorPage'; -const SRC_DIR = SRC_DIR; - - if ( ! \function_exists('Aviat\AnimeClient\loadToml')) { /** diff --git a/src/Controller.php b/src/Controller.php index de45bfa5..c6bb8e57 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -106,7 +106,7 @@ class Controller { $this->request = $container->get('request'); $this->response = $container->get('response'); - $this->baseData = array_merge((array)$this->baseData, [ + $this->baseData = array_merge($this->baseData, [ 'url' => $auraUrlGenerator, 'urlGenerator' => $urlGenerator, 'auth' => $container->get('auth'), diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php index f8f9039d..478f7bc3 100644 --- a/src/Controller/Anime.php +++ b/src/Controller/Anime.php @@ -232,9 +232,9 @@ class Anime extends BaseController { $body = $this->request->getParsedBody(); $response = $this->model->deleteLibraryItem($body['id'], $body['mal_id']); - if ((bool)$response === TRUE) + if ($response === TRUE) { - $this->setFlashMessage("Successfully deleted anime.", 'success'); + $this->setFlashMessage('Successfully deleted anime.', 'success'); $this->cache->clear(); } else diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php index a365cd80..78d30fe6 100644 --- a/src/Controller/Manga.php +++ b/src/Controller/Manga.php @@ -36,12 +36,6 @@ class Manga extends Controller { */ protected $model; - /** - * Data to ve sent to all routes in this controller - * @var array $baseData - */ - protected $baseData; - /** * Constructor * diff --git a/src/Dispatcher.php b/src/Dispatcher.php index e05f2a1c..19e17b35 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -78,15 +78,18 @@ class Dispatcher extends RoutingBase { */ public function getRoute() { - $logger = $this->container->getLogger('default'); + $logger = $this->container->getLogger(); $rawRoute = $this->request->getUri()->getPath(); $routePath = '/' . trim($rawRoute, '/'); - $logger->info('Dispatcher - Routing data from get_route method'); - $logger->info(print_r([ - 'route_path' => $routePath - ], TRUE)); + if ($logger !== NULL) + { + $logger->info('Dispatcher - Routing data from get_route method'); + $logger->info(print_r([ + 'route_path' => $routePath + ], TRUE)); + } return $this->matcher->match($this->request); } @@ -107,16 +110,19 @@ class Dispatcher extends RoutingBase { * @param object|null $route * @return void */ - public function __invoke($route = NULL) + public function __invoke($route = NULL): void { - $logger = $this->container->getLogger('default'); + $logger = $this->container->getLogger(); - if (is_null($route)) + if ($route === NULL) { $route = $this->getRoute(); - $logger->info('Dispatcher - Route invoke arguments'); - $logger->info(print_r($route, TRUE)); + if ($logger !== NULL) + { + $logger->info('Dispatcher - Route invoke arguments'); + $logger->info(print_r($route, TRUE)); + } } if ($route) @@ -147,7 +153,7 @@ class Dispatcher extends RoutingBase { * @throws \LogicException * @return array */ - protected function processRoute($route) + protected function processRoute($route): array { if (array_key_exists('controller', $route->attributes)) { @@ -155,7 +161,7 @@ class Dispatcher extends RoutingBase { } else { - throw new \LogicException("Missing controller"); + throw new \LogicException('Missing controller'); } // Get the full namespace for a controller if a short name is given @@ -181,8 +187,11 @@ class Dispatcher extends RoutingBase { } } } - $logger = $this->container->getLogger('default'); - $logger->info(json_encode($params)); + $logger = $this->container->getLogger(); + if ($logger !== NULL) + { + $logger->info(json_encode($params)); + } return [ 'controller_name' => $controllerName, @@ -205,8 +214,11 @@ class Dispatcher extends RoutingBase { $segments = explode('/', $path); $controller = reset($segments); - $logger = $this->container->getLogger('default'); - $logger->info('Controller: ' . $controller); + $logger = $this->container->getLogger(); + if ($logger !== NULL) + { + $logger->info('Controller: ' . $controller); + } if (empty($controller)) { @@ -234,7 +246,7 @@ class Dispatcher extends RoutingBase { foreach ($classFiles as $file) { - $rawClassName = basename(str_replace(".php", "", $file)); + $rawClassName = basename(str_replace('.php', '', $file)); $path = $this->string($rawClassName)->dasherize()->__toString(); $className = trim($defaultNamespace . '\\' . $rawClassName, '\\'); @@ -262,9 +274,12 @@ class Dispatcher extends RoutingBase { $controller = new $controllerName($this->container); // Run the appropriate controller method - $logger->debug('Dispatcher - controller arguments', $params); + if ($logger !== NULL) + { + $logger->debug('Dispatcher - controller arguments', $params); + } - call_user_func_array([$controller, $method], $params); + \call_user_func_array([$controller, $method], $params); } catch (FailedResponseException $e) { @@ -285,11 +300,14 @@ class Dispatcher extends RoutingBase { */ protected function getErrorParams() { - $logger = $this->container->getLogger('default'); + $logger = $this->container->getLogger(); $failure = $this->matcher->getFailedRoute(); - $logger->info('Dispatcher - failed route'); - $logger->info(print_r($failure, TRUE)); + if ($logger !== NULL) + { + $logger->info('Dispatcher - failed route'); + $logger->info(print_r($failure, TRUE)); + } $actionMethod = ERROR_MESSAGE_METHOD; @@ -354,9 +372,9 @@ class Dispatcher extends RoutingBase { $route['controller'] = $controllerClass; // Select the appropriate router method based on the http verb - $add = (array_key_exists('verb', $route)) + $add = array_key_exists('verb', $route) ? strtolower($route['verb']) - : "get"; + : 'get'; // Add the route to the router object if ( ! array_key_exists('tokens', $route)) diff --git a/src/Model/API.php b/src/Model/API.php index f08e0a9f..e4501c90 100644 --- a/src/Model/API.php +++ b/src/Model/API.php @@ -16,10 +16,13 @@ namespace Aviat\AnimeClient\Model; +use Aviat\Ion\StringWrapper; + /** * Base model for api interaction */ -class API extends AbstractModel { +class API { + use StringWrapper; /** * Whether to use the MAL api diff --git a/src/Model/AnimeCollection.php b/src/Model/AnimeCollection.php index 8a9e307d..2af9de2c 100644 --- a/src/Model/AnimeCollection.php +++ b/src/Model/AnimeCollection.php @@ -16,9 +16,7 @@ namespace Aviat\AnimeClient\Model; -use Aviat\AnimeClient\API\Kitsu; use Aviat\Ion\Di\ContainerInterface; -use Aviat\Ion\Json; use PDO; /** @@ -48,7 +46,7 @@ class AnimeCollection extends Collection { * * @return array */ - public function getCollection() + public function getCollection(): array { $rawCollection = $this->getCollectionFromDatabase(); @@ -74,7 +72,7 @@ class AnimeCollection extends Collection { * * @return array */ - public function getMediaTypeList() + public function getMediaTypeList(): array { $output = []; @@ -93,13 +91,13 @@ class AnimeCollection extends Collection { /** * Get item from collection for editing * - * @param int $id + * @param string $id * @return array */ - public function getCollectionEntry($id) + public function getCollectionEntry($id): array { $query = $this->db->from('anime_set') - ->where('hummingbird_id', (int)$id) + ->where('hummingbird_id', $id) ->get(); return $query->fetch(PDO::FETCH_ASSOC); @@ -110,7 +108,7 @@ class AnimeCollection extends Collection { * * @return array */ - private function getCollectionFromDatabase() + private function getCollectionFromDatabase(): array { if ( ! $this->validDatabase) { @@ -134,7 +132,7 @@ class AnimeCollection extends Collection { * @param array $data * @return void */ - public function add($data) + public function add($data): void { $anime = (object)$this->animeModel->getAnimeById($data['id']); $this->db->set([ @@ -160,7 +158,7 @@ class AnimeCollection extends Collection { * @param array $data * @return void */ - public function update($data) + public function update($data): void { // If there's no id to update, don't update if ( ! array_key_exists('hummingbird_id', $data)) @@ -182,7 +180,7 @@ class AnimeCollection extends Collection { * @param array $data * @return void */ - public function delete($data) + public function delete($data): void { // If there's no id to update, don't delete if ( ! array_key_exists('hummingbird_id', $data)) @@ -203,7 +201,7 @@ class AnimeCollection extends Collection { * @param int $kitsuId * @return array */ - public function get($kitsuId) + public function get($kitsuId): array { $query = $this->db->from('anime_set') ->where('hummingbird_id', $kitsuId) @@ -215,13 +213,14 @@ class AnimeCollection extends Collection { /** * Update genre information for selected anime * - * @param int $animeId The current anime + * @param string $animeId The current anime * @return void */ - private function updateGenre($animeId) + private function updateGenre($animeId): void { $genreInfo = $this->getGenreData(); - extract($genreInfo, \EXTR_SKIP); + $genres = $genreInfo['genres']; + $links = $genreInfo['links']; // Get api information $anime = $this->animeModel->getAnimeById($animeId); @@ -229,7 +228,7 @@ class AnimeCollection extends Collection { foreach ($anime['genres'] as $genre) { // Add genres that don't currently exist - if ( ! in_array($genre, $genres)) + if ( ! \in_array($genre, $genres, TRUE)) { $this->db->set('genre', $genre) ->insert('genres'); @@ -248,7 +247,7 @@ class AnimeCollection extends Collection { if (array_key_exists($animeId, $links)) { - if ( ! in_array($flippedGenres[$genre], $links[$animeId])) + if ( ! \in_array($flippedGenres[$genre], $links[$animeId], TRUE)) { $this->db->set($insertArray)->insert('genre_anime_set_link'); } @@ -265,7 +264,7 @@ class AnimeCollection extends Collection { * * @return array */ - private function getGenreData() + private function getGenreData(): array { $genres = []; $links = []; diff --git a/src/Model/Collection.php b/src/Model/Collection.php index 9d859f80..117227bf 100644 --- a/src/Model/Collection.php +++ b/src/Model/Collection.php @@ -16,8 +16,7 @@ namespace Aviat\AnimeClient\Model; -use Aviat\AnimeClient\Model\DB; -use Aviat\Ion\Di\{ContainerAware, ContainerInterface}; +use Aviat\Ion\Di\ContainerInterface; use PDO; use PDOException; @@ -26,8 +25,6 @@ use PDOException; */ class Collection extends DB { - use ContainerAware; - /** * Whether the database is valid for querying * @var boolean @@ -47,11 +44,7 @@ class Collection extends DB { { $this->db = \Query($this->dbConfig['collection']); } - catch (PDOException $e) - { - //$this->validDatabase = FALSE; - //return FALSE; - } + catch (PDOException $e) {} // Is database valid? If not, set a flag so the // app can be run without a valid database @@ -81,7 +74,7 @@ class Collection extends DB { * @param array $filter * @return array */ - public function getGenreList($filter = []) + public function getGenreList(array $filter = []): array { $this->db->select('hummingbird_id, genre') ->from('genre_anime_set_link gl') @@ -112,7 +105,7 @@ class Collection extends DB { if (array_key_exists($id, $output)) { - array_push($output[$id], $genre); + $output[$id][] = $genre; } else { diff --git a/src/Model/DB.php b/src/Model/DB.php index 79ec706e..03ee3ef4 100644 --- a/src/Model/DB.php +++ b/src/Model/DB.php @@ -17,16 +17,19 @@ namespace Aviat\AnimeClient\Model; use Aviat\Ion\Di\{ContainerAware, ContainerInterface}; +use Aviat\Ion\{ArrayWrapper, StringWrapper}; /** * Base model for database interaction */ -class DB extends AbstractModel { +class DB { + use ArrayWrapper; use ContainerAware; + use StringWrapper; /** * The query builder object - * @var \Query\Query_Builder_Interface $db + * @var \Query\Query_Builder_Interface */ protected $db; diff --git a/src/Model/MangaCollection.php b/src/Model/MangaCollection.php index 853c6410..a41b6618 100644 --- a/src/Model/MangaCollection.php +++ b/src/Model/MangaCollection.php @@ -16,9 +16,7 @@ namespace Aviat\AnimeClient\Model; -use Aviat\AnimeClient\API\Kitsu; use Aviat\Ion\Di\ContainerInterface; -use Aviat\Ion\Json; use PDO; /** @@ -48,7 +46,7 @@ class MangaCollection extends Collection { * * @return array */ - public function getCollection() + public function getCollection(): array { $rawCollection = $this->getCollectionFromDatabase(); @@ -74,7 +72,7 @@ class MangaCollection extends Collection { * * @return array */ - public function getMediaTypeList() + public function getMediaTypeList(): array { $output = []; @@ -96,10 +94,10 @@ class MangaCollection extends Collection { * @param int $id * @return array */ - public function getCollectionEntry($id) + public function getCollectionEntry($id): array { $query = $this->db->from('anime_set') - ->where('hummingbird_id', (int)$id) + ->where('hummingbird_id', $id) ->get(); return $query->fetch(PDO::FETCH_ASSOC); @@ -110,7 +108,7 @@ class MangaCollection extends Collection { * * @return array */ - private function getCollectionFromDatabase() + private function getCollectionFromDatabase(): array { if ( ! $this->validDatabase) { @@ -134,7 +132,7 @@ class MangaCollection extends Collection { * @param array $data * @return void */ - public function add($data) + public function add($data): void { $anime = (object)$this->mangaModel->getMangaById($data['id']); $this->db->set([ @@ -160,7 +158,7 @@ class MangaCollection extends Collection { * @param array $data * @return void */ - public function update($data) + public function update($data): void { // If there's no id to update, don't update if ( ! array_key_exists('hummingbird_id', $data)) @@ -182,7 +180,7 @@ class MangaCollection extends Collection { * @param array $data * @return void */ - public function delete($data) + public function delete($data): void { // If there's no id to update, don't delete if ( ! array_key_exists('hummingbird_id', $data)) @@ -200,10 +198,10 @@ class MangaCollection extends Collection { /** * Get the details of a collection item * - * @param int $kitsuId + * @param string $kitsuId * @return array */ - public function get($kitsuId) + public function get($kitsuId): array { $query = $this->db->from('manga_set') ->where('hummingbird_id', $kitsuId) @@ -215,13 +213,14 @@ class MangaCollection extends Collection { /** * Update genre information for selected manga * - * @param int $mangaId The current manga + * @param string $mangaId The current manga * @return void */ - private function updateGenre($mangaId) + private function updateGenre($mangaId): void { $genreInfo = $this->getGenreData(); - extract($genreInfo, EXTR_SKIP); + $genres = $genreInfo['genres']; + $links = $genreInfo['links']; // Get api information $manga = $this->mangaModel->getMangaById($mangaId); @@ -229,7 +228,7 @@ class MangaCollection extends Collection { foreach ($manga['genres'] as $genre) { // Add genres that don't currently exist - if ( ! in_array($genre, $genres)) + if ( ! \in_array($genre, $genres, TRUE)) { $this->db->set('genre', $genre) ->insert('genres'); @@ -248,7 +247,7 @@ class MangaCollection extends Collection { if (array_key_exists($mangaId, $links)) { - if ( ! in_array($flippedGenres[$genre], $links[$mangaId])) + if ( ! \in_array($flippedGenres[$genre], $links[$mangaId], TRUE)) { $this->db->set($insertArray)->insert('genre_manga_set_link'); } @@ -265,7 +264,7 @@ class MangaCollection extends Collection { * * @return array */ - private function getGenreData() + private function getGenreData(): array { $genres = []; $links = []; diff --git a/src/Model/AbstractModel.php b/src/constants.php similarity index 56% rename from src/Model/AbstractModel.php rename to src/constants.php index 8df1220e..3a606455 100644 --- a/src/Model/AbstractModel.php +++ b/src/constants.php @@ -14,13 +14,13 @@ * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient */ -namespace Aviat\AnimeClient\Model; +namespace Aviat\AnimeClient; -use Aviat\Ion\StringWrapper; - -/** - * Base class for Models - */ -abstract class AbstractModel { - use StringWrapper; -} \ No newline at end of file +const DEFAULT_CONTROLLER = Controller\Index::class; +const DEFAULT_CONTROLLER_METHOD = 'index'; +const DEFAULT_CONTROLLER_NAMESPACE = Controller::class; +const DEFAULT_LIST_CONTROLLER = Controller\Anime::class; +const ERROR_MESSAGE_METHOD = 'errorPage'; +const NOT_FOUND_METHOD = 'notFound'; +const SESSION_SEGMENT = 'Aviat\AnimeClient\Auth'; +const SRC_DIR = __DIR__; \ No newline at end of file diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 12a0f5fb..34fefa73 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -59,30 +59,30 @@ class ControllerTest extends AnimeClientTestCase { $this->container->setInstance('config', $config); $this->assertInstanceOf( - 'Aviat\AnimeClient\Controller', + Controller::class, new AnimeController($this->container) ); $this->assertInstanceOf( - 'Aviat\AnimeClient\Controller', + Controller::class, new MangaController($this->container) ); $this->assertInstanceOf( - 'Aviat\AnimeClient\Controller', + Controller::class, new CharacterController($this->container) ); $this->assertInstanceOf( - 'Aviat\AnimeClient\Controller', + Controller::class, new AnimeCollectionController($this->container) ); $this->assertInstanceOf( - 'Aviat\AnimeClient\Controller', + Controller::class, new MangaCollectionController($this->container) ); } public function testBaseControllerSanity() { - $this->assertTrue(is_object($this->BaseController)); + $this->assertTrue(\is_object($this->BaseController)); } public function testFormatTitle() diff --git a/tests/mocks.php b/tests/mocks.php index eb37c55b..2584a1dd 100644 --- a/tests/mocks.php +++ b/tests/mocks.php @@ -72,7 +72,7 @@ class TestTransformer extends AbstractTransformer { } trait MockViewOutputTrait { - protected function output() { + protected function output(): void { $reflect = new ReflectionClass($this); $properties = $reflect->getProperties(); $props = []; @@ -102,8 +102,8 @@ class MockUtil { } class TestView extends View { - public function send() {} - protected function output() + public function send(): void {} + protected function output(): void { /*$content =& $this->response->content; $content->set($this->output);