Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0']
php-versions: ['8.2', '8.3', '8.4']
name: PHP ${{ matrix.php-versions }}
steps:
- uses: actions/checkout@v2
Expand All @@ -22,7 +22,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: composer-${{ hashFiles('**/composer.json') }}
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
}
},
"require": {
"php": "^7.4|^8.0",
"symfony/yaml": "^4.0|^5.0|^6.0|^7.0",
"php": "^8.2||^8.3||^8.4",
"symfony/yaml": "^7.0",
"league/commonmark": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^11.0"
}
}
21 changes: 12 additions & 9 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
<!--
phpunit -c phpunit.xml
-->
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php">
<phpunit
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
failOnDeprecation="true"
failOnRisky="true"
failOnWarning="true"
colors="true"
testdox="false"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerDeprecations="true"
beStrictAboutCoverageMetadata="true">

<testsuites>
<testsuite name="Tests">
Expand Down
12 changes: 7 additions & 5 deletions src/Bridge/CommonMark/CommonMarkParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't


namespace Mni\FrontYAML\Bridge\CommonMark;

Expand All @@ -9,17 +11,17 @@
/**
* Bridge to the League CommonMark parser
*/
class CommonMarkParser implements MarkdownParser
final class CommonMarkParser implements MarkdownParser
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same please revert

