From 466e040265fb572cb584322621b77cb7b034596b Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 18 Mar 2021 06:33:06 +0100 Subject: [PATCH 1/2] Sniff: add getter for the standard, category and sniff name Includes tests. --- src/Value/Sniff.php | 26 ++++++++++++++++++++++++++ tests/Value/SniffTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Value/Sniff.php b/src/Value/Sniff.php index deb666d..46d89cc 100644 --- a/src/Value/Sniff.php +++ b/src/Value/Sniff.php @@ -8,6 +8,9 @@ class Sniff { private string $code; + private string $standardName; + private string $categoryName; + private string $sniffName; private string $docblock; /** * @var Property[] @@ -42,6 +45,11 @@ public function __construct( Assert::that($code) ->notBlank(); + $sniffNameParts = explode('.', $code); + Assert::that($sniffNameParts) + ->isArray() + ->count(3); + Assert::thatAll($properties) ->isInstanceOf(Property::class); @@ -52,6 +60,9 @@ public function __construct( ->isInstanceOf(Violation::class); $this->code = $code; + $this->standardName = $sniffNameParts[0]; + $this->categoryName = $sniffNameParts[1]; + $this->sniffName = $sniffNameParts[2]; $this->docblock = $docblock; $this->properties = array_values($properties); $this->urls = $urls; @@ -65,6 +76,21 @@ public function getCode(): string return $this->code; } + public function getStandardName(): string + { + return $this->standardName; + } + + public function getCategoryName(): string + { + return $this->categoryName; + } + + public function getSniffName(): string + { + return $this->sniffName; + } + public function getDocblock(): string { return $this->docblock; diff --git a/tests/Value/SniffTest.php b/tests/Value/SniffTest.php index 6cf5260..f1f3e1a 100644 --- a/tests/Value/SniffTest.php +++ b/tests/Value/SniffTest.php @@ -16,6 +16,9 @@ class SniffTest extends TestCase { const CODE = 'Standard.Category.Code'; + const STANDARD = 'Standard'; + const CATEGORY = 'Category'; + const SNIFFNAME = 'Code'; const DOCBLOCK = 'Docblock'; const DESCRIPTION = 'Description'; /** @@ -128,6 +131,33 @@ public function getCode() ); } + /** @test */ + public function getStandardName() + { + self::assertSame( + self::STANDARD, + $this->createSniff()->getStandardName() + ); + } + + /** @test */ + public function getCategoryName() + { + self::assertSame( + self::CATEGORY, + $this->createSniff()->getCategoryName() + ); + } + + /** @test */ + public function getSniffName() + { + self::assertSame( + self::SNIFFNAME, + $this->createSniff()->getSniffName() + ); + } + protected function setUp(): void { $this->PROPERTIES = [ From e76e70c2ea485084361e7c1efc31ba21cd85f97e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 18 Mar 2021 07:29:09 +0100 Subject: [PATCH 2/2] Add JekyllPageGenerator Jekyll is the underlying technology used for GitHub Pages. Jekyll parses markdown and liquid files to HTML pages for a site. Generally Jekyll expects "frontmatter" - a `---` delimited block at the top of the markdown file -. The existence of the frontmatter indicates to Jekyll that the page should be _processed_ by the transformation engine and not just copied over. Frontmatter also allows for adding variables to the page which can be used in the page/theme to do certain things. Even though for GitHub Pages, a plugin is active which make the frontmatter not _strictly_ necessary, for this "PHPCS docs" type of website it is useful. By default, the first `#` (H1) header will be regarded as the `page.title` and this page title is then subsequently used in the website menu and such. As for these sniff pages, the default title is the full sniffname `Standard.Category.SniffName` and the `Standard` and `Category` are already "levels" in a typical menu due to the folder structure, it is less noisy to use just the plain `SniffName` as the `page.title`. To that end, I'm adding a separate `JekyllPageGenerator` class which extends the standard `MarkdownGenerator` class and adds the _frontmatter_ to the page. This will also allow for extending the available frontmatter with additional keys in the future if deemed necessary. Includes test. --- src/Generator/JekyllPageGenerator.php | 36 ++++++++++++++++ tests/Generator/JekyllPageGeneratorTest.php | 46 +++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/Generator/JekyllPageGenerator.php create mode 100644 tests/Generator/JekyllPageGeneratorTest.php diff --git a/src/Generator/JekyllPageGenerator.php b/src/Generator/JekyllPageGenerator.php new file mode 100644 index 0000000..08c7aed --- /dev/null +++ b/src/Generator/JekyllPageGenerator.php @@ -0,0 +1,36 @@ +getFrontMatter($sniff) . "\n"; + $sniffDoc .= parent::createSniffDoc($sniff); + + return $sniffDoc; + } + + private function getFrontMatter(Sniff $sniff): string + { + $sniffName = $sniff->getSniffName(); + if ($sniffName === '') { + return <<<'MD' + --- + --- + + MD; + } + + return <<generator->createSniffDoc($doc) + ); + } + + protected function setUp(): void + { + $this->generator = new JekyllPageGenerator(); + } +}