Skip to content

Commit f5aef19

Browse files
committed
refactor: code
refs: #17
1 parent 153c45b commit f5aef19

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

src/Commands/InitCommand.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ class InitCommand extends Command implements Isolatable
6565

6666
public function handle(): void
6767
{
68-
$this->appName = $this->argument('application-name');
69-
70-
$pascalCaseAppName = $this->toPascalCase($this->appName);
71-
72-
if (!$this->isPascalCase($this->appName) && $this->confirm("The application name is not in PascalCase, would you like to use {$pascalCaseAppName}")) {
73-
$this->appName = $pascalCaseAppName;
74-
}
68+
$this->prepareAppName();
7569

7670
$kebabName = Str::kebab($this->appName);
7771

@@ -367,20 +361,14 @@ protected function saveReadme(): void
367361
file_put_contents('README.md', $this->readmeContent);
368362
}
369363

370-
protected function toPascalCase(string $string): string
364+
protected function prepareAppName(): void
371365
{
372-
// Remove non-alphanumeric characters except underscores
373-
$string = preg_replace('/[^a-zA-Z0-9_]/', '', $string);
374-
375-
// Replace underscores with spaces, then uppercase the first letter of each word
376-
$string = ucwords(str_replace('_', ' ', $string));
366+
$this->appName = $this->argument('application-name');
377367

378-
// Remove spaces
379-
return str_replace(' ', '', $string);
380-
}
368+
$pascalCaseAppName = ucfirst(Str::camel($this->appName));
381369

382-
protected function isPascalCase(string $string): bool
383-
{
384-
return preg_match('/^[A-Z][a-zA-Z0-9]*$/', $string);
370+
if ($this->appName !== $pascalCaseAppName && $this->confirm("The application name is not in PascalCase, would you like to use {$pascalCaseAppName}")) {
371+
$this->appName = $pascalCaseAppName;
372+
}
385373
}
386374
}

tests/InitCommandTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ public function testRunWithoutAdminAndReadmeCreation()
1313
$this->mockFileGetContent(
1414
[
1515
'arguments' => ['.env.example'],
16-
'result' => $this->getFixture('env.example.yml'),
16+
'result' => $this->getFixture('env.example_app_name_pascal_case.yml'),
1717
],
1818
[
1919
'arguments' => ['.env.development'],
20-
'result' => $this->getFixture('env.development.yml'),
20+
'result' => $this->getFixture('env.development_app_name_pascal_case.yml'),
2121
],
2222
);
2323

24-
$this->mockFilePutContent();
24+
$this->mockFilePutContent(
25+
'env.example_app_name_pascal_case.yml',
26+
'env.development_app_name_pascal_case.yml',
27+
);
2528

2629
$this->mockShellExec(
2730
['arguments' => 'composer require ronasit/laravel-helpers --ansi'],
@@ -32,8 +35,7 @@ public function testRunWithoutAdminAndReadmeCreation()
3235
);
3336

3437
$this
35-
->artisan('init "My App"')
36-
->expectsConfirmation('The application name is not in PascalCase, would you like to use MyApp')
38+
->artisan('init "MyApp"')
3739
->expectsOutput('Project initialized successfully!')
3840
->expectsQuestion('Please enter an application URL', 'https://mysite.com')
3941
->expectsConfirmation('Do you want to generate an admin user?')

tests/Support/Traits/InitCommandMockTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait InitCommandMockTrait
1212
public function mockFilePutContent(
1313
string $exampleEnvFixtureName = 'env.example.yml',
1414
string $developmentEnvFixtureName = 'env.development.yml',
15-
...$arguments
15+
...$arguments,
1616
): void {
1717
$callChain = [
1818
['.env.example', $this->getFixture($exampleEnvFixtureName), 'optionalParameter', 'optionalParameter'],

0 commit comments

Comments
 (0)