{
private MarkdownConverterInterface $parser;

public function __construct(MarkdownConverterInterface $commonMarkConverter = null)
public function __construct(MarkdownConverterInterface|null $commonMarkConverter = null)
{
$this->parser = $commonMarkConverter ?: new CommonMarkConverter;
$this->parser = $commonMarkConverter ?: new CommonMarkConverter();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert

}

public function parse(string $markdown): string
{
return $this->parser->convertToHtml($markdown)->getContent();
return $this->parser?->convertToHtml($markdown)?->getContent() ?? '';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parser will never be null

}
}
6 changes: 3 additions & 3 deletions src/Bridge/Symfony/SymfonyYAMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
/**
* Bridge to the Symfony YAML parser
*/
class SymfonyYAMLParser implements YAMLParser
final class SymfonyYAMLParser implements YAMLParser
{
private Parser $parser;

public function __construct()
{
$this->parser = new Parser;
$this->parser = new Parser();
}

public function parse(string $yaml)
public function parse(string $yaml): mixed
{
return $this->parser->parse($yaml);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

namespace Mni\FrontYAML;

class Document
final class Document
{
/** @var mixed */
private $yaml;
private mixed $yaml;

private string $content;

/**
* @param mixed $yaml YAML content.
* @param string $content Content of the document.
*/
public function __construct($yaml, string $content)
public function __construct(mixed $yaml, string $content)
{
$this->yaml = $yaml;
$this->content = $content;
Expand All @@ -22,7 +21,7 @@ public function __construct($yaml, string $content)
/**
* @return mixed YAML content.
*/
public function getYAML()
public function getYAML(): mixed
{
return $this->yaml;
}
Expand Down
42 changes: 20 additions & 22 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,37 @@
use Mni\FrontYAML\Markdown\MarkdownParser;
use Mni\FrontYAML\YAML\YAMLParser;

use function array_filter;
use function array_map;
use function implode;
use function ltrim;
use function preg_match;
use function preg_quote;
use function trim;

/**
* YAML Front matter parser
*/
class Parser
final class Parser
{
/**
* @var YAMLParser
*/
private $yamlParser;
private YAMLParser $yamlParser;

/**
* @var MarkdownParser
*/
private $markdownParser;
private MarkdownParser $markdownParser;

private array $startSep;

private array $endSep;

/**
* @param string|string[] $startSep
* @param string|string[] $endSep
*/
public function __construct(
?YAMLParser $yamlParser = null,
?MarkdownParser $markdownParser = null,
$startSep = '---',
$endSep = '---'
YAMLParser|null $yamlParser = null,
MarkdownParser|null $markdownParser = null,
string|array $startSep = '---',
string|array $endSep = '---'
) {
$this->yamlParser = $yamlParser ?: new SymfonyYAMLParser;
$this->markdownParser = $markdownParser ?: new CommonMarkParser;
$this->startSep = array_filter((array) $startSep, 'is_string') ?: ['---'];
$this->endSep = array_filter((array) $endSep, 'is_string') ?: ['---'];
$this->yamlParser = $yamlParser ?? new SymfonyYAMLParser();
$this->markdownParser = $markdownParser ?? new CommonMarkParser();
$this->startSep = array_filter((array) $startSep, 'is_string') ?: ['---'];
$this->endSep = array_filter((array) $endSep, 'is_string') ?: ['---'];
}

/**
Expand All @@ -51,7 +49,7 @@ public function parse(string $str, bool $parseMarkdown = true): Document
{
$yaml = null;

$quote = static function ($str) {
$quote = static function (string $str): string {
return preg_quote($str, "~");
};

Expand Down
4 changes: 1 addition & 3 deletions src/YAML/YAMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ interface YAMLParser
{
/**
* Parses a YAML string.
*
* @return mixed
*/
public function parse(string $yaml);
public function parse(string $yaml): mixed;
}
8 changes: 4 additions & 4 deletions tests/Bridge/CommonMark/CommonMarkParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class CommonMarkParserTest extends TestCase
{
public function testParseWithDefaultParser()
public function testParseWithDefaultParser(): void
{
$parser = new CommonMarkParser();

Expand All @@ -19,10 +19,10 @@ public function testParseWithDefaultParser()
$this->assertSame("<h1>This is a title</h1>\n", $html);
}

public function testParseWithCustomParser()
public function testParseWithCustomParser(): void
{
$environment = new Environment;
$environment->addExtension(new CommonMarkCoreExtension);
$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());
$converter = new MarkdownConverter($environment);

$parser = new CommonMarkParser($converter);
Expand Down
32 changes: 17 additions & 15 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use Mni\FrontYAML\Parser;
use PHPUnit\Framework\TestCase;

class FunctionalTest extends TestCase
use function str_replace;

final class FunctionalTest extends TestCase
{
public function testSimpleDocument()
public function testSimpleDocument(): void
{
$parser = new Parser;
$parser = new Parser();

$str = <<<EOF
---
Expand All @@ -23,9 +25,9 @@ public function testSimpleDocument()
$this->assertSame("<p>This <strong>strong</strong></p>\n", $document->getContent());
}

public function testEscaping()
public function testEscaping(): void
{
$parser = new Parser;
$parser = new Parser();

$str = <<<EOF
---
Expand All @@ -52,10 +54,10 @@ public function testEscaping()
$this->assertSame("<p>Foo</p>\n", $document->getContent());
}

public function testMultilineMarkdown()
public function testMultilineMarkdown(): void
{
$parser = new Parser;
$str = <<<EOF
$parser = new Parser();
$str = <<<EOF
Foo

Bar
Expand All @@ -69,9 +71,9 @@ public function testMultilineMarkdown()
$this->assertEquals($this->normalizeEOL($expected), $this->normalizeEOL($document->getContent()));
}

public function testCrossOsMultiline()
public function testCrossOsMultiline(): void
{
$parser = new Parser;
$parser = new Parser();
$content = <<<EOF
---
lorem: ipsum
Expand Down Expand Up @@ -105,10 +107,10 @@ public function testCrossOsMultiline()
$this->assertSame($this->normalizeEOL($expectedHtml), $this->normalizeEOL($dosYaml['multiline']));
}

public function testNonGreedySeparator()
public function testNonGreedySeparator(): void
{
$parser = new Parser;
$content = <<<EOF
$parser = new Parser();
$content = <<<EOF
---
lorem: ipsum
---
Expand All @@ -117,10 +119,10 @@ public function testNonGreedySeparator()
Ipsum
EOF;
$document = $parser->parse($content);
$this->assertSame(array('lorem' => 'ipsum'), $document->getYAML());
$this->assertSame(['lorem' => 'ipsum'], $document->getYAML());
}

private function normalizeEOL($str)
private function normalizeEOL(string $str): string|array
{
return str_replace("\r", '', $str);
}
Expand Down
Loading