8000 refactor: compare MAL display strings through named constants by irfan-dahir · Pull Request #606 · jikan-me/jikan · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions DECISIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ what you settled. Plans belong in a working-notes directory where they can be su

---

## <!-- DECIDE: your first section, if organising topically -->

### <!-- First entry, using the five-field format above -->
## Scraped display strings

### Compare MyAnimeList display strings through named constants, never raw literals
**Date:** 2026-10-11
**Source:** internal-decision (backend-quality-suite onboarding)
**Decision:** The display strings MyAnimeList writes on the page and jikan compares by value — `Unknown`, `N/A`, `Filler`, `Recap`, `Currently Airing`, `Publishing`, `Not available`, `None`, `Synonyms` — are referenced only through `Jikan\Helper\Constants::DISPLAY_*` constants. Every comparison *and* every emission of one of these sentinels uses the constant, so the two cannot diverge.
**Reason:** 20 findings from backend-suite `audit_api_consistency` (load-bearing-copy) measured the defect: the literal on the wire and the comparison sentinel were the same hand-typed string at 21 sites. These values are upstream data, not jikan's own copy — a consumer cannot re-voice them — so the failure mode is precisely the one the audit guards: an upstream rename (e.g. `Unknown` → `Not available`) silently drifts, and the parser keeps matching a string nobody emits. Constants make the rename a one-line change in one file. An enum was considered and rejected: the values are upstream strings, not jikan's vocabulary, so a typed enum would add a conversion at every call site for no consumer-visible gain.
**Impact:** Added the `DISPLAY_*` block to `src/Helper/Constants.php`; replaced 21 literals across 10 parser/model files (including the `AnimeCardParser` emission, so comparison and emission share the constant). `audit_api_consistency` load-bearing-copy is now clean (exit 0, 388 files). Behaviour unchanged — the constants carry the identical strings and the full PHPUnit suite still passes 394/394.

---

Expand Down
20 changes: 20 additions & 0 deletions src/Helper/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,24 @@ class Constants
public const REVIEWS_SORT_MOST_VOTED = 'mostvoted';
public const REVIEWS_SORT_OLDEST = 'oldest';
public const REVIEWS_SORT_NEWEST = 'newest';

/**
* The display strings MyAnimeList itself writes on the page, compared by
* value throughout the parsers (backend-suite audit_api_consistency,
* load-bearing-copy). They are upstream data, not jikan's own copy, but the
* comparison literal must not be hand-typed at each call site: when
* MyAnimeList renames one of these, the rename is one line here instead of a
* silent drift at a dozen sites. Where a sentinel is also the "unknown"
* meaning, the same constant is used for comparison and for emission so the
* two cannot diverge.
*/
public const DISPLAY_CURRENTLY_AIRING = 'Currently Airing';
public const DISPLAY_PUBLISHING = 'Publishing';
public const DISPLAY_NOT_AVAILABLE = 'Not available';
public const DISPLAY_UNKNOWN = 'Unknown';
public const DISPLAY_NONE = 'None';
public const DISPLAY_N_A = 'N/A';
public const DISPLAY_FILLER = 'Filler';
public const DISPLAY_RECAP = 'Recap';
public const DISPLAY_SYNONYMS = 'Synonyms';
}
3 changes: 2 additions & 1 deletion src/Model/Anime/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Model\Anime;

use Jikan\Helper\Constants;
use Jikan\Model\Common\DateRange;
use Jikan\Model\Common\MalUrl;
use Jikan\Model\Common\Url;
Expand Down Expand Up @@ -242,7 +243,7 @@ public static function fromParser(AnimeParser $parser): Anime
$instance->type = $parser->getType();
$instance->episodes = $parser->getEpisodes();
$instance->status = $parser->getStatus();
$instance->airing = $instance->status === 'Currently Airing';
$instance->airing = $instance->status === Constants::DISPLAY_CURRENTLY_AIRING;
$instance->aired = $parser->getAired();
$instance->premiered = $parser->getPremiered();
$instance->broadcast = $parser->getBroadcast();
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Common/DateRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Model\Common;

use Jikan\Helper\Constants;
use Jikan\Helper\Parser;

/**
Expand Down Expand Up @@ -40,7 +41,7 @@ public function __toString()
public function getFrom(): ?\DateTimeImmutable
{
$date = $this->date;
if ($date === 'Not available') {
if ($date === Constants::DISPLAY_NOT_AVAILABLE) {
return null;
}
if (strpos($date, ' to ') !== false || strpos($date, ' to ?') !== false) {
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Manga/Manga.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @package Jikan\Model
*/

use Jikan\Helper\Constants;
use Jikan\Model\Common\DateRange;
use Jikan\Model\Common 8000 \MalUrl;
use Jikan\Model\Common\Title;
Expand Down Expand Up @@ -205,7 +206,7 @@ public static function fromParser(MangaParser $parser): Manga
$instance->chapters = $parser->getMangaChapters();
$instance->volumes = $parser->getMangaVolumes();
$instance->status = $parser->getMangaStatus();
$instance->publishing = $instance->status === 'Publishing';
$instance->publishing = $instance->status === Constants::DISPLAY_PUBLISHING;
$instance->published = $parser->getPublished();
$instance->genres = $parser->getGenres();
$instance->explicitGenres = $parser->getExplicitGenres();
Expand Down
5 changes: 3 additions & 2 deletions src/Parser/Anime/AnimeEpisodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Anime;

use Jikan\Helper\Constants;
use Jikan\Exception\BadResponseException;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
8BD0 Expand Down Expand Up @@ -148,7 +149,7 @@ public function getFiller(): bool
return false;
}

return trim($filler->text()) === 'Filler';
return trim($filler->text()) === Constants::DISPLAY_FILLER;
}

