Skip to content

Commit 5f39ed7

Browse files
committed
refactoring the rename command
1 parent a2bffeb commit 5f39ed7

File tree

2 files changed

+64
-116
lines changed

2 files changed

+64
-116
lines changed

src/Commands/RenameCommand.php

Lines changed: 47 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Themsaid\Langman\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Support\Str;
78
use Themsaid\Langman\Manager;
89

@@ -13,14 +14,14 @@ class RenameCommand extends Command
1314
*
1415
* @var string
1516
*/
16-
protected $signature = 'langman:rename {key} {as}';
17+
protected $signature = 'langman:rename {oldKey} {newKey}';
1718

1819
/**
1920
* The name and signature of the console command.
2021
*
2122
* @var string
2223
*/
23-
protected $description = 'Remove the given key from all language files.';
24+
protected $description = 'Rename the given key.';
2425

2526
/**
2627
* The Languages manager instance.
@@ -56,129 +57,90 @@ public function __construct(Manager $manager)
5657
*/
5758
public function handle()
5859
{
59-
if ($this->areArgumentsValid()) {
60-
list($file, $key) = explode('.', $this->argument('key'), 2);
60+
$this->renameKey();
6161

62-
$files = $this->manager->files()[$file];
62+
$this->listFilesContainingOldKey();
6363

64-
$this->changeKeyNameInAllLanguageFiles($files, $key);
65-
66-
$this->generateReportForViewFilesAffected();
67-
68-
$this->info("Done!");
69-
}
64+
$this->info("Done!");
7065
}
7166

7267
/**
73-
* Check if both arguments are properly formatted.
68+
* Rename the given oldKey to the newKey.
7469
*
75-
* @return bool
70+
* @return void
7671
*/
77-
protected function areArgumentsValid()
72+
private function renameKey()
7873
{
79-
$areValid = true;
74+
list($file, $key) = explode('.', $this->argument('oldKey'), 2);
8075

81-
if (!$this->isKeyAnArgumentValid()) {
82-
$this->error("Invalid <key> argument format! Pls check and try again.");
83-
$areValid = false;
84-
}
76+
$files = $this->manager->files()[$file];
8577

86-
if (!$this->isAsAnArgumentValid()) {
87-
$this->error("Invalid <as> argument format! Pls check and try again.");
88-
$areValid = false;
89-
}
90-
91-
return $areValid;
92-
}
78+
$newKey = preg_replace('/(\w+)$/i', $this->argument('newKey'), $key);
9379

94-
/**
95-
* Change
96-
*
97-
* @param $file
98-
* @param $key
99-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
100-
*/
101-
private function changeKeyName($file, $key)
102-
{
103-
$content = $this->manager->getFileContent($file);
80+
$currentValues = [];
10481

105-
$oldKeyValue = array_pull($content, $key);
82+
foreach ($files as $languageKey => $filePath) {
83+
$content = Arr::dot($this->manager->getFileContent($filePath));
10684

107-
$newKey = preg_replace('/(\w+)$/i', $this->argument('as'), $key);
85+
$currentValues[$languageKey] = isset($content[$key]) ? $content[$key] : '';
86+
}
10887

109-
array_set($content, $newKey, $oldKeyValue);
88+
$this->manager->removeKey($file, $key);
11089

111-
$this->manager->writeFile($file, $content);
90+
$this->manager->fillKeys(
91+
$file,
92+
[$newKey => $currentValues]
93+
);
11294
}
11395

