From 6a435269c9a02d2fb301cf1a7670dff0c4c531a9 Mon Sep 17 00:00:00 2001 From: sasezaki Date: Mon, 20 Oct 2025 23:39:45 +0900 Subject: [PATCH 1/2] refactor: add PHPStan generic type annotations to Extractors and Detectors Added PHPDoc template annotations to improve type safety and PHPStan compatibility across all adapter classes. This change removes the need for inline @var annotations by properly declaring generic type relationships between Extractors and Detectors. --- src/Adapters/Archive/Detectors/AuthorName.php | 5 ++-- src/Adapters/Archive/Detectors/Code.php | 3 ++ .../Archive/Detectors/Description.php | 5 ++-- .../Archive/Detectors/ProviderName.php | 3 ++ .../Archive/Detectors/PublishedTime.php | 5 ++-- src/Adapters/Archive/Detectors/Title.php | 5 ++-- src/Adapters/Archive/Extractor.php | 6 ++-- .../Bandcamp/Detectors/ProviderName.php | 3 ++ src/Adapters/Bandcamp/Extractor.php | 6 ++-- src/Adapters/CadenaSer/Detectors/Code.php | 3 ++ src/Adapters/CadenaSer/Extractor.php | 6 ++-- src/Adapters/Facebook/Detectors/Title.php | 3 ++ src/Adapters/Facebook/Extractor.php | 2 +- src/Adapters/Flickr/Detectors/Code.php | 3 ++ src/Adapters/Flickr/Extractor.php | 6 ++-- src/Adapters/Gist/Detectors/AuthorName.php | 5 ++-- src/Adapters/Gist/Detectors/AuthorUrl.php | 5 ++-- src/Adapters/Gist/Detectors/Code.php | 5 ++-- src/Adapters/Gist/Detectors/PublishedTime.php | 5 ++-- src/Adapters/Gist/Extractor.php | 6 ++-- src/Adapters/Github/Detectors/Code.php | 3 ++ src/Adapters/Github/Extractor.php | 6 ++-- src/Adapters/Ideone/Detectors/Code.php | 3 ++ src/Adapters/Ideone/Extractor.php | 6 ++-- .../ImageShack/Detectors/AuthorName.php | 4 ++- .../ImageShack/Detectors/AuthorUrl.php | 5 ++-- .../ImageShack/Detectors/Description.php | 5 ++-- src/Adapters/ImageShack/Detectors/Image.php | 5 ++-- .../ImageShack/Detectors/ProviderName.php | 3 ++ .../ImageShack/Detectors/PublishedTime.php | 4 ++- src/Adapters/ImageShack/Detectors/Title.php | 5 ++-- src/Adapters/ImageShack/Extractor.php | 6 ++-- src/Adapters/Instagram/Extractor.php | 3 ++ src/Adapters/Pinterest/Detectors/Code.php | 3 ++ src/Adapters/Pinterest/Extractor.php | 6 ++-- src/Adapters/Sassmeister/Detectors/Code.php | 3 ++ src/Adapters/Sassmeister/Extractor.php | 6 ++-- src/Adapters/Slides/Detectors/Code.php | 3 ++ src/Adapters/Slides/Extractor.php | 6 ++-- src/Adapters/Snipplr/Detectors/Code.php | 3 ++ src/Adapters/Snipplr/Extractor.php | 6 ++-- src/Adapters/Twitch/Detectors/Code.php | 3 ++ src/Adapters/Twitch/Extractor.php | 6 ++-- src/Adapters/Twitter/Detectors/AuthorName.php | 4 ++- src/Adapters/Twitter/Detectors/AuthorUrl.php | 4 ++- .../Twitter/Detectors/Description.php | 4 ++- src/Adapters/Twitter/Detectors/Image.php | 4 ++- .../Twitter/Detectors/ProviderName.php | 3 ++ .../Twitter/Detectors/PublishedTime.php | 4 ++- src/Adapters/Twitter/Detectors/Title.php | 4 ++- src/Adapters/Twitter/Extractor.php | 6 ++-- .../Wikipedia/Detectors/Description.php | 5 ++-- src/Adapters/Wikipedia/Detectors/Title.php | 4 ++- src/Adapters/Wikipedia/Extractor.php | 6 ++-- src/Adapters/Youtube/Detectors/Feeds.php | 3 ++ src/Adapters/Youtube/Extractor.php | 6 ++-- src/Detectors/AuthorName.php | 4 +++ src/Detectors/AuthorUrl.php | 4 +++ src/Detectors/Cms.php | 4 +++ src/Detectors/Code.php | 4 +++ src/Detectors/Description.php | 4 +++ src/Detectors/Detector.php | 7 +++++ src/Detectors/Favicon.php | 4 +++ src/Detectors/Feeds.php | 4 +++ src/Detectors/Icon.php | 4 +++ src/Detectors/Image.php | 4 +++ src/Detectors/Keywords.php | 4 +++ src/Detectors/Language.php | 4 +++ src/Detectors/Languages.php | 4 +++ src/Detectors/License.php | 4 +++ src/Detectors/ProviderName.php | 4 +++ src/Detectors/ProviderUrl.php | 4 +++ src/Detectors/PublishedTime.php | 4 +++ src/Detectors/Redirect.php | 4 +++ src/Detectors/Title.php | 4 +++ src/Detectors/Url.php | 4 +++ src/Extractor.php | 29 +++++++++++++++++-- src/ExtractorFactory.php | 10 ++++--- 78 files changed, 281 insertions(+), 91 deletions(-) diff --git a/src/Adapters/Archive/Detectors/AuthorName.php b/src/Adapters/Archive/Detectors/AuthorName.php index 3aef8b7b..3caf57bd 100644 --- a/src/Adapters/Archive/Detectors/AuthorName.php +++ b/src/Adapters/Archive/Detectors/AuthorName.php @@ -3,14 +3,15 @@ namespace Embed\Adapters\Archive\Detectors; -use Embed\Adapters\Archive\Extractor; use Embed\Detectors\AuthorName as Detector; +/** + * @extends Detector<\Embed\Adapters\Archive\Extractor> + */ class AuthorName extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Archive/Detectors/Code.php b/src/Adapters/Archive/Detectors/Code.php index 1840621f..e5888c57 100644 --- a/src/Adapters/Archive/Detectors/Code.php +++ b/src/Adapters/Archive/Detectors/Code.php @@ -8,6 +8,9 @@ use function Embed\html; use function Embed\matchPath; +/** + * @extends Detector<\Embed\Adapters\Archive\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Archive/Detectors/Description.php b/src/Adapters/Archive/Detectors/Description.php index baffbba4..e23386f8 100644 --- a/src/Adapters/Archive/Detectors/Description.php +++ b/src/Adapters/Archive/Detectors/Description.php @@ -3,14 +3,15 @@ namespace Embed\Adapters\Archive\Detectors; -use Embed\Adapters\Archive\Extractor; use Embed\Detectors\Description as Detector; +/** + * @extends Detector<\Embed\Adapters\Archive\Extractor> + */ class Description extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Archive/Detectors/ProviderName.php b/src/Adapters/Archive/Detectors/ProviderName.php index e8a632ac..4f036c27 100644 --- a/src/Adapters/Archive/Detectors/ProviderName.php +++ b/src/Adapters/Archive/Detectors/ProviderName.php @@ -5,6 +5,9 @@ use Embed\Detectors\ProviderName as Detector; +/** + * @extends Detector<\Embed\Adapters\Archive\Extractor> + */ class ProviderName extends Detector { public function detect(): string diff --git a/src/Adapters/Archive/Detectors/PublishedTime.php b/src/Adapters/Archive/Detectors/PublishedTime.php index 0d298d94..81f3511b 100644 --- a/src/Adapters/Archive/Detectors/PublishedTime.php +++ b/src/Adapters/Archive/Detectors/PublishedTime.php @@ -4,14 +4,15 @@ namespace Embed\Adapters\Archive\Detectors; use DateTime; -use Embed\Adapters\Archive\Extractor; use Embed\Detectors\PublishedTime as Detector; +/** + * @extends Detector<\Embed\Adapters\Archive\Extractor> + */ class PublishedTime extends Detector { public function detect(): ?DateTime { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Archive/Detectors/Title.php b/src/Adapters/Archive/Detectors/Title.php index 328ed05a..59397147 100644 --- a/src/Adapters/Archive/Detectors/Title.php +++ b/src/Adapters/Archive/Detectors/Title.php @@ -3,14 +3,15 @@ namespace Embed\Adapters\Archive\Detectors; -use Embed\Adapters\Archive\Extractor; use Embed\Detectors\Title as Detector; +/** + * @extends Detector<\Embed\Adapters\Archive\Extractor> + */ class Title extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Archive/Extractor.php b/src/Adapters/Archive/Extractor.php index ae99596f..482e13a9 100644 --- a/src/Adapters/Archive/Extractor.php +++ b/src/Adapters/Archive/Extractor.php @@ -9,6 +9,9 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { private Api $api; @@ -28,9 +31,6 @@ public function getApi(): Api return $this->api; } - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Bandcamp/Detectors/ProviderName.php b/src/Adapters/Bandcamp/Detectors/ProviderName.php index 74575aef..a52c98a8 100644 --- a/src/Adapters/Bandcamp/Detectors/ProviderName.php +++ b/src/Adapters/Bandcamp/Detectors/ProviderName.php @@ -5,6 +5,9 @@ use Embed\Detectors\ProviderName as Detector; +/** + * @extends Detector<\Embed\Adapters\Bandcamp\Extractor> + */ class ProviderName extends Detector { public function detect(): string diff --git a/src/Adapters/Bandcamp/Extractor.php b/src/Adapters/Bandcamp/Extractor.php index 4375d9c4..94e03537 100644 --- a/src/Adapters/Bandcamp/Extractor.php +++ b/src/Adapters/Bandcamp/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/CadenaSer/Detectors/Code.php b/src/Adapters/CadenaSer/Detectors/Code.php index 0715ef2a..bf70e664 100644 --- a/src/Adapters/CadenaSer/Detectors/Code.php +++ b/src/Adapters/CadenaSer/Detectors/Code.php @@ -9,6 +9,9 @@ use function Embed\html; use function Embed\matchPath; +/** + * @extends Detector<\Embed\Adapters\CadenaSer\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/CadenaSer/Extractor.php b/src/Adapters/CadenaSer/Extractor.php index 63d1b635..e6d05ef6 100644 --- a/src/Adapters/CadenaSer/Extractor.php +++ b/src/Adapters/CadenaSer/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Facebook/Detectors/Title.php b/src/Adapters/Facebook/Detectors/Title.php index 8d051131..298d2631 100644 --- a/src/Adapters/Facebook/Detectors/Title.php +++ b/src/Adapters/Facebook/Detectors/Title.php @@ -5,6 +5,9 @@ use Embed\Detectors\Title as Detector; +/** + * @extends Detector<\Embed\Adapters\Facebook\Extractor> + */ class Title extends Detector { /** diff --git a/src/Adapters/Facebook/Extractor.php b/src/Adapters/Facebook/Extractor.php index 9d24eeb1..9b8e4fdb 100644 --- a/src/Adapters/Facebook/Extractor.php +++ b/src/Adapters/Facebook/Extractor.php @@ -8,7 +8,7 @@ class Extractor extends Base { /** - * @return array + * @return array{title: Detectors\Title} */ public function createCustomDetectors(): array { diff --git a/src/Adapters/Flickr/Detectors/Code.php b/src/Adapters/Flickr/Detectors/Code.php index 273a0d61..9f14eb19 100644 --- a/src/Adapters/Flickr/Detectors/Code.php +++ b/src/Adapters/Flickr/Detectors/Code.php @@ -9,6 +9,9 @@ use function Embed\html; use function Embed\matchPath; +/** + * @extends Detector<\Embed\Adapters\Flickr\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Flickr/Extractor.php b/src/Adapters/Flickr/Extractor.php index 263b8733..0a4369c8 100644 --- a/src/Adapters/Flickr/Extractor.php +++ b/src/Adapters/Flickr/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Gist/Detectors/AuthorName.php b/src/Adapters/Gist/Detectors/AuthorName.php index 214910f3..2aee6dca 100644 --- a/src/Adapters/Gist/Detectors/AuthorName.php +++ b/src/Adapters/Gist/Detectors/AuthorName.php @@ -3,14 +3,15 @@ namespace Embed\Adapters\Gist\Detectors; -use Embed\Adapters\Gist\Extractor; use Embed\Detectors\AuthorName as Detector; +/** + * @extends Detector<\Embed\Adapters\Gist\Extractor> + */ class AuthorName extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Gist/Detectors/AuthorUrl.php b/src/Adapters/Gist/Detectors/AuthorUrl.php index 0060136a..d9bae2a8 100644 --- a/src/Adapters/Gist/Detectors/AuthorUrl.php +++ b/src/Adapters/Gist/Detectors/AuthorUrl.php @@ -3,15 +3,16 @@ namespace Embed\Adapters\Gist\Detectors; -use Embed\Adapters\Gist\Extractor; use Embed\Detectors\AuthorUrl as Detector; use Psr\Http\Message\UriInterface; +/** + * @extends Detector<\Embed\Adapters\Gist\Extractor> + */ class AuthorUrl extends Detector { public function detect(): ?UriInterface { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); $owner = $api->str('owner'); diff --git a/src/Adapters/Gist/Detectors/Code.php b/src/Adapters/Gist/Detectors/Code.php index b7df106d..28d510f0 100644 --- a/src/Adapters/Gist/Detectors/Code.php +++ b/src/Adapters/Gist/Detectors/Code.php @@ -3,11 +3,13 @@ namespace Embed\Adapters\Gist\Detectors; -use Embed\Adapters\Gist\Extractor; use Embed\Detectors\Code as Detector; use Embed\EmbedCode; use function Embed\html; +/** + * @extends Detector<\Embed\Adapters\Gist\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode @@ -18,7 +20,6 @@ public function detect(): ?EmbedCode private function fallback(): ?EmbedCode { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Gist/Detectors/PublishedTime.php b/src/Adapters/Gist/Detectors/PublishedTime.php index d49c5da2..775b6cee 100644 --- a/src/Adapters/Gist/Detectors/PublishedTime.php +++ b/src/Adapters/Gist/Detectors/PublishedTime.php @@ -4,14 +4,15 @@ namespace Embed\Adapters\Gist\Detectors; use DateTime; -use Embed\Adapters\Gist\Extractor; use Embed\Detectors\PublishedTime as Detector; +/** + * @extends Detector<\Embed\Adapters\Gist\Extractor> + */ class PublishedTime extends Detector { public function detect(): ?DateTime { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Gist/Extractor.php b/src/Adapters/Gist/Extractor.php index 369ad17c..088f874a 100644 --- a/src/Adapters/Gist/Extractor.php +++ b/src/Adapters/Gist/Extractor.php @@ -9,6 +9,9 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { private Api $api; @@ -28,9 +31,6 @@ public function getApi(): Api return $this->api; } - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Github/Detectors/Code.php b/src/Adapters/Github/Detectors/Code.php index e3adc4d7..03cb9a3c 100644 --- a/src/Adapters/Github/Detectors/Code.php +++ b/src/Adapters/Github/Detectors/Code.php @@ -8,6 +8,9 @@ use function Embed\html; use function Embed\matchPath; +/** + * @extends Detector<\Embed\Adapters\Github\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Github/Extractor.php b/src/Adapters/Github/Extractor.php index 19763888..6762be87 100644 --- a/src/Adapters/Github/Extractor.php +++ b/src/Adapters/Github/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Ideone/Detectors/Code.php b/src/Adapters/Ideone/Detectors/Code.php index e14c0273..6459d867 100644 --- a/src/Adapters/Ideone/Detectors/Code.php +++ b/src/Adapters/Ideone/Detectors/Code.php @@ -7,6 +7,9 @@ use Embed\EmbedCode; use function Embed\html; +/** + * @extends Detector<\Embed\Adapters\Ideone\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Ideone/Extractor.php b/src/Adapters/Ideone/Extractor.php index 1581c0a3..2dc9116a 100644 --- a/src/Adapters/Ideone/Extractor.php +++ b/src/Adapters/Ideone/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/ImageShack/Detectors/AuthorName.php b/src/Adapters/ImageShack/Detectors/AuthorName.php index f77bbbc8..8eeed2db 100644 --- a/src/Adapters/ImageShack/Detectors/AuthorName.php +++ b/src/Adapters/ImageShack/Detectors/AuthorName.php @@ -6,11 +6,13 @@ use Embed\Adapters\ImageShack\Extractor; use Embed\Detectors\AuthorName as Detector; +/** + * @extends Detector<\Embed\Adapters\ImageShack\Extractor> + */ class AuthorName extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/ImageShack/Detectors/AuthorUrl.php b/src/Adapters/ImageShack/Detectors/AuthorUrl.php index 598e319a..7fcd0b08 100644 --- a/src/Adapters/ImageShack/Detectors/AuthorUrl.php +++ b/src/Adapters/ImageShack/Detectors/AuthorUrl.php @@ -3,15 +3,16 @@ namespace Embed\Adapters\ImageShack\Detectors; -use Embed\Adapters\ImageShack\Extractor; use Embed\Detectors\AuthorUrl as Detector; use Psr\Http\Message\UriInterface; +/** + * @extends Detector<\Embed\Adapters\ImageShack\Extractor> + */ class AuthorUrl extends Detector { public function detect(): ?UriInterface { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); $owner = $api->str('owner', 'username'); diff --git a/src/Adapters/ImageShack/Detectors/Description.php b/src/Adapters/ImageShack/Detectors/Description.php index ecd7af69..0ebd1dfa 100644 --- a/src/Adapters/ImageShack/Detectors/Description.php +++ b/src/Adapters/ImageShack/Detectors/Description.php @@ -3,14 +3,15 @@ namespace Embed\Adapters\ImageShack\Detectors; -use Embed\Adapters\ImageShack\Extractor; use Embed\Detectors\Description as Detector; +/** + * @extends Detector<\Embed\Adapters\ImageShack\Extractor> + */ class Description extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/ImageShack/Detectors/Image.php b/src/Adapters/ImageShack/Detectors/Image.php index c11e8910..531e306c 100644 --- a/src/Adapters/ImageShack/Detectors/Image.php +++ b/src/Adapters/ImageShack/Detectors/Image.php @@ -3,15 +3,16 @@ namespace Embed\Adapters\ImageShack\Detectors; -use Embed\Adapters\ImageShack\Extractor; use Embed\Detectors\Image as Detector; use Psr\Http\Message\UriInterface; +/** + * @extends Detector<\Embed\Adapters\ImageShack\Extractor> + */ class Image extends Detector { public function detect(): ?UriInterface { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/ImageShack/Detectors/ProviderName.php b/src/Adapters/ImageShack/Detectors/ProviderName.php index 661d45e4..fffe0d30 100644 --- a/src/Adapters/ImageShack/Detectors/ProviderName.php +++ b/src/Adapters/ImageShack/Detectors/ProviderName.php @@ -5,6 +5,9 @@ use Embed\Detectors\ProviderName as Detector; +/** + * @extends Detector<\Embed\Adapters\ImageShack\Extractor> + */ class ProviderName extends Detector { public function detect(): string diff --git a/src/Adapters/ImageShack/Detectors/PublishedTime.php b/src/Adapters/ImageShack/Detectors/PublishedTime.php index 8224860e..ad1f803b 100644 --- a/src/Adapters/ImageShack/Detectors/PublishedTime.php +++ b/src/Adapters/ImageShack/Detectors/PublishedTime.php @@ -7,11 +7,13 @@ use Embed\Adapters\ImageShack\Extractor; use Embed\Detectors\PublishedTime as Detector; +/** + * @extends Detector<\Embed\Adapters\ImageShack\Extractor> + */ class PublishedTime extends Detector { public function detect(): ?DateTime { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/ImageShack/Detectors/Title.php b/src/Adapters/ImageShack/Detectors/Title.php index 4d74f0cd..6f53df58 100644 --- a/src/Adapters/ImageShack/Detectors/Title.php +++ b/src/Adapters/ImageShack/Detectors/Title.php @@ -3,14 +3,15 @@ namespace Embed\Adapters\ImageShack\Detectors; -use Embed\Adapters\ImageShack\Extractor; use Embed\Detectors\Title as Detector; +/** + * @extends Detector<\Embed\Adapters\ImageShack\Extractor> + */ class Title extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/ImageShack/Extractor.php b/src/Adapters/ImageShack/Extractor.php index 327bf9b0..416228d3 100644 --- a/src/Adapters/ImageShack/Extractor.php +++ b/src/Adapters/ImageShack/Extractor.php @@ -9,6 +9,9 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { private Api $api; @@ -28,9 +31,6 @@ public function getApi(): Api return $this->api; } - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Instagram/Extractor.php b/src/Adapters/Instagram/Extractor.php index 8e0e73ee..c7a7802e 100644 --- a/src/Adapters/Instagram/Extractor.php +++ b/src/Adapters/Instagram/Extractor.php @@ -9,6 +9,9 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { public function __construct(UriInterface $uri, RequestInterface $request, ResponseInterface $response, Crawler $crawler) diff --git a/src/Adapters/Pinterest/Detectors/Code.php b/src/Adapters/Pinterest/Detectors/Code.php index b033ec70..626975bd 100644 --- a/src/Adapters/Pinterest/Detectors/Code.php +++ b/src/Adapters/Pinterest/Detectors/Code.php @@ -8,6 +8,9 @@ use function Embed\html; use function Embed\matchPath; +/** + * @extends Detector<\Embed\Adapters\Pinterest\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Pinterest/Extractor.php b/src/Adapters/Pinterest/Extractor.php index 39aa79dd..5c627aa5 100644 --- a/src/Adapters/Pinterest/Extractor.php +++ b/src/Adapters/Pinterest/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Sassmeister/Detectors/Code.php b/src/Adapters/Sassmeister/Detectors/Code.php index f9b39b67..f421a52f 100644 --- a/src/Adapters/Sassmeister/Detectors/Code.php +++ b/src/Adapters/Sassmeister/Detectors/Code.php @@ -8,6 +8,9 @@ use function Embed\html; use function Embed\matchPath; +/** + * @extends Detector<\Embed\Adapters\Sassmeister\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Sassmeister/Extractor.php b/src/Adapters/Sassmeister/Extractor.php index 718e79e0..4e3cde5a 100644 --- a/src/Adapters/Sassmeister/Extractor.php +++ b/src/Adapters/Sassmeister/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Slides/Detectors/Code.php b/src/Adapters/Slides/Detectors/Code.php index 8e6ffb91..2db43646 100644 --- a/src/Adapters/Slides/Detectors/Code.php +++ b/src/Adapters/Slides/Detectors/Code.php @@ -8,6 +8,9 @@ use Embed\EmbedCode; use function Embed\html; +/** + * @extends Detector<\Embed\Adapters\Slides\Extractor> + */ class Code extends Detector { public function detect(): EmbedCode diff --git a/src/Adapters/Slides/Extractor.php b/src/Adapters/Slides/Extractor.php index 949cc7b7..c7d9daac 100644 --- a/src/Adapters/Slides/Extractor.php +++ b/src/Adapters/Slides/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Snipplr/Detectors/Code.php b/src/Adapters/Snipplr/Detectors/Code.php index 2663039f..a4968627 100644 --- a/src/Adapters/Snipplr/Detectors/Code.php +++ b/src/Adapters/Snipplr/Detectors/Code.php @@ -8,6 +8,9 @@ use function Embed\html; use function Embed\matchPath; +/** + * @extends Detector<\Embed\Adapters\Snipplr\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Snipplr/Extractor.php b/src/Adapters/Snipplr/Extractor.php index 06ab210b..fc349555 100644 --- a/src/Adapters/Snipplr/Extractor.php +++ b/src/Adapters/Snipplr/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Twitch/Detectors/Code.php b/src/Adapters/Twitch/Detectors/Code.php index 2780dc4b..b06f39f9 100644 --- a/src/Adapters/Twitch/Detectors/Code.php +++ b/src/Adapters/Twitch/Detectors/Code.php @@ -7,6 +7,9 @@ use Embed\EmbedCode; use function Embed\html; +/** + * @extends Detector<\Embed\Adapters\Twitch\Extractor> + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Adapters/Twitch/Extractor.php b/src/Adapters/Twitch/Extractor.php index 990f62b2..f54552d0 100644 --- a/src/Adapters/Twitch/Extractor.php +++ b/src/Adapters/Twitch/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Twitter/Detectors/AuthorName.php b/src/Adapters/Twitter/Detectors/AuthorName.php index d9c050dc..a97d2d41 100644 --- a/src/Adapters/Twitter/Detectors/AuthorName.php +++ b/src/Adapters/Twitter/Detectors/AuthorName.php @@ -6,11 +6,13 @@ use Embed\Adapters\Twitter\Extractor; use Embed\Detectors\AuthorName as Detector; +/** + * @extends Detector<\Embed\Adapters\Twitter\Extractor> + */ class AuthorName extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Twitter/Detectors/AuthorUrl.php b/src/Adapters/Twitter/Detectors/AuthorUrl.php index c30c47dd..d2b38b93 100644 --- a/src/Adapters/Twitter/Detectors/AuthorUrl.php +++ b/src/Adapters/Twitter/Detectors/AuthorUrl.php @@ -7,11 +7,13 @@ use Embed\Detectors\AuthorUrl as Detector; use Psr\Http\Message\UriInterface; +/** + * @extends Detector<\Embed\Adapters\Twitter\Extractor> + */ class AuthorUrl extends Detector { public function detect(): ?UriInterface { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); $username = $api->str('includes', 'users', '0', 'username'); diff --git a/src/Adapters/Twitter/Detectors/Description.php b/src/Adapters/Twitter/Detectors/Description.php index d61db345..fb01d2ae 100644 --- a/src/Adapters/Twitter/Detectors/Description.php +++ b/src/Adapters/Twitter/Detectors/Description.php @@ -6,11 +6,13 @@ use Embed\Adapters\Twitter\Extractor; use Embed\Detectors\Description as Detector; +/** + * @extends Detector<\Embed\Adapters\Twitter\Extractor> + */ class Description extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Twitter/Detectors/Image.php b/src/Adapters/Twitter/Detectors/Image.php index 75fad8e0..4e5879ef 100644 --- a/src/Adapters/Twitter/Detectors/Image.php +++ b/src/Adapters/Twitter/Detectors/Image.php @@ -7,11 +7,13 @@ use Embed\Detectors\Image as Detector; use Psr\Http\Message\UriInterface; +/** + * @extends Detector<\Embed\Adapters\Twitter\Extractor> + */ class Image extends Detector { public function detect(): ?UriInterface { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); $preview = $api->url('includes', 'media', '0', 'preview_image_url'); diff --git a/src/Adapters/Twitter/Detectors/ProviderName.php b/src/Adapters/Twitter/Detectors/ProviderName.php index e5c99c0b..93bed7aa 100644 --- a/src/Adapters/Twitter/Detectors/ProviderName.php +++ b/src/Adapters/Twitter/Detectors/ProviderName.php @@ -5,6 +5,9 @@ use Embed\Detectors\ProviderName as Detector; +/** + * @extends Detector<\Embed\Adapters\Twitter\Extractor> + */ class ProviderName extends Detector { public function detect(): string diff --git a/src/Adapters/Twitter/Detectors/PublishedTime.php b/src/Adapters/Twitter/Detectors/PublishedTime.php index 49cef116..a8897387 100644 --- a/src/Adapters/Twitter/Detectors/PublishedTime.php +++ b/src/Adapters/Twitter/Detectors/PublishedTime.php @@ -7,11 +7,13 @@ use Embed\Adapters\Twitter\Extractor; use Embed\Detectors\PublishedTime as Detector; +/** + * @extends Detector<\Embed\Adapters\Twitter\Extractor> + */ class PublishedTime extends Detector { public function detect(): ?DateTime { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Twitter/Detectors/Title.php b/src/Adapters/Twitter/Detectors/Title.php index 36e8c127..d37231b9 100644 --- a/src/Adapters/Twitter/Detectors/Title.php +++ b/src/Adapters/Twitter/Detectors/Title.php @@ -6,11 +6,13 @@ use Embed\Adapters\Twitter\Extractor; use Embed\Detectors\Title as Detector; +/** + * @extends Detector<\Embed\Adapters\Twitter\Extractor> + */ class Title extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); $name = $api->str('includes', 'users', '0', 'name'); diff --git a/src/Adapters/Twitter/Extractor.php b/src/Adapters/Twitter/Extractor.php index 0908b5ac..d505e5ee 100644 --- a/src/Adapters/Twitter/Extractor.php +++ b/src/Adapters/Twitter/Extractor.php @@ -9,6 +9,9 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { private Api $api; @@ -28,9 +31,6 @@ public function getApi(): Api return $this->api; } - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Wikipedia/Detectors/Description.php b/src/Adapters/Wikipedia/Detectors/Description.php index fdb18dd3..102c4d04 100644 --- a/src/Adapters/Wikipedia/Detectors/Description.php +++ b/src/Adapters/Wikipedia/Detectors/Description.php @@ -3,14 +3,15 @@ namespace Embed\Adapters\Wikipedia\Detectors; -use Embed\Adapters\Wikipedia\Extractor; use Embed\Detectors\Description as Detector; +/** + * @extends Detector<\Embed\Adapters\Wikipedia\Extractor> + */ class Description extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Wikipedia/Detectors/Title.php b/src/Adapters/Wikipedia/Detectors/Title.php index bfe001af..413c5877 100644 --- a/src/Adapters/Wikipedia/Detectors/Title.php +++ b/src/Adapters/Wikipedia/Detectors/Title.php @@ -6,11 +6,13 @@ use Embed\Adapters\Wikipedia\Extractor; use Embed\Detectors\Title as Detector; +/** + * @extends Detector<\Embed\Adapters\Wikipedia\Extractor> + */ class Title extends Detector { public function detect(): ?string { - /** @var Extractor $extractor */ $extractor = $this->extractor; $api = $extractor->getApi(); diff --git a/src/Adapters/Wikipedia/Extractor.php b/src/Adapters/Wikipedia/Extractor.php index 75afb5ea..c432707f 100644 --- a/src/Adapters/Wikipedia/Extractor.php +++ b/src/Adapters/Wikipedia/Extractor.php @@ -9,6 +9,9 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { private Api $api; @@ -28,9 +31,6 @@ public function getApi(): Api return $this->api; } - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Adapters/Youtube/Detectors/Feeds.php b/src/Adapters/Youtube/Detectors/Feeds.php index 0bc9559f..b63aeeb7 100644 --- a/src/Adapters/Youtube/Detectors/Feeds.php +++ b/src/Adapters/Youtube/Detectors/Feeds.php @@ -8,6 +8,9 @@ use function Embed\matchPath; use Psr\Http\Message\UriInterface; +/** + * @extends Detector<\Embed\Adapters\Youtube\Extractor> + */ class Feeds extends Detector { /** diff --git a/src/Adapters/Youtube/Extractor.php b/src/Adapters/Youtube/Extractor.php index ea43c122..1b81d63f 100644 --- a/src/Adapters/Youtube/Extractor.php +++ b/src/Adapters/Youtube/Extractor.php @@ -5,11 +5,11 @@ use Embed\Extractor as Base; +/** + * @template-extends Base<\Embed\Detectors\Detector> + */ class Extractor extends Base { - /** - * @return array - */ public function createCustomDetectors(): array { return [ diff --git a/src/Detectors/AuthorName.php b/src/Detectors/AuthorName.php index 3d1dc67b..a65881fe 100644 --- a/src/Detectors/AuthorName.php +++ b/src/Detectors/AuthorName.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class AuthorName extends Detector { public function detect(): ?string diff --git a/src/Detectors/AuthorUrl.php b/src/Detectors/AuthorUrl.php index c11f2af1..9903e638 100644 --- a/src/Detectors/AuthorUrl.php +++ b/src/Detectors/AuthorUrl.php @@ -5,6 +5,10 @@ use Psr\Http\Message\UriInterface; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class AuthorUrl extends Detector { public function detect(): ?UriInterface diff --git a/src/Detectors/Cms.php b/src/Detectors/Cms.php index 027de530..d3df134b 100644 --- a/src/Detectors/Cms.php +++ b/src/Detectors/Cms.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Cms extends Detector { public const BLOGSPOT = 'blogspot'; diff --git a/src/Detectors/Code.php b/src/Detectors/Code.php index 883ee85a..870c82c9 100644 --- a/src/Detectors/Code.php +++ b/src/Detectors/Code.php @@ -6,6 +6,10 @@ use Embed\EmbedCode; use function Embed\html; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Code extends Detector { public function detect(): ?EmbedCode diff --git a/src/Detectors/Description.php b/src/Detectors/Description.php index 0fffdee2..cc845afb 100644 --- a/src/Detectors/Description.php +++ b/src/Detectors/Description.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Description extends Detector { public function detect(): ?string diff --git a/src/Detectors/Detector.php b/src/Detectors/Detector.php index ca67a7ba..036087e9 100644 --- a/src/Detectors/Detector.php +++ b/src/Detectors/Detector.php @@ -5,12 +5,19 @@ use Embed\Extractor; +/** + * @template TExtractor of Extractor + */ abstract class Detector { + /** @var TExtractor */ protected Extractor $extractor; /** @var array */ private array $cache = []; + /** + * @param TExtractor $extractor + */ public function __construct(Extractor $extractor) { $this->extractor = $extractor; diff --git a/src/Detectors/Favicon.php b/src/Detectors/Favicon.php index dbeca1ab..2e0b1930 100644 --- a/src/Detectors/Favicon.php +++ b/src/Detectors/Favicon.php @@ -5,6 +5,10 @@ use Psr\Http\Message\UriInterface; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Favicon extends Detector { public function detect(): UriInterface diff --git a/src/Detectors/Feeds.php b/src/Detectors/Feeds.php index 8660726a..b00c2051 100644 --- a/src/Detectors/Feeds.php +++ b/src/Detectors/Feeds.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Feeds extends Detector { /** @var string[] */ diff --git a/src/Detectors/Icon.php b/src/Detectors/Icon.php index 29f24d44..eae81474 100644 --- a/src/Detectors/Icon.php +++ b/src/Detectors/Icon.php @@ -5,6 +5,10 @@ use Psr\Http\Message\UriInterface; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Icon extends Detector { public function detect(): ?UriInterface diff --git a/src/Detectors/Image.php b/src/Detectors/Image.php index d7ffa4f2..10c7ae70 100644 --- a/src/Detectors/Image.php +++ b/src/Detectors/Image.php @@ -5,6 +5,10 @@ use Psr\Http\Message\UriInterface; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Image extends Detector { public function detect(): ?UriInterface diff --git a/src/Detectors/Keywords.php b/src/Detectors/Keywords.php index a4ce5b9b..472c9ed7 100644 --- a/src/Detectors/Keywords.php +++ b/src/Detectors/Keywords.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Keywords extends Detector { /** diff --git a/src/Detectors/Language.php b/src/Detectors/Language.php index ed667db3..b8dd1e49 100644 --- a/src/Detectors/Language.php +++ b/src/Detectors/Language.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Language extends Detector { public function detect(): ?string diff --git a/src/Detectors/Languages.php b/src/Detectors/Languages.php index eb765a6a..5f899c11 100644 --- a/src/Detectors/Languages.php +++ b/src/Detectors/Languages.php @@ -5,6 +5,10 @@ use function Embed\isEmpty; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Languages extends Detector { /** diff --git a/src/Detectors/License.php b/src/Detectors/License.php index 3c74d29f..a9cf26df 100644 --- a/src/Detectors/License.php +++ b/src/Detectors/License.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class License extends Detector { public function detect(): ?string diff --git a/src/Detectors/ProviderName.php b/src/Detectors/ProviderName.php index 4ae7aa84..1340aa7b 100644 --- a/src/Detectors/ProviderName.php +++ b/src/Detectors/ProviderName.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class ProviderName extends Detector { /** @var string[] */ diff --git a/src/Detectors/ProviderUrl.php b/src/Detectors/ProviderUrl.php index 7396fd51..9201179b 100644 --- a/src/Detectors/ProviderUrl.php +++ b/src/Detectors/ProviderUrl.php @@ -5,6 +5,10 @@ use Psr\Http\Message\UriInterface; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class ProviderUrl extends Detector { public function detect(): UriInterface diff --git a/src/Detectors/PublishedTime.php b/src/Detectors/PublishedTime.php index 5b9c097d..c6ed932d 100644 --- a/src/Detectors/PublishedTime.php +++ b/src/Detectors/PublishedTime.php @@ -5,6 +5,10 @@ use DateTime; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class PublishedTime extends Detector { public function detect(): ?DateTime diff --git a/src/Detectors/Redirect.php b/src/Detectors/Redirect.php index 717bcf96..1f18437f 100644 --- a/src/Detectors/Redirect.php +++ b/src/Detectors/Redirect.php @@ -5,6 +5,10 @@ use Psr\Http\Message\UriInterface; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Redirect extends Detector { public function detect(): ?UriInterface diff --git a/src/Detectors/Title.php b/src/Detectors/Title.php index cda77ba5..7f38b006 100644 --- a/src/Detectors/Title.php +++ b/src/Detectors/Title.php @@ -3,6 +3,10 @@ namespace Embed\Detectors; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Title extends Detector { public function detect(): ?string diff --git a/src/Detectors/Url.php b/src/Detectors/Url.php index 41f5acc2..7771eef6 100644 --- a/src/Detectors/Url.php +++ b/src/Detectors/Url.php @@ -5,6 +5,10 @@ use Psr\Http\Message\UriInterface; +/** + * @template TExtractor of \Embed\Extractor + * @template-extends Detector + */ class Url extends Detector { public function detect(): UriInterface diff --git a/src/Extractor.php b/src/Extractor.php index ac8d6afa..641968a9 100644 --- a/src/Extractor.php +++ b/src/Extractor.php @@ -53,6 +53,8 @@ * @property UriInterface|null $redirect * @property string|null $title * @property UriInterface $url + * + * @template TDetector of Detector = Detector */ class Extractor { @@ -68,27 +70,45 @@ class Extractor /** @var array */ private array $settings = []; - /** @var array */ + /** @var array> */ private array $customDetectors = []; - + /** @var AuthorName<$this> */ protected AuthorName $authorName; + /** @var AuthorUrl<$this> */ protected AuthorUrl $authorUrl; + /** @var Cms<$this> */ protected Cms $cms; + /** @var Code<$this> */ protected Code $code; + /** @var Description<$this> */ protected Description $description; + /** @var Favicon<$this> */ protected Favicon $favicon; + /** @var Feeds<$this> */ protected Feeds $feeds; + /** @var Icon<$this> */ protected Icon $icon; + /** @var Image<$this> */ protected Image $image; + /** @var Keywords<$this> */ protected Keywords $keywords; + /** @var Language<$this> */ protected Language $language; + /** @var Languages<$this> */ protected Languages $languages; + /** @var License<$this> */ protected License $license; + /** @var ProviderName<$this> */ protected ProviderName $providerName; + /** @var ProviderUrl<$this> */ protected ProviderUrl $providerUrl; + /** @var PublishedTime<$this> */ protected PublishedTime $publishedTime; + /** @var Redirect<$this> */ protected Redirect $redirect; + /** @var Title<$this> */ protected Title $title; + /** @var Url<$this> */ protected Url $url; public function __construct(UriInterface $uri, RequestInterface $request, ResponseInterface $response, Crawler $crawler) @@ -150,13 +170,16 @@ public function __get(string $name) } /** - * @return array + * @return array */ public function createCustomDetectors(): array { return []; } + /** + * @phpstan-param Detector<$this> $detector + */ public function addDetector(string $name, Detector $detector): void { $this->customDetectors[$name] = $detector; diff --git a/src/ExtractorFactory.php b/src/ExtractorFactory.php index f93c60a0..dc43daa2 100644 --- a/src/ExtractorFactory.php +++ b/src/ExtractorFactory.php @@ -10,6 +10,7 @@ class ExtractorFactory { + /** @var class-string */ private string $default = Extractor::class; /** @var array> */ private array $adapters = [ @@ -33,7 +34,7 @@ class ExtractorFactory 'twitter.com' => Adapters\Twitter\Extractor::class, 'x.com' => Adapters\Twitter\Extractor::class, ]; - /** @var array> */ + /** @var array>> */ private array $customDetectors = []; /** @var array */ private array $settings; @@ -65,12 +66,10 @@ public function createExtractor(UriInterface $uri, RequestInterface $request, Re } } - /** @var Extractor $extractor */ $extractor = new $class($uri, $request, $response, $crawler); $extractor->setSettings($this->settings); foreach ($this->customDetectors as $name => $detectorClass) { - /** @var Detectors\Detector */ $detector = new $detectorClass($extractor); $extractor->addDetector($name, $detector); } @@ -91,7 +90,7 @@ public function addAdapter(string $pattern, string $class): void } /** - * @param class-string $class + * @param class-string> $class */ public function addDetector(string $name, string $class): void { @@ -103,6 +102,9 @@ public function removeAdapter(string $pattern): void unset($this->adapters[$pattern]); } + /** + * @param class-string $class + */ public function setDefault(string $class): void { $this->default = $class; From 02a058bd48a128023edf3e505bca23fb4673f0ab Mon Sep 17 00:00:00 2001 From: sasezaki Date: Wed, 22 Oct 2025 16:44:57 +0900 Subject: [PATCH 2/2] refactor: remove redundant intermediate variables in Detector classes Simplify code by directly accessing API from extractor property instead of using intermediate variable across multiple adapter detectors --- src/Adapters/Archive/Detectors/AuthorName.php | 3 +-- src/Adapters/Archive/Detectors/Description.php | 3 +-- src/Adapters/Archive/Detectors/PublishedTime.php | 3 +-- src/Adapters/Archive/Detectors/Title.php | 3 +-- src/Adapters/Gist/Detectors/AuthorName.php | 3 +-- src/Adapters/Gist/Detectors/Code.php | 3 +-- src/Adapters/Gist/Detectors/PublishedTime.php | 3 +-- src/Adapters/ImageShack/Detectors/AuthorName.php | 3 +-- src/Adapters/ImageShack/Detectors/Description.php | 3 +-- src/Adapters/ImageShack/Detectors/Image.php | 3 +-- src/Adapters/ImageShack/Detectors/PublishedTime.php | 3 +-- src/Adapters/ImageShack/Detectors/Title.php | 3 +-- src/Adapters/Twitter/Detectors/AuthorName.php | 3 +-- src/Adapters/Twitter/Detectors/Description.php | 3 +-- src/Adapters/Twitter/Detectors/Image.php | 3 +-- src/Adapters/Twitter/Detectors/PublishedTime.php | 3 +-- src/Adapters/Twitter/Detectors/Title.php | 3 +-- src/Adapters/Wikipedia/Detectors/Description.php | 3 +-- src/Adapters/Wikipedia/Detectors/Title.php | 3 +-- 19 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/Adapters/Archive/Detectors/AuthorName.php b/src/Adapters/Archive/Detectors/AuthorName.php index 3caf57bd..a58abfbc 100644 --- a/src/Adapters/Archive/Detectors/AuthorName.php +++ b/src/Adapters/Archive/Detectors/AuthorName.php @@ -12,8 +12,7 @@ class AuthorName extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('metadata', 'creator'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Archive/Detectors/Description.php b/src/Adapters/Archive/Detectors/Description.php index e23386f8..b9553044 100644 --- a/src/Adapters/Archive/Detectors/Description.php +++ b/src/Adapters/Archive/Detectors/Description.php @@ -12,8 +12,7 @@ class Description extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('metadata', 'extract'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Archive/Detectors/PublishedTime.php b/src/Adapters/Archive/Detectors/PublishedTime.php index 81f3511b..c2f76aec 100644 --- a/src/Adapters/Archive/Detectors/PublishedTime.php +++ b/src/Adapters/Archive/Detectors/PublishedTime.php @@ -13,8 +13,7 @@ class PublishedTime extends Detector { public function detect(): ?DateTime { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $fields = ['publicdate', 'addeddate', 'date']; foreach ($fields as $field) { diff --git a/src/Adapters/Archive/Detectors/Title.php b/src/Adapters/Archive/Detectors/Title.php index 59397147..a5b83eb1 100644 --- a/src/Adapters/Archive/Detectors/Title.php +++ b/src/Adapters/Archive/Detectors/Title.php @@ -12,8 +12,7 @@ class Title extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('metadata', 'title'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Gist/Detectors/AuthorName.php b/src/Adapters/Gist/Detectors/AuthorName.php index 2aee6dca..a4566624 100644 --- a/src/Adapters/Gist/Detectors/AuthorName.php +++ b/src/Adapters/Gist/Detectors/AuthorName.php @@ -12,8 +12,7 @@ class AuthorName extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('owner'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Gist/Detectors/Code.php b/src/Adapters/Gist/Detectors/Code.php index 28d510f0..8eb7a909 100644 --- a/src/Adapters/Gist/Detectors/Code.php +++ b/src/Adapters/Gist/Detectors/Code.php @@ -20,8 +20,7 @@ public function detect(): ?EmbedCode private function fallback(): ?EmbedCode { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $code = $api->html('div'); $stylesheet = $api->str('stylesheet'); diff --git a/src/Adapters/Gist/Detectors/PublishedTime.php b/src/Adapters/Gist/Detectors/PublishedTime.php index 775b6cee..6a29aad1 100644 --- a/src/Adapters/Gist/Detectors/PublishedTime.php +++ b/src/Adapters/Gist/Detectors/PublishedTime.php @@ -13,8 +13,7 @@ class PublishedTime extends Detector { public function detect(): ?DateTime { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->time('created_at'); return $result !== null ? $result : parent::detect(); diff --git a/src/Adapters/ImageShack/Detectors/AuthorName.php b/src/Adapters/ImageShack/Detectors/AuthorName.php index 8eeed2db..75336997 100644 --- a/src/Adapters/ImageShack/Detectors/AuthorName.php +++ b/src/Adapters/ImageShack/Detectors/AuthorName.php @@ -13,8 +13,7 @@ class AuthorName extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('owner', 'username'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/ImageShack/Detectors/Description.php b/src/Adapters/ImageShack/Detectors/Description.php index 0ebd1dfa..1294e9dd 100644 --- a/src/Adapters/ImageShack/Detectors/Description.php +++ b/src/Adapters/ImageShack/Detectors/Description.php @@ -12,8 +12,7 @@ class Description extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('description'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/ImageShack/Detectors/Image.php b/src/Adapters/ImageShack/Detectors/Image.php index 531e306c..477cc0b3 100644 --- a/src/Adapters/ImageShack/Detectors/Image.php +++ b/src/Adapters/ImageShack/Detectors/Image.php @@ -13,8 +13,7 @@ class Image extends Detector { public function detect(): ?UriInterface { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->url('direct_link'); return $result !== null ? $result : parent::detect(); diff --git a/src/Adapters/ImageShack/Detectors/PublishedTime.php b/src/Adapters/ImageShack/Detectors/PublishedTime.php index ad1f803b..ff6d07bf 100644 --- a/src/Adapters/ImageShack/Detectors/PublishedTime.php +++ b/src/Adapters/ImageShack/Detectors/PublishedTime.php @@ -14,8 +14,7 @@ class PublishedTime extends Detector { public function detect(): ?DateTime { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->time('creation_date'); return $result !== null ? $result : parent::detect(); diff --git a/src/Adapters/ImageShack/Detectors/Title.php b/src/Adapters/ImageShack/Detectors/Title.php index 6f53df58..8bab3936 100644 --- a/src/Adapters/ImageShack/Detectors/Title.php +++ b/src/Adapters/ImageShack/Detectors/Title.php @@ -12,8 +12,7 @@ class Title extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('title'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Twitter/Detectors/AuthorName.php b/src/Adapters/Twitter/Detectors/AuthorName.php index a97d2d41..c24d84dc 100644 --- a/src/Adapters/Twitter/Detectors/AuthorName.php +++ b/src/Adapters/Twitter/Detectors/AuthorName.php @@ -13,8 +13,7 @@ class AuthorName extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('includes', 'users', '0', 'name'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Twitter/Detectors/Description.php b/src/Adapters/Twitter/Detectors/Description.php index fb01d2ae..3d4a9585 100644 --- a/src/Adapters/Twitter/Detectors/Description.php +++ b/src/Adapters/Twitter/Detectors/Description.php @@ -13,8 +13,7 @@ class Description extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('data', 'text'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Twitter/Detectors/Image.php b/src/Adapters/Twitter/Detectors/Image.php index 4e5879ef..f8ca1019 100644 --- a/src/Adapters/Twitter/Detectors/Image.php +++ b/src/Adapters/Twitter/Detectors/Image.php @@ -14,8 +14,7 @@ class Image extends Detector { public function detect(): ?UriInterface { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $preview = $api->url('includes', 'media', '0', 'preview_image_url'); if ($preview !== null) { diff --git a/src/Adapters/Twitter/Detectors/PublishedTime.php b/src/Adapters/Twitter/Detectors/PublishedTime.php index a8897387..a68e02ef 100644 --- a/src/Adapters/Twitter/Detectors/PublishedTime.php +++ b/src/Adapters/Twitter/Detectors/PublishedTime.php @@ -14,8 +14,7 @@ class PublishedTime extends Detector { public function detect(): ?DateTime { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->time('data', 'created_at'); return $result !== null ? $result : parent::detect(); diff --git a/src/Adapters/Twitter/Detectors/Title.php b/src/Adapters/Twitter/Detectors/Title.php index d37231b9..6774f7af 100644 --- a/src/Adapters/Twitter/Detectors/Title.php +++ b/src/Adapters/Twitter/Detectors/Title.php @@ -13,8 +13,7 @@ class Title extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $name = $api->str('includes', 'users', '0', 'name'); if ($name !== null) { diff --git a/src/Adapters/Wikipedia/Detectors/Description.php b/src/Adapters/Wikipedia/Detectors/Description.php index 102c4d04..f138d56f 100644 --- a/src/Adapters/Wikipedia/Detectors/Description.php +++ b/src/Adapters/Wikipedia/Detectors/Description.php @@ -12,8 +12,7 @@ class Description extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('extract'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect(); diff --git a/src/Adapters/Wikipedia/Detectors/Title.php b/src/Adapters/Wikipedia/Detectors/Title.php index 413c5877..44d8531f 100644 --- a/src/Adapters/Wikipedia/Detectors/Title.php +++ b/src/Adapters/Wikipedia/Detectors/Title.php @@ -13,8 +13,7 @@ class Title extends Detector { public function detect(): ?string { - $extractor = $this->extractor; - $api = $extractor->getApi(); + $api = $this->extractor->getApi(); $result = $api->str('title'); return (is_string($result) && trim($result) !== '') ? $result : parent::detect();