From 919f30efff9e4160337d02410b7e692305ab8d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 30 Dec 2020 10:50:11 +0100 Subject: [PATCH] Fix: Remove Parsed\FrontMatter and Parsed\Content value objects --- CHANGELOG.md | 17 +++- Makefile | 3 +- README.md | 2 +- phpstan-baseline.neon | 11 +-- psalm-baseline.xml | 15 +-- src/Parsed.php | 8 +- src/Parsed/Content.php | 34 ------- src/Parsed/FrontMatter.php | 60 ------------ src/YamlParser.php | 20 +++- test/Unit/Parsed/ContentTest.php | 39 -------- test/Unit/Parsed/FrontMatterTest.php | 60 ------------ test/Unit/ParsedTest.php | 9 +- test/Unit/YamlParserTest.php | 132 ++++++++++----------------- 13 files changed, 87 insertions(+), 323 deletions(-) delete mode 100644 src/Parsed/Content.php delete mode 100644 src/Parsed/FrontMatter.php delete mode 100644 test/Unit/Parsed/ContentTest.php delete mode 100644 test/Unit/Parsed/FrontMatterTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 3056ac5e..894cbb62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased -For a full diff see [`0.1.0...main`][0.1.0...main]. +For a full diff see [`0.2.0...main`][0.2.0...main]. + +## [`0.2.0`][0.2.0] + +For a full diff see [`0.1.0...0.2.0`][0.1.0...0.2.0]. + +### Removed + +* Removed `Parsed\FrontMatter` and `Parsed\Content` ([#8]), by [@localheinz] ## [`0.1.0`][0.1.0] @@ -16,11 +24,14 @@ For a full diff see [`4e97e14...0.1.0`][4e97e14...0.1.0]. * Added `YamlParser` ([#2]), by [@localheinz] -[0.1.0]: https://github.com/ergebnis/front-matter/releases/tag/1.0.0 +[0.1.0]: https://github.com/ergebnis/front-matter/releases/tag/0.1.0 +[0.2.0]: https://github.com/ergebnis/front-matter/releases/tag/0.2.0 [4e97e14...0.1.0]: https://github.com/ergebnis/front-matter/compare/4e97e14...0.1.0 -[0.1.0...main]: https://github.com/ergebnis/front-matter/compare/0.1.0...main +[0.1.0...0.2.0]: https://github.com/ergebnis/front-matter/compare/0.1.0...0.2.0 +[0.2.0...main]: https://github.com/ergebnis/front-matter/compare/0.2.0...main [#2]: https://github.com/ergebnis/front-matter/pull/2 +[#8]: https://github.com/ergebnis/front-matter/pull/8 [@localheinz]: https://github.com/localheinz diff --git a/Makefile b/Makefile index 8cd1dd39..9e7ae568 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,8 @@ static-code-analysis: vendor ## Runs a static code analysis with phpstan/phpstan .PHONY: static-code-analysis-baseline static-code-analysis-baseline: vendor ## Generates a baseline for static code analysis with phpstan/phpstan and vimeo/psalm mkdir -p .build/phpstan - vendor/bin/phpstan analyze --configuration=phpstan.neon --generate-baseline=phpstan-baseline.neon --memory-limit=-1 + echo '' > phpstan-baseline.neon + vendor/bin/phpstan analyze --configuration=phpstan.neon --error-format=baselineNeon --memory-limit=-1 > phpstan-baseline.neon || true mkdir -p .build/psalm vendor/bin/psalm --config=psalm.xml --set-baseline=psalm-baseline.xml diff --git a/README.md b/README.md index bfc04ced..94468d39 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ $parsed = $parser->parse($valueWithoutFrontMatter); - the front matter cannot be parsed because it is invalid YAML - the front matter data does not describe an associative array -:bulb: When the value does not contain front matter or contains valid YAML front matter, the `YamlParser` returns an [`Ergebnis\FrontMatter\Parsed`](src/Parsed.php) value object. This object composes an instance of [`Ergebnis\FrontMatter\Parsed\FrontMatter`](src/Parsed/FrontMatter.php) and an instance of [`Ergebnis\FrontMatter\Parsed\Content`](src/Parsed/Content.php). +:bulb: When the value does not contain front matter or contains valid YAML front matter, the `YamlParser` returns an [`Ergebnis\FrontMatter\Parsed`](src/Parsed.php) value object. This object composes the parsed front matter and the content after the front matter. ## Changelog diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index dfe486ce..846a8dfe 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,12 +1,3 @@ parameters: - ignoreErrors: - - - message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#" - count: 1 - path: src/Parsed/FrontMatter.php - - - - message: "#^Parameter \\#1 \\$value of static method Ergebnis\\\\FrontMatter\\\\Parsed\\\\FrontMatter\\:\\:fromArray\\(\\) expects array\\, array\\ given\\.$#" - count: 1 - path: test/Unit/Parsed/FrontMatterTest.php + ignoreErrors: [] diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3066b59f..73c95cf8 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,21 +1,8 @@ - - - - return !\is_string($key); - - + - - (array) $data - $data - - - $value - - diff --git a/src/Parsed.php b/src/Parsed.php index d2fe7402..6f95465d 100644 --- a/src/Parsed.php +++ b/src/Parsed.php @@ -19,13 +19,13 @@ final class Parsed private $content; - private function __construct(Parsed\FrontMatter $frontMatter, Parsed\Content $content) + private function __construct(array $frontMatter, string $content) { $this->frontMatter = $frontMatter; $this->content = $content; } - public static function fromFrontMatterAndContent(Parsed\FrontMatter $frontMatter, Parsed\Content $content): self + public static function fromFrontMatterAndContent(array $frontMatter, string $content): self { return new self( $frontMatter, @@ -33,12 +33,12 @@ public static function fromFrontMatterAndContent(Parsed\FrontMatter $frontMatter ); } - public function frontMatter(): Parsed\FrontMatter + public function frontMatter(): array { return $this->frontMatter; } - public function content(): Parsed\Content + public function content(): string { return $this->content; } diff --git a/src/Parsed/Content.php b/src/Parsed/Content.php deleted file mode 100644 index f5fab3de..00000000 --- a/src/Parsed/Content.php +++ /dev/null @@ -1,34 +0,0 @@ -value = $value; - } - - public static function fromString(string $value): self - { - return new self($value); - } - - public function toString(): string - { - return $this->value; - } -} diff --git a/src/Parsed/FrontMatter.php b/src/Parsed/FrontMatter.php deleted file mode 100644 index 6fab3c2c..00000000 --- a/src/Parsed/FrontMatter.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ - private $value; - - /** - * @param array $value - */ - private function __construct(array $value) - { - $this->value = $value; - } - - /** - * @param array $value - * - * @throws Exception\InvalidFrontMatter - */ - public static function fromArray(array $value): self - { - $keys = \array_keys($value); - - $invalidKeys = \array_filter($keys, static function ($key): bool { - return !\is_string($key); - }); - - if ([] !== $invalidKeys) { - throw Exception\InvalidFrontMatter::keysCanNotBeNumeric(); - } - - return new self($value); - } - - /** - * @return array - */ - public function toArray(): array - { - return $this->value; - } -} diff --git a/src/YamlParser.php b/src/YamlParser.php index 4ca00d5c..3b4376ea 100644 --- a/src/YamlParser.php +++ b/src/YamlParser.php @@ -28,18 +28,18 @@ public function parse(string $value): Parsed { if (\preg_match(self::PATTERN, $value, $matches) !== 1) { return Parsed::fromFrontMatterAndContent( - Parsed\FrontMatter::fromArray([]), - Parsed\Content::fromString($value) + [], + $value ); } - $content = Parsed\Content::fromString($matches['content']); + $content = $matches['content']; $rawfrontMatter = $matches['frontMatter']; if ('' === $rawfrontMatter) { return Parsed::fromFrontMatterAndContent( - Parsed\FrontMatter::fromArray([]), + [], $content ); } @@ -50,7 +50,17 @@ public function parse(string $value): Parsed throw Exception\InvalidFrontMatter::create(); } - $frontMatter = Parsed\FrontMatter::fromArray((array) $data); + $frontMatter = (array) $data; + + $keys = \array_keys($frontMatter); + + $keysThatAreNotStrings = \array_filter($keys, static function ($key): bool { + return !\is_string($key); + }); + + if ([] !== $keysThatAreNotStrings) { + throw Exception\InvalidFrontMatter::keysCanNotBeNumeric(); + } return Parsed::fromFrontMatterAndContent( $frontMatter, diff --git a/test/Unit/Parsed/ContentTest.php b/test/Unit/Parsed/ContentTest.php deleted file mode 100644 index 60293fd4..00000000 --- a/test/Unit/Parsed/ContentTest.php +++ /dev/null @@ -1,39 +0,0 @@ -toString()); - } -} diff --git a/test/Unit/Parsed/FrontMatterTest.php b/test/Unit/Parsed/FrontMatterTest.php deleted file mode 100644 index 15e7ed99..00000000 --- a/test/Unit/Parsed/FrontMatterTest.php +++ /dev/null @@ -1,60 +0,0 @@ -word, - $faker->word, - $faker->word, - ]; - - $this->expectException(Exception\InvalidFrontMatter::class); - - FrontMatter::fromArray($value); - } - - public function testFromArrayReturnsFrontMatter(): void - { - $faker = self::faker(); - - $value = [ - 'layout' => $faker->word, - 'title' => $faker->sentence, - ]; - - $frontMatter = FrontMatter::fromArray($value); - - self::assertSame($value, $frontMatter->toArray()); - } -} diff --git a/test/Unit/ParsedTest.php b/test/Unit/ParsedTest.php index db69f17b..a588f30e 100644 --- a/test/Unit/ParsedTest.php +++ b/test/Unit/ParsedTest.php @@ -21,9 +21,6 @@ * @internal * * @covers \Ergebnis\FrontMatter\Parsed - * - * @uses \Ergebnis\FrontMatter\Parsed\Content - * @uses \Ergebnis\FrontMatter\Parsed\FrontMatter */ final class ParsedTest extends Framework\TestCase { @@ -33,13 +30,13 @@ public function testFromFrontMatterAndContentReturnsParsed(): void { $faker = self::faker(); - $frontMatter = Parsed\FrontMatter::fromArray([ + $frontMatter = [ 'foo' => $faker->words, 'bar' => $faker->randomFloat(), 'baz' => $faker->randomNumber(), - ]); + ]; - $content = Parsed\Content::fromString($faker->realText()); + $content = $faker->realText(); $parsed = Parsed::fromFrontMatterAndContent( $frontMatter, diff --git a/test/Unit/YamlParserTest.php b/test/Unit/YamlParserTest.php index 303d28be..52a94dd5 100644 --- a/test/Unit/YamlParserTest.php +++ b/test/Unit/YamlParserTest.php @@ -14,7 +14,6 @@ namespace Ergebnis\FrontMatter\Test\Unit; use Ergebnis\FrontMatter\Exception; -use Ergebnis\FrontMatter\Parsed; use Ergebnis\FrontMatter\YamlParser; use Ergebnis\Test\Util; use PHPUnit\Framework; @@ -26,8 +25,6 @@ * * @uses \Ergebnis\FrontMatter\Exception\InvalidFrontMatter * @uses \Ergebnis\FrontMatter\Parsed - * @uses \Ergebnis\FrontMatter\Parsed\Content - * @uses \Ergebnis\FrontMatter\Parsed\FrontMatter */ final class YamlParserTest extends Framework\TestCase { @@ -319,15 +316,12 @@ public function testParseReturnsParsedWhenValueIsFrontMatterDelimiter(): void --- TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString($value); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals($value, $parsed->content()); } public function testParseReturnsParsedWhenValueIsFrontMatterDelimiterWithTrailingWhitespace(): void @@ -337,15 +331,12 @@ public function testParseReturnsParsedWhenValueIsFrontMatterDelimiterWithTrailin TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString($value); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals($value, $parsed->content()); } public function testParseReturnsParsedWhenValueIsFrontMatterDelimiterWithContent(): void @@ -357,15 +348,12 @@ public function testParseReturnsParsedWhenValueIsFrontMatterDelimiterWithContent TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString($value); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals($value, $parsed->content()); } public function testParseReturnsParsedWhenValueIsFrontMatterDelimiterWithTrailingWhitespaceAndContent(): void @@ -378,15 +366,12 @@ public function testParseReturnsParsedWhenValueIsFrontMatterDelimiterWithTrailin TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString($value); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals($value, $parsed->content()); } public function testParseReturnsParsedWhenValueIsAlmostEmptyFrontMatter(): void @@ -396,15 +381,12 @@ public function testParseReturnsParsedWhenValueIsAlmostEmptyFrontMatter(): void -- TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString($value); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals($value, $parsed->content()); } public function testParseReturnsParsedWhenValueIsEmptyFrontMatter(): void @@ -414,15 +396,12 @@ public function testParseReturnsParsedWhenValueIsEmptyFrontMatter(): void --- TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString(''); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals('', $parsed->content()); } public function testParseReturnsParsedWhenValueHasEmptyFrontMatterAndBlankContent(): void @@ -433,15 +412,12 @@ public function testParseReturnsParsedWhenValueHasEmptyFrontMatterAndBlankConten TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString(''); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals('', $parsed->content()); } public function testParseReturnsParsedWhenValueHasEmptyFrontMatterAndContent(): void @@ -456,22 +432,20 @@ public function testParseReturnsParsedWhenValueHasEmptyFrontMatterAndContent(): TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString( + $content = <<<'TXT'