11496
/**
115-
* @param $files
116-
* @param $key
117-
* @return mixed
97+
* Show a table with application files containing the old key.
98+
*
99+
* @return void
118100
*/
119-
private function changeKeyNameInAllLanguageFiles($files, $key)
101+
private function listFilesContainingOldKey()
120102
{
121-
foreach ($files as $file) {
122-
$this->changeKeyName($file, $key);
123-
}
124-
}
103+
if ($files = $this->getFilesContainingOldKey()) {
104+
$this->info('Renamed key was found in '.count($files).' file(s).');
125105

126-
/**
127-
* @param $affected
128-
*/
129-
private function generateReportForViewFilesAffected()
130-
{
131-
if ($affected = $this->getOnlyViewFilesAffected()) {
132-
$rows = $this->generateReportRows($affected);
133-
$this->info(count($affected) . ' views files has been affected.');
134-
$this->table([ 'Times', 'View File' ], $rows);
106+
$this->table(['Encounters', 'File'], $this->getTableRows($files));
135107
}
136108
}
137109

138110
/**
111+
* Get an array of application files containing the old key.
112+
*
139113
* @return array
140114
*/
141-
private function getOnlyViewFilesAffected()
115+
private function getFilesContainingOldKey()
142116
{
143-
$affected = [ ];
117+
$affectedFiles = [];
144118

145-
foreach ($this->manager->getAllViewFilesWithTranslations() as $file => $references) {
146-
foreach ($references as $reference) {
147-
if ($reference == $this->argument('key')) {
148-
$affected[ $file ][] = $reference;
119+
foreach ($this->manager->getAllViewFilesWithTranslations() as $file => $keys) {
120+
foreach ($keys as $key) {
121+
if ($key == $this->argument('oldKey')) {
122+
$affectedFiles[$file][] = $key;
149123
}
150124
}
151125
}
152-
return $affected;
126+
127+
return $affectedFiles;
153128
}
154129

155130
/**
156-
* @param $affected
157-
* @param $report
131+
* Get table rows for the list of files containing the old key.
132+
*
133+
* @param array $files
158134
* @return array
159135
*/
160-
private function generateReportRows($affected)
136+
private function getTableRows($files)
161137
{
162138
$rows = [];
163-
foreach ($affected as $file => $keys) {
164-
$rows[] = [ count($keys), $file ];
165-
}
166-
return $rows;
167-
}
168139

169-
/**
170-
* @return bool
171-
*/
172-
protected function isKeyAnArgumentValid()
173-
{
174-
return Str::contains($this->argument('key'), '.') && !is_null($this->argument('key'));
175-
}
140+
foreach ($files as $file => $keys) {
141+
$rows[] = [count($keys), $file];
142+
}
176143

177-
/**
178-
* @return bool
179-
*/
180-
protected function isAsAnArgumentValid()
181-
{
182-
return !Str::contains($this->argument('as'), '.') && !is_null($this->argument('as'));
144+
return $rows;
183145
}
184146
}

tests/RenameCommandTest.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?php
22

3-
use Themsaid\Langman\Manager;
4-
53
class RenameCommandTest extends TestCase
64
{
75
public function testRenameAKeyValue()
86
{
97
$this->createTempFiles([
108
'en' => ['user' => "<?php\n return['mobile' => 'Mobile'];"],
119
]);
12-
$expectedValues = ['contact' => 'Mobile'];
1310

14-
$this->artisan('langman:rename', ['key' => 'user.mobile', 'as' => 'contact']);
11+
$this->artisan('langman:rename', ['oldKey' => 'user.mobile', 'newKey' => 'contact']);
1512

1613
$newValue = (array) include $this->app['config']['langman.path'].'/en/user.php';
17-
$this->assertEquals($expectedValues, $newValue);
14+
15+
$this->assertEquals(['contact' => 'Mobile'], $newValue);
1816
}
1917

2018
public function testRenameAKeyValueForAllLanguages()
@@ -26,10 +24,11 @@ public function testRenameAKeyValueForAllLanguages()
2624
$expectedValueEN = ['contact' => 'Mobile'];
2725
$expectedValueES = ['contact' => 'Movil'];
2826

29-
$this->artisan('langman:rename', ['key' => 'user.mobile', 'as' => 'contact']);
27+
$this->artisan('langman:rename', ['oldKey' => 'user.mobile', 'newKey' => 'contact']);
28+
29+
$newValueEN = (array) include $this->app['config']['langman.path'].'/en/user.php';
30+
$newValueES = (array) include $this->app['config']['langman.path'].'/es/user.php';
3031

31-
$newValueEN =(array) include $this->app['config']['langman.path'].'/en/user.php';
32-
$newValueES =(array) include $this->app['config']['langman.path'].'/es/user.php';
3332
$this->assertEquals($expectedValueEN, $newValueEN);
3433
$this->assertEquals($expectedValueES, $newValueES);
3534
}
@@ -43,10 +42,11 @@ public function testRenameANestedKeyValueForAllLanguages()
4342
$expectedValueEN = ['contact' => ['mobile' => 'Mobile']];
4443
$expectedValueES = ['contact' => ['mobile' => 'Movil']];
4544

46-
$this->artisan('langman:rename', ['key' => 'user.contact.cellphone', 'as' => 'mobile']);
45+
$this->artisan('langman:rename', ['oldKey' => 'user.contact.cellphone', 'newKey' => 'mobile']);
4746

48-
$newValueEN =(array) include $this->app['config']['langman.path'].'/en/user.php';
49-
$newValueES =(array) include $this->app['config']['langman.path'].'/es/user.php';
47+
$newValueEN = (array) include $this->app['config']['langman.path'].'/en/user.php';
48+
49+
$newValueES = (array) include $this->app['config']['langman.path'].'/es/user.php';
5050
$this->assertEquals($expectedValueEN, $newValueEN);
5151
$this->assertEquals($expectedValueES, $newValueES);
5252
}
@@ -61,10 +61,10 @@ public function testRenameOfANestedKeyValueForAllLanguagesInAnyDepth()
6161
$expectedValueEN = ['contact' => ['mobile' => 'Mobile', 'others' => ['mail' => 'E-mail']]];
6262
$expectedValueES = ['contact' => ['mobile' => 'Movil', 'others' => ['mail' => 'Correo electronico']]];
6363

64-
$this->artisan('langman:rename', ['key' => 'user.contact.others.msn', 'as' => 'mail']);
64+
$this->artisan('langman:rename', ['oldKey' => 'user.contact.others.msn', 'newKey' => 'mail']);
6565

66-
$newValueEN =(array) include $this->app['config']['langman.path'].'/en/user.php';
67-
$newValueES =(array) include $this->app['config']['langman.path'].'/es/user.php';
66+
$newValueEN = (array) include $this->app['config']['langman.path'].'/en/user.php';
67+
$newValueES = (array) include $this->app['config']['langman.path'].'/es/user.php';
6868
$this->assertEquals($expectedValueEN, $newValueEN);
6969
$this->assertEquals($expectedValueES, $newValueES);
7070
}
@@ -85,29 +85,15 @@ public function testRenameCommandShowViewFilesAffectedForTheChange()
8585
mkdir(__DIR__.'/views_temp/users');
8686
file_put_contents(__DIR__.'/views_temp/users/index.blade.php', "{{ trans('users.name') }} {{ trans('users.city') }} {{ trans('users.name') }}");
8787

88-
$this->artisan('langman:rename', ['key' => 'users.name', 'as' => 'username']);
88+
$this->artisan('langman:rename', ['oldKey' => 'users.name', 'newKey' => 'username']);
8989

9090
array_map('unlink', glob(__DIR__.'/views_temp/users/index.blade.php'));
9191
array_map('rmdir', glob(__DIR__.'/views_temp/users'));
9292
array_map('unlink', glob(__DIR__.'/views_temp/users.blade.php'));
9393

94-
$this->assertContains("2 views files has been affected.\n", $this->consoleOutput());
95-
$this->assertRegExp('/Times(?:.*)View File/', $this->consoleOutput());
94+
$this->assertContains("Renamed key was found in 2 file(s).", $this->consoleOutput());
95+
$this->assertRegExp('/Encounters(?:.*)File/', $this->consoleOutput());
9696
$this->assertRegExp('/1(?:.*)users\.blade\.php/', $this->consoleOutput());
9797
$this->assertRegExp('/2(?:.*)users(\\\|\/)index\.blade\.php/', $this->consoleOutput());
9898
}
99-
100-
public function testThrowErrorMessageForInvalidKeyArgument()
101-
{
102-
$this->artisan('langman:rename', ['key' => 'name', 'as' => 'username']);
103-
104-
$this->assertContains('Invalid <key> argument format! Pls check and try again.', $this->consoleOutput());
105-
}
106-
107-
public function testThrowErrorMessageForInvalidAsArgument()
108-
{
109-
$this->artisan('langman:rename', ['key' => 'user.name', 'as' => 'user.username']);
110-
111-
$this->assertContains('Invalid <as> argument format! Pls check and try again.', $this->consoleOutput());
112-
}
11399
}

0 commit comments

Comments
 (0)