Skip to content

Commit

Permalink
added minor code quality fixes
Browse files Browse the repository at this point in the history
- explicit throw if no URL can be found for reviews
- usage of array_filter for the "each" method call on dom search
  • Loading branch information
pushrbx committed Jun 5, 2022
1 parent b54c96d commit d916326
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Model/Reviews/Reviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function fromParser(ReviewerParser $parser): self
{
$instance = new self();

$instance->url= $parser->getUrl();
$instance->url = $parser->getUrl();
$instance->images = UserImageResource::factory($parser->getImageUrl());
$instance->username = $parser->getUsername();

Expand Down
15 changes: 9 additions & 6 deletions src/Parser/Reviews/MangaReviewParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Jikan\Parser\Reviews;

use Jikan\Exception\ParserException;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model\Common\AnimeMeta;
use Jikan\Model\Common\MangaMeta;
use Jikan\Model\Manga\MangaReview;
use Jikan\Model\Manga\MangaReviewer;
use Jikan\Model\Manga\MangaReviewScores;
use Jikan\Model\Reviews\Reviewer;
use Jikan\Parser\Manga\MangaReviewScoresParser;
Expand All @@ -22,9 +21,9 @@
class MangaReviewParser implements ParserInterface
{
/**
* @var Crawler
* @var \Symfony\Component\DomCrawler\Crawler
*/
private $crawler;
private Crawler $crawler;

/**
* MangaReviewParser constructor.
Expand All @@ -37,7 +36,7 @@ public function __construct(Crawler $crawler)
}

/**
* @return MangaReview
* @return \Jikan\Model\Manga\MangaReview
* @throws \Exception
* @throws \RuntimeException
*/
Expand All @@ -46,6 +45,9 @@ public function getModel(): MangaReview
return MangaReview::fromParser($this);
}

/**
* @throws \Jikan\Exception\ParserException
*/
public function getManga() : MangaMeta
{
return new MangaMeta(
Expand Down Expand Up @@ -192,6 +194,7 @@ public function getReviewedTitle(): string
/**
* @return string
* @throws \InvalidArgumentException
* @throws \Jikan\Exception\ParserException
*/
public function getReviewedImageUrl(): string
{
Expand All @@ -211,7 +214,7 @@ public function getReviewedImageUrl(): string
return Parser::parseImageQuality($node->attr('data-src'));
}

return "";
throw new ParserException("Couldn't find the URL of reviewed image.");
}

/**
Expand Down
38 changes: 20 additions & 18 deletions src/Parser/Reviews/RecentReviewsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,32 @@ public function getModel(): RecentReviews
/**
* @return array
* @throws \RuntimeException
* @throws \InvalidArgumentException
* @throws \InvalidArgumentException|\Exception
*/
public function getRecentReviews(): array
{
return $this->crawler
->filterXPath('//*[@id="content"]/div[@class="borderDark"]')
->each(
function (Crawler $crawler) {
// If requested by $type `Constants::TOP_REVIEWS_BEST_VOTED`; both types can be returned
// So we check types here to allow the ability to parse and return both
return array_filter(
$this->crawler
->filterXPath('//*[@id="content"]/div[@class="borderDark"]')
->each(
function (Crawler $crawler) {
// If requested by $type `Constants::TOP_REVIEWS_BEST_VOTED`; both types can be returned
// So we check types here to allow the ability to parse and return both

// Anime Review
if ($crawler->filterXPath('//div[1]/div[1]/div[2]/small')->text() === '(Anime)') {
return RecentAnimeReview::fromParser(new AnimeReviewParser($crawler));
}
// Anime Review
if ($crawler->filterXPath('//div[1]/div[1]/div[2]/small')->text() === '(Anime)') {
return RecentAnimeReview::fromParser(new AnimeReviewParser($crawler));
}

// Manga Review
if ($crawler->filterXPath('//div[1]/div[1]/div[2]/small')->text() === '(Manga)') {
return RecentMangaReview::fromParser(new MangaReviewParser($crawler));
}
// Manga Review
if ($crawler->filterXPath('//div[1]/div[1]/div[2]/small')->text() === '(Manga)') {
return RecentMangaReview::fromParser(new MangaReviewParser($crawler));
}

return null;
}
);
return null;
}
)
);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Parser/Reviews/ReviewerParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Jikan\Parser\Reviews;

use Jikan\Helper\JString;
use Jikan\Exception\ParserException;
use Jikan\Helper\Parser;
use Jikan\Model\Anime\AnimeReviewScores;
use Jikan\Model\Manga\MangaReviewScores;
Expand Down Expand Up @@ -41,6 +41,7 @@ public function getModel()
/**
* @return string
* @throws \InvalidArgumentException
* @throws \Jikan\Exception\ParserException
*/
public function getUrl(): string
{
Expand All @@ -56,7 +57,7 @@ public function getUrl(): string
return $node->attr('href');
}

return "";
throw new ParserException("Couldn't find any URL on review pages.");
}

/**
Expand Down

0 comments on commit d916326

Please sign in to comment.