Skip to content

Commit 4f7f418

Browse files
committed
refactoring
1 parent faf8d69 commit 4f7f418

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

src/Commands/MissingCommand.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,42 +95,36 @@ private function collectValues(array $missing)
9595
$values = [];
9696

9797
foreach ($missing as $missingKey) {
98-
// var_dump($this->getDefaultValueFor($missingKey));
9998
$values[$missingKey] = $this->ask(
100-
"<fg=yellow>{$missingKey}</> translation", $this->getDefaultValueFor($missingKey)
99+
"<fg=yellow>{$missingKey}</> translation", $this->getDefaultValue($missingKey)
101100
);
102101
}
103102

104103
return $values;
105104
}
106105

107106
/**
108-
* Get default value for the current translation request.
109-
* @param string $missingKey
110-
* @return mixed
111-
*/
107+
* Get translation in default locale for the given key.
108+
*
109+
* @param string $missingKey
110+
* @return string
111+
*/
112112
private function getDefaultValue($missingKey)
113113
{
114-
return $this->option('default') ? $this->getTranslationInDefaultLocaleFor($missingKey) : null;
115-
}
114+
if (! $this->option('default')) {
115+
return null;
116+
}
116117

117-
/**
118-
* Get translation in default locale for given key
119-
* @param string $missingKey
120-
* @return string
121-
*/
122-
private function getTranslationInDefaultLocaleFor($missingKey)
123-
{
124118
try {
125-
list($langKey, $lang) = explode(':', $missingKey);
119+
$missingKey = explode(':', $missingKey)[0];
126120

127-
list($file, $key) = explode('.', $langKey);
121+
list($file, $key) = explode('.', $missingKey);
128122

129123
$filePath = $this->manager->files()[$file][config('app.locale')];
130124

131-
return config('app.locale') . ":{$this->manager->getFileContent($filePath)[$key]}";
125+
return config('app.locale').":{$this->manager->getFileContent($filePath)[$key]}";
132126
} catch (\Exception $e) {
133-
return "Sorry. File Language not exists for default locale.";
127+
return null;
134128
}
135129
}
136130

tests/MissingCommandTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function testCommandOutput()
2222
]);
2323

2424
$command = m::mock('\Themsaid\Langman\Commands\MissingCommand[ask]', [$manager]);
25-
$command->shouldReceive('getDefaultValueFor')->times(7)->with(m::any())->andReturn(null);
2625
$command->shouldReceive('ask')->once()->with('/user\.age:nl/', null)->andReturn('fill_age');
2726
$command->shouldReceive('ask')->once()->with('/product\.name:en/', null)->andReturn('fill_name');
2827
$command->shouldReceive('ask')->once()->with('/product\.color:nl/', null)->andReturn('fill_color');
@@ -51,7 +50,10 @@ public function testCommandOutput()
5150

5251
public function testAllowSeeTranslationInDefaultLanguage()
5352
{
53+
$manager = $this->app[Manager::class];
54+
5455
$this->app['config']->set('app.locale', 'en');
56+
5557
$this->createTempFiles([
5658
'en' => [
5759
'user' => "<?php\n return ['name' => 'Name', 'age' => 'Age'];",
@@ -60,18 +62,21 @@ public function testAllowSeeTranslationInDefaultLanguage()
6062
'user' => "<?php\n return ['name' => 'Naam'];",
6163
],
6264
]);
63-
$manager = $this->app[Manager::class];
65+
6466
$command = m::mock('\Themsaid\Langman\Commands\MissingCommand[ask]', [$manager]);
65-
$command->shouldReceive('getDefaultValueFor')->once()->with('/user\.age/')->andReturn('en:Age');
6667
$command->shouldReceive('ask')->once()->with('/<fg=yellow>user\.age:nl<\/> translation/', '/en:Age/');
68+
6769
$this->app['artisan']->add($command);
6870

6971
$this->artisan('langman:missing', ['--default' => true]);
7072
}
7173

72-
public function testThrowDefaultMessageWhenLanguageFileIsNotFound()
74+
public function testShowsNoDefaultWhenDefaultLanguageFileIsNotFound()
7375
{
76+
$manager = $this->app[Manager::class];
77+
7478
$this->app['config']->set('app.locale', 'es');
79+
7580
$this->createTempFiles([
7681
'en' => [
7782
'user' => "<?php\n return ['name' => 'Name', 'age' => 'Age'];",
@@ -80,10 +85,10 @@ public function testThrowDefaultMessageWhenLanguageFileIsNotFound()
8085
'user' => "<?php\n return ['name' => 'Naam'];",
8186
],
8287
]);
83-
$manager = $this->app[Manager::class];
88+
8489
$command = m::mock('\Themsaid\Langman\Commands\MissingCommand[ask]', [$manager]);
85-
$command->shouldReceive('getDefaultValueFor')->once()->with('/user\.age/')->andReturn("Sorry. File Language not exists for default locale.");
86-
$command->shouldReceive('ask')->once()->with('/<fg=yellow>user\.age:nl<\/> translation/', '/Sorry\. File Language not exists for default locale\./');
90+
$command->shouldReceive('ask')->once()->with('/<fg=yellow>user\.age:nl<\/> translation/', null);
91+
8792
$this->app['artisan']->add($command);
8893

8994
$this->artisan('langman:missing', ['--default' => true]);

0 commit comments

Comments
 (0)