Skip to content

Commit 66835f0

Browse files
committed
refactor: Improve formatting and consistency in RefactorCommand output messages
1 parent 809242a commit 66835f0

1 file changed

Lines changed: 33 additions & 36 deletions

File tree

src/Commands/RefactorCommand.php

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ private function runNormalizeKeys(): int
266266
$this->findNonUppercaseKeys();
267267

268268
if (empty($this->keyNormalizationIssues)) {
269-
$this->info('All enum keys are already UPPERCASE!');
269+
$this->info('All enum keys are already UPPERCASE!');
270270

271271
return self::SUCCESS;
272272
}
@@ -313,7 +313,7 @@ private function runNormalizeKeysInteractive(): int
313313
$this->findNonUppercaseKeys();
314314

315315
if (empty($this->keyNormalizationIssues)) {
316-
$this->info('All enum keys are already UPPERCASE!');
316+
$this->info('All enum keys are already UPPERCASE!');
317317

318318
return self::SUCCESS;
319319
}
@@ -397,7 +397,7 @@ private function loadEnums(): void
397397
}
398398

399399
$count = count($this->enums);
400-
$this->info("✅ Loaded {$count} enum".($count !== 1 ? 's' : ''));
400+
$this->info("✅ Loaded {$count} enum" . ($count !== 1 ? 's' : ''));
401401
$this->newLine();
402402
}
403403

@@ -436,7 +436,7 @@ private function getClassFromFile(string $path): ?string
436436
return null;
437437
}
438438

439-
return $namespaceMatch[1].'\\'.$enumMatch[1];
439+
return $namespaceMatch[1] . '\\' . $enumMatch[1];
440440
}
441441

