Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions system/CLI/AbstractGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,13 @@ protected function buildContent(string $class): string

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

return str_replace(trim($match['imports']), implode("\n", $imports), $template);
return str_replace(trim($match['imports']), implode($eol, $imports), $template);
}

return $template;
Expand Down
14 changes: 3 additions & 11 deletions tests/_support/Commands/Modern/ImportSortingGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@
#[GeneratorCommand(component: 'Widget', template: 'config.tpl.php', directory: 'Widgets')]
class ImportSortingGeneratorCommand extends AbstractGeneratorCommand
{
public string $renderedTemplate = "<?php\n\nuse App\\Zebra;\nuse App\\Alpha;\n\nclass Foo\n{\n}\n";

protected function renderTemplate(array $data = []): string
{
return <<<'PHP'
<?php

use App\Zebra;
use App\Alpha;

class Foo
{
}

PHP;
return $this->renderedTemplate;
}
}
32 changes: 27 additions & 5 deletions tests/system/CLI/AbstractGeneratorCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Commands\Modern\GeneratorFixtureCommand;
use Tests\Support\Commands\Modern\ImportSortingGeneratorCommand;
Expand Down Expand Up @@ -498,16 +499,37 @@ protected function execute(array $arguments, array $options): int
};
}

public function testImportsAreSortedInGeneratedContent(): void
#[DataProvider('provideImportsAreSortedInGeneratedContent')]
public function testImportsAreSortedInGeneratedContent(string $template, string $expected): void
{
$command = new ImportSortingGeneratorCommand(new Commands());
$command = new ImportSortingGeneratorCommand(new Commands());
$command->renderedTemplate = $template;
$command->setInteractive(false);

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

$content = file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php');
$this->assertIsString($content);
$this->assertStringContainsString("use App\\Alpha;\nuse App\\Zebra;", $content);
$this->assertSame($expected, file_get_contents(APPPATH . 'Widgets' . DIRECTORY_SEPARATOR . 'Foo.php'));
}

/**
* @return iterable<string, array{string, string}>
*/
public static function provideImportsAreSortedInGeneratedContent(): iterable
{
yield 'LF' => [
"<?php\n\nuse App\\Zebra;\nuse App\\Alpha;\n\nclass Foo {}\n",
"<?php\n\nuse App\\Alpha;\nuse App\\Zebra;\n\nclass Foo {}\n",
];

yield 'CRLF' => [
"<?php\r\n\r\nuse App\\Zebra;\r\nuse App\\Alpha;\r\n\r\nclass Foo {}\r\n",
"<?php\r\n\r\nuse App\\Alpha;\r\nuse App\\Zebra;\r\n\r\nclass Foo {}\r\n",
];

yield 'mixed' => [
"<?php\r\n\r\nuse App\\Zebra;\r\nuse App\\Beta;\nuse App\\Alpha;\r\n\r\nclass Foo {}\r\n",
"<?php\r\n\r\nuse App\\Alpha;\r\nuse App\\Beta;\r\nuse App\\Zebra;\r\n\r\nclass Foo {}\r\n",
];
}

public function testImportSortingCanBeDisabled(): void
Expand Down
Loading