/**
Expand All @@ -162,7 +163,7 @@ public function getRecap(): bool
return false;
}

return trim($recap->text()) === 'Recap';
return trim($recap->text()) === Constants::DISPLAY_RECAP;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Parser/Anime/AnimeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Anime;

use Jikan\Helper\Constants;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model\Anime\Anime;
Expand Down Expand Up @@ -204,7 +205,7 @@ public function getType(): ?string
}

$type = JString::cleanse(str_replace($type->text(), '', $type->ancestors()->text()));
return $type === 'Unknown' ? null : $type;
return $type === Constants::DISPLAY_UNKNOWN ? null : $type;
}

/**
Expand All @@ -225,7 +226,7 @@ public function getEpisodes(): ?int
(
trim(
str_replace($episodes->text(), '', $episodes->ancestors()->text())
) === 'Unknown'
) === Constants::DISPLAY_UNKNOWN
)
? null
: (int) str_replace(
Expand Down Expand Up @@ -541,7 +542,7 @@ public function getRating(): ?string
str_replace($rating->text(), '', $rating->ancestors()->text())
);

if ($rating === 'None') {
if ($rating === Constants::DISPLAY_NONE) {
return null;
}

Expand All @@ -562,7 +563,7 @@ public function getScore(): ?float

$score = JString::cleanse($score->text());

if ($score === 'N/A') {
if ($score === Constants::DISPLAY_N_A) {
return null;
}

Expand Down Expand Up @@ -613,7 +614,7 @@ public function getRank(): ?int
Parser::removeChildNodes($rank->ancestors())->text()
);

if ($ranked === 'N/A') {
if ($ranked === Constants::DISPLAY_N_A) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Anime/EpisodeListItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Anime;

use Jikan\Helper\Constants;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model\Anime\EpisodeListItem;
Expand Down Expand Up @@ -110,7 +111,7 @@ public function getAired(): ?\DateTimeImmutable
{
$aired = $this->crawler->filterXPath('//td[contains(@class, \'episode-aired\')]')->text();

if ($aired === 'N/A') {
if ($aired === Constants::DISPLAY_N_A) {
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Common/AlternativeTitleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Common;

use Jikan\Helper\Constants;
use Jikan\Helper\JString;
use Jikan\Model\Common\Title;
use Jikan\Parser\ParserInterface;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function getModel(): array
{
[$type, $title] = explode(':', $this->crawler->text(), 2);

if ($type !== 'Synonyms') {
if ($type !== Constants::DISPLAY_SYNONYMS) {
return [new Title($type, JString::cleanse($title))];
}

Expand Down
7 changes: 4 additions & 3 deletions src/Parser/Common/AnimeCardParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Common;

use Jikan\Helper\Constants;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model;
Expand Down Expand Up @@ -211,7 +212,7 @@ public function getType(): ?string
$type = $matches[1];

if ($type === '-') {
$type = 'Unknown';
$type = Constants::DISPLAY_UNKNOWN;
}

return $type;
Expand Down Expand Up @@ -341,7 +342,7 @@ public function getAnimeScore(): ?float
if ($node->count()) {
$score = JString::cleanse($node->text());

if ($score === 'N/A') {
if ($score === Constants::DISPLAY_N_A) {
return null;
}
}
Expand All @@ -350,7 +351,7 @@ public function getAnimeScore(): ?float
$node = $this->crawler->filterXPath('//div[contains(@class, "widget")]/div[@class="stars"]');
if ($node->count()) {
$score = JString::cleanse($node->text());
if ($score === 'N/A') {
if ($score === Constants::DISPLAY_N_A) {
return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Common/MangaCardParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Common;

use Jikan\Helper\Constants;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model;
Expand Down Expand Up @@ -256,7 +257,7 @@ public function getMangaImage(): ?string
public function getMangaScore(): ?float
{
$score = JString::cleanse($this->crawler->filterXPath('//div[contains(@class, "information")]/div/div/div[1]')->text());
if ($score === 'N/A') {
if ($score === Constants::DISPLAY_N_A) {
return null;
}

Expand Down
11 changes: 6 additions & 5 deletions src/Parser/Manga/MangaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jikan\Parser\Manga;

use Jikan\Helper\Constants;
use Jikan\Helper\JString;
use Jikan\Helper\Parser;
use Jikan\Model\Common\DateRange;
Expand Down Expand Up @@ -214,7 +215,7 @@ public function getMangaType(): ?string
}

$type = JString::cleanse(str_replace($type->text(), '', $type->ancestors()->text()));
return $type === 'Unknown' ? null : $type;
return $type === Constants::DISPLAY_UNKNOWN ? null : $type;
}

/**
Expand All @@ -235,7 +236,7 @@ public function getMangaChapters(): ?int
(
trim(
str_replace($chapters->text(), '', $chapters->ancestors()->text())
) === 'Unknown'
) === Constants::DISPLAY_UNKNOWN
)
? null
: (int) str_replace(
Expand Down Expand Up @@ -263,7 +264,7 @@ public function getMangaVolumes(): ?int
(
trim(
str_replace($volumes->text(), '', $volumes->ancestors()->text())
) === 'Unknown'
) === Constants::DISPLAY_UNKNOWN
)
? null
: (int) str_replace(
Expand Down Expand Up @@ -465,7 +466,7 @@ public function getScore(): ?float

$score = JString::cleanse($score->text());

if ($score === 'N/A') {
if ($score === Constants::DISPLAY_N_A) {
return null;
}

Expand Down Expand Up @@ -523,7 +524,7 @@ public function getMangaRank(): ?int
)
);

return $ranked !== 'N/A' ? (int) $ranked : null;
return $ranked !== Constants::DISPLAY_N_A ? (int) $ranked : null;
}

/**
Expand Down
Loading
0