Skip to content

Commit f47c00c

Browse files
committed
Introduce prefix, comment, blockComment and docComment methods
1 parent f161bd1 commit f47c00c

File tree

2 files changed

+348
-0
lines changed

2 files changed

+348
-0
lines changed

src/CodeGenerator.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,62 @@ public static function resolveIterable(array | Closure | Generator | string $ite
637637

638638
return $resolved;
639639
}
640+
641+
/**
642+
* Adds a prefix to every line of the iterable
643+
*
644+
* @param CodeLines $data
645+
* @return Generator<string|Group>
646+
*/
647+
public function prefix(string $prefix, array | Closure | Generator | string $data) : Generator
648+
{
649+
foreach (CodeGenerator::resolveIterable($data) as $line) {
650+
if ($line instanceof Group) {
651+
yield Group::indent($this->prefix($prefix, $line->lines), $line->indention);
652+
653+
continue;
654+
}
655+
656+
foreach (explode(PHP_EOL, $line) as $singleLine) {
657+
yield $prefix . $singleLine;
658+
}
659+
}
660+
}
661+
662+
/**
663+
* Adds single-line comment prefix to every line
664+
*
665+
* @param CodeLines $data
666+
* @return Generator<string|Group>
667+
*/
668+
public function comment(array | Closure | Generator | string $data) : Generator
669+
{
670+
yield from $this->prefix('// ', $data);
671+
}
672+
673+
/**
674+
* Wraps content in a block comment
675+
*
676+
* @param CodeLines $data
677+
* @return Generator<string|Group>
678+
*/
679+
public function blockComment(array | Closure | Generator | string $data) : Generator
680+
{
681+
foreach ($this->maybeDump(['/*'], $this->prefix(' * ', $data), [' */']) as $line) {
682+
yield $line;
683+
}
684+
}
685+
686+
/**
687+
* Wraps content in a PHPDoc comment
688+
*
689+
* @param CodeLines $data
690+
* @return Generator<string|Group>
691+
*/
692+
public function docComment(array | Closure | Generator | string $data) : Generator
693+
{
694+
foreach ($this->maybeDump(['/**'], $this->prefix(' * ', $data), [' */']) as $line) {
695+
yield $line;
696+
}
697+
}
640698
}

tests/CodeGeneratorTest.php

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,4 +1167,294 @@ public function nested()
11671167

