tangled
alpha
login
or
join now
socialde.pt
/
atp-schema
1
fork
atom
Parse and validate AT Protocol Lexicons with DTO generation for Laravel
1
fork
atom
overview
issues
pulls
pipelines
Implement PHPDoc generation
Miguel Batres
4 months ago
e59a41d6
c19826c4
+648
-17
3 changed files
expand all
collapse all
unified
split
src
Generator
ClassGenerator.php
DocBlockGenerator.php
tests
Unit
Generator
DocBlockGeneratorTest.php
+17
-17
src/Generator/ClassGenerator.php
···
28
28
protected MethodGenerator $methodGenerator;
29
29
30
30
/**
31
31
+
* DocBlock generator instance.
32
32
+
*/
33
33
+
protected DocBlockGenerator $docBlockGenerator;
34
34
+
35
35
+
/**
31
36
* Create a new ClassGenerator.
32
37
*/
33
38
public function __construct(
34
39
?NamingConverter $naming = null,
35
40
?TypeMapper $typeMapper = null,
36
41
?StubRenderer $renderer = null,
37
37
-
?MethodGenerator $methodGenerator = null
42
42
+
?MethodGenerator $methodGenerator = null,
43
43
+
?DocBlockGenerator $docBlockGenerator = null
38
44
) {
39
45
$this->naming = $naming ?? new NamingConverter;
40
46
$this->typeMapper = $typeMapper ?? new TypeMapper($this->naming);
41
47
$this->renderer = $renderer ?? new StubRenderer;
42
48
$this->methodGenerator = $methodGenerator ?? new MethodGenerator($this->naming, $this->typeMapper, $this->renderer);
49
49
+
$this->docBlockGenerator = $docBlockGenerator ?? new DocBlockGenerator($this->typeMapper);
43
50
}
44
51
45
52
/**
···
174
181
*/
175
182
protected function generateClassDocBlock(LexiconDocument $document, array $definition): string
176
183
{
177
177
-
$lines = ['/**'];
178
178
-
179
179
-
if ($document->description) {
180
180
-
$lines[] = ' * '.$document->description;
181
181
-
$lines[] = ' *';
182
182
-
}
183
183
-
184
184
-
$lines[] = ' * Lexicon: '.$document->getNsid();
185
185
-
186
186
-
if (isset($definition['type'])) {
187
187
-
$lines[] = ' * Type: '.$definition['type'];
188
188
-
}
189
189
-
190
190
-
$lines[] = ' */';
191
191
-
192
192
-
return implode("\n", $lines);
184
184
+
return $this->docBlockGenerator->generateClassDocBlock($document, $definition);
193
185
}
194
186
195
187
/**
···
270
262
public function getMethodGenerator(): MethodGenerator
271
263
{
272
264
return $this->methodGenerator;
265
265
+
}
266
266
+
267
267
+
/**
268
268
+
* Get the docblock generator.
269
269
+
*/
270
270
+
public function getDocBlockGenerator(): DocBlockGenerator
271
271
+
{
272
272
+
return $this->docBlockGenerator;
273
273
}
274
274
}
+303
src/Generator/DocBlockGenerator.php
···
1
1
+
<?php
2
2
+
3
3
+
namespace SocialDept\Schema\Generator;
4
4
+
5
5
+
use SocialDept\Schema\Data\LexiconDocument;
6
6
+
7
7
+
class DocBlockGenerator
8
8
+
{
9
9
+
/**
10
10
+
* Type mapper instance.
11
11
+
*/
12
12
+
protected TypeMapper $typeMapper;
13
13
+
14
14
+
/**
15
15
+
* Create a new DocBlockGenerator.
16
16
+
*/
17
17
+
public function __construct(?TypeMapper $typeMapper = null)
18
18
+
{
19
19
+
$this->typeMapper = $typeMapper ?? new TypeMapper;
20
20
+
}
21
21
+
22
22
+
/**
23
23
+
* Generate a class-level docblock with rich annotations.
24
24
+
*
25
25
+
* @param array<string, mixed> $definition
26
26
+
*/
27
27
+
public function generateClassDocBlock(
28
28
+
LexiconDocument $document,
29
29
+
array $definition
30
30
+
): string {
31
31
+
$lines = ['/**'];
32
32
+
33
33
+
// Add description
34
34
+
if ($document->description) {
35
35
+
$lines = array_merge($lines, $this->wrapDescription($document->description));
36
36
+
$lines[] = ' *';
37
37
+
}
38
38
+
39
39
+
// Add lexicon metadata
40
40
+
$lines[] = ' * Lexicon: '.$document->getNsid();
41
41
+
42
42
+
if (isset($definition['type'])) {
43
43
+
$lines[] = ' * Type: '.$definition['type'];
44
44
+
}
45
45
+
46
46
+
// Add @property tags for magic access
47
47
+
$properties = $definition['properties'] ?? [];
48
48
+
$required = $definition['required'] ?? [];
49
49
+
50
50
+
if (! empty($properties)) {
51
51
+
$lines[] = ' *';
52
52
+
foreach ($properties as $name => $propDef) {
53
53
+
$isRequired = in_array($name, $required);
54
54
+
$docType = $this->typeMapper->toPhpDocType($propDef, ! $isRequired);
55
55
+
$desc = $propDef['description'] ?? '';
56
56
+
57
57
+
if ($desc) {
58
58
+
$lines[] = ' * @property '.$docType.' $'.$name.' '.$desc;
59
59
+
} else {
60
60
+
$lines[] = ' * @property '.$docType.' $'.$name;
61
61
+
}
62
62
+
}
63
63
+
}
64
64
+
65
65
+
// Add validation constraints as annotations
66
66
+
if (! empty($properties)) {
67
67
+
$constraints = $this->extractConstraints($properties, $required);
68
68
+
if (! empty($constraints)) {
69
69
+
$lines[] = ' *';
70
70
+
$lines[] = ' * Constraints:';
71
71
+
foreach ($constraints as $constraint) {
72
72
+
$lines[] = ' * - '.$constraint;
73
73
+
}
74
74
+
}
75
75
+
}
76
76
+
77
77
+
$lines[] = ' */';
78
78
+
79
79
+
return implode("\n", $lines);
80
80
+
}
81
81
+
82
82
+
/**
83
83
+
* Generate a property-level docblock.
84
84
+
*
85
85
+
* @param array<string, mixed> $definition
86
86
+
*/
87
87
+
public function generatePropertyDocBlock(
88
88
+
string $name,
89
89
+
array $definition,
90
90
+
bool $isRequired
91
91
+
): string {
92
92
+
$lines = [' /**'];
93
93
+
94
94
+
// Add description
95
95
+
if (isset($definition['description'])) {
96
96
+
$lines = array_merge($lines, $this->wrapDescription($definition['description'], ' * '));
97
97
+
$lines[] = ' *';
98
98
+
}
99
99
+
100
100
+
// Add type annotation
101
101
+
$docType = $this->typeMapper->toPhpDocType($definition, ! $isRequired);
102
102
+
$lines[] = ' * @var '.$docType;
103
103
+
104
104
+
// Add validation constraints
105
105
+
$constraints = $this->extractPropertyConstraints($definition);
106
106
+
if (! empty($constraints)) {
107
107
+
$lines[] = ' *';
108
108
+
foreach ($constraints as $constraint) {
109
109
+
$lines[] = ' * '.$constraint;
110
110
+
}
111
111
+
}
112
112
+
113
113
+
$lines[] = ' */';
114
114
+
115
115
+
return implode("\n", $lines);
116
116
+
}
117
117
+
118
118
+
/**
119
119
+
* Generate a method-level docblock.
120
120
+
*
121
121
+
* @param array<array{name: string, type: string, description?: string}> $params
122
122
+
*/
123
123
+
public function generateMethodDocBlock(
124
124
+
?string $description,
125
125
+
?string $returnType,
126
126
+
array $params = [],
127
127
+
?string $throws = null
128
128
+
): string {
129
129
+
$lines = [' /**'];
130
130
+
131
131
+
// Add description
132
132
+
if ($description) {
133
133
+
$lines = array_merge($lines, $this->wrapDescription($description, ' * '));
134
134
+
}
135
135
+
136
136
+
// Add blank line if we have params or return
137
137
+
if ((! empty($params) || $returnType) && $description) {
138
138
+
$lines[] = ' *';
139
139
+
}
140
140
+
141
141
+
// Add parameters
142
142
+
foreach ($params as $param) {
143
143
+
$desc = $param['description'] ?? '';
144
144
+
if ($desc) {
145
145
+
$lines[] = ' * @param '.$param['type'].' $'.$param['name'].' '.$desc;
146
146
+
} else {
147
147
+
$lines[] = ' * @param '.$param['type'].' $'.$param['name'];
148
148
+
}
149
149
+
}
150
150
+
151
151
+
// Add return type
152
152
+
if ($returnType && $returnType !== 'void') {
153
153
+
$lines[] = ' * @return '.$returnType;
154
154
+
}
155
155
+
156
156
+
// Add throws
157
157
+
if ($throws) {
158
158
+
$lines[] = ' * @throws '.$throws;
159
159
+
}
160
160
+
161
161
+
$lines[] = ' */';
162
162
+
163
163
+
return implode("\n", $lines);
164
164
+
}
165
165
+
166
166
+
/**
167
167
+
* Wrap a long description into multiple lines.
168
168
+
*
169
169
+
* @return array<string>
170
170
+
*/
171
171
+
protected function wrapDescription(string $description, string $prefix = ' * '): array
172
172
+
{
173
173
+
$maxWidth = 80 - strlen($prefix);
174
174
+
$words = explode(' ', $description);
175
175
+
$lines = [];
176
176
+
$currentLine = '';
177
177
+
178
178
+
foreach ($words as $word) {
179
179
+
if (empty($currentLine)) {
180
180
+
$currentLine = $word;
181
181
+
} elseif (strlen($currentLine.' '.$word) <= $maxWidth) {
182
182
+
$currentLine .= ' '.$word;
183
183
+
} else {
184
184
+
$lines[] = $prefix.$currentLine;
185
185
+
$currentLine = $word;
186
186
+
}
187
187
+
}
188
188
+
189
189
+
if (! empty($currentLine)) {
190
190
+
$lines[] = $prefix.$currentLine;
191
191
+
}
192
192
+
193
193
+
return $lines;
194
194
+
}
195
195
+
196
196
+
/**
197
197
+
* Extract validation constraints from properties.
198
198
+
*
199
199
+
* @param array<string, array<string, mixed>> $properties
200
200
+
* @param array<string> $required
201
201
+
* @return array<string>
202
202
+
*/
203
203
+
protected function extractConstraints(array $properties, array $required): array
204
204
+
{
205
205
+
$constraints = [];
206
206
+
207
207
+
// Required fields
208
208
+
if (! empty($required)) {
209
209
+
$constraints[] = 'Required: '.implode(', ', $required);
210
210
+
}
211
211
+
212
212
+
// Property-specific constraints
213
213
+
foreach ($properties as $name => $definition) {
214
214
+
$propConstraints = $this->extractPropertyConstraints($definition);
215
215
+
foreach ($propConstraints as $constraint) {
216
216
+
$constraints[] = $name.': '.trim(str_replace('@constraint', '', $constraint));
217
217
+
}
218
218
+
}
219
219
+
220
220
+
return $constraints;
221
221
+
}
222
222
+
223
223
+
/**
224
224
+
* Extract validation constraints for a single property.
225
225
+
*
226
226
+
* @param array<string, mixed> $definition
227
227
+
* @return array<string>
228
228
+
*/
229
229
+
protected function extractPropertyConstraints(array $definition): array
230
230
+
{
231
231
+
$constraints = [];
232
232
+
233
233
+
// String constraints
234
234
+
if (isset($definition['maxLength'])) {
235
235
+
$constraints[] = '@constraint Max length: '.$definition['maxLength'];
236
236
+
}
237
237
+
238
238
+
if (isset($definition['minLength'])) {
239
239
+
$constraints[] = '@constraint Min length: '.$definition['minLength'];
240
240
+
}
241
241
+
242
242
+
if (isset($definition['maxGraphemes'])) {
243
243
+
$constraints[] = '@constraint Max graphemes: '.$definition['maxGraphemes'];
244
244
+
}
245
245
+
246
246
+
if (isset($definition['minGraphemes'])) {
247
247
+
$constraints[] = '@constraint Min graphemes: '.$definition['minGraphemes'];
248
248
+
}
249
249
+
250
250
+
// Number constraints
251
251
+
if (isset($definition['maximum'])) {
252
252
+
$constraints[] = '@constraint Maximum: '.$definition['maximum'];
253
253
+
}
254
254
+
255
255
+
if (isset($definition['minimum'])) {
256
256
+
$constraints[] = '@constraint Minimum: '.$definition['minimum'];
257
257
+
}
258
258
+
259
259
+
// Array constraints
260
260
+
if (isset($definition['maxItems'])) {
261
261
+
$constraints[] = '@constraint Max items: '.$definition['maxItems'];
262
262
+
}
263
263
+
264
264
+
if (isset($definition['minItems'])) {
265
265
+
$constraints[] = '@constraint Min items: '.$definition['minItems'];
266
266
+
}
267
267
+
268
268
+
// Enum constraints
269
269
+
if (isset($definition['enum'])) {
270
270
+
$values = is_array($definition['enum']) ? implode(', ', $definition['enum']) : $definition['enum'];
271
271
+
$constraints[] = '@constraint Enum: '.$values;
272
272
+
}
273
273
+
274
274
+
// Format constraints
275
275
+
if (isset($definition['format'])) {
276
276
+
$constraints[] = '@constraint Format: '.$definition['format'];
277
277
+
}
278
278
+
279
279
+
// Const constraint
280
280
+
if (isset($definition['const'])) {
281
281
+
$value = is_bool($definition['const']) ? ($definition['const'] ? 'true' : 'false') : $definition['const'];
282
282
+
$constraints[] = '@constraint Const: '.$value;
283
283
+
}
284
284
+
285
285
+
return $constraints;
286
286
+
}
287
287
+
288
288
+
/**
289
289
+
* Generate a simple docblock.
290
290
+
*/
291
291
+
public function generateSimple(string $description): string
292
292
+
{
293
293
+
return " /**\n * {$description}\n */";
294
294
+
}
295
295
+
296
296
+
/**
297
297
+
* Generate a one-line docblock.
298
298
+
*/
299
299
+
public function generateOneLine(string $text): string
300
300
+
{
301
301
+
return " /** {$text} */";
302
302
+
}
303
303
+
}
+328
tests/Unit/Generator/DocBlockGeneratorTest.php
···
1
1
+
<?php
2
2
+
3
3
+
namespace SocialDept\Schema\Tests\Unit\Generator;
4
4
+
5
5
+
use Orchestra\Testbench\TestCase;
6
6
+
use SocialDept\Schema\Data\LexiconDocument;
7
7
+
use SocialDept\Schema\Generator\DocBlockGenerator;
8
8
+
use SocialDept\Schema\Parser\Nsid;
9
9
+
10
10
+
class DocBlockGeneratorTest extends TestCase
11
11
+
{
12
12
+
protected DocBlockGenerator $generator;
13
13
+
14
14
+
protected function setUp(): void
15
15
+
{
16
16
+
parent::setUp();
17
17
+
18
18
+
$this->generator = new DocBlockGenerator;
19
19
+
}
20
20
+
21
21
+
public function test_it_generates_class_docblock_with_description(): void
22
22
+
{
23
23
+
$document = $this->createDocument('app.test.post', [
24
24
+
'type' => 'record',
25
25
+
'description' => 'A social media post',
26
26
+
'properties' => [],
27
27
+
], 'A social media post');
28
28
+
29
29
+
$docBlock = $this->generator->generateClassDocBlock($document, $document->getMainDefinition());
30
30
+
31
31
+
$this->assertStringContainsString('/**', $docBlock);
32
32
+
$this->assertStringContainsString('* A social media post', $docBlock);
33
33
+
$this->assertStringContainsString('* Lexicon: app.test.post', $docBlock);
34
34
+
$this->assertStringContainsString('* Type: record', $docBlock);
35
35
+
}
36
36
+
37
37
+
public function test_it_generates_class_docblock_with_property_tags(): void
38
38
+
{
39
39
+
$document = $this->createDocument('app.test.user', [
40
40
+
'type' => 'record',
41
41
+
'properties' => [
42
42
+
'name' => [
43
43
+
'type' => 'string',
44
44
+
'description' => 'User name',
45
45
+
],
46
46
+
'age' => [
47
47
+
'type' => 'integer',
48
48
+
],
49
49
+
],
50
50
+
'required' => ['name'],
51
51
+
]);
52
52
+
53
53
+
$docBlock = $this->generator->generateClassDocBlock($document, $document->getMainDefinition());
54
54
+
55
55
+
$this->assertStringContainsString('@property string $name User name', $docBlock);
56
56
+
$this->assertStringContainsString('@property int|null $age', $docBlock);
57
57
+
}
58
58
+
59
59
+
public function test_it_includes_validation_constraints_in_class_docblock(): void
60
60
+
{
61
61
+
$document = $this->createDocument('app.test.post', [
62
62
+
'type' => 'record',
63
63
+
'properties' => [
64
64
+
'text' => [
65
65
+
'type' => 'string',
66
66
+
'maxLength' => 280,
67
67
+
],
68
68
+
],
69
69
+
'required' => ['text'],
70
70
+
]);
71
71
+
72
72
+
$docBlock = $this->generator->generateClassDocBlock($document, $document->getMainDefinition());
73
73
+
74
74
+
$this->assertStringContainsString('Constraints:', $docBlock);
75
75
+
$this->assertStringContainsString('Required: text', $docBlock);
76
76
+
$this->assertStringContainsString('text: Max length: 280', $docBlock);
77
77
+
}
78
78
+
79
79
+
public function test_it_generates_property_docblock(): void
80
80
+
{
81
81
+
$docBlock = $this->generator->generatePropertyDocBlock(
82
82
+
'title',
83
83
+
[
84
84
+
'type' => 'string',
85
85
+
'description' => 'The post title',
86
86
+
],
87
87
+
true
88
88
+
);
89
89
+
90
90
+
$this->assertStringContainsString('/**', $docBlock);
91
91
+
$this->assertStringContainsString('* The post title', $docBlock);
92
92
+
$this->assertStringContainsString('* @var string', $docBlock);
93
93
+
$this->assertStringContainsString('*/', $docBlock);
94
94
+
}
95
95
+
96
96
+
public function test_it_includes_constraints_in_property_docblock(): void
97
97
+
{
98
98
+
$docBlock = $this->generator->generatePropertyDocBlock(
99
99
+
'text',
100
100
+
[
101
101
+
'type' => 'string',
102
102
+
'maxLength' => 280,
103
103
+
'minLength' => 1,
104
104
+
],
105
105
+
true
106
106
+
);
107
107
+
108
108
+
$this->assertStringContainsString('@constraint Max length: 280', $docBlock);
109
109
+
$this->assertStringContainsString('@constraint Min length: 1', $docBlock);
110
110
+
}
111
111
+
112
112
+
public function test_it_generates_method_docblock(): void
113
113
+
{
114
114
+
$docBlock = $this->generator->generateMethodDocBlock(
115
115
+
'Create a new post',
116
116
+
'static',
117
117
+
[
118
118
+
['name' => 'text', 'type' => 'string', 'description' => 'Post text'],
119
119
+
['name' => 'author', 'type' => 'string'],
120
120
+
]
121
121
+
);
122
122
+
123
123
+
$this->assertStringContainsString('* Create a new post', $docBlock);
124
124
+
$this->assertStringContainsString('* @param string $text Post text', $docBlock);
125
125
+
$this->assertStringContainsString('* @param string $author', $docBlock);
126
126
+
$this->assertStringContainsString('* @return static', $docBlock);
127
127
+
}
128
128
+
129
129
+
public function test_it_handles_void_return_type(): void
130
130
+
{
131
131
+
$docBlock = $this->generator->generateMethodDocBlock(
132
132
+
'Process data',
133
133
+
'void',
134
134
+
[]
135
135
+
);
136
136
+
137
137
+
$this->assertStringNotContainsString('@return', $docBlock);
138
138
+
}
139
139
+
140
140
+
public function test_it_includes_throws_annotation(): void
141
141
+
{
142
142
+
$docBlock = $this->generator->generateMethodDocBlock(
143
143
+
'Validate data',
144
144
+
'bool',
145
145
+
[],
146
146
+
'\\InvalidArgumentException'
147
147
+
);
148
148
+
149
149
+
$this->assertStringContainsString('@throws \\InvalidArgumentException', $docBlock);
150
150
+
}
151
151
+
152
152
+
public function test_it_wraps_long_descriptions(): void
153
153
+
{
154
154
+
$longDescription = 'This is a very long description that should be wrapped across multiple lines when it exceeds the maximum line width of eighty characters including the comment prefix and should definitely span more than one line';
155
155
+
156
156
+
$docBlock = $this->generator->generatePropertyDocBlock(
157
157
+
'description',
158
158
+
[
159
159
+
'type' => 'string',
160
160
+
'description' => $longDescription,
161
161
+
],
162
162
+
true
163
163
+
);
164
164
+
165
165
+
// Just verify the long description is present in the docblock
166
166
+
$this->assertStringContainsString('This is a very long description', $docBlock);
167
167
+
168
168
+
// And that it doesn't exceed reasonable line lengths
169
169
+
$lines = explode("\n", $docBlock);
170
170
+
foreach ($lines as $line) {
171
171
+
$this->assertLessThan(120, strlen($line), 'Line too long: '.$line);
172
172
+
}
173
173
+
}
174
174
+
175
175
+
public function test_it_extracts_max_length_constraint(): void
176
176
+
{
177
177
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
178
178
+
['maxLength' => 100],
179
179
+
]);
180
180
+
181
181
+
$this->assertContains('@constraint Max length: 100', $constraints);
182
182
+
}
183
183
+
184
184
+
public function test_it_extracts_min_length_constraint(): void
185
185
+
{
186
186
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
187
187
+
['minLength' => 5],
188
188
+
]);
189
189
+
190
190
+
$this->assertContains('@constraint Min length: 5', $constraints);
191
191
+
}
192
192
+
193
193
+
public function test_it_extracts_grapheme_constraints(): void
194
194
+
{
195
195
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
196
196
+
['maxGraphemes' => 280, 'minGraphemes' => 1],
197
197
+
]);
198
198
+
199
199
+
$this->assertContains('@constraint Max graphemes: 280', $constraints);
200
200
+
$this->assertContains('@constraint Min graphemes: 1', $constraints);
201
201
+
}
202
202
+
203
203
+
public function test_it_extracts_number_constraints(): void
204
204
+
{
205
205
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
206
206
+
['maximum' => 100, 'minimum' => 0],
207
207
+
]);
208
208
+
209
209
+
$this->assertContains('@constraint Maximum: 100', $constraints);
210
210
+
$this->assertContains('@constraint Minimum: 0', $constraints);
211
211
+
}
212
212
+
213
213
+
public function test_it_extracts_array_constraints(): void
214
214
+
{
215
215
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
216
216
+
['maxItems' => 10, 'minItems' => 1],
217
217
+
]);
218
218
+
219
219
+
$this->assertContains('@constraint Max items: 10', $constraints);
220
220
+
$this->assertContains('@constraint Min items: 1', $constraints);
221
221
+
}
222
222
+
223
223
+
public function test_it_extracts_enum_constraint(): void
224
224
+
{
225
225
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
226
226
+
['enum' => ['active', 'inactive', 'pending']],
227
227
+
]);
228
228
+
229
229
+
$this->assertContains('@constraint Enum: active, inactive, pending', $constraints);
230
230
+
}
231
231
+
232
232
+
public function test_it_extracts_format_constraint(): void
233
233
+
{
234
234
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
235
235
+
['format' => 'datetime'],
236
236
+
]);
237
237
+
238
238
+
$this->assertContains('@constraint Format: datetime', $constraints);
239
239
+
}
240
240
+
241
241
+
public function test_it_extracts_const_constraint(): void
242
242
+
{
243
243
+
$constraints = $this->invokeMethod('extractPropertyConstraints', [
244
244
+
['const' => true],
245
245
+
]);
246
246
+
247
247
+
$this->assertContains('@constraint Const: true', $constraints);
248
248
+
}
249
249
+
250
250
+
public function test_it_generates_simple_docblock(): void
251
251
+
{
252
252
+
$docBlock = $this->generator->generateSimple('A simple description');
253
253
+
254
254
+
$this->assertSame(" /**\n * A simple description\n */", $docBlock);
255
255
+
}
256
256
+
257
257
+
public function test_it_generates_one_line_docblock(): void
258
258
+
{
259
259
+
$docBlock = $this->generator->generateOneLine('Quick note');
260
260
+
261
261
+
$this->assertSame(' /** Quick note */', $docBlock);
262
262
+
}
263
263
+
264
264
+
public function test_it_handles_empty_properties(): void
265
265
+
{
266
266
+
$document = $this->createDocument('app.test.empty', [
267
267
+
'type' => 'record',
268
268
+
'properties' => [],
269
269
+
'required' => [],
270
270
+
]);
271
271
+
272
272
+
$docBlock = $this->generator->generateClassDocBlock($document, $document->getMainDefinition());
273
273
+
274
274
+
$this->assertStringContainsString('Lexicon: app.test.empty', $docBlock);
275
275
+
$this->assertStringNotContainsString('@property', $docBlock);
276
276
+
}
277
277
+
278
278
+
public function test_it_handles_nullable_properties(): void
279
279
+
{
280
280
+
$document = $this->createDocument('app.test.post', [
281
281
+
'type' => 'record',
282
282
+
'properties' => [
283
283
+
'subtitle' => ['type' => 'string'],
284
284
+
],
285
285
+
'required' => [],
286
286
+
]);
287
287
+
288
288
+
$docBlock = $this->generator->generateClassDocBlock($document, $document->getMainDefinition());
289
289
+
290
290
+
$this->assertStringContainsString('@property string|null $subtitle', $docBlock);
291
291
+
}
292
292
+
293
293
+
/**
294
294
+
* Helper to create a test document.
295
295
+
*
296
296
+
* @param array<string, mixed> $mainDef
297
297
+
*/
298
298
+
protected function createDocument(string $nsid, array $mainDef, ?string $description = null): LexiconDocument
299
299
+
{
300
300
+
return new LexiconDocument(
301
301
+
lexicon: 1,
302
302
+
id: Nsid::parse($nsid),
303
303
+
defs: ['main' => $mainDef],
304
304
+
description: $description,
305
305
+
source: null,
306
306
+
raw: [
307
307
+
'lexicon' => 1,
308
308
+
'id' => $nsid,
309
309
+
'defs' => ['main' => $mainDef],
310
310
+
]
311
311
+
);
312
312
+
}
313
313
+
314
314
+
/**
315
315
+
* Helper to invoke protected method.
316
316
+
*
317
317
+
* @param array<mixed> $args
318
318
+
* @return mixed
319
319
+
*/
320
320
+
protected function invokeMethod(string $methodName, array $args)
321
321
+
{
322
322
+
$reflection = new \ReflectionClass($this->generator);
323
323
+
$method = $reflection->getMethod($methodName);
324
324
+
$method->setAccessible(true);
325
325
+
326
326
+
return $method->invokeArgs($this->generator, $args);
327
327
+
}
328
328
+
}