Skip to content

Commit b4ef0d8

Browse files
committed
fix: sort AbstractGeneratorCommand imports in templates with CRLF line endings
1 parent ec777a9 commit b4ef0d8

3 files changed

Lines changed: 34 additions & 19 deletions

File tree

‎system/CLI/AbstractGeneratorCommand.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,13 @@ protected function buildContent(string $class): string
240240

241241
if (
242242
$this->sortImports
243-
&& preg_match('/(?P<imports>(?:^use [^;]+;$\n?)+)/m', $template, $match) === 1
243+
&& preg_match('/(?P<imports>(?:^use [^;]+;\r?$\n?)+)/m', $template, $match) === 1
244244
) {
245-
$imports = explode("\n", trim($match['imports']));
245+
$eol = str_contains($match['imports'], "\r\n") ? "\r\n" : "\n";
246+
$imports = explode("\n", str_replace("\r\n", "\n", trim($match['imports'])));
246247
sort($imports);
247248

248-
return str_replace(trim($match['imports']), implode("\n", $imports), $template);
249+
return str_replace(trim($match['imports']), implode($eol, $imports), $template);
249250
}
250251

251252
return $template;

‎tests/_support/Commands/Modern/ImportSortingGeneratorCommand.php‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,10 @@
2121
#[GeneratorCommand(component: 'Widget', template: 'config.tpl.php', directory: 'Widgets')]
2222
class ImportSortingGeneratorCommand extends AbstractGeneratorCommand
2323
{
24+
public string $renderedTemplate = "<?php\n\nuse App\\Zebra;\nuse App\\Alpha;\n\nclass Foo\n{\n}\n";
25+
2426
protected function renderTemplate(array $data = []): string
2527
{
26-
return <<<'PHP'
27-
<?php
28-
29-
use App\Zebra;
30-
use App\Alpha;
31-
32-
class Foo
33-
{
34-
}
35-
36-
PHP;
28+
return $this->renderedTemplate;
3729
}
3830
}

‎tests/system/CLI/AbstractGeneratorCommandTest.php‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPUnit\Framework\Attributes\After;
2222
use PHPUnit\Framework\Attributes\Before;
2323
use PHPUnit\Framework\Attributes\CoversClass;
24+
use PHPUnit\Framework\Attributes\DataProvider;
2425
use PHPUnit\Framework\Attributes\Group;
2526
use Tests\Support\Commands\Modern\GeneratorFixtureCommand;
2627
use Tests\Support\Commands\Modern\ImportSortingGeneratorCommand;
@@ -413,16 +414,37 @@ public function testAcceptingCodeIgniterNamespacePromptProceeds(): void
413414
$this->assertStringContainsString('File exists: ', $io->getOutput());
414415
}
415416

416-
public function testImportsAreSortedInGeneratedContent(): void
417+
#[DataProvider('provideImportsAreSortedInGeneratedContent')]
418+
public function testImportsAreSortedInGeneratedContent(string $template, string $expected): void
417419
{
418-
$command = new ImportSortingGeneratorCommand(new Commands());
420+
$command = new ImportSortingGeneratorCommand(new Commands());
421+
$command->renderedTemplate = $template;
419422
$command->setInteractive(false);
420423

421424
$command->run(['foo'], []);
422425

423-
$content = file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php');
424-
$this->assertIsString($content);
425-
$this->assertStringContainsString("use App\\Alpha;\nuse App\\Zebra;", $content);
426+
$this->assertSame($expected, file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php'));
427+
}
428+
429+
/**
430+
* @return iterable<string, array{string, string}>
431+
*/
432+
public static function provideImportsAreSortedInGeneratedContent(): iterable
433+
{
434+
yield 'LF' => [
435+
"<?php\n\nuse App\\Zebra;\nuse App\\Alpha;\n\nclass Foo {}\n",
436+
"<?php\n\nuse App\\Alpha;\nuse App\\Zebra;\n\nclass Foo {}\n",
437+
];
438+
439+
yield 'CRLF' => [
440+
"<?php\r\n\r\nuse App\\Zebra;\r\nuse App\\Alpha;\r\n\r\nclass Foo {}\r\n",
441+
"<?php\r\n\r\nuse App\\Alpha;\r\nuse App\\Zebra;\r\n\r\nclass Foo {}\r\n",
442+
];
443+
444+
yield 'mixed' => [
445+
"<?php\r\n\r\nuse App\\Zebra;\r\nuse App\\Beta;\nuse App\\Alpha;\r\n\r\nclass Foo {}\r\n",
446+
"<?php\r\n\r\nuse App\\Alpha;\r\nuse App\\Beta;\r\nuse App\\Zebra;\r\n\r\nclass Foo {}\r\n",
447+
];
426448
}
427449

428450
public function testImportSortingCanBeDisabled(): void

0 commit comments

Comments
 (0)