Commit fa2d79c7 by Timothy J Warren

Miscellaneous style updates

parent 70832f8b
Pipeline #414 passed with stage
in 5 minutes 7 seconds
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2018 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API;
abstract class AbstractListItem implements ListItemInterface {
}
\ No newline at end of file
......@@ -21,15 +21,17 @@ use const Aviat\AnimeClient\SESSION_SEGMENT;
use function Amp\Promise\wait;
use Amp\Artax\Request;
use Aviat\AnimeClient\API\{AbstractListItem, HummingbirdClient};
use Aviat\AnimeClient\API\{
HummingbirdClient,
ListItemInterface
};
use Aviat\Ion\Di\ContainerAware;
use Aviat\Ion\Json;
use RuntimeException;
/**
* CRUD operations for Kitsu list items
*/
class ListItem extends AbstractListItem {
class ListItem implements ListItemInterface {
use ContainerAware;
use KitsuTrait;
......@@ -41,7 +43,7 @@ class ListItem extends AbstractListItem {
->get('session')
->getSegment(SESSION_SEGMENT);
if ( ! is_null($sessionSegment->get('auth_token')))
if ($sessionSegment->get('auth_token') !== NULL)
{
$token = $sessionSegment->get('auth_token');
return "bearer {$token}";
......
......@@ -169,7 +169,7 @@ class Model {
*/
public function getUserIdByUsername(string $username = NULL): string
{
if (is_null($username))
if ($username === NULL)
{
$username = $this->getUsername();
}
......
......@@ -18,7 +18,6 @@ namespace Aviat\AnimeClient\API\MAL;
use Amp\Artax\{FormBody, Request};
use Aviat\AnimeClient\API\{
AbstractListItem,
XML
};
use Aviat\Ion\Di\ContainerAware;
......
......@@ -18,8 +18,7 @@ namespace Aviat\AnimeClient\API\MAL;
use Aviat\AnimeClient\API\{
APIRequestBuilder,
MAL as M,
XML
MAL as M
};
class MALRequestBuilder extends APIRequestBuilder {
......
......@@ -178,7 +178,7 @@ trait MALTrait {
$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)
{
......
......@@ -40,7 +40,7 @@ class ParallelAPIRequest {
*/
public function addRequest($request, $key = NULL): self
{
if ( ! is_null($key))
if ($key !== NULL)
{
$this->requests[$key] = $request;
return $this;
......
......@@ -174,14 +174,12 @@ class XML {
*/
private static function stripXMLWhitespace(string $xml): string
{
// Get rid of unimportant text nodes by removing
// whitespace characters from between xml tags,
// except for the xml declaration tag, Which looks
// something like:
/* <?xml version="1.0" encoding="UTF-8"?> */
return preg_replace('/([^\?])>\s+</', '$1><', $xml);
return preg_replace('/([^?])>\s+</', '$1><', $xml);
}
/**
......@@ -191,7 +189,7 @@ class XML {
* @param DOMNodeList $nodeList The current NodeList object
* @return void
*/
private static function childNodesToArray(array &$root, DOMNodelist $nodeList)
private static function childNodesToArray(array &$root, DOMNodelist $nodeList): void
{
$length = $nodeList->length;
for ($i = 0; $i < $length; $i++)
......@@ -200,14 +198,14 @@ class XML {
$current =& $root[$el->nodeName];
// It's a top level element!
if (is_a($el->childNodes->item(0), 'DomText') OR ( ! $el->hasChildNodes()))
if (( ! $el->hasChildNodes()) || is_a($el->childNodes->item(0), 'DomText'))
{
$current = $el->textContent;
continue;
}
// An empty value at the current root
if (is_null($current))
if ($current === NULL)
{
$current = [];
static::childNodesToArray($current, $el->childNodes);
......@@ -230,7 +228,7 @@ class XML {
$current = [$current];
}
array_push($current, []);
$current[] = [];
$index = count($current) - 1;
static::childNodesToArray($current[$index], $el->childNodes);
......@@ -245,7 +243,7 @@ class XML {
* @param array $data The data for the current node
* @return void
*/
private static function arrayPropertiesToXmlNodes(DOMDocument &$dom, DOMNode &$parent, array $data)
private static function arrayPropertiesToXmlNodes(DOMDocument $dom, DOMNode $parent, array $data): void
{
foreach($data as $key => $props)
{
......@@ -260,7 +258,7 @@ class XML {
$node = $dom->createElement($key);
if (is_array($props))
if (\is_array($props))
{
static::arrayPropertiesToXmlNodes($dom, $node, $props);
}
......
......@@ -22,10 +22,6 @@ use Aviat\Ion\Di\{
ContainerAware,
ContainerInterface
};
use Aviat\Ion\Di\Exception\{
ContainerException,
NotFoundException
};
use Aviat\Ion\Exception\DoubleRenderException;
use Aviat\Ion\View\{HtmlView, HttpView, JsonView};
use InvalidArgumentException;
......@@ -92,9 +88,9 @@ class Controller {
/**
* Constructor
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @param ContainerInterface $container
* @throws ContainerException
* @throws NotFoundException
*/
public function __construct(ContainerInterface $container)
{
......@@ -135,7 +131,7 @@ class Controller {
*
* @return void
*/
public function redirectToPrevious()
public function redirectToPrevious(): void
{
$previous = $this->session->getFlash('previous');
$this->redirect($previous, 303);
......@@ -144,9 +140,9 @@ class Controller {
/**
* Set the current url in the session as the target of a future redirect
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @param string|null $url
* @throws ContainerException
* @throws NotFoundException
* @return void
*/
public function setSessionRedirect(string $url = NULL): void
......@@ -312,6 +308,7 @@ class Controller {
/**
* Redirect to the default controller/url from an empty path
*
* @throws InvalidArgumentException
* @return void
*/
public function redirectToDefaultRoute(): void
......
......@@ -41,6 +41,8 @@ class Anime extends BaseController {
* Constructor
*
* @param ContainerInterface $container
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
*/
public function __construct(ContainerInterface $container)
{
......@@ -63,11 +65,14 @@ class Anime extends BaseController {
*
* @param string|int $type - The section of the list
* @param string $view - List or cover view
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function index($type = KitsuWatchingStatus::WATCHING, string $view = NULL)
public function index($type = KitsuWatchingStatus::WATCHING, string $view = NULL): void
{
$title = (array_key_exists($type, AnimeWatchingStatus::ROUTE_TO_TITLE))
$title = array_key_exists($type, AnimeWatchingStatus::ROUTE_TO_TITLE)
? $this->formatTitle(
$this->config->get('whose_list') . "'s Anime List",
AnimeWatchingStatus::ROUTE_TO_TITLE[$type]
......@@ -92,9 +97,13 @@ class Anime extends BaseController {
/**
* Form to add an anime
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \Aura\Router\Exception\RouteNotFound
* @throws \InvalidArgumentException
* @return void
*/
public function addForm()
public function addForm(): void
{
$this->setSessionRedirect();
$this->outputHTML('anime/add', [
......@@ -110,9 +119,11 @@ class Anime extends BaseController {
/**
* Add an anime to the list
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function add()
public function add(): void
{
$data = $this->request->getParsedBody();
if ( ! array_key_exists('id', $data))
......@@ -140,9 +151,12 @@ class Anime extends BaseController {
*
* @param int $id
* @param string $status
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function edit($id, $status = "all")
public function edit($id, $status = 'all'): void
{
$item = $this->model->getLibraryItem($id);
$this->setSessionRedirect();
......@@ -165,7 +179,7 @@ class Anime extends BaseController {
*
* @return void
*/
public function search()
public function search(): void
{
$queryParams = $this->request->getQueryParams();
$query = $queryParams['query'];
......@@ -175,9 +189,11 @@ class Anime extends BaseController {
/**
* Update an anime item via a form submission
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function formUpdate()
public function formUpdate(): void
{
$data = $this->request->getParsedBody();
......@@ -205,7 +221,7 @@ class Anime extends BaseController {
*
* @return void
*/
public function update()
public function update(): void
{
if (stripos($this->request->getHeader('content-type')[0], 'application/json') !== FALSE)
{
......@@ -225,9 +241,11 @@ class Anime extends BaseController {
/**
* Remove an anime from the list
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function delete()
public function delete(): void
{
$body = $this->request->getParsedBody();
$response = $this->model->deleteLibraryItem($body['id'], $body['mal_id']);
......@@ -249,9 +267,12 @@ class Anime extends BaseController {
* View details of an anime
*
* @param string $animeId
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function details(string $animeId)
public function details(string $animeId): void
{
$show_data = $this->model->getAnime($animeId);
$characters = [];
......@@ -287,5 +308,14 @@ class Anime extends BaseController {
]);
}
/**
* Find anime matching the selected genre
*
* @param string $genre
*/
public function genre(string $genre): void
{
// @TODO: implement
}
}
// End of AnimeController.php
\ No newline at end of file
......@@ -44,6 +44,8 @@ class AnimeCollection extends BaseController {
* Constructor
*
* @param ContainerInterface $container
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
*/
public function __construct(ContainerInterface $container)
{
......@@ -63,6 +65,7 @@ class AnimeCollection extends BaseController {
/**
* Search for anime
*
* @throws \Aviat\Ion\Exception\DoubleRenderException
* @return void
*/
public function search()
......@@ -76,6 +79,9 @@ class AnimeCollection extends BaseController {
* Show the anime collection page
*
* @param string $view
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function index($view)
......@@ -98,13 +104,17 @@ class AnimeCollection extends BaseController {
* Show the anime collection add/edit form
*
* @param integer|null $id
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \Aura\Router\Exception\RouteNotFound
* @throws \InvalidArgumentException
* @return void
*/
public function form($id = NULL)
{
$this->setSessionRedirect();
$action = (is_null($id)) ? "Add" : "Edit";
$action = $id === NULL ? 'Add' : 'Edit';
$urlAction = strtolower($action);
$this->outputHTML('collection/' . $urlAction, [
......@@ -115,13 +125,16 @@ class AnimeCollection extends BaseController {
$action
),
'media_items' => $this->animeCollectionModel->getMediaTypeList(),
'item' => ($action === "Edit") ? $this->animeCollectionModel->get($id) : []
'item' => ($action === 'Edit') ? $this->animeCollectionModel->get($id) : []
]);
}
/**
* Update a collection item
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function edit()
......@@ -143,6 +156,9 @@ class AnimeCollection extends BaseController {
/**
* Add a collection item
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function add()
......@@ -171,13 +187,13 @@ class AnimeCollection extends BaseController {
$data = $this->request->getParsedBody();
if ( ! array_key_exists('hummingbird_id', $data))
{
$this->redirect("/anime-collection/view", 303);
$this->redirect('/anime-collection/view', 303);
}
$this->animeCollectionModel->delete($data);
$this->setFlashMessage("Successfully removed anime from collection.", 'success');
$this->setFlashMessage('Successfully removed anime from collection.', 'success');
$this->redirect("/anime-collection/view", 303);
$this->redirect('/anime-collection/view', 303);
}
}
// End of CollectionController.php
\ No newline at end of file
......@@ -27,7 +27,16 @@ class Character extends BaseController {
use ArrayWrapper;
public function index(string $slug)
/**
* Show information about a character
*
* @param string $slug
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function index(string $slug): void
{
$model = $this->container->get('kitsu-model');
......@@ -88,8 +97,9 @@ class Character extends BaseController {
$person = current($role['relationships']['person']['people'])['attributes'];
$hasName = array_key_exists($person['name'], $people);
if ( ! array_key_exists($person['name'], $people))
if ( ! $hasName)
{
$people[$person['name']] = $i;
$role['relationships']['media']['anime'] = [current($role['relationships']['media']['anime'])];
......@@ -99,15 +109,13 @@ class Character extends BaseController {
continue;
}
else if(array_key_exists($person['name'], $people))
if (array_key_exists('anime', $role['relationships']['media']))
{
if (array_key_exists('anime', $role['relationships']['media']))
{
$key = $people[$person['name']];
$output[$key]['relationships']['media']['anime'][] = current($role['relationships']['media']['anime']);
}
continue;
$key = $people[$person['name']];
$output[$key]['relationships']['media']['anime'][] = current($role['relationships']['media']['anime']);
}
continue;
}
return $output;
......@@ -122,7 +130,7 @@ class Character extends BaseController {
if (
array_key_exists('attributes', $role) &&
array_key_exists('role', $role['attributes']) &&
( ! is_null($role['attributes']['role']))
$role['attributes']['role'] !== NULL
) {
$count++;
}
......
......@@ -30,6 +30,9 @@ class Index extends BaseController {
/**
* Purges the API cache
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function clearCache()
......@@ -37,13 +40,16 @@ class Index extends BaseController {
$this->cache->clear();
$this->outputHTML('blank', [
'title' => 'Cache cleared'
], NULL, 200);
]);
}
/**
* Show the login form
*
* @param string $status
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function login(string $status = '')
......@@ -69,6 +75,10 @@ class Index extends BaseController {
/**
* Attempt login authentication
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \Aura\Router\Exception\RouteNotFound
* @throws \InvalidArgumentException
* @return void
*/
public function loginAction()
......@@ -88,6 +98,9 @@ class Index extends BaseController {
/**
* Deauthorize the current user
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function logout()
......@@ -101,6 +114,9 @@ class Index extends BaseController {
/**
* Show the user profile page
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function me()
......@@ -111,8 +127,8 @@ class Index extends BaseController {
$orgData = JsonAPI::organizeData($data)[0];
$rels = $orgData['relationships'] ?? [];
$favorites = array_key_exists('favorites', $rels) ? $rels['favorites'] : [];
$this->outputHTML('me', [
'title' => 'About ' . $this->config->get('whose_list'),
'data' => $orgData,
......@@ -125,9 +141,17 @@ class Index extends BaseController {
/**
* Get image covers from kitsu
*
* @param string $type The category of image
* @param string $file The filename to look for
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @throws \TypeError
* @throws \Error
* @throws \Throwable
* @return void
*/
public function images($type, $file)
public function images(string $type, string $file): void
{
$kitsuUrl = 'https://media.kitsu.io/';
list($id, $ext) = explode('.', basename($file));
......@@ -164,9 +188,14 @@ class Index extends BaseController {
echo $data;
}
/**
* Reorganize favorites data to be more useful
*
* @param array $rawfavorites
* @return array
*/
private function organizeFavorites(array $rawfavorites): array
{
// return $rawfavorites;
$output = [];
unset($rawfavorites['data']);
......
......@@ -40,6 +40,8 @@ class Manga extends Controller {
* Constructor
*
* @param ContainerInterface $container
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
*/
public function __construct(ContainerInterface $container)
{
......@@ -59,9 +61,12 @@ class Manga extends Controller {
*
* @param string $status
* @param string $view
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function index($status = "all", $view = "")
public function index($status = 'all', $view = ''): void
{
$statusTitle = MangaReadingStatus::ROUTE_TO_TITLE[$status];
......@@ -88,9 +93,13 @@ class Manga extends Controller {
/**
* Form to add an manga
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \Aura\Router\Exception\RouteNotFound
* @throws \InvalidArgumentException
* @return void
*/
public function addForm()
public function addForm(): void
{
$statuses = MangaReadingStatus::KITSU_TO_TITLE;
......@@ -108,14 +117,16 @@ class Manga extends Controller {
/**
* Add an manga to the list
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function add()
public function add(): void
{
$data = $this->request->getParsedBody();
if ( ! array_key_exists('id', $data))
{
$this->redirect("manga/add", 303);
$this->redirect('manga/add', 303);
}
$result = $this->model->createLibraryItem($data);
......@@ -138,9 +149,13 @@ class Manga extends Controller {
*
* @param string $id
* @param string $status
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \Aura\Router\Exception\RouteNotFound
* @throws \InvalidArgumentException
* @return void
*/
public function edit($id, $status = "All")
public function edit($id, $status = 'All'): void
{
$this->setSessionRedirect();
$item = $this->model->getLibraryItem($id);
......@@ -164,7 +179,7 @@ class Manga extends Controller {
*
* @return void
*/
public function search()
public function search(): void
{
$query_data = $this->request->getQueryParams();
$this->outputJSON($this->model->search($query_data['query']));
......@@ -173,9 +188,11 @@ class Manga extends Controller {
/**
* Update an manga item via a form submission
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function formUpdate()
public function formUpdate(): void
{
$data = $this->request->getParsedBody();
......@@ -202,9 +219,11 @@ class Manga extends Controller {
/**
* Update a manga item
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function update()
public function update(): void
{
if (stripos($this->request->getHeader('content-type')[0], 'application/json') !== FALSE)
{
......@@ -224,9 +243,11 @@ class Manga extends Controller {
/**
* Remove an manga from the list
*
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @return void
*/
public function delete()
public function delete(): void
{
$body = $this->request->getParsedBody();
$id = $body['id'];
......@@ -250,9 +271,12 @@ class Manga extends Controller {
* View details of an manga
*
* @param string $manga_id
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
* @throws \InvalidArgumentException
* @return void
*/
public function details($manga_id)
public function details($manga_id): void
{
$data = $this->model->getManga($manga_id);
$characters = [];
......@@ -286,5 +310,15 @@ class Manga extends Controller {
'data' => $data,
]);
}
/**
* Find manga matching the selected genre
*