|
45 | 45 | use PHPStan\PhpDocParser\Parser\TokenIterator; |
46 | 46 | use PHPStan\PhpDocParser\Parser\TypeParser; |
47 | 47 | use PHPUnit\Framework\TestCase; |
| 48 | +use function array_map; |
48 | 49 | use function array_pop; |
| 50 | +use function array_slice; |
49 | 51 | use function array_splice; |
50 | 52 | use function array_unshift; |
51 | 53 | use function array_values; |
52 | 54 | use function count; |
| 55 | +use function implode; |
| 56 | +use function preg_match; |
| 57 | +use function preg_replace_callback; |
| 58 | +use function preg_split; |
| 59 | +use function str_repeat; |
| 60 | +use function str_replace; |
| 61 | +use function strlen; |
53 | 62 | use const PHP_EOL; |
54 | 63 |
|
55 | | -function nowdoc(string $str): string |
| 64 | +class PrinterTest extends TestCase |
56 | 65 | { |
57 | | - $lines = \preg_split('/\\n/', $str); |
58 | 66 |
|
59 | | - if ($lines === false) { |
60 | | - return ''; |
61 | | - } |
| 67 | + /** @var TypeParser */ |
| 68 | + private $typeParser; |
62 | 69 |
|
63 | | - if (\count($lines) <= 2) { |
64 | | - return ''; |
65 | | - } |
| 70 | + /** @var PhpDocParser */ |
| 71 | + private $phpDocParser; |
66 | 72 |
|
67 | | - // Toss out the first line |
68 | | - $lines = \array_slice($lines, 1, \count($lines) - 1); |
| 73 | + static function nowdoc(string $str): string |
| 74 | + { |
| 75 | + $lines = preg_split('/\\n/', $str); |
69 | 76 |
|
70 | | - // normalize any tabs to spaces |
71 | | - $lines = array_map(static function($line) { |
72 | | - return preg_replace_callback('/(\t+)/m', function ($matches) { |
73 | | - $fixed = str_repeat(' ', strlen($matches[1])); |
74 | | - return $fixed; |
75 | | - }, $line); |
76 | | - }, $lines); |
| 77 | + if ($lines === false) { |
| 78 | + return ''; |
| 79 | + } |
77 | 80 |
|
78 | | - // take the ws from the first line and subtract them from all lines |
79 | | - $matches = []; |
80 | | - \preg_match('/(^[ \t]+)/', $lines[0] ?? '', $matches); |
| 81 | + if (count($lines) <= 2) { |
| 82 | + return ''; |
| 83 | + } |
81 | 84 |
|
82 | | - $numLines = \count($lines); |
83 | | - for ($i = 0; $i < $numLines; ++$i) { |
84 | | - $lines[$i] = \str_replace($matches[0], '', $lines[$i]); |
85 | | - } |
| 85 | + // Toss out the first line |
| 86 | + $lines = array_slice($lines, 1, count($lines) - 1); |
86 | 87 |
|
87 | | - return \implode("\n", $lines); |
88 | | -} |
| 88 | + // normalize any tabs to spaces |
| 89 | + $lines = array_map(static function ($line) { |
| 90 | + return preg_replace_callback('/(\t+)/m', static function ($matches) { |
| 91 | + $fixed = str_repeat(' ', strlen($matches[1])); |
| 92 | + return $fixed; |
| 93 | + }, $line); |
| 94 | + }, $lines); |
89 | 95 |
|
90 | | -class PrinterTest extends TestCase |
91 | | -{ |
| 96 | + // take the ws from the first line and subtract them from all lines |
| 97 | + $matches = []; |
| 98 | + preg_match('/(^[ \t]+)/', $lines[0] ?? '', $matches); |
92 | 99 |
|
93 | | - /** @var TypeParser */ |
94 | | - private $typeParser; |
| 100 | + $numLines = count($lines); |
| 101 | + for ($i = 0; $i < $numLines; ++$i) { |
| 102 | + $lines[$i] = str_replace($matches[0], '', $lines[$i] ?? ''); |
| 103 | + } |
95 | 104 |
|
96 | | - /** @var PhpDocParser */ |
97 | | - private $phpDocParser; |
| 105 | + return implode("\n", $lines); |
| 106 | + } |
98 | 107 |
|
99 | 108 | protected function setUp(): void |
100 | 109 | { |
@@ -976,35 +985,35 @@ public function enterNode(Node $node) |
976 | 985 | }; |
977 | 986 |
|
978 | 987 | yield [ |
979 | | - nowdoc(' |
| 988 | + self::nowdoc(' |
980 | 989 | /** |
981 | 990 | * @return object{} |
982 | 991 | */'), |
983 | | - nowdoc(' |
| 992 | + self::nowdoc(' |
984 | 993 | /** |
985 | 994 | * @return object{foo: int} |
986 | 995 | */'), |
987 | 996 | $addItemsToObjectShape, |
988 | 997 | ]; |
989 | 998 |
|
990 | 999 | yield [ |
991 | | - nowdoc(' |
| 1000 | + self::nowdoc(' |
992 | 1001 | /** |
993 | 1002 | * @return object{bar: string} |
994 | 1003 | */'), |
995 | | - nowdoc(' |
| 1004 | + self::nowdoc(' |
996 | 1005 | /** |
997 | 1006 | * @return object{bar: string, foo: int} |
998 | 1007 | */'), |
999 | 1008 | $addItemsToObjectShape, |
1000 | 1009 | ]; |
1001 | 1010 |
|
1002 | 1011 | yield [ |
1003 | | - nowdoc(' |
| 1012 | + self::nowdoc(' |
1004 | 1013 | /** |
1005 | 1014 | * @return object{bar: string} |
1006 | 1015 | */'), |
1007 | | - nowdoc(' |
| 1016 | + self::nowdoc(' |
1008 | 1017 | /** |
1009 | 1018 | * @return object{bar: string, foo: int} |
1010 | 1019 | */'), |
|
0 commit comments