Skip to content

Commit 5b4104e

Browse files
authored
Merge pull request #34 from kg-bot/analysis-8w7e3o
Apply fixes from StyleCI
2 parents 47481c0 + 0fcfc7b commit 5b4104e

File tree

2 files changed

+103
-119
lines changed

2 files changed

+103
-119
lines changed

src/Classes/ExportLocalizations.php

+92-108
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ExportLocalizations implements \JsonSerializable
3131
/**
3232
* @var string
3333
*/
34-
protected $excludePath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR;
34+
protected $excludePath = DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR;
3535

3636
/**
3737
* @var string
@@ -46,41 +46,37 @@ class ExportLocalizations implements \JsonSerializable
4646
public function export()
4747
{
4848
// Check if value is cached and set array to cached version
49-
if ( Cache::has( config( 'laravel-localization.caches.key' ) ) ) {
50-
$this->strings = Cache::get( config( 'laravel-localization.caches.key' ) );
49+
if (Cache::has(config('laravel-localization.caches.key'))) {
50+
$this->strings = Cache::get(config('laravel-localization.caches.key'));
5151

5252
return $this;
5353
}
5454

55-
foreach ( config( 'laravel-localization.paths.lang_dirs' ) as $dir ) {
56-
55+
foreach (config('laravel-localization.paths.lang_dirs') as $dir) {
5756
try {
5857

5958
// Collect language files and build array with translations
60-
$files = $this->findLanguageFiles( $dir );
59+
$files = $this->findLanguageFiles($dir);
6160

6261
// Parse translations and create final array
63-
array_walk( $files[ 'lang' ], [ $this, 'parseLangFiles' ], $dir );
64-
array_walk( $files[ 'vendor' ], [ $this, 'parseVendorFiles' ], $dir );
65-
array_walk( $files[ 'json' ], [ $this, 'parseJsonFiles' ], $dir );
66-
67-
} catch ( \Exception $exception ) {
68-
69-
\Log::critical( 'Can\'t read lang directory ' . $dir . ', error: ' . $exception->getMessage() );
62+
array_walk($files['lang'], [$this, 'parseLangFiles'], $dir);
63+
array_walk($files['vendor'], [$this, 'parseVendorFiles'], $dir);
64+
array_walk($files['json'], [$this, 'parseJsonFiles'], $dir);
65+
} catch (\Exception $exception) {
66+
\Log::critical('Can\'t read lang directory '.$dir.', error: '.$exception->getMessage());
7067
}
71-
7268
}
7369

7470
// Trigger event for final translated array
75-
event( new LaravelLocalizationExported( $this->strings ) );
71+
event(new LaravelLocalizationExported($this->strings));
7672

7773
// If timeout > 0 save array to cache
78-
if ( config( 'laravel-localization.caches.timeout', 0 ) > 0 ) {
79-
Cache::store( config( 'laravel-localization.caches.driver', 'file' ) )
74+
if (config('laravel-localization.caches.timeout', 0) > 0) {
75+
Cache::store(config('laravel-localization.caches.driver', 'file'))
8076
->put(
81-
config( 'laravel-localization.caches.key', 'localization.array' ),
77+
config('laravel-localization.caches.key', 'localization.array'),
8278
$this->strings,
83-
config( 'laravel-localization.caches.timeout', 60 )
79+
config('laravel-localization.caches.timeout', 60)
8480
);
8581
}
8682

@@ -94,58 +90,58 @@ public function export()
9490
*
9591
* @return array
9692
*/
97-
protected function findLanguageFiles( $path )
93+
protected function findLanguageFiles($path)
9894
{
9995
// Loop through directories
100-
$dirIterator = new \RecursiveDirectoryIterator( $path, \RecursiveDirectoryIterator::SKIP_DOTS );
101-
$recIterator = new \RecursiveIteratorIterator( $dirIterator );
96+
$dirIterator = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
97+
$recIterator = new \RecursiveIteratorIterator($dirIterator);
10298

10399
// Fetch only php files - skip others
104100
$phpFiles = array_values(
105-
array_map( 'current',
101+
array_map('current',
106102
iterator_to_array(
107-
new \RegexIterator( $recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH )
103+
new \RegexIterator($recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH)
108104
)
109105
)
110106
);
111107

112108
$jsonFiles = array_values(
113-
array_map( 'current',
109+
array_map('current',
114110
iterator_to_array(
115-
new \RegexIterator( $recIterator, $this->jsonRegex, \RecursiveRegexIterator::GET_MATCH )
111+
new \RegexIterator($recIterator, $this->jsonRegex, \RecursiveRegexIterator::GET_MATCH)
116112
)
117113
)
118114
);
119115

120-
$files = array_merge( $phpFiles, $jsonFiles );
116+
$files = array_merge($phpFiles, $jsonFiles);
121117

122118
// Sort array by filepath
123-
sort( $files );
119+
sort($files);
124120

125121
// Remove full path from items
126-
array_walk( $files, function ( &$item ) use ( $path ) {
127-
$item = str_replace( $path, '', $item );
128-
} );
122+
array_walk($files, function (&$item) use ($path) {
123+
$item = str_replace($path, '', $item);
124+
});
129125

130126
// Fetch non-vendor files from filtered php files
131-
$nonVendorFiles = array_filter( $files, function ( $file ) {
132-
return strpos( $file, $this->excludePath ) === false && strpos( $file, '.json' ) === false;
133-
} );
127+
$nonVendorFiles = array_filter($files, function ($file) {
128+
return strpos($file, $this->excludePath) === false && strpos($file, '.json') === false;
129+
});
134130

135131
// Fetch vendor files from filtered php files
136-
$vendorFiles = array_filter( array_diff( $files, $nonVendorFiles ), function ( $file ) {
137-
return strpos( $file, 'json' ) === false;
138-
} );
132+
$vendorFiles = array_filter(array_diff($files, $nonVendorFiles), function ($file) {
133+
return strpos($file, 'json') === false;
134+
});
139135

140136
// Fetch .json files from filtered files
141-
$jsonFiles = array_filter( $files, function ( $file ) {
142-
return strpos( $file, '.json' ) !== false;
143-
} );
137+
$jsonFiles = array_filter($files, function ($file) {
138+
return strpos($file, '.json') !== false;
139+
});
144140

145141
return [
146-
'lang' => array_values( $nonVendorFiles ),
147-
'vendor' => array_values( $vendorFiles ),
148-
'json' => array_values( $jsonFiles ),
142+
'lang' => array_values($nonVendorFiles),
143+
'vendor' => array_values($vendorFiles),
144+
'json' => array_values($jsonFiles),
149145
];
150146
}
151147

@@ -178,43 +174,43 @@ public function toArray()
178174
*
179175
* @return array
180176
*/
181-
public function toFlat( $prefix = '.' )
177+
public function toFlat($prefix = '.')
182178
{
183-
$results = [];
184-
$default_locale = config( 'laravel-localization.js.default_locale' );
179+
$results = [];
180+
$default_locale = config('laravel-localization.js.default_locale');
185181
$default_json_strings = null;
186182

187-
foreach ( $this->strings as $lang => $strings ) {
188-
if ( $lang !== 'json' ) {
189-
foreach ( $strings as $lang_array => $lang_messages ) {
190-
$key = $lang . $prefix . $lang_array;
191-
$results[ $key ] = $lang_messages;
183+
foreach ($this->strings as $lang => $strings) {
184+
if ($lang !== 'json') {
185+
foreach ($strings as $lang_array => $lang_messages) {
186+
$key = $lang.$prefix.$lang_array;
187+
$results[$key] = $lang_messages;
192188
}
193189
} else {
194-
foreach ( $strings as $json_lang => $json_strings ) {
195-
$key = $json_lang . $prefix . '__JSON__';
196-
if ( array_key_exists( $key, $results ) ) {
197-
$results[ $key ] = $json_strings;
190+
foreach ($strings as $json_lang => $json_strings) {
191+
$key = $json_lang.$prefix.'__JSON__';
192+
if (array_key_exists($key, $results)) {
193+
$results[$key] = $json_strings;
198194
} else {
199-
$results[ $key ] = $json_strings;
195+
$results[$key] = $json_strings;
200196
}
201197

202198
// Pick only the first $json_strings
203-
if ( !$default_json_strings ) {
199+
if (! $default_json_strings) {
204200
$default_json_strings = $json_strings;
205201
}
206202
}
207203
}
208204
}
209205

210206
// Create a JSON key value pair for the default language
211-
$default_key = $default_locale . $prefix . '__JSON__';
212-
if ( !array_key_exists( $default_key, $results ) ) {
207+
$default_key = $default_locale.$prefix.'__JSON__';
208+
if (! array_key_exists($default_key, $results)) {
213209
$buffer = array_keys(
214-
$default_json_strings ? get_object_vars( $default_json_strings ) : []
210+
$default_json_strings ? get_object_vars($default_json_strings) : []
215211
);
216212

217-
$results[ $default_key ] = array_combine( $buffer, $buffer );
213+
$results[$default_key] = array_combine($buffer, $buffer);
218214
}
219215

220216
return $results;
@@ -227,39 +223,35 @@ public function toFlat( $prefix = '.' )
227223
*/
228224
public function toCollection()
229225
{
230-
return collect( $this->strings );
226+
return collect($this->strings);
231227
}
232228

233229
/**
234230
* Method to parse language files.
235231
*
236232
* @param string $file
237233
*/
238-
protected function parseLangFiles( $file, $key, $dir )
234+
protected function parseLangFiles($file, $key, $dir)
239235
{
240236
// Base package name without file ending
241-
$packageName = basename( $file, '.php' );
237+
$packageName = basename($file, '.php');
242238

243239
// Get package, language and file contents from language file
244240
// /<language_code>/(<package/)<filename>.php
245-
$language = explode( DIRECTORY_SEPARATOR, $file )[ 1 ];
246-
$fileContents = require $dir . DIRECTORY_SEPARATOR . $file;
241+
$language = explode(DIRECTORY_SEPARATOR, $file)[1];
242+
$fileContents = require $dir.DIRECTORY_SEPARATOR.$file;
247243

248244
// Check if language already exists in array
249-
if ( array_key_exists( $language, $this->strings ) ) {
250-
251-
if ( array_key_exists( $packageName, $this->strings[ $language ] ) ) {
252-
253-
$this->strings[ $language ][ $packageName ] =
254-
array_replace_recursive( (array) $this->strings[ $language ][ $packageName ], (array)
255-
$fileContents );
245+
if (array_key_exists($language, $this->strings)) {
246+
if (array_key_exists($packageName, $this->strings[$language])) {
247+
$this->strings[$language][$packageName] =
248+
array_replace_recursive((array) $this->strings[$language][$packageName], (array)
249+
$fileContents);
256250
} else {
257-
258-
$this->strings[ $language ][ $packageName ] = $fileContents;
251+
$this->strings[$language][$packageName] = $fileContents;
259252
}
260-
261253
} else {
262-
$this->strings[ $language ] = [
254+
$this->strings[$language] = [
263255
$packageName => $fileContents,
264256
];
265257
}
@@ -270,39 +262,34 @@ protected function parseLangFiles( $file, $key, $dir )
270262
*
271263
* @param string $file
272264
*/
273-
protected function parseVendorFiles( $file, $key, $dir )
265+
protected function parseVendorFiles($file, $key, $dir)
274266
{
275267
// Base package name without file ending
276-
$packageName = basename( $file, '.php' );
268+
$packageName = basename($file, '.php');
277269

278270
// Get package, language and file contents from language file
279271
// /vendor/<package>/<language_code>/<filename>.php
280-
$package = explode( DIRECTORY_SEPARATOR, $file )[ 2 ];
281-
$language = explode( DIRECTORY_SEPARATOR, $file )[ 3 ];
282-
$fileContents = require $dir . DIRECTORY_SEPARATOR . $file;
272+
$package = explode(DIRECTORY_SEPARATOR, $file)[2];
273+
$language = explode(DIRECTORY_SEPARATOR, $file)[3];
274+
$fileContents = require $dir.DIRECTORY_SEPARATOR.$file;
283275

284276
// Check if language already exists in array
285-
if ( array_key_exists( $language, $this->strings ) ) {
277+
if (array_key_exists($language, $this->strings)) {
286278
// Check if package already exists in language
287-
if ( array_key_exists( $package, $this->strings[ $language ] ) ) {
288-
289-
if ( array_key_exists( $packageName, $this->strings[ $language ][ $package ] ) ) {
290-
291-
$this->strings[ $language ][ $package ][ $packageName ] =
292-
array_replace_recursive( (array) $this->strings[ $language ][ $package ][ $packageName ],
279+
if (array_key_exists($package, $this->strings[$language])) {
280+
if (array_key_exists($packageName, $this->strings[$language][$package])) {
281+
$this->strings[$language][$package][$packageName] =
282+
array_replace_recursive((array) $this->strings[$language][$package][$packageName],
293283
(array)
294-
$fileContents );
295-
284+
$fileContents);
296285
} else {
297-
298-
$this->strings[ $language ][ $package ][ $packageName ] = $fileContents;
286+
$this->strings[$language][$package][$packageName] = $fileContents;
299287
}
300288
} else {
301-
302-
$this->strings[ $language ][ $package ] = [ $packageName => $fileContents ];
289+
$this->strings[$language][$package] = [$packageName => $fileContents];
303290
}
304291
} else {
305-
$this->strings[ $language ] = [
292+
$this->strings[$language] = [
306293

307294
$package => [
308295

@@ -312,28 +299,25 @@ protected function parseVendorFiles( $file, $key, $dir )
312299
}
313300
}
314301

315-
protected function parseJsonFiles( $file, $key, $dir )
302+
protected function parseJsonFiles($file, $key, $dir)
316303
{
317304
// Base package name without file ending
318-
$language = basename( $file, '.json' );
305+
$language = basename($file, '.json');
319306

320307
// Get package, language and file contents from language file
321308
// /<language_code>/(<package/)<filename>.php
322-
$fileContents = json_decode( file_get_contents( $dir . $file ) );
309+
$fileContents = json_decode(file_get_contents($dir.$file));
323310

324311
// Check if language already exists in array
325-
if ( array_key_exists( 'json', $this->strings ) ) {
326-
327-
if ( array_key_exists( $language, $this->strings[ 'json' ] ) ) {
328-
329-
$this->strings[ 'json' ][ $language ] =
330-
array_replace_recursive( (array) $this->strings[ 'json' ][ $language ], (array) $fileContents );
331-
312+
if (array_key_exists('json', $this->strings)) {
313+
if (array_key_exists($language, $this->strings['json'])) {
314+
$this->strings['json'][$language] =
315+
array_replace_recursive((array) $this->strings['json'][$language], (array) $fileContents);
332316
} else {
333-
$this->strings[ 'json' ][ $language ] = $fileContents;
317+
$this->strings['json'][$language] = $fileContents;
334318
}
335319
} else {
336-
$this->strings[ 'json' ] = [
320+
$this->strings['json'] = [
337321

338322
$language => $fileContents,
339323
];

0 commit comments

Comments
 (0)