Skip to content

Commit

Permalink
Merge pull request #8 from ergebnis/fix/value
Browse files Browse the repository at this point in the history
Fix: Remove Parsed\FrontMatter and Parsed\Content value objects
  • Loading branch information
ergebnis-bot committed Dec 30, 2020
2 parents 4dc5ce0 + 919f30e commit 5f8968f
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 323 deletions.
17 changes: 14 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 1 addition & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -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\\<string, mixed\\>, array\\<int, string\\> given\\.$#"
count: 1
path: test/Unit/Parsed/FrontMatterTest.php
ignoreErrors: []

15 changes: 1 addition & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.3.1@2feba22a005a18bf31d4c7b9bdb9252c73897476">
<file src="src/Parsed/FrontMatter.php">
<DocblockTypeContradiction occurrences="1">
<code>return !\is_string($key);</code>
</DocblockTypeContradiction>
</file>
<files psalm-version="4.3.2@57b53ff26237074fdf5cbcb034f7da5172be4524">
<file src="src/YamlParser.php">
<MixedArgumentTypeCoercion occurrences="1">
<code>(array) $data</code>
</MixedArgumentTypeCoercion>
<MixedAssignment occurrences="1">
<code>$data</code>
</MixedAssignment>
</file>
<file src="test/Unit/Parsed/FrontMatterTest.php">
<InvalidScalarArgument occurrences="1">
<code>$value</code>
</InvalidScalarArgument>
</file>
</files>
8 changes: 4 additions & 4 deletions src/Parsed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ 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,
$content
);
}

public function frontMatter(): Parsed\FrontMatter
public function frontMatter(): array
{
return $this->frontMatter;
}

public function content(): Parsed\Content
public function content(): string
{
return $this->content;
}
Expand Down
34 changes: 0 additions & 34 deletions src/Parsed/Content.php

This file was deleted.

60 changes: 0 additions & 60 deletions src/Parsed/FrontMatter.php

This file was deleted.

20 changes: 15 additions & 5 deletions src/YamlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand All @@ -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,
Expand Down
39 changes: 0 additions & 39 deletions test/Unit/Parsed/ContentTest.php

This file was deleted.

60 changes: 0 additions & 60 deletions test/Unit/Parsed/FrontMatterTest.php

This file was deleted.

9 changes: 3 additions & 6 deletions test/Unit/ParsedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand Down
Loading

0 comments on commit 5f8968f

Please sign in to comment.