Skip to content

Commit b03fc00

Browse files
committed
Create markdown generator for UserDoc
1 parent 07f1ea0 commit b03fc00

File tree

4 files changed

+176
-5
lines changed

4 files changed

+176
-5
lines changed

src/Generator/Generator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Generator;
5+
6+
use App\Value\UserDoc;
7+
8+
interface Generator
9+
{
10+
public function createUserDoc(UserDoc $doc): string;
11+
}

src/Generator/MarkdownGenerator.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Generator;
5+
6+
use App\Value\Diff;
7+
use App\Value\Url;
8+
use App\Value\UserDoc;
9+
use Stringy\Stringy;
10+
use function Stringy\create as s;
11+
12+
class MarkdownGenerator implements Generator
13+
{
14+
public function __construct()
15+
{
16+
}
17+
18+
public function createUserDoc(UserDoc $doc): string
19+
{
20+
return <<<MD
21+
# {$doc->getRuleCode()}
22+
23+
{$doc->getDescription()}
24+
25+
{$this->getComparisons($doc)}
26+
{$this->getSeeAlso($doc)}
27+
MD;
28+
}
29+
30+
private function getComparisons(UserDoc $doc): string
31+
{
32+
if ($doc->getDiffs() === []) {
33+
return '';
34+
}
35+
36+
$diffBlocks = implode("\n\n", $this->createDiffBlocks($doc));
37+
38+
return <<<MD
39+
## Comparisons
40+
41+
{$diffBlocks}
42+
43+
MD;
44+
}
45+
46+
private function getSeeAlso(UserDoc $doc): string
47+
{
48+
if ($doc->getLinks() === []) {
49+
return '';
50+
}
51+
52+
$links = implode("\n", $this->createLinks($doc));
53+
54+
return <<<MD
55+
## See Also
56+
57+
{$links}
58+
59+
MD;
60+
}
61+
62+
private function prependLinesWith(string $prefix, string $lines): string
63+
{
64+
$prependedLines = array_map(function (Stringy $line) use ($prefix) {
65+
return (string)$line->prepend($prefix);
66+
}, s($lines)->lines());
67+
68+
return implode("\n", $prependedLines);
69+
}
70+
71+
/**
72+
* @return string[]
73+
*/
74+
private function createDiffBlocks(UserDoc $doc): array
75+
{
76+
return array_map(function (Diff $diff): string {
77+
return <<<MD
78+
```diff
79+
{$this->prependLinesWith('-', $diff->getBefore())}
80+
{$this->prependLinesWith('+', $diff->getAfter())}
81+
```
82+
MD;
83+
}, $doc->getDiffs());
84+
}
85+
86+
/**
87+
* @return string[]
88+
*/
89+
private function createLinks(UserDoc $doc): array
90+
{
91+
return array_map(function (Url $url) {
92+
return "- [$url]($url)";
93+
}, $doc->getLinks());
94+
}
95+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Tests\Generator;
5+
6+
use App\Generator\MarkdownGenerator;
7+
use App\Value\Diff;
8+
use App\Value\Url;
9+
use App\Value\UserDoc;
10+
use PHPUnit\Framework\TestCase;
11+
12+
/** @covers \App\Generator\MarkdownGenerator */
13+
class MarkdownGeneratorTest extends TestCase
14+
{
15+
private MarkdownGenerator $generator;
16+
17+
protected function setUp(): void
18+
{
19+
$this->generator = new MarkdownGenerator();
20+
}
21+
22+
/** @test */
23+
public function createUserDoc()
24+
{
25+
$doc = new UserDoc(
26+
'Rule.Code',
27+
'Description',
28+
[
29+
new Diff('a();', 'b();'),
30+
new Diff('a();', 'b();')
31+
],
32+
[
33+
new Url('http://link1.com'),
34+
new Url('http://link2.com')
35+
]
36+
);
37+
38+
self::assertEquals(
39+
<<<MD
40+
# Rule.Code
41+
42+
Description
43+
44+
## Comparisons
45+
46+
```diff
47+
-a();
48+
+b();
49+
```
50+
51+
```diff
52+
-a();
53+
+b();
54+
```
55+
56+
## See Also
57+
58+
- [http://link1.com](http://link1.com)
59+
- [http://link2.com](http://link2.com)
60+
61+
MD,
62+
$this->generator->createUserDoc($doc)
63+
);
64+
}
65+
}

tests/Parser/UserDocParserTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,29 @@ public function getUserDoc_WithCodeComparisons_AddTrimmedDiffs()
5252
<code_comparison>
5353
<code>
5454
<![CDATA[
55-
function a() {
55+
function b() {
5656
}
5757
]]>
5858
</code>
5959
<code>
6060
<![CDATA[
61-
function b() {
61+
function a() {
6262
}
6363
]]>
6464
</code>
6565
</code_comparison>
6666
<code_comparison>
67-
<code>a();</code>
6867
<code>b();</code>
68+
<code>a();</code>
6969
</code_comparison>
7070
</documentation>
7171
XML;
7272
file_put_contents(self::XML_FILE_PATH, $content);
7373
$doc = $this->parser->getUserDoc(self::XML_FILE_PATH);
7474
self::assertEquals(
7575
[
76-
new Diff("function b() {\n}", "function a() {\n}"),
77-
new Diff('b();', 'a();'),
76+
new Diff("function a() {\n}", "function b() {\n}"),
77+
new Diff('a();', 'b();'),
7878
],
7979
$doc->getDiffs()
8080
);

0 commit comments

Comments
 (0)