Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dc7c469

Browse files
committedFeb 15, 2025··
PrinterTest - nowdoc helper for nicer formatting
1 parent 19d74a8 commit dc7c469

File tree

1 file changed

+517
-368
lines changed

1 file changed

+517
-368
lines changed
 

‎tests/PHPStan/Printer/PrinterTest.php

Lines changed: 517 additions & 368 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,21 @@
5050
use PHPStan\PhpDocParser\Parser\TypeParser;
5151
use PHPStan\PhpDocParser\ParserConfig;
5252
use PHPUnit\Framework\TestCase;
53+
use function array_map;
5354
use function array_pop;
55+
use function array_slice;
5456
use function array_splice;
5557
use function array_unshift;
5658
use function array_values;
5759
use function assert;
5860
use function count;
61+
use function implode;
62+
use function preg_match;
63+
use function preg_replace_callback;
64+
use function preg_split;
65+
use function str_repeat;
66+
use function str_replace;
67+
use function strlen;
5968
use const PHP_EOL;
6069

6170
class PrinterTest extends TestCase
@@ -96,12 +105,14 @@ public function dataPrintFormatPreserving(): iterable
96105
$noopVisitor,
97106
];
98107
yield [
99-
'/**
100-
* @param Foo $foo
101-
*/',
102-
'/**
103-
* @param Foo $foo
104-
*/',
108+
self::nowdoc('
109+
/**
110+
* @param Foo $foo
111+
*/'),
112+
self::nowdoc('
113+
/**
114+
* @param Foo $foo
115+
*/'),
105116
$noopVisitor,
106117
];
107118

@@ -143,33 +154,39 @@ public function enterNode(Node $node)
143154
];
144155

145156
yield [
146-
'/**
147-
* @param Foo $foo
148-
*/',
149-
'/**
150-
*/',
157+
self::nowdoc('
158+
/**
159+
* @param Foo $foo
160+
*/'),
161+
self::nowdoc('
162+
/**
163+
*/'),
151164
$removeFirst,
152165
];
153166

154167
yield [
155-
'/**
156-
* @param Foo $foo
157-
* @param Bar $bar
158-
*/',
159-
'/**
160-
* @param Bar $bar
161-
*/',
168+
self::nowdoc('
169+
/**
170+
* @param Foo $foo
171+
* @param Bar $bar
172+
*/'),
173+
self::nowdoc('
174+
/**
175+
* @param Bar $bar
176+
*/'),
162177
$removeFirst,
163178
];
164179

165180
yield [
166-
'/**
167-
* @param Foo $foo
168-
* @param Bar $bar
169-
*/',
170-
'/**
171-
* @param Bar $bar
172-
*/',
181+
self::nowdoc('
182+
/**
183+
* @param Foo $foo
184+
* @param Bar $bar
185+
*/'),
186+
self::nowdoc('
187+
/**
188+
* @param Bar $bar
189+
*/'),
173190
$removeFirst,
174191
];
175192

@@ -189,13 +206,15 @@ public function enterNode(Node $node)
189206
};
190207

191208
yield [
192-
'/**
193-
* @param Foo $foo
194-
* @param Bar $bar
195-
*/',
196-
'/**
197-
* @param Foo $foo
198-
*/',
209+
self::nowdoc('
210+
/**
211+
* @param Foo $foo
212+
* @param Bar $bar
213+
*/'),
214+
self::nowdoc('
215+
/**
216+
* @param Foo $foo
217+
*/'),
199218
$removeLast,
200219
];
201220

@@ -216,39 +235,45 @@ public function enterNode(Node $node)
216235
};
217236

218237
yield [
219-
'/**
220-
* @param Foo $foo
221-
* @param Bar $bar
222-
*/',
223-
'/**
224-
* @param Foo $foo
225-
*/',
238+
self::nowdoc('
239+
/**
240+
* @param Foo $foo
241+
* @param Bar $bar
242+
*/'),
243+
self::nowdoc('
244+
/**
245+
* @param Foo $foo
246+
*/'),
226247
$removeSecond,
227248
];
228249

229250
yield [
230-
'/**
231-
* @param Foo $foo
232-
* @param Bar $bar
233-
* @param Baz $baz
234-
*/',
235-
'/**
236-
* @param Foo $foo
237-
* @param Baz $baz
238-
*/',
251+
self::nowdoc('
252+
/**
253+
* @param Foo $foo
254+
* @param Bar $bar
255+
* @param Baz $baz
256+
*/'),
257+
self::nowdoc('
258+
/**
259+
* @param Foo $foo
260+
* @param Baz $baz
261+
*/'),
239262
$removeSecond,
240263
];
241264

242265
yield [
243-
'/**
244-
* @param Foo $foo
245-
* @param Bar $bar
246-
* @param Baz $baz
247-
*/',
248-
'/**
249-
* @param Foo $foo
250-
* @param Baz $baz
251-
*/',
266+
self::nowdoc('
267+
/**
268+
* @param Foo $foo
269+
* @param Bar $bar
270+
* @param Baz $baz
271+
*/'),
272+
self::nowdoc('
273+
/**
274+
* @param Foo $foo
275+
* @param Baz $baz
276+
*/'),
252277
$removeSecond,
253278
];
254279

@@ -280,58 +305,66 @@ public function enterNode(Node $node)
280305
];
281306

282307
yield [
283-
'/**
284-
* @return Foo
285-
* @param Foo $foo
286-
* @param Bar $bar
287-
*/',
288-
'/**
289-
* @return Bar
290-
* @param Foo $foo
291-
* @param Bar $bar
292-
*/',
308+
self::nowdoc('
309+
/**
310+
* @return Foo
311+
* @param Foo $foo
312+
* @param Bar $bar
313+
*/'),
314+
self::nowdoc('
315+
/**
316+
* @return Bar
317+
* @param Foo $foo
318+
* @param Bar $bar
319+
*/'),
293320
$changeReturnType,
294321
];
295322

296323
yield [
297-
'/**
298-
* @param Foo $foo
299-
* @return Foo
300-
* @param Bar $bar
301-
*/',
302-
'/**
303-
* @param Foo $foo
304-
* @return Bar
305-
* @param Bar $bar
306-
*/',
324+
self::nowdoc('
325+
/**
326+
* @param Foo $foo
327+
* @return Foo
328+
* @param Bar $bar
329+
*/'),
330+
self::nowdoc('
331+
/**
332+
* @param Foo $foo
333+
* @return Bar
334+
* @param Bar $bar
335+
*/'),
307336
$changeReturnType,
308337
];
309338

310339
yield [
311-
'/**
312-
* @return Foo
313-
* @param Foo $foo
314-
* @param Bar $bar
315-
*/',
316-
'/**
317-
* @return Bar
318-
* @param Foo $foo
319-
* @param Bar $bar
320-
*/',
340+
self::nowdoc('
341+
/**
342+
* @return Foo
343+
* @param Foo $foo
344+
* @param Bar $bar
345+
*/'),
346+
self::nowdoc('
347+
/**
348+
* @return Bar
349+
* @param Foo $foo
350+
* @param Bar $bar
351+
*/'),
321352
$changeReturnType,
322353
];
323354

324355
yield [
325-
'/**
326-
* @param Foo $foo Foo description
327-
* @return Foo Foo return description
328-
* @param Bar $bar Bar description
329-
*/',
330-
'/**
331-
* @param Foo $foo Foo description
332-
* @return Bar Foo return description
333-
* @param Bar $bar Bar description
334-
*/',
356+
self::nowdoc('
357+
/**
358+
* @param Foo $foo Foo description
359+
* @return Foo Foo return description
360+
* @param Bar $bar Bar description
361+
*/'),
362+
self::nowdoc('
363+
/**
364+
* @param Foo $foo Foo description
365+
* @return Bar Foo return description
366+
* @param Bar $bar Bar description
367+
*/'),
335368
$changeReturnType,
336369
];
337370

@@ -356,22 +389,26 @@ public function enterNode(Node $node)
356389
];
357390

358391
yield [
359-
'/**
360-
* @param Foo $foo
361-
*/',
362-
'/**
363-
* @param Baz $a
364-
*/',
392+
self::nowdoc('
393+
/**
394+
* @param Foo $foo
395+
*/'),
396+
self::nowdoc('
397+
/**
398+
* @param Baz $a
399+
*/'),
365400
$replaceFirst,
366401
];
367402

368403
yield [
369-
'/**
370-
* @param Foo $foo
371-
*/',
372-
'/**
373-
* @param Baz $a
374-
*/',
404+
self::nowdoc('
405+
/**
406+
* @param Foo $foo
407+
*/'),
408+
self::nowdoc('
409+
/**
410+
* @param Baz $a
411+
*/'),
375412
$replaceFirst,
376413
];
377414

@@ -391,24 +428,28 @@ public function enterNode(Node $node)
391428
};
392429

393430
yield [
394-
'/**
395-
* @param Foo $foo
396-
*/',
397-
'/**
398-
* @param Baz $a
399-
* @param Foo $foo
400-
*/',
431+
self::nowdoc('
432+
/**
433+
* @param Foo $foo
434+
*/'),
435+
self::nowdoc('
436+
/**
437+
* @param Baz $a
438+
* @param Foo $foo
439+
*/'),
401440
$insertFirst,
402441
];
403442

404443
yield [
405-
'/**
406-
* @param Foo $foo
407-
*/',
408-
'/**
409-
* @param Baz $a
410-
* @param Foo $foo
411-
*/',
444+
self::nowdoc('
445+
/**
446+
* @param Foo $foo
447+
*/'),
448+
self::nowdoc('
449+
/**
450+
* @param Baz $a
451+
* @param Foo $foo
452+
*/'),
412453
$insertFirst,
413454
];
414455

@@ -430,52 +471,60 @@ public function enterNode(Node $node)
430471
};
431472

432473
yield [
433-
'/**
434-
* @param Foo $foo
435-
*/',
436-
'/**
437-
* @param Foo $foo
438-
* @param Baz $a
439-
*/',
474+
self::nowdoc('
475+
/**
476+
* @param Foo $foo
477+
*/'),
478+
self::nowdoc('
479+
/**
480+
* @param Foo $foo
481+
* @param Baz $a
482+
*/'),
440483
$insertSecond,
441484
];
442485

443486
yield [
444-
'/**
445-
* @param Foo $foo
446-
* @param Bar $bar
447-
*/',
448-
'/**
449-
* @param Foo $foo
450-
* @param Baz $a
451-
* @param Bar $bar
452-
*/',
487+
self::nowdoc('
488+
/**
489+
* @param Foo $foo
490+
* @param Bar $bar
491+
*/'),
492+
self::nowdoc('
493+
/**
494+
* @param Foo $foo
495+
* @param Baz $a
496+
* @param Bar $bar
497+
*/'),
453498
$insertSecond,
454499
];
455500

456501
yield [
457-
'/**
458-
* @param Foo $foo
459-
* @param Bar $bar
460-
*/',
461-
'/**
462-
* @param Foo $foo
463-
* @param Baz $a
464-
* @param Bar $bar
465-
*/',
502+
self::nowdoc('
503+
/**
504+
* @param Foo $foo
505+
* @param Bar $bar
506+
*/'),
507+
self::nowdoc('
508+
/**
509+
* @param Foo $foo
510+
* @param Baz $a
511+
* @param Bar $bar
512+
*/'),
466513
$insertSecond,
467514
];
468515

469516
yield [
470-
'/**
471-
* @param Foo $foo
472-
* @param Bar $bar
473-
*/',
474-
'/**
475-
* @param Foo $foo
476-
* @param Baz $a
477-
* @param Bar $bar
478-
*/',
517+
self::nowdoc('
518+
/**
519+
* @param Foo $foo
520+
* @param Bar $bar
521+
*/'),
522+
self::nowdoc('
523+
/**
524+
* @param Foo $foo
525+
* @param Baz $a
526+
* @param Bar $bar
527+
*/'),
479528
$insertSecond,
480529
];
481530

@@ -494,24 +543,28 @@ public function enterNode(Node $node)
494543
};
495544

496545
yield [
497-
'/**
498-
* @param Foo $foo
499-
*/',
500-
'/**
501-
* @param Baz $a
502-
*/',
546+
self::nowdoc('
547+
/**
548+
* @param Foo $foo
549+
*/'),
550+
self::nowdoc('
551+
/**
552+
* @param Baz $a
553+
*/'),
503554
$replaceLast,
504555
];
505556

506557
yield [
507-
'/**
508-
* @param Foo $foo
509-
* @param Bar $bar
510-
*/',
511-
'/**
512-
* @param Foo $foo
513-
* @param Baz $a
514-
*/',
558+
self::nowdoc('
559+
/**
560+
* @param Foo $foo
561+
* @param Bar $bar
562+
*/'),
563+
self::nowdoc('
564+
/**
565+
* @param Foo $foo
566+
* @param Baz $a
567+
*/'),
515568
$replaceLast,
516569
];
517570

@@ -529,24 +582,28 @@ public function enterNode(Node $node)
529582
};
530583

531584
yield [
532-
'/**
533-
* @param Bar|Baz $foo
534-
*/',
535-
'/**
536-
* @param Foo|Bar|Baz $foo
537-
*/',
585+
self::nowdoc('
586+
/**
587+
* @param Bar|Baz $foo
588+
*/'),
589+
self::nowdoc('
590+
/**
591+
* @param Foo|Bar|Baz $foo
592+
*/'),
538593
$insertFirstTypeInUnionType,
539594
];
540595

541596
yield [
542-
'/**
543-
* @param Bar|Baz $foo
544-
* @param Foo $bar
545-
*/',
546-
'/**
547-
* @param Foo|Bar|Baz $foo
548-
* @param Foo $bar
549-
*/',
597+
self::nowdoc('
598+
/**
599+
* @param Bar|Baz $foo
600+
* @param Foo $bar
601+
*/'),
602+
self::nowdoc('
603+
/**
604+
* @param Foo|Bar|Baz $foo
605+
* @param Foo $bar
606+
*/'),
550607
$insertFirstTypeInUnionType,
551608
];
552609

@@ -567,12 +624,14 @@ public function enterNode(Node $node)
567624
};
568625

569626
yield [
570-
'/**
571-
* @param Foo|Bar $bar
572-
*/',
573-
'/**
574-
* @param Lorem|Ipsum $bar
575-
*/',
627+
self::nowdoc('
628+
/**
629+
* @param Foo|Bar $bar
630+
*/'),
631+
self::nowdoc('
632+
/**
633+
* @param Lorem|Ipsum $bar
634+
*/'),
576635
$replaceTypesInUnionType,
577636
];
578637

@@ -622,12 +681,14 @@ public function enterNode(Node $node)
622681
];
623682

624683
yield [
625-
'/**
626-
* @param callable(): void $cb
627-
*/',
628-
'/**
629-
* @param callable(Foo $foo, Bar $bar): void $cb
630-
*/',
684+
self::nowdoc('
685+
/**
686+
* @param callable(): void $cb
687+
*/'),
688+
self::nowdoc('
689+
/**
690+
* @param callable(Foo $foo, Bar $bar): void $cb
691+
*/'),
631692
$replaceParametersInCallableType,
632693
];
633694

@@ -645,12 +706,14 @@ public function enterNode(Node $node)
645706
};
646707

647708
yield [
648-
'/**
649-
* @param callable(Foo $foo, Bar $bar): void $cb
650-
*/',
651-
'/**
652-
* @param callable(): void $cb
653-
*/',
709+
self::nowdoc('
710+
/**
711+
* @param callable(Foo $foo, Bar $bar): void $cb
712+
*/'),
713+
self::nowdoc('
714+
/**
715+
* @param callable(): void $cb
716+
*/'),
654717
$removeParametersInCallableType,
655718
];
656719

@@ -668,14 +731,16 @@ public function enterNode(Node $node)
668731
};
669732

670733
yield [
671-
'/**
672-
* @param callable(Foo $foo, Bar $bar): void $cb
673-
* @param callable(): void $cb2
674-
*/',
675-
'/**
676-
* @param Closure(Foo $foo, Bar $bar): void $cb
677-
* @param Closure(): void $cb2
678-
*/',
734+
self::nowdoc('
735+
/**
736+
* @param callable(Foo $foo, Bar $bar): void $cb
737+
* @param callable(): void $cb2
738+
*/'),
739+
self::nowdoc('
740+
/**
741+
* @param Closure(Foo $foo, Bar $bar): void $cb
742+
* @param Closure(): void $cb2
743+
*/'),
679744
$changeCallableTypeIdentifier,
680745
];
681746

@@ -706,120 +771,134 @@ public function enterNode(Node $node)
706771
];
707772

708773
yield [
709-
'/**
710-
* @return array{float, Foo}
711-
*/',
712-
'/**
713-
* @return array{float, int, Foo, string}
714-
*/',
774+
self::nowdoc('
775+
/**
776+
* @return array{float, Foo}
777+
*/'),
778+
self::nowdoc('
779+
/**
780+
* @return array{float, int, Foo, string}
781+
*/'),
715782
$addItemsToArrayShape,
716783
];
717784

718785
yield [
719-
'/**
720-
* @return array{
721-
* float,
722-
* Foo,
723-
* }
724-
*/',
725-
'/**
726-
* @return array{
727-
* float,
728-
* int,
729-
* Foo,
730-
* string,
731-
* }
732-
*/',
786+
self::nowdoc('
787+
/**
788+
* @return array{
789+
* float,
790+
* Foo,
791+
* }
792+
*/'),
793+
self::nowdoc('
794+
/**
795+
* @return array{
796+
* float,
797+
* int,
798+
* Foo,
799+
* string,
800+
* }
801+
*/'),
733802
$addItemsToArrayShape,
734803
];
735804

736805
yield [
737-
'/**
738-
* @return array{
739-
* float,
740-
* Foo
741-
* }
742-
*/',
743-
'/**
744-
* @return array{
745-
* float,
746-
* int,
747-
* Foo,
748-
* string
749-
* }
750-
*/',
806+
self::nowdoc('
807+
/**
808+
* @return array{
809+
* float,
810+
* Foo
811+
* }
812+
*/'),
813+
self::nowdoc('
814+
/**
815+
* @return array{
816+
* float,
817+
* int,
818+
* Foo,
819+
* string
820+
* }
821+
*/'),
751822
$addItemsToArrayShape,
752823
];
753824

754825
yield [
755-
'/**
756-
* @return array{
757-
* float,
758-
* Foo
759-
* }
760-
*/',
761-
'/**
762-
* @return array{
763-
* float,
764-
* int,
765-
* Foo,
766-
* string
767-
* }
768-
*/',
826+
self::nowdoc('
827+
/**
828+
* @return array{
829+
* float,
830+
* Foo
831+
* }
832+
*/'),
833+
self::nowdoc('
834+
/**
835+
* @return array{
836+
* float,
837+
* int,
838+
* Foo,
839+
* string
840+
* }
841+
*/'),
769842
$addItemsToArrayShape,
770843
];
771844

772845
yield [
773-
'/**
774-
* @return array{
775-
* float,
776-
* Foo,
777-
* }
778-
*/',
779-
'/**
780-
* @return array{
781-
* float,
782-
* int,
783-
* Foo,
784-
* string,
785-
* }
786-
*/',
846+
self::nowdoc('
847+
/**
848+
* @return array{
849+
* float,
850+
* Foo,
851+
* }
852+
*/'),
853+
self::nowdoc('
854+
/**
855+
* @return array{
856+
* float,
857+
* int,
858+
* Foo,
859+
* string,
860+
* }
861+
*/'),
787862
$addItemsToArrayShape,
788863
];
789864

790865
yield [
791-
'/**
792-
* @return array{
793-
* float,
794-
* Foo
795-
* }
796-
*/',
797-
'/**
798-
* @return array{
799-
* float,
800-
* int,
801-
* Foo,
802-
* string
803-
* }
804-
*/',
866+
self::nowdoc('
867+
/**
868+
* @return array{
869+
* float,
870+
* Foo
871+
* }
872+
*/'),
873+
self::nowdoc('
874+
/**
875+
* @return array{
876+
* float,
877+
* int,
878+
* Foo,
879+
* string
880+
* }
881+
*/'),
805882
$addItemsToArrayShape,
806883
];
807884

808885
yield [
809-
'/**
810-
* @return array{
811-
* float,
812-
* Foo
813-
* }
814-
*/',
815-
'/**
816-
* @return array{
817-
* float,
818-
* int,
819-
* Foo,
820-
* string
821-
* }
822-
*/',
886+
self::nowdoc('
887+
/**
888+
* @return array{
889+
* float,
890+
* Foo
891+
* }
892+
*/'),
893+
self::nowdoc('
894+
/**
895+
* @return array{
896+
* float,
897+
* int,
898+
* Foo,
899+
* string
900+
* }
901+
*/'),
823902
$addItemsToArrayShape,
824903
];
825904

@@ -837,22 +916,38 @@ public function enterNode(Node $node)
837916
};
838917

839918
yield [
840-
'/**
841-
* @return object{}
842-
*/',
843-
'/**
844-
* @return object{foo: int}
845-
*/',
919+
self::nowdoc('
920+
/**
921+
* @return object{}
922+
*/'),
923+
self::nowdoc('
924+
/**
925+
* @return object{foo: int}
926+
*/'),
846927
$addItemsToObjectShape,
847928
];
848929

849930
yield [
850-
'/**
851-
* @return object{bar: string}
852-
*/',
853-
'/**
854-
* @return object{bar: string, foo: int}
855-
*/',
931+
self::nowdoc('
932+
/**
933+
* @return object{bar: string}
934+
*/'),
935+
self::nowdoc('
936+
/**
937+
* @return object{bar: string, foo: int}
938+
*/'),
939+
$addItemsToObjectShape,
940+
];
941+
942+
yield [
943+
self::nowdoc('
944+
/**
945+
* @return object{bar: string}
946+
*/'),
947+
self::nowdoc('
948+
/**
949+
* @return object{bar: string, foo: int}
950+
*/'),
856951
$addItemsToObjectShape,
857952
];
858953

@@ -1096,36 +1191,42 @@ public function enterNode(Node $node)
10961191
];
10971192

10981193
yield [
1099-
'/**
1100-
* @param int $a
1101-
*/',
1102-
'/**
1103-
* @param int $bz
1104-
*/',
1194+
self::nowdoc('
1195+
/**
1196+
* @param int $a
1197+
*/'),
1198+
self::nowdoc('
1199+
/**
1200+
* @param int $bz
1201+
*/'),
11051202
$changeParameterName,
11061203
];
11071204

11081205
yield [
1109-
'/**
1110-
* @param int $a
1111-
* @return string
1112-
*/',
1113-
'/**
1114-
* @param int $bz
1115-
* @return string
1116-
*/',
1206+
self::nowdoc('
1207+
/**
1208+
* @param int $a
1209+
* @return string
1210+
*/'),
1211+
self::nowdoc('
1212+
/**
1213+
* @param int $bz
1214+
* @return string
1215+
*/'),
11171216
$changeParameterName,
11181217
];
11191218

11201219
yield [
1121-
'/**
1122-
* @param int $a haha description
1123-
* @return string
1124-
*/',
1125-
'/**
1126-
* @param int $bz haha description
1127-
* @return string
1128-
*/',
1220+
self::nowdoc('
1221+
/**
1222+
* @param int $a haha description
1223+
* @return string
1224+
*/'),
1225+
self::nowdoc('
1226+
/**
1227+
* @param int $bz haha description
1228+
* @return string
1229+
*/'),
11291230
$changeParameterName,
11301231
];
11311232

@@ -1161,12 +1262,14 @@ public function enterNode(Node $node)
11611262
];
11621263

11631264
yield [
1164-
'/**
1165-
* @param int $a haha
1166-
*/',
1167-
'/**
1168-
* @param int $a hehe
1169-
*/',
1265+
self::nowdoc('
1266+
/**
1267+
* @param int $a haha
1268+
*/'),
1269+
self::nowdoc('
1270+
/**
1271+
* @param int $a hehe
1272+
*/'),
11701273
$changeParameterDescription,
11711274
];
11721275

@@ -1184,12 +1287,14 @@ public function enterNode(Node $node)
11841287
};
11851288

11861289
yield [
1187-
'/**
1188-
* @param Foo[awesome] $a haha
1189-
*/',
1190-
'/**
1191-
* @param Foo[baz] $a haha
1192-
*/',
1290+
self::nowdoc('
1291+
/**
1292+
* @param Foo[awesome] $a haha
1293+
*/'),
1294+
self::nowdoc('
1295+
/**
1296+
* @param Foo[baz] $a haha
1297+
*/'),
11931298
$changeOffsetAccess,
11941299
];
11951300

@@ -1207,12 +1312,14 @@ public function enterNode(Node $node)
12071312
};
12081313

12091314
yield [
1210-
'/**
1211-
* @phpstan-import-type TypeAlias from AnotherClass as DifferentAlias
1212-
*/',
1213-
'/**
1214-
* @phpstan-import-type TypeAlias from AnotherClass as Ciao
1215-
*/',
1315+
self::nowdoc('
1316+
/**
1317+
* @phpstan-import-type TypeAlias from AnotherClass as DifferentAlias
1318+
*/'),
1319+
self::nowdoc('
1320+
/**
1321+
* @phpstan-import-type TypeAlias from AnotherClass as Ciao
1322+
*/'),
12161323
$changeTypeAliasImportAs,
12171324
];
12181325

@@ -1230,12 +1337,14 @@ public function enterNode(Node $node)
12301337
};
12311338

12321339
yield [
1233-
'/**
1234-
* @phpstan-import-type TypeAlias from AnotherClass as DifferentAlias
1235-
*/',
1236-
'/**
1237-
* @phpstan-import-type TypeAlias from AnotherClass
1238-
*/',
1340+
self::nowdoc('
1341+
/**
1342+
* @phpstan-import-type TypeAlias from AnotherClass as DifferentAlias
1343+
*/'),
1344+
self::nowdoc('
1345+
/**
1346+
* @phpstan-import-type TypeAlias from AnotherClass
1347+
*/'),
12391348
$removeImportAs,
12401349
];
12411350

@@ -1638,15 +1747,19 @@ public function enterNode(Node $node)
16381747
];
16391748

16401749
yield [
1641-
'/** @Foo(
1642-
* 1,
1643-
* 2,
1644-
* ) */',
1645-
'/** @Foo(
1646-
* 1,
1647-
* 2,
1648-
* 3,
1649-
* ) */',
1750+
self::nowdoc('
1751+
/** @Foo(
1752+
* 1,
1753+
* 2,
1754+
* )
1755+
*/'),
1756+
self::nowdoc('
1757+
/** @Foo(
1758+
* 1,
1759+
* 2,
1760+
* 3,
1761+
* )
1762+
*/'),
16501763
new class extends AbstractNodeVisitor {
16511764

16521765
public function enterNode(Node $node)
@@ -2160,4 +2273,40 @@ public function testPrintPhpDocNode(PhpDocNode $node, string $expectedResult): v
21602273
);
21612274
}
21622275

2276+
public static function nowdoc(string $str): string
2277+
{
2278+
$lines = preg_split('/\\n/', $str);
2279+
2280+
if ($lines === false) {
2281+
return '';
2282+
}
2283+
2284+
if (count($lines) < 2) {
2285+
return '';
2286+
}
2287+
2288+
// Toss out the first line
2289+
$lines = array_slice($lines, 1, count($lines) - 1);
2290+
2291+
// normalize any tabs to spaces
2292+
$lines = array_map(static fn ($line) => preg_replace_callback('/(\t+)/m', static function ($matches) {
2293+
$fixed = str_repeat(' ', strlen($matches[1]));
2294+
return $fixed;
2295+
}, $line), $lines);
2296+
2297+
// take the ws from the first line and subtract them from all lines
2298+
$matches = [];
2299+
2300+
if (preg_match('/(^[ \t]+)/', $lines[0] ?? '', $matches) !== 1) {
2301+
return '';
2302+
}
2303+
2304+
$numLines = count($lines);
2305+
for ($i = 0; $i < $numLines; ++$i) {
2306+
$lines[$i] = str_replace($matches[0], '', $lines[$i] ?? '');
2307+
}
2308+
2309+
return implode("\n", $lines);
2310+
}
2311+
21632312
}

0 commit comments

Comments
 (0)
Please sign in to comment.