From 625edf5d0c430ce438fd97cf8e45748c2b7cb1ed Mon Sep 17 00:00:00 2001
From: "Timothy J. Warren" <tim@timshomepage.net>
Date: Sat, 10 Aug 2019 10:09:07 -0400
Subject: [PATCH] Improve 404 checks for detail pages

---
 src/Controller/Anime.php   | 42 ++++++++++++++++++++++++--------------
 src/Controller/Manga.php   |  2 +-
 src/Model/Anime.php        | 12 ++++-------
 src/Model/Manga.php        |  8 ++++----
 src/Types/AbstractType.php | 30 ++++++++++++++++++++++++++-
 5 files changed, 65 insertions(+), 29 deletions(-)

diff --git a/src/Controller/Anime.php b/src/Controller/Anime.php
index 0f63dc6d..3bcb39cc 100644
--- a/src/Controller/Anime.php
+++ b/src/Controller/Anime.php
@@ -299,28 +299,40 @@ final class Anime extends BaseController {
 	 */
 	public function details(string $animeId): void
 	{
-		$data = $this->model->getAnime($animeId);
+		try
+		{
+			$data = $this->model->getAnime($animeId);
 
-		if (empty($data))
+			if ($data->isEmpty())
+			{
+				$this->notFound(
+					$this->config->get('whose_list') .
+					"'s Anime List &middot; Anime &middot; " .
+					'Anime not found',
+					'Anime Not Found'
+				);
+
+				return;
+			}
+
+			$this->outputHTML('anime/details', [
+				'title' => $this->formatTitle(
+					$this->config->get('whose_list') . "'s Anime List",
+					'Anime',
+					$data->title
+				),
+				'data' => $data,
+			]);
+		}
+		catch (\TypeError $e)
 		{
 			$this->notFound(
 				$this->config->get('whose_list') .
-					"'s Anime List &middot; Anime &middot; " .
-					'Anime not found',
+				"'s Anime List &middot; Anime &middot; " .
+				'Anime not found',
 				'Anime Not Found'
 			);
-
-			return;
 		}
-
-		$this->outputHTML('anime/details', [
-			'title' => $this->formatTitle(
-				$this->config->get('whose_list') . "'s Anime List",
-				'Anime',
-				$data->title
-			),
-			'data' => $data,
-		]);
 	}
 
 	/**
diff --git a/src/Controller/Manga.php b/src/Controller/Manga.php
index 771aeecc..b8add390 100644
--- a/src/Controller/Manga.php
+++ b/src/Controller/Manga.php
@@ -304,7 +304,7 @@ final class Manga extends Controller {
 		$staff = [];
 		$characters = [];
 
-		if (empty($data))
+		if ($data->isEmpty())
 		{
 			$this->notFound(
 				$this->config->get('whose_list') .
diff --git a/src/Model/Anime.php b/src/Model/Anime.php
index c33d3766..4de786c0 100644
--- a/src/Model/Anime.php
+++ b/src/Model/Anime.php
@@ -166,7 +166,7 @@ class Anime extends API {
 		$requester = new ParallelAPIRequest();
 		$requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu');
 
-		if ($data['mal_id'] !== null && $this->anilistEnabled)
+		if ($this->anilistEnabled && $data['mal_id'] !== null)
 		{
 			$requester->addRequest($this->anilistModel->createListItem($data, 'ANIME'), 'anilist');
 		}
@@ -187,9 +187,7 @@ class Anime extends API {
 		$requester = new ParallelAPIRequest();
 		$requester->addRequest($this->kitsuModel->incrementListItem($data), 'kitsu');
 
-		$array = $data->toArray();
-
-		if ($array['mal_id'] !== null && $this->anilistEnabled)
+		if ($this->anilistEnabled && $data['mal_id'] !== null)
 		{
 			$requester->addRequest($this->anilistModel->incrementListItem($data, 'ANIME'), 'anilist');
 		}
@@ -216,9 +214,7 @@ class Anime extends API {
 		$requester = new ParallelAPIRequest();
 		$requester->addRequest($this->kitsuModel->updateListItem($data), 'kitsu');
 
-		$array = $data->toArray();
-
-		if ($array['mal_id'] !== null && $this->anilistEnabled)
+		if ($this->anilistEnabled && $data['mal_id'] !== null)
 		{
 			$requester->addRequest($this->anilistModel->updateListItem($data, 'ANIME'), 'anilist');
 		}
@@ -246,7 +242,7 @@ class Anime extends API {
 		$requester = new ParallelAPIRequest();
 		$requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu');
 
-		if ($malId !== null && $this->anilistEnabled)
+		if ($this->anilistEnabled && $malId !== null)
 		{
 			$requester->addRequest($this->anilistModel->deleteListItem($malId, 'ANIME'), 'anilist');
 		}
diff --git a/src/Model/Manga.php b/src/Model/Manga.php
index d4b4b07e..2acad927 100644
--- a/src/Model/Manga.php
+++ b/src/Model/Manga.php
@@ -136,7 +136,7 @@ class Manga extends API {
 		$requester = new ParallelAPIRequest();
 		$requester->addRequest($this->kitsuModel->createListItem($data), 'kitsu');
 
-		if (array_key_exists('mal_id', $data) && $this->anilistEnabled)
+		if ($this->anilistEnabled && array_key_exists('mal_id', $data))
 		{
 			$requester->addRequest($this->anilistModel->createListItem($data, 'MANGA'), 'anilist');
 		}
@@ -159,7 +159,7 @@ class Manga extends API {
 
 		$array = $data->toArray();
 
-		if (array_key_exists('mal_id', $array) && $this->anilistEnabled)
+		if ($this->anilistEnabled && array_key_exists('mal_id', $array))
 		{
 			$requester->addRequest($this->anilistModel->updateListItem($data, 'MANGA'), 'anilist');
 		}
@@ -187,7 +187,7 @@ class Manga extends API {
 
 		$array = $data->toArray();
 
-		if (array_key_exists('mal_id', $array) && $this->anilistEnabled)
+		if ($this->anilistEnabled && array_key_exists('mal_id', $array))
 		{
 			$requester->addRequest($this->anilistModel->incrementListItem($data, 'MANGA'), 'anilist');
 		}
@@ -214,7 +214,7 @@ class Manga extends API {
 		$requester = new ParallelAPIRequest();
 		$requester->addRequest($this->kitsuModel->deleteListItem($id), 'kitsu');
 
-		if ($malId !== null && $this->anilistEnabled)
+		if ($this->anilistEnabled && $malId !== null)
 		{
 			$requester->addRequest($this->anilistModel->deleteListItem($malId, 'MANGA'), 'anilist');
 		}
diff --git a/src/Types/AbstractType.php b/src/Types/AbstractType.php
index ec00bca8..5f9740b5 100644
--- a/src/Types/AbstractType.php
+++ b/src/Types/AbstractType.php
@@ -108,6 +108,16 @@ abstract class AbstractType implements ArrayAccess {
 		throw new UndefinedPropertyException("Trying to get undefined property: '$name'");
 	}
 
+	/**
+	 * Create a string representation of the object for debugging
+	 *
+	 * @return string
+	 */
+	public function __toString(): string
+	{
+		return print_r($this, TRUE);
+	}
+
 	/**
 	 * Implementing ArrayAccess
 	 *
@@ -180,4 +190,22 @@ abstract class AbstractType implements ArrayAccess {
 
 		return $output;
 	}
-}
\ No newline at end of file
+
+	/**
+	 * Determine whether the type has any properties set
+	 *
+	 * @return bool
+	 */
+	public function isEmpty(): bool
+	{
+		foreach ($this as $key => $value)
+		{
+			if ( ! empty($value))
+			{
+				return FALSE;
+			}
+		}
+
+		return TRUE;
+	}
+}