4
4
namespace App \Generator ;
5
5
6
6
use App \Value \Diff ;
7
+ use App \Value \Property ;
8
+ use App \Value \Sniff ;
7
9
use App \Value \Url ;
8
- use App \Value \UserDoc ;
10
+ use App \Value \Violation ;
9
11
use Stringy \Stringy ;
10
12
use function Stringy \create as s ;
11
13
@@ -15,63 +17,149 @@ public function __construct()
15
17
{
16
18
}
17
19
18
- public function createUserDoc ( UserDoc $ doc ): string
20
+ public function fromSniff ( Sniff $ doc ): string
19
21
{
20
22
return <<<MD
21
- # {$ doc ->getRuleCode ()}
23
+ # {$ doc ->getCode ()}
22
24
23
- {$ doc ->getDescription ()}
25
+ {$ doc ->getDocblock ()}
24
26
25
- {$ this ->getComparisons ($ doc )}
26
- {$ this ->getSeeAlso ($ doc )}
27
+ {$ this ->getPublicProperties ($ doc ->getProperties ())}
28
+ {$ this ->getSeeAlso ($ doc ->getLinks ())}
29
+ {$ this ->getViolations ($ doc ->getViolations ())}
27
30
MD ;
28
31
}
29
32
30
- private function getComparisons (UserDoc $ doc ): string
33
+ /**
34
+ * @param Property[] $properties
35
+ */
36
+ private function getPublicProperties (array $ properties ): string
31
37
{
32
- if ($ doc -> getDiffs () === []) {
38
+ if ($ properties === []) {
33
39
return '' ;
34
40
}
35
41
36
- $ diffBlocks = implode ("\n\n " , $ this ->createDiffBlocks ( $ doc ));
42
+ $ propertyLines = implode ("\n" , $ this ->getPublicPropertyLines ( $ properties ));
37
43
38
44
return <<<MD
39
- ## Comparisons
45
+ ## Public Properties
40
46
41
- {$ diffBlocks }
47
+ {$ propertyLines }
42
48
43
49
MD ;
44
50
}
45
51
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
47
68
{
48
- if ($ doc -> getLinks () === []) {
69
+ if ($ links === []) {
49
70
return '' ;
50
71
}
51
72
52
- $ links = implode ("\n" , $ this ->createLinks ( $ doc ));
73
+ $ linkLines = implode ("\n" , $ this ->getLinkLines ( $ links ));
53
74
54
75
return <<<MD
55
76
## See Also
56
77
57
- {$ links }
78
+ {$ linkLines }
58
79
59
80
MD ;
60
81
}
61
82
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
63
88
{
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
+ }
67
93
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 ;
69
156
}
70
157
71
158
/**
159
+ * @param Diff[] $diffs
72
160
* @return string[]
73
161
*/
74
- private function createDiffBlocks ( UserDoc $ doc ): array
162
+ private function getDiffBlocks ( array $ diffs ): array
75
163
{
76
164
return array_map (function (Diff $ diff ): string {
77
165
return <<<MD
@@ -80,16 +168,15 @@ private function createDiffBlocks(UserDoc $doc): array
80
168
{$ this ->prependLinesWith ('+ ' , $ diff ->getAfter ())}
81
169
```
82
170
MD ;
83
- }, $ doc -> getDiffs () );
171
+ }, $ diffs );
84
172
}
85
173
86
- /**
87
- * @return string[]
88
- */
89
- private function createLinks (UserDoc $ doc ): array
174
+ private function prependLinesWith (string $ prefix , string $ lines ): string
90
175
{
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 );
94
181
}
95
182
}
0 commit comments