From 9cb583995275e5b611a1df70e75b5a2c5e75dafa Mon Sep 17 00:00:00 2001 From: Danil Ovchinnikov Date: Sat, 20 Jan 2024 12:36:10 +0700 Subject: [PATCH] More typing in the project --- .../AtomFeedGenerator.php | 37 ++++++++++--------- .../DependencyInjection/Configuration.php | 2 +- .../SculpinAtomFeedGeneratorExtension.php | 2 +- .../DependencyInjection/Configuration.php | 2 +- .../SculpinSharingImageGeneratorExtension.php | 2 +- .../SharingImageGenerator.php | 16 ++++---- .../TwigSharingImageGeneratorExtension.php | 11 +++--- app/src/Seo/SharingImageGenerator.php | 30 +++++++-------- app/src/Sitemap/Author.php | 13 +++---- app/src/Sitemap/Entry.php | 24 ++++++------ app/src/Sitemap/SitemapGenerator.php | 26 ++++++++++--- 11 files changed, 90 insertions(+), 75 deletions(-) diff --git a/app/src/Bundles/AtomFeedGeneratorBundle/AtomFeedGenerator.php b/app/src/Bundles/AtomFeedGeneratorBundle/AtomFeedGenerator.php index e18b27e2..df1bd304 100644 --- a/app/src/Bundles/AtomFeedGeneratorBundle/AtomFeedGenerator.php +++ b/app/src/Bundles/AtomFeedGeneratorBundle/AtomFeedGenerator.php @@ -14,7 +14,12 @@ class AtomFeedGenerator implements EventSubscriberInterface { - protected Configuration $configuration; + private Configuration $configuration; + + public function __construct(Configuration $configuration) + { + $this->configuration = $configuration; + } public static function getSubscribedEvents(): array { @@ -23,11 +28,6 @@ public static function getSubscribedEvents(): array ]; } - public function __construct(Configuration $configuration) - { - $this->configuration = $configuration; - } - public function afterRun(SourceSetEvent $sourceSetEvent): void { $sourceSet = $sourceSetEvent->sourceSet(); @@ -75,22 +75,23 @@ public function afterRun(SourceSetEvent $sourceSetEvent): void $this->generateFeed($entries); } + /** + * @param Author[] $entries + */ protected function fetchEntry(Configuration $data, array $authors): Entry { $baseUrl = $this->configuration->get('url') ?? 'http://localhost'; - $post = new Entry(); - - $post->title = $data->get('title'); - $post->link = $baseUrl . $data->get('url'); - $post->authors = $authors; - $post->description = $data->get('blocks.content'); - $post->published_at = new \DateTimeImmutable($data->get('published_at')); - - return $post; + return new Entry( + $data->get('title'), + $baseUrl . $data->get('url'), + $authors, + $data->get('blocks.content'), + new \DateTimeImmutable($data->get('published_at')), + ); } - protected function slug($author): string + protected function slug(string $author): string { return mb_strtolower(preg_replace('/\W/', '_', $author)); } @@ -100,10 +101,10 @@ protected function generateFeed(array $entries = []): void foreach ($entries as $filePath => $posts) { $rss = new SitemapGenerator(); if ($title = $this->configuration->get('title')) { - $rss->title = $title; + $rss->setTitle($title); } if ($subtitle = $this->configuration->get('subtitle')) { - $rss->description = $subtitle; + $rss->setDescription($subtitle); } foreach ($posts as $post) { diff --git a/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/Configuration.php b/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/Configuration.php index 1dab63e7..05e0a76e 100644 --- a/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/Configuration.php +++ b/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/Configuration.php @@ -7,7 +7,7 @@ class Configuration implements ConfigurationInterface { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { return new TreeBuilder('sculpin_atom_feed_generator'); } diff --git a/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/SculpinAtomFeedGeneratorExtension.php b/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/SculpinAtomFeedGeneratorExtension.php index 2ebe606c..fa3ee38c 100644 --- a/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/SculpinAtomFeedGeneratorExtension.php +++ b/app/src/Bundles/AtomFeedGeneratorBundle/DependencyInjection/SculpinAtomFeedGeneratorExtension.php @@ -9,7 +9,7 @@ class SculpinAtomFeedGeneratorExtension extends Extension { - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.xml'); diff --git a/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/Configuration.php b/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/Configuration.php index 7392b8ec..82e3fb74 100644 --- a/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/Configuration.php +++ b/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/Configuration.php @@ -7,7 +7,7 @@ class Configuration implements ConfigurationInterface { - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { return new TreeBuilder('sculpin_sharing_image_generator'); } diff --git a/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/SculpinSharingImageGeneratorExtension.php b/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/SculpinSharingImageGeneratorExtension.php index 80238ff3..ee004ced 100644 --- a/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/SculpinSharingImageGeneratorExtension.php +++ b/app/src/Bundles/SharingImageGeneratorBundle/DependencyInjection/SculpinSharingImageGeneratorExtension.php @@ -9,7 +9,7 @@ class SculpinSharingImageGeneratorExtension extends Extension { - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.xml'); diff --git a/app/src/Bundles/SharingImageGeneratorBundle/SharingImageGenerator.php b/app/src/Bundles/SharingImageGeneratorBundle/SharingImageGenerator.php index 5b23b5ce..4867b3bf 100644 --- a/app/src/Bundles/SharingImageGeneratorBundle/SharingImageGenerator.php +++ b/app/src/Bundles/SharingImageGeneratorBundle/SharingImageGenerator.php @@ -11,21 +11,21 @@ class SharingImageGenerator implements EventSubscriberInterface { - protected $configuration; + private Configuration $configuration; - public static function getSubscribedEvents() + public function __construct(Configuration $configuration) { - return [ - Sculpin::EVENT_BEFORE_RUN => 'beforeRun', - ]; + $this->configuration = $configuration; } - public function __construct(Configuration $configuration) + public static function getSubscribedEvents(): array { - $this->configuration = $configuration; + return [ + Sculpin::EVENT_BEFORE_RUN => 'beforeRun', + ]; } - public function beforeRun(SourceSetEvent $sourceSetEvent) + public function beforeRun(SourceSetEvent $sourceSetEvent): void { $sourceSet = $sourceSetEvent->sourceSet(); diff --git a/app/src/Bundles/SharingImageGeneratorBundle/TwigSharingImageGeneratorExtension.php b/app/src/Bundles/SharingImageGeneratorBundle/TwigSharingImageGeneratorExtension.php index dc6e5d8d..6ab784ae 100644 --- a/app/src/Bundles/SharingImageGeneratorBundle/TwigSharingImageGeneratorExtension.php +++ b/app/src/Bundles/SharingImageGeneratorBundle/TwigSharingImageGeneratorExtension.php @@ -2,32 +2,33 @@ namespace App\Bundles\SharingImageGeneratorBundle; +use Dflydev\DotAccessConfiguration\Configuration; use Symfony\Component\Filesystem\Filesystem; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; class TwigSharingImageGeneratorExtension extends AbstractExtension { - protected $configuration; + private Configuration $configuration; - public function __construct($configuration) + public function __construct(Configuration $configuration) { $this->configuration = $configuration; } - public function getName() + public function getName(): string { return 'sharing_image_generator'; } - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('sharing_image', [$this, 'getSharingImage']), ]; } - public function getSharingImage($filename) + public function getSharingImage(string $filename): string { $filesystem = new Filesystem(); diff --git a/app/src/Seo/SharingImageGenerator.php b/app/src/Seo/SharingImageGenerator.php index 2aa2f028..100255ed 100644 --- a/app/src/Seo/SharingImageGenerator.php +++ b/app/src/Seo/SharingImageGenerator.php @@ -4,39 +4,39 @@ use GdImage; -class SharingImageGenerator +final class SharingImageGenerator { - public const int IMAGE_WIDTH = 1600; + private const int IMAGE_WIDTH = 1600; - public const int IMAGE_HEIGHT = 900; + private const int IMAGE_HEIGHT = 900; - public const int IMAGE_MARGINS = 72; + private const int IMAGE_MARGINS = 72; - public const int TITLE_FONT_SIZE = 72; + private const int TITLE_FONT_SIZE = 72; - public const int AUTHOR_FONT_SIZE = 24; + private const int AUTHOR_FONT_SIZE = 24; - protected bool $inverse = false; + private bool $inverse = false; - protected string $title; + private string $title; - protected ?string $author = null; + private ?string $author = null; - public function setTitle($title): SharingImageGenerator + public function setTitle(string $title): self { $this->title = $title; return $this; } - public function setAuthor($author): SharingImageGenerator + public function setAuthor(?string $author): self { $this->author = $author; return $this; } - public function setInverse($inverse): SharingImageGenerator + public function setInverse(bool $inverse): self { $this->inverse = $inverse; @@ -52,7 +52,7 @@ public function output(): void imagedestroy($image); } - public function save($path): void + public function save(string $path): void { $image = $this->prepare(); @@ -60,7 +60,7 @@ public function save($path): void imagedestroy($image); } - protected function prepare(): GdImage + private function prepare(): GdImage { $image = imagecreate(self::IMAGE_WIDTH, self::IMAGE_HEIGHT); @@ -92,7 +92,7 @@ protected function prepare(): GdImage $title ); - if ($this->author) { + if ($this->author !== null) { $author = wordwrap($this->author, 40); imagettftext( diff --git a/app/src/Sitemap/Author.php b/app/src/Sitemap/Author.php index 35a459f3..c6c1e205 100644 --- a/app/src/Sitemap/Author.php +++ b/app/src/Sitemap/Author.php @@ -2,15 +2,12 @@ namespace App\Sitemap; -class Author +readonly class Author { - public function __construct($name, $url) + public function __construct( + public string $name, + public string $url + ) { - $this->name = $name; - $this->url = $url; } - - public string $name; - - public string $url; } diff --git a/app/src/Sitemap/Entry.php b/app/src/Sitemap/Entry.php index 04998fbb..9a344d6c 100644 --- a/app/src/Sitemap/Entry.php +++ b/app/src/Sitemap/Entry.php @@ -2,16 +2,18 @@ namespace App\Sitemap; -class Entry +readonly class Entry { - public string $title; - - public string $link; - - /** @var Author[] */ - public array $authors = []; - - public string $description; - - public \DateTimeImmutable $published_at; + /** + * @param Author[] $authors + */ + public function __construct( + public string $title, + public string $link, + public array $authors, + public string $description, + public \DateTimeImmutable $published_at, + ) + { + } } diff --git a/app/src/Sitemap/SitemapGenerator.php b/app/src/Sitemap/SitemapGenerator.php index acf41334..4180efce 100644 --- a/app/src/Sitemap/SitemapGenerator.php +++ b/app/src/Sitemap/SitemapGenerator.php @@ -4,20 +4,35 @@ use DOMDocument; -class SitemapGenerator +final class SitemapGenerator { - public string $title; + private string $title; - public string $description; + private string $description; - protected array $entries = []; + /** @var Entry[] */ + private array $entries = []; + + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } public function addEntry(Entry $entry): void { $this->entries[] = $entry; } - public function saveFeed($path): void + public function saveFeed(string $path): void { $dom = new DOMDocument('1.0', 'utf-8'); $feed = $dom->createElement('feed'); @@ -32,7 +47,6 @@ public function saveFeed($path): void $feed->append($dom->createElement('updated', date('c'))); - /** @var Entry $entry */ foreach ($this->entries as $entry) { $element = $dom->createElement('entry');