11681168
self::assertSame($expected, $code);
11691169
}
1170+
1171+
public function testPrefixWithSimpleArray() : void
1172+
{
1173+
$generator = new CodeGenerator();
1174+
1175+
$result = iterator_to_array($generator->prefix('// ', ['line1', 'line2', 'line3']));
1176+
1177+
self::assertSame(['// line1', '// line2', '// line3'], $result);
1178+
}
1179+
1180+
public function testPrefixWithMultilineString() : void
1181+
{
1182+
$generator = new CodeGenerator();
1183+
1184+
$result = iterator_to_array($generator->prefix('> ', "first line\nsecond line\nthird line"));
1185+
1186+
self::assertSame(['> first line', '> second line', '> third line'], $result);
1187+
}
1188+
1189+
public function testPrefixWithGroup() : void
1190+
{
1191+
$generator = new CodeGenerator();
1192+
1193+
$data = function () {
1194+
yield 'class Example';
1195+
yield '{';
1196+
yield Group::indent([
1197+
'public function test()',
1198+
'{',
1199+
Group::indent(['return true;']),
1200+
'}',
1201+
]);
1202+
yield '}';
1203+
};
1204+
1205+
$prefixed = $generator->prefix('// ', $data);
1206+
1207+
$code = $generator->dump($prefixed);
1208+
1209+
$expected = <<<'PHP'
1210+
<?php
1211+
1212+
declare(strict_types=1);
1213+
1214+
// class Example
1215+
// {
1216+
// public function test()
1217+
// {
1218+
// return true;
1219+
// }
1220+
// }
1221+
1222+
PHP;
1223+
1224+
self::assertSame($expected, $code);
1225+
}
1226+
1227+
public function testCommentWithSimpleArray() : void
1228+
{
1229+
$generator = new CodeGenerator();
1230+
1231+
$result = iterator_to_array($generator->comment(['line1', 'line2', 'line3']));
1232+
1233+
self::assertSame(['// line1', '// line2', '// line3'], $result);
1234+
}
1235+
1236+
public function testCommentWithMultilineString() : void
1237+
{
1238+
$generator = new CodeGenerator();
1239+
1240+
$result = iterator_to_array($generator->comment("first line\nsecond line"));
1241+
1242+
self::assertSame(['// first line', '// second line'], $result);
1243+
}
1244+
1245+
public function testBlockComment() : void
1246+
{
1247+
$generator = new CodeGenerator();
1248+
1249+
$data = [
1250+
'This is a block comment',
1251+
'with multiple lines',
1252+
'of text',
1253+
];
1254+
1255+
$result = iterator_to_array($generator->blockComment($data));
1256+
1257+
self::assertSame([
1258+
'/*',
1259+
' * This is a block comment',
1260+
' * with multiple lines',
1261+
' * of text',
1262+
' */',
1263+
], $result);
1264+
}
1265+
1266+
public function testBlockCommentWithMultilineString() : void
1267+
{
1268+
$generator = new CodeGenerator();
1269+
1270+
$result = iterator_to_array($generator->blockComment("Line 1\nLine 2\nLine 3"));
1271+
1272+
self::assertSame([
1273+
'/*',
1274+
' * Line 1',
1275+
' * Line 2',
1276+
' * Line 3',
1277+
' */',
1278+
], $result);
1279+
}
1280+
1281+
public function testDocComment() : void
1282+
{
1283+
$generator = new CodeGenerator();
1284+
1285+
$data = [
1286+
'This is a PHPDoc comment',
1287+
'@param string $name The name parameter',
1288+
'@return void',
1289+
];
1290+
1291+
$result = iterator_to_array($generator->docComment($data));
1292+
1293+
self::assertSame([
1294+
'/**',
1295+
' * This is a PHPDoc comment',
1296+
' * @param string $name The name parameter',
1297+
' * @return void',
1298+
' */',
1299+
], $result);
1300+
}
1301+
1302+
public function testDocCommentWithFullDump() : void
1303+
{
1304+
$generator = new CodeGenerator();
1305+
1306+
$code = $generator->dump([
1307+
$generator->docComment([
1308+
'Calculate the sum of two numbers',
1309+
'',
1310+
'@param int $a First number',
1311+
'@param int $b Second number',
1312+
'@return int The sum',
1313+
]),
1314+
'function add(int $a, int $b): int',
1315+
'{',
1316+
Group::indent(['return $a + $b;']),
1317+
'}',
1318+
]);
1319+
1320+
$expected = <<<'PHP'
1321+
<?php
1322+
1323+
declare(strict_types=1);
1324+
1325+
/**
1326+
* Calculate the sum of two numbers
1327+
*
1328+
* @param int $a First number
1329+
* @param int $b Second number
1330+
* @return int The sum
1331+
*/
1332+
function add(int $a, int $b): int
1333+
{
1334+
return $a + $b;
1335+
}
1336+
1337+
PHP;
1338+
1339+
self::assertSame($expected, $code);
1340+
}
1341+
1342+
public function testCommentWithGroup() : void
1343+
{
1344+
$generator = new CodeGenerator();
1345+
1346+
$data = [
1347+
'class Test',
1348+
'{',
1349+
Group::indent([
1350+
'public function method()',
1351+
'{',
1352+
Group::indent(['return true;']),
1353+
'}',
1354+
]),
1355+
'}',
1356+
];
1357+
1358+
$commented = $generator->comment($data);
1359+
$code = $generator->dump($commented);
1360+
1361+
$expected = <<<'PHP'
1362+
<?php
1363+
1364+
declare(strict_types=1);
1365+
1366+
// class Test
1367+
// {
1368+
// public function method()
1369+
// {
1370+
// return true;
1371+
// }
1372+
// }
1373+
1374+
PHP;
1375+
1376+
self::assertSame($expected, $code);
1377+
}
1378+
1379+
public function testBlockCommentWithGroup() : void
1380+
{
1381+
$generator = new CodeGenerator();
1382+
1383+
$data = [
1384+
'Example code:',
1385+
Group::indent([
1386+
'if ($condition) {',
1387+
Group::indent(['return true;']),
1388+
'}',
1389+
]),
1390+
];
1391+
1392+
$result = $generator->blockComment($data);
1393+
$code = $generator->dump($result);
1394+
1395+
$expected = <<<'PHP'
1396+
<?php
1397+
1398+
declare(strict_types=1);
1399+
1400+
/*
1401+
* Example code:
1402+
* if ($condition) {
1403+
* return true;
1404+
* }
1405+
*/
1406+
1407+
PHP;
1408+
1409+
self::assertSame($expected, $code);
1410+
}
1411+
1412+
public function testBlockCommentWithEmptyContent() : void
1413+
{
1414+
$generator = new CodeGenerator();
1415+
1416+
$result = iterator_to_array($generator->blockComment([]));
1417+
1418+
self::assertSame([], $result);
1419+
}
1420+
1421+
public function testDocCommentWithEmptyContent() : void
1422+
{
1423+
$generator = new CodeGenerator();
1424+
1425+
$result = iterator_to_array($generator->docComment([]));
1426+
1427+
self::assertSame([], $result);
1428+
}
1429+
1430+
public function testCommentWithEmptyContent() : void
1431+
{
1432+
$generator = new CodeGenerator();
1433+
1434+
$result = iterator_to_array($generator->comment([]));
1435+
1436+
self::assertSame([], $result);
1437+
}
1438+
1439+
public function testBlockCommentWithEmptyString() : void
1440+
{
1441+
$generator = new CodeGenerator();
1442+
1443+
$result = iterator_to_array($generator->blockComment(''));
1444+
1445+
self::assertSame([
1446+
'/*',
1447+
' * ',
1448+
' */',
1449+
], $result);
1450+
}
1451+
1452+
public function testDocCommentWithEmptyGenerator() : void
1453+
{
1454+
$generator = new CodeGenerator();
1455+
1456+
$result = iterator_to_array($generator->docComment([]));
1457+
1458+
self::assertSame([], $result);
1459+
}
11701460
}

0 commit comments

Comments
 (0)