Skip to content

Commit 7b6ae8f

Browse files
authored
fix: sort AbstractGeneratorCommand imports in templates with CRLF line endings (#10623)
1 parent b18f4b7 commit 7b6ae8f

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
@@ -255,12 +255,13 @@ protected function buildContent(string $class): string
255255

256256
if (
257257
$this->sortImports
258-
&& preg_match('/(?P<imports>(?:^use [^;]+;$\n?)+)/m', $template, $match) === 1
258+
&& preg_match('/(?P<imports>(?:^use [^;]+;\r?$\n?)+)/m', $template, $match) === 1
259259
) {
260-
$imports = explode("\n", trim($match['imports']));
260+
$eol = str_contains($match['imports'], "\r\n") ? "\r\n" : "\n";
261+
$imports = explode("\n", str_replace("\r\n", "\n", trim($match['imports'])));
261262
sort($imports);
262263

263-
return str_replace(trim($match['imports']), implode("\n", $imports), $template);
264+
return str_replace(trim($match['imports']), implode($eol, $imports), $template);
264265
}
265266

266267
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
@@ -23,6 +23,7 @@
2323
use PHPUnit\Framework\Attributes\After;
2424
use PHPUnit\Framework\Attributes\Before;
2525
use PHPUnit\Framework\Attributes\CoversClass;
26+
use PHPUnit\Framework\Attributes\DataProvider;
2627
use PHPUnit\Framework\Attributes\Group;
2728
use Tests\Support\Commands\Modern\GeneratorFixtureCommand;
2829
use Tests\Support\Commands\Modern\ImportSortingGeneratorCommand;
@@ -498,16 +499,37 @@ protected function execute(array $arguments, array $options): int
498499
};
499500
}
500501

501-
public function testImportsAreSortedInGeneratedContent(): void
502+
#[DataProvider('provideImportsAreSortedInGeneratedContent')]
503+
public function testImportsAreSortedInGeneratedContent(string $template, string $expected): void
502504
{
503-
$command = new ImportSortingGeneratorCommand(new Commands());
505+
$command = new ImportSortingGeneratorCommand(new Commands());
506+
$command->renderedTemplate = $template;
504507
$command->setInteractive(false);
505508

506509
$command->run(['foo'], []);
507510

508-
$content = file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php');
509-
$this->assertIsString($content);
510-
$this->assertStringContainsString("use App\\Alpha;\nuse App\\Zebra;", $content);
511+
$this->assertSame($expected, file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php'));
512+
}
513+
514+
/**
515+
* @return iterable<string, array{string, string}>
516+
*/
517+
public static function provideImportsAreSortedInGeneratedContent(): iterable
518+
{
519+
yield 'LF' => [
520+
"<?php\n\nuse App\\Zebra;\nuse App\\Alpha;\n\nclass Foo {}\n",
521+
"<?php\n\nuse App\\Alpha;\nuse App\\Zebra;\n\nclass Foo {}\n",
522+
];
523+
524+
yield 'CRLF' => [
525+
"<?php\r\n\r\nuse App\\Zebra;\r\nuse App\\Alpha;\r\n\r\nclass Foo {}\r\n",
526+
"<?php\r\n\r\nuse App\\Alpha;\r\nuse App\\Zebra;\r\n\r\nclass Foo {}\r\n",
527+
];
528+
529+
yield 'mixed' => [
530+
"<?php\r\n\r\nuse App\\Zebra;\r\nuse App\\Beta;\nuse App\\Alpha;\r\n\r\nclass Foo {}\r\n",
531+
"<?php\r\n\r\nuse App\\Alpha;\r\nuse App\\Beta;\r\nuse App\\Zebra;\r\n\r\nclass Foo {}\r\n",
532+
];
511533
}
512534

513535
public function testImportSortingCanBeDisabled(): void

0 commit comments

Comments
 (0)