diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e81e5ba..f58f53f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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') }} diff --git a/composer.json b/composer.json index aaa7372..423ece6 100644 --- a/composer.json +++ b/composer.json @@ -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" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 93a96e8..b9122d8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,15 +2,18 @@ - + diff --git a/src/Bridge/CommonMark/CommonMarkParser.php b/src/Bridge/CommonMark/CommonMarkParser.php index b1fc2c0..dd90e8b 100644 --- a/src/Bridge/CommonMark/CommonMarkParser.php +++ b/src/Bridge/CommonMark/CommonMarkParser.php @@ -1,4 +1,6 @@ -parser = $commonMarkConverter ?: new CommonMarkConverter; + $this->parser = $commonMarkConverter ?: new CommonMarkConverter(); } public function parse(string $markdown): string { - return $this->parser->convertToHtml($markdown)->getContent(); + return $this->parser?->convertToHtml($markdown)?->getContent() ?? ''; } } diff --git a/src/Bridge/Symfony/SymfonyYAMLParser.php b/src/Bridge/Symfony/SymfonyYAMLParser.php index bf22d88..b186ebc 100644 --- a/src/Bridge/Symfony/SymfonyYAMLParser.php +++ b/src/Bridge/Symfony/SymfonyYAMLParser.php @@ -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); } diff --git a/src/Document.php b/src/Document.php index 4af4e8d..a80e159 100644 --- a/src/Document.php +++ b/src/Document.php @@ -2,10 +2,9 @@ namespace Mni\FrontYAML; -class Document +final class Document { - /** @var mixed */ - private $yaml; + private mixed $yaml; private string $content; @@ -13,7 +12,7 @@ class Document * @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; @@ -22,7 +21,7 @@ public function __construct($yaml, string $content) /** * @return mixed YAML content. */ - public function getYAML() + public function getYAML(): mixed { return $this->yaml; } diff --git a/src/Parser.php b/src/Parser.php index 2aa7fd4..8efdfd1 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -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') ?: ['---']; } /** @@ -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, "~"); }; diff --git a/src/YAML/YAMLParser.php b/src/YAML/YAMLParser.php index 0b4c6c2..4e53efc 100644 --- a/src/YAML/YAMLParser.php +++ b/src/YAML/YAMLParser.php @@ -9,8 +9,6 @@ interface YAMLParser { /** * Parses a YAML string. - * - * @return mixed */ - public function parse(string $yaml); + public function parse(string $yaml): mixed; } diff --git a/tests/Bridge/CommonMark/CommonMarkParserTest.php b/tests/Bridge/CommonMark/CommonMarkParserTest.php index ae2998b..14ab83c 100644 --- a/tests/Bridge/CommonMark/CommonMarkParserTest.php +++ b/tests/Bridge/CommonMark/CommonMarkParserTest.php @@ -10,7 +10,7 @@ class CommonMarkParserTest extends TestCase { - public function testParseWithDefaultParser() + public function testParseWithDefaultParser(): void { $parser = new CommonMarkParser(); @@ -19,10 +19,10 @@ public function testParseWithDefaultParser() $this->assertSame("

This is a title

\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); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index c518cf4..ffb6e48 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -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 = <<assertSame("

This strong

\n", $document->getContent()); } - public function testEscaping() + public function testEscaping(): void { - $parser = new Parser; + $parser = new Parser(); $str = <<assertSame("

Foo

\n", $document->getContent()); } - public function testMultilineMarkdown() + public function testMultilineMarkdown(): void { - $parser = new Parser; - $str = <<assertEquals($this->normalizeEOL($expected), $this->normalizeEOL($document->getContent())); } - public function testCrossOsMultiline() + public function testCrossOsMultiline(): void { - $parser = new Parser; + $parser = new Parser(); $content = <<assertSame($this->normalizeEOL($expectedHtml), $this->normalizeEOL($dosYaml['multiline'])); } - public function testNonGreedySeparator() + public function testNonGreedySeparator(): void { - $parser = new Parser; - $content = <<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); } diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 056902d..2e54fc2 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -2,14 +2,16 @@ namespace Mni\FrontYAML\Test; +use Mni\FrontYAML\Markdown\MarkdownParser; use Mni\FrontYAML\Parser; -use PHPUnit\Framework\TestCase; use Mni\FrontYAML\YAML\YAMLParser; -use Mni\FrontYAML\Markdown\MarkdownParser; +use PHPUnit\Framework\TestCase; + +use function trim; -class ParserTest extends TestCase +final class ParserTest extends TestCase { - public function testParseEmptyString() + public function testParseEmptyString(): void { $parser = new Parser(); $document = $parser->parse('', false); @@ -17,7 +19,7 @@ public function testParseEmptyString() $this->assertSame('', $document->getContent()); } - public function testParseNoYAML() + public function testParseNoYAML(): void { $parser = new Parser(); $document = $parser->parse('foo', false); @@ -25,7 +27,7 @@ public function testParseNoYAML() $this->assertSame('foo', $document->getContent()); } - public function testParseNoYAML2() + public function testParseNoYAML2(): void { $parser = new Parser(); $str = <<assertSame($str, $document->getContent()); } - public function testParseFrontYAMLDelimiter() + public function testParseFrontYAMLDelimiter(): void { $parser = new Parser(); $document = $parser->parse('---', false); @@ -45,7 +47,7 @@ public function testParseFrontYAMLDelimiter() $this->assertSame('---', $document->getContent()); } - public function testParseFrontYAMLDelimiters() + public function testParseFrontYAMLDelimiters(): void { $parser = new Parser(); $str = <<assertSame('', $document->getContent()); } - public function testParseFrontYAMLPregMatchDelimiter() + public function testParseFrontYAMLPregMatchDelimiter(): void { $parser = new Parser(null, null, '~', '~'); $str = <<assertSame('', $document->getContent()); } - public function testParseYAML() + public function testParseYAML(): void { - $yamlParser = $this->getMockForAbstractClass(YAMLParser::class); + $yamlParser = $this->createMock(YAMLParser::class); $yamlParser->expects($this->once()) ->method('parse') ->with('foo') ->willReturn('bar'); - $markdownParser = $this->getMockForAbstractClass(MarkdownParser::class); + $markdownParser = $this->createMock(MarkdownParser::class); $markdownParser->expects($this->never()) ->method('parse'); @@ -95,15 +97,15 @@ public function testParseYAML() $this->assertSame('bim', $document->getContent()); } - public function testParseYAMLMarkdown() + public function testParseYAMLMarkdown(): void { - $yamlParser = $this->getMockForAbstractClass(YAMLParser::class); + $yamlParser = $this->createMock(YAMLParser::class); $yamlParser->expects($this->once()) ->method('parse') ->with('foo') ->willReturn('bar'); - $markdownParser = $this->getMockForAbstractClass(MarkdownParser::class); + $markdownParser = $this->createMock(MarkdownParser::class); $markdownParser->expects($this->once()) ->method('parse') ->with('bim') @@ -123,13 +125,13 @@ public function testParseYAMLMarkdown() $this->assertSame('bam', $document->getContent()); } - public function testParseMarkdownNoYAML1Line() + public function testParseMarkdownNoYAML1Line(): void { - $yamlParser = $this->getMockForAbstractClass(YAMLParser::class); + $yamlParser = $this->createMock(YAMLParser::class); $yamlParser->expects($this->never()) ->method('parse'); - $markdownParser = $this->getMockForAbstractClass(MarkdownParser::class); + $markdownParser = $this->createMock(MarkdownParser::class); $markdownParser->expects($this->once()) ->method('parse') ->with('bim') @@ -146,13 +148,13 @@ public function testParseMarkdownNoYAML1Line() $this->assertSame('bam', $document->getContent()); } - public function testParseMarkdownNoYAML2Lines() + public function testParseMarkdownNoYAML2Lines(): void { - $yamlParser = $this->getMockForAbstractClass(YAMLParser::class); + $yamlParser = $this->createMock(YAMLParser::class); $yamlParser->expects($this->never()) ->method('parse'); - $markdownParser = $this->getMockForAbstractClass(MarkdownParser::class); + $markdownParser = $this->createMock(MarkdownParser::class); $markdownParser->expects($this->once()) ->method('parse') ->willReturn('foo'); @@ -169,11 +171,11 @@ public function testParseMarkdownNoYAML2Lines() $this->assertSame('foo', $document->getContent()); } - public function testMarkdownParserNotCalled() + public function testMarkdownParserNotCalled(): void { - $yamlParser = $this->getMockForAbstractClass(YAMLParser::class); + $yamlParser = $this->createMock(YAMLParser::class); - $markdownParser = $this->getMockForAbstractClass(MarkdownParser::class); + $markdownParser = $this->createMock(MarkdownParser::class); $markdownParser->expects($this->never()) ->method('parse'); @@ -181,17 +183,17 @@ public function testMarkdownParserNotCalled() $parser->parse('foo', false); } - public function testParseFrontYAMLEdgeCaseDelimiters() + public function testParseFrontYAMLEdgeCaseDelimiters(): void { $start = '*_-\)``.|.``(/-_*'; $end = '--({@}{._.}{@})--'; - $yamlParser = $this->getMockForAbstractClass(YAMLParser::class); + $yamlParser = $this->createMock(YAMLParser::class); $yamlParser->expects($this->once()) ->method('parse') ->with('foo: bar') ->willReturn(['foo' => 'bar']); - $markdownParser = $this->getMockForAbstractClass(MarkdownParser::class); + $markdownParser = $this->createMock(MarkdownParser::class); $markdownParser->expects($this->never()) ->method('parse'); @@ -203,21 +205,21 @@ public function testParseFrontYAMLEdgeCaseDelimiters() bim EOF; $document = $parser->parse($str, false); - $this->assertSame(array('foo' => 'bar'), $document->getYAML()); + $this->assertSame(['foo' => 'bar'], $document->getYAML()); $this->assertSame('bim', trim($document->getContent())); } - public function testParseFrontYAMLArrayDelimiters() + public function testParseFrontYAMLArrayDelimiters(): void { $start = ['---','']; - $yamlParser = $this->getMockForAbstractClass(YAMLParser::class); + $yamlParser = $this->createMock(YAMLParser::class); $yamlParser->expects($this->exactly(2)) ->method('parse') ->with('foo: bar') ->willReturn(['foo' => 'bar']); - $markdownParser = $this->getMockForAbstractClass(MarkdownParser::class); + $markdownParser = $this->createMock(MarkdownParser::class); $markdownParser->expects($this->never()) ->method('parse'); @@ -236,7 +238,7 @@ public function testParseFrontYAMLArrayDelimiters() EOF; $document1 = $parser->parse($str1, false); $document2 = $parser->parse($str2, false); - $this->assertSame(array('foo' => 'bar'), $document1->getYAML()); + $this->assertSame(['foo' => 'bar'], $document1->getYAML()); $this->assertSame('bim', trim($document1->getContent())); $this->assertSame($document1->getYAML(), $document2->getYAML()); $this->assertSame($document1->getContent(), $document2->getContent());