442442
/**
@@ -479,7 +479,7 @@ private function findNonUppercaseKeys(): void
479479
private function findEnumReferences(string $enumName, string $caseName): array
480480
{
481481
$references = [];
482-
$searchPattern = $enumName.'::'.$caseName;
482+
$searchPattern = $enumName . '::' . $caseName;
483483

484484
$pathOption = $this->option('path');
485485
if ($pathOption) {
@@ -505,7 +505,7 @@ private function findEnumReferences(string $enumName, string $caseName): array
505505
foreach ($lines as $lineNum => $line) {
506506
if (str_contains($line, $searchPattern)) {
507507
$references[] = [
508-
'file' => str_replace(base_path().'/', '', $file->getPathname()),
508+
'file' => str_replace(base_path() . '/', '', $file->getPathname()),
509509
'line' => $lineNum + 1,
510510
'code' => trim($line),
511511
];
@@ -521,7 +521,7 @@ private function findEnumReferences(string $enumName, string $caseName): array
521521
*/
522522
private function displayKeyNormalizationResults(): void
523523
{
524-
$this->warn('⚠️ Found '.count($this->keyNormalizationIssues).' key(s) to normalize:');
524+
$this->warn('⚠️ Found ' . count($this->keyNormalizationIssues) . ' key(s) to normalize:');
525525
$this->newLine();
526526

527527
$totalRefs = 0;
@@ -531,7 +531,7 @@ private function displayKeyNormalizationResults(): void
531531
$totalRefs += $refCount;
532532

533533
$this->line("<fg=cyan>{$issue['enum']}</>");
534-
$this->line(" <fg=red>{$issue['oldKey']}</> → <fg=green>{$issue['newKey']}</> <fg=gray>({$refCount} reference".($refCount !== 1 ? 's' : '').')</>');
534+
$this->line(" <fg=red>{$issue['oldKey']}</> → <fg=green>{$issue['newKey']}</> <fg=gray>({$refCount} reference" . ($refCount !== 1 ? 's' : '') . ')</>');
535535

536536
if ($this->option('detailed') && ! empty($issue['references'])) {
537537
foreach ($issue['references'] as $ref) {
@@ -541,7 +541,7 @@ private function displayKeyNormalizationResults(): void
541541
}
542542

543543
$this->newLine();
544-
$this->info('📊 Total: '.count($this->keyNormalizationIssues)." keys, {$totalRefs} references");
544+
$this->info('📊 Total: ' . count($this->keyNormalizationIssues) . " keys, {$totalRefs} references");
545545
}
546546

547547
/**
@@ -572,14 +572,14 @@ private function applyKeyNormalization(bool $withBackup): int
572572

573573
foreach ($issues as $issue) {
574574
// Replace case declaration: case OldKey = 'value' -> case NEW_KEY = 'value'
575-
$pattern = '/case\s+'.preg_quote($issue['oldKey'], '/').'\s*=/';
576-
$replacement = 'case '.$issue['newKey'].' =';
575+
$pattern = '/case\s+' . preg_quote($issue['oldKey'], '/') . '\s*=/';
576+
$replacement = 'case ' . $issue['newKey'] . ' =';
577577
$content = preg_replace($pattern, $replacement, $content);
578578

579579
// Replace self references within the enum file
580580
$content = str_replace(
581-
'self::'.$issue['oldKey'],
582-
'self::'.$issue['newKey'],
581+
'self::' . $issue['oldKey'],
582+
'self::' . $issue['newKey'],
583583
$content
584584
);
585585

@@ -589,8 +589,8 @@ private function applyKeyNormalization(bool $withBackup): int
589589
file_put_contents($filePath, $content);
590590
$filesChanged++;
591591

592-
$relativePath = str_replace(base_path().'/', '', $filePath);
593-
$this->line("<fg=green>✓</> {$relativePath} <fg=gray>(".count($issues).' keys)</>');
592+
$relativePath = str_replace(base_path() . '/', '', $filePath);
593+
$this->line("<fg=green>✓</> {$relativePath} <fg=gray>(" . count($issues) . ' keys)</>');
594594
}
595595

596596
// Update references throughout the codebase
@@ -622,17 +622,17 @@ private function applyKeyNormalization(bool $withBackup): int
622622
}
623623

624624
foreach ($refs as $ref) {
625-
$search = $ref['enum'].'::'.$ref['oldKey'];
626-
$replace = $ref['enum'].'::'.$ref['newKey'];
625+
$search = $ref['enum'] . '::' . $ref['oldKey'];
626+
$replace = $ref['enum'] . '::' . $ref['newKey'];
627627
$content = str_replace($search, $replace, $content);
628628
$refsUpdated++;
629629
}
630630

631631
file_put_contents($filePath, $content);
632632
$filesChanged++;
633633

634-
$relativePath = str_replace(base_path().'/', '', $filePath);
635-
$this->line("<fg=green>✓</> {$relativePath} <fg=gray>(".count($refs).' refs)</>');
634+
$relativePath = str_replace(base_path() . '/', '', $filePath);
635+
$this->line("<fg=green>✓</> {$relativePath} <fg=gray>(" . count($refs) . ' refs)</>');
636636
}
637637

638638
$this->newLine();
@@ -717,12 +717,9 @@ private function scanDirectory(string $path, ?array $targetEnums = null): void
717717
return;
718718
}
719719

720-
$fileCount = count($phpFiles);
721-
$this->line("Scanning {$fileCount} file".($fileCount !== 1 ? 's' : '').'...');
722-
723-
foreach ($phpFiles as $file) {
720+
$this->withProgressBar($phpFiles, function ($file) use ($targetEnums) {
724721
$this->scanFile($file->getPathname(), $targetEnums);
725-
}
722+
});
726723

727724
$this->newLine();
728725
}
@@ -735,7 +732,7 @@ private function scanDirectory(string $path, ?array $targetEnums = null): void
735732
private function scanFile(string $filePath, ?array $targetEnums = null): void
736733
{
737734
$content = file_get_contents($filePath);
738-
$relativePath = str_replace(base_path().'/', '', $filePath);
735+
$relativePath = str_replace(base_path() . '/', '', $filePath);
739736
$lines = explode("\n", $content);
740737

741738
foreach ($this->patterns as $type => $pattern) {
@@ -878,7 +875,7 @@ private function displayResults(): void
878875
return;
879876
}
880877

881-
$this->warn('⚠️ Found '.count($this->issues).' potential hardcoded enum value(s):');
878+
$this->warn('⚠️ Found ' . count($this->issues) . ' potential hardcoded enum value(s):');
882879
$this->newLine();
883880

884881
$byFile = [];
@@ -887,7 +884,7 @@ private function displayResults(): void
887884
}
888885

889886
foreach ($byFile as $file => $issues) {
890-
$this->line("<fg=cyan>{$file}</> <fg=gray>(".count($issues).' issue'.(count($issues) > 1 ? 's' : '').')</>');
887+
$this->line("<fg=cyan>{$file}</> <fg=gray>(" . count($issues) . ' issue' . (count($issues) > 1 ? 's' : '') . ')</>');
891888

892889
foreach ($issues as $issue) {
893890
$suggestion = $this->generateSuggestion($issue);
@@ -1075,7 +1072,7 @@ private function applyChanges(bool $withBackup): int
10751072
file_put_contents($fullPath, $content);
10761073
$filesChanged++;
10771074

1078-
$this->line("<fg=green>✓</> {$file} <fg=gray>(".count($issues).' changes)</>');
1075+
$this->line("<fg=green>✓</> {$file} <fg=gray>(" . count($issues) . ' changes)</>');
10791076
}
10801077

10811078
$this->newLine();
@@ -1093,14 +1090,14 @@ private function applyChanges(bool $withBackup): int
10931090
*/
10941091
private function createBackup(string $fullPath, string $content): void
10951092
{
1096-
$backupDir = storage_path('app/enumify-refactor-backups/'.date('Y-m-d_His'));
1093+
$backupDir = storage_path('app/enumify-refactor-backups/' . date('Y-m-d_His'));
10971094

10981095
if (! is_dir($backupDir)) {
10991096
mkdir($backupDir, 0755, true);
11001097
}
11011098

1102-
$relativePath = str_replace(base_path().'/', '', $fullPath);
1103-
$backupPath = $backupDir.'/'.str_replace('/', '_', $relativePath);
1099+
$relativePath = str_replace(base_path() . '/', '', $fullPath);
1100+
$backupPath = $backupDir . '/' . str_replace('/', '_', $relativePath);
11041101

11051102
file_put_contents($backupPath, $content);
11061103
$this->backups[$fullPath] = $backupPath;
@@ -1140,10 +1137,10 @@ private function addImports(string $content, array $imports): string
11401137
$lastUse = end($useMatches[0]);
11411138
$insertPos = $namespaceEnd + $lastUse[1] + mb_strlen($lastUse[0]);
11421139

1143-
return mb_substr($content, 0, $insertPos)."\n".implode("\n", $newImports).mb_substr($content, $insertPos);
1140+
return mb_substr($content, 0, $insertPos) . "\n" . implode("\n", $newImports) . mb_substr($content, $insertPos);
11441141
}
11451142

1146-
return mb_substr($content, 0, $namespaceEnd)."\n\n".implode("\n", $newImports).$afterNamespace;
1143+
return mb_substr($content, 0, $namespaceEnd) . "\n\n" . implode("\n", $newImports) . $afterNamespace;
11471144
}
11481145

11491146
/**
@@ -1223,12 +1220,12 @@ private function generateMarkdownReport(): string
12231220
$lines = [
12241221
'# Enumify Refactor Report',
12251222
'',
1226-
'Generated: '.now()->format('Y-m-d H:i:s'),
1223+
'Generated: ' . now()->format('Y-m-d H:i:s'),
12271224
'',
12281225
'## Summary',
12291226
'',
1230-
'- **Total Issues:** '.count($this->issues),
1231-
'- **Enums Scanned:** '.count($this->enums),
1227+
'- **Total Issues:** ' . count($this->issues),
1228+
'- **Enums Scanned:** ' . count($this->enums),
12321229
'',
12331230
'## Issues by File',
12341231
'',

0 commit comments

Comments
 (0)