Skip to content

Commit

Permalink
Merge pull request #719 from msmakouz/bugfix/truncate-postfix
Browse files Browse the repository at this point in the history
Added check before adding postfix in Scaffolder
  • Loading branch information
butschster authored Jun 16, 2022
2 parents 55d9d44 + 3f152f2 commit c18e0b7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/Scaffolder/src/Bootloader/ScaffolderBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ public function init(ConsoleBootloader $console): void
* This is set of comment lines to be applied to every scaffolded file, you can use env() function
* to make it developer specific or set one universal pattern per project.
*/
'header' => [
'{project-name}',
'',
'@author {author-name}',
],
'header' => [],

/*
* Base directory for generated classes, class will be automatically localed into sub directory
Expand Down
2 changes: 1 addition & 1 deletion src/Scaffolder/src/Command/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function perform(): int
$declaration = $this->createDeclaration(CommandDeclaration::class);

$declaration->setAlias((string) ($this->argument('alias') ?? $this->argument('name')));
$declaration->setDescription($this->option('description'));
$declaration->setDescription((string) $this->option('description'));

$this->writeDeclaration($declaration);

Expand Down
5 changes: 4 additions & 1 deletion src/Scaffolder/src/Config/ScaffolderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public function className(string $element, string $name): string
{
['name' => $name] = $this->parseName($name);

return $this->classify($name) . $this->elementPostfix($element);
$class = $this->classify($name);
$postfix = $this->elementPostfix($element);

return \str_ends_with($class, $postfix) ? $class : $class . $postfix;
}

public function classNamespace(string $element, string $name = ''): string
Expand Down
5 changes: 5 additions & 0 deletions src/Scaffolder/tests/App/config/scaffolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
declare(strict_types=1);

return [
'header' => [
'{project-name}',
'',
'@author {author-name}',
],
'directory' => directory('app'),
'namespace' => 'Spiral\\Tests\\Scaffolder\\App',
];
3 changes: 3 additions & 0 deletions src/Scaffolder/tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function testScaffold(string $className, string $name, ?string $alias): v

$reflection = new ReflectionClass($className);
$content = $this->files()->read($reflection->getFileName());
$classNameParts = \explode('\\', $className);

$this->assertStringContainsString('strict_types=1', $content);
$this->assertStringContainsString('{project-name}', $content);
Expand All @@ -55,6 +56,7 @@ public function testScaffold(string $className, string $name, ?string $alias): v
$this->assertTrue($reflection->hasConstant('OPTIONS'));
$this->assertSame($alias ?? $name, $reflection->getConstant('NAME'));
$this->assertSame('My sample command description', $reflection->getConstant('DESCRIPTION'));
$this->assertSame($classNameParts[\array_key_last($classNameParts)], $reflection->getShortName());

$this->deleteDeclaration($className);
}
Expand All @@ -63,6 +65,7 @@ public function commandDataProvider(): array
{
return [
['\\Spiral\\Tests\\Scaffolder\\App\\Command\\SampleCommand', 'sample', null],
['\\Spiral\\Tests\\Scaffolder\\App\\Command\\SomeCommand', 'SomeCommand', null],
['\\Spiral\\Tests\\Scaffolder\\App\\Command\\SampleAliasCommand', 'sampleAlias', 'my-sample-command-alias'],
];
}
Expand Down

0 comments on commit c18e0b7

Please sign in to comment.