From 727861ad811aa1325ac96043b2139ac01d3379ae Mon Sep 17 00:00:00 2001 From: core23 Date: Thu, 16 Sep 2021 08:55:10 +0200 Subject: [PATCH] Add type hints --- .../BaseBreadcrumbMenuBlockService.php | 5 +- src/Command/SitemapGeneratorCommand.php | 21 +-- src/Event/BreadcrumbListener.php | 5 +- src/Seo/SeoAwareTrait.php | 4 +- src/Seo/SeoPage.php | 88 ++++----- src/Seo/SeoPageInterface.php | 170 +++--------------- src/Sitemap/SourceManager.php | 11 +- src/Twig/Extension/SeoExtension.php | 12 +- tests/Block/Breadcrumb/BreadcrumbTest.php | 3 +- .../DependencyInjection/ConfigurationTest.php | 4 +- 10 files changed, 85 insertions(+), 238 deletions(-) diff --git a/src/Block/Breadcrumb/BaseBreadcrumbMenuBlockService.php b/src/Block/Breadcrumb/BaseBreadcrumbMenuBlockService.php index 8fe5192a..2de3abbf 100644 --- a/src/Block/Breadcrumb/BaseBreadcrumbMenuBlockService.php +++ b/src/Block/Breadcrumb/BaseBreadcrumbMenuBlockService.php @@ -28,10 +28,7 @@ */ abstract class BaseBreadcrumbMenuBlockService extends AbstractBlockService implements BreadcrumbInterface { - /** - * @var FactoryInterface - */ - private $factory; + private FactoryInterface $factory; public function __construct(Environment $twig, FactoryInterface $factory) { diff --git a/src/Command/SitemapGeneratorCommand.php b/src/Command/SitemapGeneratorCommand.php index 56fbb3e3..23bd5c68 100644 --- a/src/Command/SitemapGeneratorCommand.php +++ b/src/Command/SitemapGeneratorCommand.php @@ -33,25 +33,16 @@ */ final class SitemapGeneratorCommand extends Command { - /** - * @var RouterInterface - */ - private $router; + private RouterInterface $router; - /** - * @var SourceManager - */ - private $sitemapManager; + private SourceManager $sitemapManager; - /** - * @var Filesystem - */ - private $filesystem; + private Filesystem $filesystem; public function __construct( - ?RouterInterface $router, - ?SourceManager $sitemapManager, - ?Filesystem $filesystem + RouterInterface $router, + SourceManager $sitemapManager, + Filesystem $filesystem ) { $this->router = $router; $this->sitemapManager = $sitemapManager; diff --git a/src/Event/BreadcrumbListener.php b/src/Event/BreadcrumbListener.php index 846d6c09..61aa6193 100644 --- a/src/Event/BreadcrumbListener.php +++ b/src/Event/BreadcrumbListener.php @@ -24,10 +24,7 @@ */ final class BreadcrumbListener { - /** - * @var array - */ - private $blockServices = []; + private array $blockServices = []; /** * Add a renderer to the status services list. diff --git a/src/Seo/SeoAwareTrait.php b/src/Seo/SeoAwareTrait.php index 2ad776ae..70cd3705 100644 --- a/src/Seo/SeoAwareTrait.php +++ b/src/Seo/SeoAwareTrait.php @@ -16,11 +16,9 @@ trait SeoAwareTrait { /** - * @var SeoPageInterface|null - * * @required */ - protected $seoPage; + protected ?SeoPageInterface $seoPage; public function setSeoPage(?SeoPageInterface $seoPage = null): void { diff --git a/src/Seo/SeoPage.php b/src/Seo/SeoPage.php index f292a0c9..11c8384d 100644 --- a/src/Seo/SeoPage.php +++ b/src/Seo/SeoPage.php @@ -18,50 +18,38 @@ */ final class SeoPage implements SeoPageInterface { - /** - * @var string - */ - private $title; + private string $title; /** * @var array> */ - private $metas; + private array $metas; /** * @var array */ - private $htmlAttributes; + private array $htmlAttributes; - /** - * @var string - */ - private $linkCanonical; + private string $linkCanonical; - /** - * @var string - */ - private $separator; + private string $separator; /** * @var array */ - private $headAttributes; + private array $headAttributes; /** * @var array */ - private $langAlternates; + private array $langAlternates; /** * @var array */ - private $oembedLinks; + private array $oembedLinks; - /** - * @param string $title - */ - public function __construct($title = '') + public function __construct(string $title = '') { $this->title = $title; $this->metas = [ @@ -80,31 +68,31 @@ public function __construct($title = '') $this->oembedLinks = []; } - public function setTitle($title) + public function setTitle(string $title): self { $this->title = $title; return $this; } - public function addTitle($title) + public function addTitle(string $title): self { $this->title = $title.$this->separator.$this->title; return $this; } - public function getTitle() + public function getTitle(): string { return $this->title; } - public function getMetas() + public function getMetas(): array { return $this->metas; } - public function addMeta($type, $name, $value, array $extras = []) + public function addMeta(string $type, string $name, string $value, array $extras = []): self { if (!\is_string($value)) { @trigger_error(sprintf( @@ -123,19 +111,19 @@ public function addMeta($type, $name, $value, array $extras = []) return $this; } - public function hasMeta($type, $name) + public function hasMeta(string $type, string $name): bool { return isset($this->metas[$type][$name]); } - public function removeMeta($type, $name) + public function removeMeta(string $type, string $name): self { unset($this->metas[$type][$name]); return $this; } - public function setMetas(array $metas) + public function setMetas(array $metas): self { $this->metas = []; @@ -154,76 +142,76 @@ public function setMetas(array $metas) return $this; } - public function setHtmlAttributes(array $attributes) + public function setHtmlAttributes(array $attributes): self { $this->htmlAttributes = $attributes; return $this; } - public function addHtmlAttributes($name, $value) + public function addHtmlAttributes(string $name, string $value): self { $this->htmlAttributes[$name] = $value; return $this; } - public function removeHtmlAttributes($name) + public function removeHtmlAttributes(string $name): self { unset($this->htmlAttributes[$name]); return $this; } - public function getHtmlAttributes() + public function getHtmlAttributes(): array { return $this->htmlAttributes; } - public function hasHtmlAttribute($name) + public function hasHtmlAttribute(string $name): bool { return isset($this->htmlAttributes[$name]); } - public function setHeadAttributes(array $attributes) + public function setHeadAttributes(array $attributes): self { $this->headAttributes = $attributes; return $this; } - public function addHeadAttribute($name, $value) + public function addHeadAttribute(string $name, string $value): self { $this->headAttributes[$name] = $value; return $this; } - public function removeHeadAttribute($name) + public function removeHeadAttribute(string $name): self { unset($this->headAttributes[$name]); return $this; } - public function getHeadAttributes() + public function getHeadAttributes(): array { return $this->headAttributes; } - public function hasHeadAttribute($name) + public function hasHeadAttribute(string $name): bool { return isset($this->headAttributes[$name]); } - public function setLinkCanonical($link) + public function setLinkCanonical(string $link): self { $this->linkCanonical = $link; return $this; } - public function getLinkCanonical() + public function getLinkCanonical(): string { return $this->linkCanonical; } @@ -233,58 +221,58 @@ public function removeLinkCanonical(): void $this->linkCanonical = ''; } - public function setSeparator($separator) + public function setSeparator(string $separator): self { $this->separator = $separator; return $this; } - public function setLangAlternates(array $langAlternates) + public function setLangAlternates(array $langAlternates): self { $this->langAlternates = $langAlternates; return $this; } - public function addLangAlternate($href, $hrefLang) + public function addLangAlternate(string $href, string $hrefLang): self { $this->langAlternates[$href] = $hrefLang; return $this; } - public function removeLangAlternate($href) + public function removeLangAlternate(string $href): self { unset($this->langAlternates[$href]); return $this; } - public function hasLangAlternate($href) + public function hasLangAlternate(string $href): bool { return isset($this->langAlternates[$href]); } - public function getLangAlternates() + public function getLangAlternates(): array { return $this->langAlternates; } - public function addOEmbedLink($title, $link) + public function addOEmbedLink(string $title, string $link): self { $this->oembedLinks[$title] = $link; return $this; } - public function getOEmbedLinks() + public function getOEmbedLinks(): array { return $this->oembedLinks; } /** - * @param string|array $meta + * @param array|string $meta */ private function normalize($meta): array { diff --git a/src/Seo/SeoPageInterface.php b/src/Seo/SeoPageInterface.php index 1b2d2a11..f3609107 100644 --- a/src/Seo/SeoPageInterface.php +++ b/src/Seo/SeoPageInterface.php @@ -13,205 +13,91 @@ namespace Sonata\SeoBundle\Seo; -/** - * @method SeoPageInterface removeMeta(string $type, string $name) - * @method SeoPageInterface removeHtmlAttributes(string $name) - * @method bool hasHtmlAttribute(string $name) - * @method SeoPageInterface removeHeadAttribute(string $name) - * @method bool hasHeadAttribute(string $name) - * @method SeoPageInterface removeLangAlternate(string $href) - * @method bool hasLangAlternate(string $href) - */ interface SeoPageInterface { - /** - * @param string $title - * - * @return SeoPageInterface - */ - public function setTitle($title); + public function setTitle(string $title): self; - /** - * @param string $title - * - * @return SeoPageInterface - */ - public function addTitle($title); + public function addTitle(string $title): self; - /** - * @return string - */ - public function getTitle(); + public function getTitle(): string; /** - * @param string $type - * @param string $name - * @param string $value * @param array $extras - * - * @return SeoPageInterface */ - public function addMeta($type, $name, $value, array $extras = []); + public function addMeta(string $type, string $name, string $value, array $extras = []): self; - /** - * @param string $type - * @param string $name - * - * @return bool - */ - public function hasMeta($type, $name); + public function hasMeta(string $type, string $name): bool; /** * @return array> */ - public function getMetas(); + public function getMetas(): array; /** * @param array> $metas - * - * @return SeoPageInterface */ - public function setMetas(array $metas); + public function setMetas(array $metas): self; /** * @param array $attributes - * - * @return SeoPageInterface */ - public function setHtmlAttributes(array $attributes); + public function setHtmlAttributes(array $attributes): self; - /** - * @param string $name - * @param string $value - * - * @return SeoPageInterface - */ - public function addHtmlAttributes($name, $value); + public function addHtmlAttributes(string $name, string $value): self; /** * @return array */ - public function getHtmlAttributes(); + public function getHtmlAttributes(): array; /** * @param array $attributes - * - * @return SeoPageInterface */ - public function setHeadAttributes(array $attributes); + public function setHeadAttributes(array $attributes): self; - /** - * @param string $name - * @param string $value - * - * @return SeoPageInterface - */ - public function addHeadAttribute($name, $value); + public function addHeadAttribute(string $name, string $value): self; /** * @return array */ - public function getHeadAttributes(); + public function getHeadAttributes(): array; - /** - * @param string $link - * - * @return SeoPageInterface - */ - public function setLinkCanonical($link); + public function setLinkCanonical(string $link): self; - /** - * @return string - */ - public function getLinkCanonical(); + public function getLinkCanonical(): string; - /** - * @param string $separator - * - * @return SeoPageInterface - */ - public function setSeparator($separator); + public function setSeparator(string $separator): self; /** * @param array $langAlternates - * - * @return SeoPageInterface */ - public function setLangAlternates(array $langAlternates); + public function setLangAlternates(array $langAlternates): self; - /** - * @param string $href - * @param string $hrefLang - * - * @return SeoPageInterface - */ - public function addLangAlternate($href, $hrefLang); + public function addLangAlternate(string $href, string $hrefLang): self; /** * @return array */ - public function getLangAlternates(); + public function getLangAlternates(): array; - /** - * @param string $title - * @param string $link - * - * @return SeoPageInterface - */ - public function addOEmbedLink($title, $link); + public function addOEmbedLink(string $title, string $link): self; /** * @return array */ - public function getOEmbedLinks(); + public function getOEmbedLinks(): array; - /** - * @param string $type - * @param string $name - * - * @return $this - */ - public function removeMeta($type, $name); + public function removeMeta(string $type, string $name): self; - /** - * @param string $name - * - * @return $this - */ - public function removeHtmlAttributes($name); + public function removeHtmlAttributes(string $name): self; - /** - * @param string $name - * - * @return bool - */ - public function hasHtmlAttribute($name); + public function hasHtmlAttribute(string $name): bool; - /** - * @param string $name - * - * @return $this - */ - public function removeHeadAttribute($name); + public function removeHeadAttribute(string $name): self; - /** - * @param string $name - * - * @return bool - */ - public function hasHeadAttribute($name); + public function hasHeadAttribute(string $name): bool; - /** - * @param string $href - * - * @return $this - */ - public function removeLangAlternate($href); + public function removeLangAlternate(string $href): self; - /** - * @param string $href - * - * @return bool - */ - public function hasLangAlternate($href); + public function hasLangAlternate(string $href): bool; } diff --git a/src/Sitemap/SourceManager.php b/src/Sitemap/SourceManager.php index 83e13296..d83c562f 100644 --- a/src/Sitemap/SourceManager.php +++ b/src/Sitemap/SourceManager.php @@ -21,10 +21,7 @@ */ final class SourceManager implements SourceIteratorInterface { - /** - * @var \ArrayIterator - */ - private $sources; + private \ArrayIterator $sources; public function __construct() { @@ -33,10 +30,8 @@ public function __construct() /** * Adding source with his group. - * - * @param string $group */ - public function addSource($group, SourceIteratorInterface $source, array $types = []): void + public function addSource(string $group, SourceIteratorInterface $source, array $types = []): void { if (!isset($this->sources[$group])) { $this->sources[$group] = new \stdClass(); @@ -67,7 +62,7 @@ public function key() return $this->sources->key(); } - public function valid() + public function valid(): bool { return $this->sources->valid(); } diff --git a/src/Twig/Extension/SeoExtension.php b/src/Twig/Extension/SeoExtension.php index d880743f..0c31c9c5 100644 --- a/src/Twig/Extension/SeoExtension.php +++ b/src/Twig/Extension/SeoExtension.php @@ -19,15 +19,9 @@ final class SeoExtension extends AbstractExtension { - /** - * @var SeoPageInterface - */ - private $page; + private SeoPageInterface $page; - /** - * @var string - */ - private $encoding; + private string $encoding; public function __construct(SeoPageInterface $page, string $encoding) { @@ -35,7 +29,7 @@ public function __construct(SeoPageInterface $page, string $encoding) $this->encoding = $encoding; } - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('sonata_seo_title', [$this, 'getTitle'], ['is_safe' => ['html']]), diff --git a/tests/Block/Breadcrumb/BreadcrumbTest.php b/tests/Block/Breadcrumb/BreadcrumbTest.php index 344ada41..70bf8ea8 100644 --- a/tests/Block/Breadcrumb/BreadcrumbTest.php +++ b/tests/Block/Breadcrumb/BreadcrumbTest.php @@ -14,6 +14,7 @@ namespace Sonata\SeoBundle\Tests\Block\Breadcrumb; use Knp\Menu\FactoryInterface; +use Knp\Menu\ItemInterface; use Knp\Menu\MenuFactory; use Sonata\BlockBundle\Block\BlockContext; use Sonata\BlockBundle\Block\BlockContextInterface; @@ -30,7 +31,7 @@ protected function getContext(): string return 'test'; } - protected function getMenu(BlockContextInterface $blockContext) + protected function getMenu(BlockContextInterface $blockContext): ItemInterface { return $this->getRootMenu($blockContext); } diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 6ab8adde..46634336 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -82,7 +82,7 @@ public function testWithYamlConfig(): void static::assertSame('website', $config['page']['metas']['property']['og:type']); } - private function getDefaultConfiguration() + private function getDefaultConfiguration(): array { return [ 'page' => [ @@ -100,7 +100,7 @@ private function getDefaultConfiguration() ]; } - private function processConfiguration(array $configs) + private function processConfiguration(array $configs): array { $configuration = new Configuration(); $processor = new Processor();