Hello

-TXT - ); +TXT; $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); + self::assertEquals([], $parsed->frontMatter()); self::assertEquals($content, $parsed->content()); } @@ -484,15 +458,12 @@ public function testParseReturnsParsedWhenValueIsEmptyFrontMatterWithWhitespace( --- TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString(''); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals('', $parsed->content()); } public function testParseReturnsParsedWhenValueHasEmptyFrontMatterWithWhitespaceAndBlankContent(): void @@ -505,15 +476,12 @@ public function testParseReturnsParsedWhenValueHasEmptyFrontMatterWithWhitespace TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString(''); - $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals([], $parsed->frontMatter()); + self::assertEquals('', $parsed->content()); } public function testParseReturnsParsedWhenValueHasEmptyFrontMatterWithWhitespaceAndContent(): void @@ -530,22 +498,20 @@ public function testParseReturnsParsedWhenValueHasEmptyFrontMatterWithWhitespace TXT; - $frontMatter = Parsed\FrontMatter::fromArray([]); - $content = Parsed\Content::fromString( + $content = <<<'TXT'

Hello

-TXT - ); +TXT; $parser = new YamlParser(); $parsed = $parser->parse($value); - self::assertEquals($frontMatter, $parsed->frontMatter()); + self::assertEquals([], $parsed->frontMatter()); self::assertEquals($content, $parsed->content()); } @@ -651,22 +617,20 @@ public function testParseReturnsParsedWhenValueIsNonEmptyFrontMatter(): void --- TXT; - $frontMatter = Parsed\FrontMatter::fromArray([ + $frontMatter = [ 'foo' => 'bar', 'baz' => [ 'qux', 'quz', ], - ]); - - $content = Parsed\Content::fromString(''); + ]; $parser = new YamlParser(); $parsed = $parser->parse($value); self::assertEquals($frontMatter, $parsed->frontMatter()); - self::assertEquals($content, $parsed->content()); + self::assertEquals('', $parsed->content()); } public function testParseReturnsParsedWhenValueHasNonEmptyFrontMatterAndBlankContent(): void @@ -682,20 +646,19 @@ public function testParseReturnsParsedWhenValueHasNonEmptyFrontMatterAndBlankCon TXT; - $frontMatter = Parsed\FrontMatter::fromArray([ + $frontMatter = [ 'foo' => 'bar', 'baz' => [ 'qux', 'quz', ], - ]); + ]; - $content = Parsed\Content::fromString( + $content = <<<'TXT' -TXT - ); +TXT; $parser = new YamlParser(); @@ -721,23 +684,22 @@ public function testParseReturnsParsedWhenValueHasNonEmptyFrontMatterAndContent( TXT; - $frontMatter = Parsed\FrontMatter::fromArray([ + $frontMatter = [ 'foo' => 'bar', 'baz' => [ 'qux', 'quz', ], - ]); + ]; - $content = Parsed\Content::fromString( + $content = <<<'TXT'

Hello

-TXT - ); +TXT; $parser = new YamlParser(); @@ -760,15 +722,15 @@ public function testParseReturnsParsedWhenValueIsNonEmptyFrontMatterWithWhitespa --- TXT; - $frontMatter = Parsed\FrontMatter::fromArray([ + $frontMatter = [ 'foo' => 'bar', 'baz' => [ 'qux', 'quz', ], - ]); + ]; - $content = Parsed\Content::fromString(''); + $content = ''; $parser = new YamlParser(); @@ -793,20 +755,19 @@ public function testParseReturnsParsedWhenValueHasNonEmptyFrontMatterWithWhitesp TXT; - $frontMatter = Parsed\FrontMatter::fromArray([ + $frontMatter = [ 'foo' => 'bar', 'baz' => [ 'qux', 'quz', ], - ]); + ]; - $content = Parsed\Content::fromString( + $content = <<<'TXT' -TXT - ); +TXT; $parser = new YamlParser(); @@ -834,23 +795,22 @@ public function testParseReturnsParsedWhenValueHasNonEmptyFrontMatterWithWhitesp TXT; - $frontMatter = Parsed\FrontMatter::fromArray([ + $frontMatter = [ 'foo' => 'bar', 'baz' => [ 'qux', 'quz', ], - ]); + ]; - $content = Parsed\Content::fromString( + $content = <<<'TXT'

Hello

-TXT - ); +TXT; $parser = new YamlParser();