-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.php
More file actions
746 lines (619 loc) · 21 KB
/
CodeGenerator.php
File metadata and controls
746 lines (619 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
<?php
declare(strict_types=1);
namespace Ruudk\CodeGenerator;
use Closure;
use Generator;
use UnitEnum;
/**
* @phpstan-type CodeLine string|Group
* @phpstan-type CodeLines Closure(): (Generator<CodeLine>|array<CodeLine>|string)|array<CodeLine|Generator<CodeLine>>|Generator<CodeLine>|string
*/
final class CodeGenerator
{
/**
* @var array<string, Alias|FullyQualified|FunctionName|NamespaceName>
*/
private array $imports = [];
private readonly ?NamespaceName $namespace;
public function __construct(
null | NamespaceName | string $namespace = null,
) {
$this->namespace = NamespaceName::maybeFromString($namespace);
}
/**
* Dumps the generated code with proper formatting (removes consecutive newlines, trims)
* @param CodeLines $iterable
*/
public function dump(array | Closure | Generator | string $iterable) : string
{
$output = implode(
PHP_EOL,
self::resolveIterable($this->maybeIndent($iterable)),
);
// Replace consecutive newlines with a single newline
$output = preg_replace('/(\r?\n){3,}/', PHP_EOL . PHP_EOL, $output) ?? '';
return rtrim($output);
}
/**
* Dumps a complete PHP file with opening tag, declare, namespace, and imports
* @param CodeLines $iterable
*/
public function dumpFile(array | Closure | Generator | string $iterable) : string
{
$content = $this->dump(function () use ($iterable) {
yield '<?php';
yield '';
yield 'declare(strict_types=1);';
yield '';
if ($this->namespace !== null) {
yield sprintf('namespace %s;', $this->namespace);
yield '';
}
$content = self::resolveIterable($iterable);
yield from $this->maybeDump([], $this->dumpImports(), ['']);
yield from $content;
});
// Ensure file ends with a newline
return $content === '' ? '' : $content . PHP_EOL;
}
/**
* Generates sorted import statements for all registered imports
* @return Generator<string>
*/
private function dumpImports() : Generator
{
uasort($this->imports, fn($left, $right) => $left->compare($right));
foreach ($this->imports as $alias => $import) {
if ($import instanceof Alias) {
yield $import->toUseStatement();
continue;
}
if ($import instanceof FunctionName) {
// Handle function imports
yield sprintf('use %s;', $import);
continue;
}
if ($import instanceof NamespaceName) {
// Parent namespace import - check if we need an alias
$lastPart = $import->lastPart;
if ($alias !== $lastPart) {
yield sprintf('use %s as %s;', $import, $alias);
} else {
yield sprintf('use %s;', $import);
}
continue;
}
// Skip if it's in the same namespace as the file
if ($import->namespace !== null && $this->namespace !== null && $import->namespace->equals($this->namespace)) {
continue;
}
// Check if we need an alias
if ($alias !== $import->className->name) {
yield sprintf('use %s as %s;', $import, $alias);
} else {
yield sprintf('use %s;', $import);
}
}
}
/**
* Yields from data with optional before/after content.
* Only yields before/after if the data is not empty.
*
* @param null|CodeLines $before
* @param CodeLines $data
* @param null|CodeLines $after
*
* @return Generator<CodeLine>
*/
public function maybeDump(
null | array | Closure | Generator | string $before,
array | Closure | Generator | string $data,
null | array | Closure | Generator | string $after,
) : Generator {
$hasContent = false;
foreach (self::resolveIterable($data) as $item) {
if ( ! $hasContent) {
$hasContent = true;
if ($before !== null) {
yield from self::resolveIterable($before);
}
}
yield $item;
}
if ( ! $hasContent || $after === null) {
return;
}
yield from self::resolveIterable($after);
}
/**
* Finds an available alias for a type, appending numbers if the alias is already taken
*/
private function findAvailableAlias(Importable $type, string $alias, int $i = 1) : string
{
$aliasToCheck = $i === 1 ? $alias : sprintf('%s%d', $alias, $i);
if ( ! isset($this->imports[$aliasToCheck])) {
return $aliasToCheck;
}
$existing = $this->imports[$aliasToCheck];
// Check if it's the same import
if ($existing->equals($type)) {
return $aliasToCheck;
}
return $this->findAvailableAlias($type, $alias, $i + 1);
}
/**
* Imports an enum and returns the fully qualified reference to use in the generated code
*/
public function importEnum(UnitEnum $enum) : string
{
return sprintf('%s::%s', $this->import($enum::class), $enum->name);
}
/**
* Imports a class, namespace, or function and returns the alias to use in the generated code
*/
public function import(Importable | string $fqcnOrEnum) : string
{
if ($fqcnOrEnum instanceof FunctionName) {
$alias = $this->findAvailableAlias($fqcnOrEnum, $fqcnOrEnum->shortName);
$this->imports[$alias] = $fqcnOrEnum;
return $alias;
}
if ($fqcnOrEnum instanceof NamespaceName) {
$alias = $this->findAvailableAlias($fqcnOrEnum, $fqcnOrEnum->lastPart);
$this->imports[$alias] = $fqcnOrEnum;
return $alias;
}
$fqcn = FullyQualified::maybeFromString($fqcnOrEnum);
$alias = $this->findAvailableAlias($fqcn, $fqcn->className->name);
$this->imports[$alias] = $fqcn;
return $alias;
}
/**
* Imports a class by importing its parent namespace and returning the relative path
*/
public function importByParent(Importable | string $name) : string
{
$fqcn = FullyQualified::maybeFromString($name);
// If there's no namespace, just return the class name
if ($fqcn->namespace === null) {
return (string) $fqcn->className;
}
// Check if the full target namespace is the same as the current namespace
if ($this->namespace?->equals($fqcn->namespace) === true) {
return (string) $fqcn->className;
}
// Import the namespace and return the alias with class name
return (string) new FullyQualified(
$this->import($fqcn->namespace),
$fqcn->className,
);
}
/**
* Generates a PHP attribute string for the given fully qualified class name
*/
public function dumpAttribute(FullyQualified | string $fqcn) : string
{
return sprintf('#[%s]', $this->import($fqcn));
}
/**
* Generates a class reference string (e.g., Foo::class)
*/
public function dumpClassReference(FullyQualified | string $fqcn, bool $import = true) : string
{
return sprintf('%s::class', $import ? $this->import($fqcn) : '\\' . (string) $fqcn);
}
/**
* Applies indentation to lines based on their level and Group indentation
*
* @param CodeLines $data
* @return Generator<string>
*/
private function maybeIndent(array | Closure | Generator | string $data, int $level = 0) : Generator
{
foreach (self::resolveIterable($data) as $line) {
if ($line instanceof Group) {
yield from $this->maybeIndent($line->lines, $level + $line->indention);
continue;
}
$line = implode(
PHP_EOL,
array_map(
fn($line) => str_repeat(' ', $level) . $line,
explode(PHP_EOL, $line),
),
);
yield trim($line) === '' ? '' : $line;
}
}
/**
* Formats a string as a nowdoc if it contains newlines, otherwise as a regular string
*/
public function maybeNowDoc(string $input, string $tag = 'EOD') : string
{
if ( ! str_contains($input, PHP_EOL)) {
return var_export($input, true);
}
return sprintf("<<<'%s'\n%s", $tag, implode(
PHP_EOL,
array_map(
fn($line) => ' ' . $line,
[...explode(PHP_EOL, $input), $tag],
),
));
}
/**
* Wraps code lines as a statement by adding a semicolon to the last line
* @param CodeLines $data
* @return Generator<CodeLine>
*/
public function statement(array | Closure | Generator | string $data) : Generator
{
yield from $this->suffixLast(';', $data);
}
/**
* Adds a suffix to the last line of the iterable
*
* @param CodeLines $data
* @return Generator<CodeLine>
*/
public function suffixLast(string $suffix, array | Closure | Generator | string $data) : Generator
{
foreach (self::resolveIterable($data) as $line) {
if (isset($previousValue)) {
yield $previousValue;
}
$previousValue = $line;
}
if (isset($previousValue)) {
if ($previousValue instanceof Group) {
yield Group::indent($this->suffixLast($suffix, $previousValue->lines), $previousValue->indention);
return;
}
yield $previousValue . $suffix;
}
}
/**
* Trims empty lines from the start and end of the iterable
*
* @param CodeLines $data
* @return Generator<CodeLine>
*/
public function trim(array | Closure | Generator | string $data) : Generator
{
$lines = self::resolveIterable($data);
// Find first non-empty line
$start = 0;
foreach ($lines as $i => $line) {
if ( ! ($line instanceof Group) && trim($line) === '') {
++$start;
} else {
break;
}
}
// Find last non-empty line
$end = count($lines) - 1;
for ($i = $end; $i >= $start; --$i) {
if ( ! ($lines[$i] instanceof Group) && trim($lines[$i]) === '') {
--$end;
} else {
break;
}
}
// Yield the trimmed content
for ($i = $start; $i <= $end; ++$i) {
yield $lines[$i];
}
}
/**
* Creates an indented Group, optionally trimming empty lines first
*
* @param CodeLines $data
*/
public function indent(array | Closure | Generator | string $data, bool $trim = true, int $indention = 1) : Group
{
return Group::indent($trim ? $this->trim($data) : $data, $indention);
}
/**
* Wraps code lines with a prefix and optional suffix
*
* @param CodeLines $data
* @return Generator<CodeLine>
*/
public function wrap(string $prefix, array | Closure | Generator | string $data, ?string $suffix = null) : Generator
{
yield from $this->prefixFirst(
$prefix,
$suffix !== null ? $this->suffixLast($suffix, $data) : $data,
);
}
/**
* Conditionally wraps code lines with a prefix and optional suffix
* @param CodeLines $data
* @return Generator<CodeLine>
*/
public function maybeWrap(bool $condition, string $prefix, array | Closure | Generator | string $data, ?string $suffix = null) : Generator
{
$data = self::resolveIterable($data);
if ($condition) {
yield from $this->wrap($prefix, $data, $suffix);
} else {
yield from $data;
}
}
/**
* Adds a prefix to the first line of the iterable
*
* @param CodeLines $data
* @return Generator<CodeLine>
*/
public function prefixFirst(string $prefix, array | Closure | Generator | string $data) : Generator
{
$first = true;
foreach (self::resolveIterable($data) as $line) {
if ($first) {
$first = false;
if ($line instanceof Group) {
yield Group::indent($this->prefixFirst($prefix, $line->lines), $line->indention);
continue;
}
yield $prefix . $line;
continue;
}
yield $line;
}
}
/**
* Adds a suffix to the first line of the iterable
*
* @param CodeLines $data
*
* @return Generator<string|Group>
*/
public function suffixFirst(string $suffix, array | Closure | Generator | string $data) : Generator
{
$first = true;
foreach (self::resolveIterable($data) as $line) {
if ($first) {
$first = false;
if ($line instanceof Group) {
yield Group::indent($this->suffixFirst($suffix, $line->lines), $line->indention);
continue;
}
yield $line . $suffix;
continue;
}
yield $line;
}
}
/**
* Joins code lines with a delimiter into a single string
*
* @param CodeLines $data
*/
public function join(string $delimiter, array | Closure | Generator | string $data) : string
{
$resolved = [];
foreach (self::resolveIterable($data) as $item) {
$resolved[] = $item instanceof Group ? '' : (string) $item;
}
return implode($delimiter, $resolved);
}
/**
* Joins the first two elements of the iterable together
*
* @param CodeLines $data
*
* @return Generator<string|Group>
*/
public function joinFirstPair(array | Closure | Generator | string $data) : Generator
{
$first = null;
$i = 0;
foreach (self::resolveIterable($data) as $line) {
if ($i === 0) {
++$i;
$first = $line;
continue;
}
if ($i === 1) {
++$i;
if ($line instanceof Group && $first !== null) {
$prefix = $first instanceof Group ? '' : (string) $first;
$lines = self::resolveIterable($line->lines);
if ($lines === []) {
yield $prefix;
} else {
yield Group::indent($this->prefixFirst($prefix, $lines), $line->indention);
}
continue;
}
if ($first !== null) {
$prefix = $first instanceof Group ? '' : (string) $first;
$suffix = $line instanceof Group ? '' : (string) $line;
yield $prefix . $suffix;
} else {
yield $line;
}
continue;
}
++$i;
yield $line;
}
if ($i === 1 && $first !== null) {
yield $first;
}
}
/**
* Adds a suffix to all lines except comments
*
* @param CodeLines $data
*
* @return Generator<CodeLine>
*/
public function allSuffix(string $suffix, array | Closure | Generator | string $data) : Generator
{
foreach (self::resolveIterable($data) as $value) {
if ($value instanceof Group) {
yield new ($value::class)($this->suffixLast($suffix, $value->lines));
continue;
}
if (str_starts_with($value, '//')) {
yield $value;
continue;
}
yield $value . $suffix;
}
}
/**
* Generates a method call or constructor invocation
*
* @param CodeLines $object
* @param CodeLines $args
*
* @return Generator<CodeLine>
*/
public function dumpCall(
array | Closure | Generator | string $object,
string $method,
array | Closure | Generator | string $args = [],
bool $static = false,
bool $addCommaAfterEachArgument = true,
) : Generator {
$args = self::resolveIterable($args);
if ( ! is_string($object)) {
yield from self::resolveIterable($object);
yield Group::indent(function () use ($addCommaAfterEachArgument, $method, $args) {
if ($args === []) {
yield sprintf('->%s()', $method);
return;
}
if (count($args) === 1) {
yield from $this->wrap(sprintf('->%s(', $method), $args, ')');
return;
}
yield sprintf('->%s(', $method);
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args);
yield ')';
});
return;
}
if (($static && $method !== '__construct') || ( ! $static && $method === '__construct')) {
$object = $this->import($object);
}
$call = match (true) {
! $static && $method === '__construct' => sprintf('new %s', $object),
default => sprintf('%s%s%s', $object, $static ? '::' : '->', $method),
};
if ($args === []) {
yield sprintf('%s()', $call);
return;
}
if (count($args) === 1) {
yield from $this->wrap(sprintf('%s(', $call), $args, ')');
return;
}
yield sprintf('%s(', $call);
yield Group::indent($addCommaAfterEachArgument ? $this->allSuffix(',', $args) : $args);
yield ')';
}
/**
* Generates a function call with arguments
*
* @param CodeLines $args
*
* @return Generator<CodeLine>
*/
public function dumpFunctionCall(string $function, array | Closure | Generator | string $args = []) : Generator
{
$args = self::resolveIterable($args);
if (count($args) === 0) {
yield sprintf('%s()', $function);
return;
}
if (count($args) === 1) {
yield from $this->prefixFirst(
sprintf('%s(', $function),
$this->suffixLast(
')',
$args,
),
);
return;
}
yield sprintf('%s(', $function);
yield Group::indent($this->allSuffix(',', $args));
yield ')';
}
/**
* Resolves a callable, iterable, or string into an array of lines
* @param CodeLines $iterable
*
* @return ($iterable is iterable<string> ? array<string> : array<CodeLine>)
*/
public static function resolveIterable(array | Closure | Generator | string $iterable) : array
{
if (is_callable($iterable)) {
$iterable = $iterable();
}
if (is_string($iterable)) {
return [$iterable];
}
$resolved = [];
foreach ($iterable as $item) {
if ($item instanceof Generator) {
$resolved = array_merge($resolved, iterator_to_array($item, false));
} else {
$resolved[] = $item;
}
}
return $resolved;
}
/**
* Adds a prefix to every line of the iterable
*
* @param CodeLines $data
* @return Generator<string|Group>
*/
public function prefix(string $prefix, array | Closure | Generator | string $data) : Generator
{
foreach (CodeGenerator::resolveIterable($data) as $line) {
if ($line instanceof Group) {
yield Group::indent($this->prefix($prefix, $line->lines), $line->indention);
continue;
}
foreach (explode(PHP_EOL, $line) as $singleLine) {
yield $prefix . $singleLine;
}
}
}
/**
* Adds single-line comment prefix to every line
*
* @param CodeLines $data
* @return Generator<string|Group>
*/
public function comment(array | Closure | Generator | string $data) : Generator
{
yield from $this->prefix('// ', $data);
}
/**
* Wraps content in a block comment
*
* @param CodeLines $data
* @return Generator<string|Group>
*/
public function blockComment(array | Closure | Generator | string $data) : Generator
{
yield from $this->maybeDump('/*', $this->prefix(' * ', $data), ' */');
}
/**
* Wraps content in a PHPDoc comment
*
* @param CodeLines $data
* @return Generator<string|Group>
*/
public function docComment(array | Closure | Generator | string $data) : Generator
{
yield from $this->maybeDump('/**', $this->prefix(' * ', $data), ' */');
}
}