From 21f28ade40aa2d79fafd18b1b58a4dc35136b66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Tue, 31 Dec 2024 11:17:31 +0100 Subject: [PATCH] Improve code --- src/Document.php | 5 ++--- src/FrontMatterChain.php | 2 +- src/Markdown/FrontMatterLeagueCommonMarkExtension.php | 7 +++---- src/Processor/JsonWithoutBracesProcessor.php | 4 ++-- src/Twig/DataToTwigConvertor.php | 4 +--- src/Twig/FrontMatterLoader.php | 11 +++-------- 6 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/Document.php b/src/Document.php index 4c492f7..99d607a 100644 --- a/src/Document.php +++ b/src/Document.php @@ -14,11 +14,10 @@ class Document { - /** @var string */ - private $content; + private string $content; /** @var array */ - private $data; + private array $data; /** * @param array $data diff --git a/src/FrontMatterChain.php b/src/FrontMatterChain.php index 4bf2ca6..96504a6 100644 --- a/src/FrontMatterChain.php +++ b/src/FrontMatterChain.php @@ -17,7 +17,7 @@ final class FrontMatterChain implements FrontMatterInterface { - /** @var FrontMatterInterface[] */ + /** @var list */ private array $adapters = []; public function __construct(iterable $adapters) diff --git a/src/Markdown/FrontMatterLeagueCommonMarkExtension.php b/src/Markdown/FrontMatterLeagueCommonMarkExtension.php index a8908e1..cb13a7d 100644 --- a/src/Markdown/FrontMatterLeagueCommonMarkExtension.php +++ b/src/Markdown/FrontMatterLeagueCommonMarkExtension.php @@ -12,7 +12,7 @@ namespace Webuni\FrontMatter\Markdown; -use Dflydev\DotAccessData\Data; +use Dflydev\DotAccessData\DataInterface; use League\CommonMark\Environment\EnvironmentBuilderInterface; use League\CommonMark\Event\DocumentPreParsedEvent; use League\CommonMark\Extension\ExtensionInterface; @@ -21,8 +21,7 @@ class FrontMatterLeagueCommonMarkExtension implements ExtensionInterface { - /** @var FrontMatterInterface */ - private $frontMatter; + private FrontMatterInterface $frontMatter; public function __construct(FrontMatterInterface $frontMatter) { @@ -40,7 +39,7 @@ public function parse(DocumentPreParsedEvent $event): void $document = $this->frontMatter->parse($content); $data = $event->getDocument()->data; - $data->import($document->getData(), Data::MERGE); + $data->import($document->getData(), DataInterface::MERGE); $event->replaceMarkdown(new MarkdownInput($document->getContent())); } } diff --git a/src/Processor/JsonWithoutBracesProcessor.php b/src/Processor/JsonWithoutBracesProcessor.php index afd93b0..cfb863a 100644 --- a/src/Processor/JsonWithoutBracesProcessor.php +++ b/src/Processor/JsonWithoutBracesProcessor.php @@ -16,7 +16,7 @@ final class JsonWithoutBracesProcessor implements ProcessorInterface { public function parse(string $string): array { - if (false !== strpos($string, '":')) { + if (str_contains($string, '":')) { $string = '{'.$string.'}'; } @@ -31,7 +31,7 @@ public function dump(array $data): string $result = (string) json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); - if ('{' === substr($result, 0, 1) && '}' === substr($result, -1)) { + if (str_starts_with($result, '{') && str_ends_with($result, '}')) { $result = substr($result, 1, -1); } diff --git a/src/Twig/DataToTwigConvertor.php b/src/Twig/DataToTwigConvertor.php index 4085f9b..492dc4b 100644 --- a/src/Twig/DataToTwigConvertor.php +++ b/src/Twig/DataToTwigConvertor.php @@ -79,10 +79,8 @@ public static function var(string $name, bool $force = true): self /** * @psalm-suppress MixedAssignment - * - * @param mixed $value */ - protected static function valueToTwig($value): string + protected static function valueToTwig(mixed $value): string { if ($value instanceof \DateTimeInterface) { return '('.$value->getTimestamp()."|date_modify('0sec'))"; diff --git a/src/Twig/FrontMatterLoader.php b/src/Twig/FrontMatterLoader.php index c374642..eb2867e 100644 --- a/src/Twig/FrontMatterLoader.php +++ b/src/Twig/FrontMatterLoader.php @@ -18,14 +18,9 @@ class FrontMatterLoader implements LoaderInterface { - /** @var LoaderInterface */ - private $loader; - - /** @var FrontMatterInterface */ - private $parser; - - /** @var DataToTwigConvertor */ - private $convertor; + private LoaderInterface $loader; + private FrontMatterInterface $parser; + private DataToTwigConvertor $convertor; public function __construct( FrontMatterInterface $parser,