Skip to content

Commit 5bc3e88

Browse files
authored
Merge pull request #10 from PHPCSStandards/feature/add-jekyllpagegenerator
Add JekyllPageGenerator
2 parents 4ff6332 + e76e70c commit 5bc3e88

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

src/Generator/JekyllPageGenerator.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Generator;
5+
6+
use App\Value\Sniff;
7+
8+
class JekyllPageGenerator extends MarkdownGenerator implements Generator
9+
{
10+
public function createSniffDoc(Sniff $sniff): string
11+
{
12+
$sniffDoc = $this->getFrontMatter($sniff) . "\n";
13+
$sniffDoc .= parent::createSniffDoc($sniff);
14+
15+
return $sniffDoc;
16+
}
17+
18+
private function getFrontMatter(Sniff $sniff): string
19+
{
20+
$sniffName = $sniff->getSniffName();
21+
if ($sniffName === '') {
22+
return <<<'MD'
23+
---
24+
---
25+
26+
MD;
27+
}
28+
29+
return <<<MD
30+
---
31+
title: {$sniffName}
32+
---
33+
34+
MD;
35+
}
36+
}

src/Value/Sniff.php

+26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
class Sniff
99
{
1010
private string $code;
11+
private string $standardName;
12+
private string $categoryName;
13+
private string $sniffName;
1114
private string $docblock;
1215
/**
1316
* @var Property[]
@@ -42,6 +45,11 @@ public function __construct(
4245
Assert::that($code)
4346
->notBlank();
4447

48+
$sniffNameParts = explode('.', $code);
49+
Assert::that($sniffNameParts)
50+
->isArray()
51+
->count(3);
52+
4553
Assert::thatAll($properties)
4654
->isInstanceOf(Property::class);
4755

@@ -52,6 +60,9 @@ public function __construct(
5260
->isInstanceOf(Violation::class);
5361

5462
$this->code = $code;
63+
$this->standardName = $sniffNameParts[0];
64+
$this->categoryName = $sniffNameParts[1];
65+
$this->sniffName = $sniffNameParts[2];
5566
$this->docblock = $docblock;
5667
$this->properties = array_values($properties);
5768
$this->urls = $urls;
@@ -65,6 +76,21 @@ public function getCode(): string
6576
return $this->code;
6677
}
6778

79+
public function getStandardName(): string
80+
{
81+
return $this->standardName;
82+
}
83+
84+
public function getCategoryName(): string
85+
{
86+
return $this->categoryName;
87+
}
88+
89+
public function getSniffName(): string
90+
{
91+
return $this->sniffName;
92+
}
93+
6894
public function getDocblock(): string
6995
{
7096
return $this->docblock;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\Generator;
5+
6+
use App\Generator\JekyllPageGenerator;
7+
use App\Value\Sniff;
8+
use App\Value\UrlList;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/** @covers \App\Generator\JekyllPage */
12+
class JekyllPageTest extends TestCase
13+
{
14+
private JekyllPageGenerator $generator;
15+
16+
/** @test */
17+
public function fromSniff_WithMinimalData_WriteMinimalDetails()
18+
{
19+
$doc = new Sniff(
20+
'Standard.Category.My',
21+
'',
22+
[],
23+
new UrlList([]),
24+
'',
25+
[],
26+
[]
27+
);
28+
29+
self::assertSame(
30+
<<<MD
31+
---
32+
title: My
33+
---
34+
35+
# Standard.Category.My
36+
37+
MD,
38+
$this->generator->createSniffDoc($doc)
39+
);
40+
}
41+
42+
protected function setUp(): void
43+
{
44+
$this->generator = new JekyllPageGenerator();
45+
}
46+
}

tests/Value/SniffTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
class SniffTest extends TestCase
1717
{
1818
const CODE = 'Standard.Category.Code';
19+
const STANDARD = 'Standard';
20+
const CATEGORY = 'Category';
21+
const SNIFFNAME = 'Code';
1922
const DOCBLOCK = 'Docblock';
2023
const DESCRIPTION = 'Description';
2124
/**
@@ -128,6 +131,33 @@ public function getCode()
128131
);
129132
}
130133

134+
/** @test */
135+
public function getStandardName()
136+
{
137+
self::assertSame(
138+
self::STANDARD,
139+
$this->createSniff()->getStandardName()
140+
);
141+
}
142+
143+
/** @test */
144+
public function getCategoryName()
145+
{
146+
self::assertSame(
147+
self::CATEGORY,
148+
$this->createSniff()->getCategoryName()
149+
);
150+
}
151+
152+
/** @test */
153+
public function getSniffName()
154+
{
155+
self::assertSame(
156+
self::SNIFFNAME,
157+
$this->createSniff()->getSniffName()
158+
);
159+
}
160+
131161
protected function setUp(): void
132162
{
133163
$this->PROPERTIES = [

0 commit comments

Comments
 (0)