Skip to content

Commit 0a69bcf

Browse files
committed
Refactor to account for multiple violations per sniff
1 parent b03fc00 commit 0a69bcf

16 files changed

+561
-274
lines changed

composer.lock

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Generator/Generator.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
namespace App\Generator;
55

6-
use App\Value\UserDoc;
6+
use App\Value\Sniff;
7+
use App\Value\Violation;
78

89
interface Generator
910
{
10-
public function createUserDoc(UserDoc $doc): string;
11+
public function getViolation(Violation $doc): string;
12+
13+
public function fromSniff(Sniff $doc): string;
1114
}

src/Generator/MarkdownGenerator.php

+116-29
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
namespace App\Generator;
55

66
use App\Value\Diff;
7+
use App\Value\Property;
8+
use App\Value\Sniff;
79
use App\Value\Url;
8-
use App\Value\UserDoc;
10+
use App\Value\Violation;
911
use Stringy\Stringy;
1012
use function Stringy\create as s;
1113

@@ -15,63 +17,149 @@ public function __construct()
1517
{
1618
}
1719

18-
public function createUserDoc(UserDoc $doc): string
20+
public function fromSniff(Sniff $doc): string
1921
{
2022
return <<<MD
21-
# {$doc->getRuleCode()}
23+
# {$doc->getCode()}
2224
23-
{$doc->getDescription()}
25+
{$doc->getDocblock()}
2426
25-
{$this->getComparisons($doc)}
26-
{$this->getSeeAlso($doc)}
27+
{$this->getPublicProperties($doc->getProperties())}
28+
{$this->getSeeAlso($doc->getLinks())}
29+
{$this->getViolations($doc->getViolations())}
2730
MD;
2831
}
2932

30-
private function getComparisons(UserDoc $doc): string
33+
/**
34+
* @param Property[] $properties
35+
*/
36+
private function getPublicProperties(array $properties): string
3137
{
32-
if ($doc->getDiffs() === []) {
38+
if ($properties === []) {
3339
return '';
3440
}
3541

36-
$diffBlocks = implode("\n\n", $this->createDiffBlocks($doc));
42+
$propertyLines = implode("\n", $this->getPublicPropertyLines($properties));
3743

3844
return <<<MD
39-
## Comparisons
45+
## Public Properties
4046
41-
{$diffBlocks}
47+
{$propertyLines}
4248
4349
MD;
4450
}
4551

46-
private function getSeeAlso(UserDoc $doc): string
52+
/**
53+
* @param Property[] $properties
54+
* @return string[]
55+
*/
56+
private function getPublicPropertyLines(array $properties): array
57+
{
58+
return array_map(function (Property $property) {
59+
return "- \${$property->getName()} : {$property->getType()} {$property->getDescription()}";
60+
}, $properties);
61+
}
62+
63+
/**
64+
* @param Url[] $links
65+
* @return string
66+
*/
67+
private function getSeeAlso(array $links): string
4768
{
48-
if ($doc->getLinks() === []) {
69+
if ($links === []) {
4970
return '';
5071
}
5172

52-
$links = implode("\n", $this->createLinks($doc));
73+
$linkLines = implode("\n", $this->getLinkLines($links));
5374

5475
return <<<MD
5576
## See Also
5677
57-
{$links}
78+
{$linkLines}
5879
5980
MD;
6081
}
6182

62-
private function prependLinesWith(string $prefix, string $lines): string
83+
/**
84+
* @param Url[] $links
85+
* @return string[]
86+
*/
87+
private function getLinkLines(array $links): array
6388
{
64-
$prependedLines = array_map(function (Stringy $line) use ($prefix) {
65-
return (string)$line->prepend($prefix);
66-
}, s($lines)->lines());
89+
return array_map(function (Url $url) {
90+
return "- [$url]($url)";
91+
}, $links);
92+
}
6793

68-
return implode("\n", $prependedLines);
94+
/**
95+
* @param Violation[] $violations
96+
* @return string
97+
*/
98+
private function getViolations(array $violations): string
99+
{
100+
if ($violations === []) {
101+
return '';
102+
}
103+
104+
$violations = implode("\n", $this->getViolationBlocks($violations));
105+
106+
return <<<MD
107+
## Troubleshooting
108+
109+
{$violations}
110+
111+
MD;
112+
}
113+
114+
/**
115+
* @param Violation[] $violations
116+
* @return string[]
117+
*/
118+
private function getViolationBlocks(array $violations): array
119+
{
120+
return array_map(function (Violation $violation): string {
121+
return <<<MD
122+
```
123+
<details>
124+
<summary>{$violation->getCode()}</summary>
125+
{$this->getViolation($violation)}
126+
</details>
127+
```
128+
MD;
129+
}, $violations);
130+
}
131+
132+
public function getViolation(Violation $doc): string
133+
{
134+
return <<<MD
135+
{$doc->getDescription()}
136+
137+
{$this->getComparisons($doc)}
138+
{$this->getSeeAlso($doc->getLinks())}
139+
MD;
140+
}
141+
142+
private function getComparisons(Violation $doc): string
143+
{
144+
if ($doc->getDiffs() === []) {
145+
return '';
146+
}
147+
148+
$diffBlocks = implode("\n\n", $this->getDiffBlocks($doc->getDiffs()));
149+
150+
return <<<MD
151+
## Comparisons
152+
153+
{$diffBlocks}
154+
155+
MD;
69156
}
70157

71158
/**
159+
* @param Diff[] $diffs
72160
* @return string[]
73161
*/
74-
private function createDiffBlocks(UserDoc $doc): array
162+
private function getDiffBlocks(array $diffs): array
75163
{
76164
return array_map(function (Diff $diff): string {
77165
return <<<MD
@@ -80,16 +168,15 @@ private function createDiffBlocks(UserDoc $doc): array
80168
{$this->prependLinesWith('+', $diff->getAfter())}
81169
```
82170
MD;
83-
}, $doc->getDiffs());
171+
}, $diffs);
84172
}
85173

86-
/**
87-
* @return string[]
88-
*/
89-
private function createLinks(UserDoc $doc): array
174+
private function prependLinesWith(string $prefix, string $lines): string
90175
{
91-
return array_map(function (Url $url) {
92-
return "- [$url]($url)";
93-
}, $doc->getLinks());
176+
$prependedLines = array_map(function (Stringy $line) use ($prefix) {
177+
return (string)$line->prepend($prefix);
178+
}, s($lines)->lines());
179+
180+
return implode("\n", $prependedLines);
94181
}
95182
}

src/Parser/DevDocParser.php

-80
This file was deleted.

0 commit comments

Comments
 (0)