From 390ea31c3980cead60fc734d5ae33018a3ff2d70 Mon Sep 17 00:00:00 2001 From: Stefan Ninic Date: Sat, 23 Jun 2018 13:44:56 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Classes/ExportLocalizations.php | 101 ++++++++++++++-------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/src/Classes/ExportLocalizations.php b/src/Classes/ExportLocalizations.php index d2581da..a9f0e69 100644 --- a/src/Classes/ExportLocalizations.php +++ b/src/Classes/ExportLocalizations.php @@ -26,7 +26,7 @@ class ExportLocalizations implements \JsonSerializable /** * @var string */ - protected $excludePath = DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR; + protected $excludePath = DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR; /** * @var string @@ -41,29 +41,29 @@ class ExportLocalizations implements \JsonSerializable public function export() { // Check if value is cached and set array to cached version - if ( Cache::has( config( 'laravel-localization.caches.key' ) ) ) { - $this->strings = Cache::get( config( 'laravel-localization.caches.key' ) ); + if (Cache::has(config('laravel-localization.caches.key'))) { + $this->strings = Cache::get(config('laravel-localization.caches.key')); return $this; } // Collect language files and build array with translations - $files = $this->findLanguageFiles( resource_path( 'lang' ) ); + $files = $this->findLanguageFiles(resource_path('lang')); // Parse translations and create final array - array_walk( $files[ 'lang' ], [ $this, 'parseLangFiles' ] ); - array_walk( $files[ 'vendor' ], [ $this, 'parseVendorFiles' ] ); + array_walk($files['lang'], [$this, 'parseLangFiles']); + array_walk($files['vendor'], [$this, 'parseVendorFiles']); // Trigger event for final translated array - event( new LaravelLocalizationExported( $this->strings ) ); + event(new LaravelLocalizationExported($this->strings)); // If timeout > 0 save array to cache - if ( config( 'laravel-localization.caches.timeout', 0 ) > 0 ) { - Cache::store( config( 'laravel-localization.caches.driver', 'file' ) ) + if (config('laravel-localization.caches.timeout', 0) > 0) { + Cache::store(config('laravel-localization.caches.driver', 'file')) ->put( - config( 'laravel-localization.caches.key', 'localization.array' ), + config('laravel-localization.caches.key', 'localization.array'), $this->strings, - config( 'laravel-localization.caches.timeout', 60 ) + config('laravel-localization.caches.timeout', 60) ); } @@ -77,40 +77,40 @@ public function export() * * @return array */ - protected function findLanguageFiles( $path ) + protected function findLanguageFiles($path) { // Loop through directories - $dirIterator = new \RecursiveDirectoryIterator( $path, \RecursiveDirectoryIterator::SKIP_DOTS ); - $recIterator = new \RecursiveIteratorIterator( $dirIterator ); + $dirIterator = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS); + $recIterator = new \RecursiveIteratorIterator($dirIterator); // Fetch only php files - skip others $phpFiles = array_values( - array_map( 'current', + array_map('current', iterator_to_array( - new \RegexIterator( $recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH ) + new \RegexIterator($recIterator, $this->phpRegex, \RecursiveRegexIterator::GET_MATCH) ) ) ); // Sort array by filepath - sort( $phpFiles ); + sort($phpFiles); // Remove full path from items - array_walk( $phpFiles, function ( &$item ) { - $item = str_replace( resource_path( 'lang' ), '', $item ); - } ); + array_walk($phpFiles, function (&$item) { + $item = str_replace(resource_path('lang'), '', $item); + }); // Fetch non-vendor files from filtered php files - $nonVendorFiles = array_filter( $phpFiles, function ( $file ) { - return strpos( $file, $this->excludePath ) === false; - } ); + $nonVendorFiles = array_filter($phpFiles, function ($file) { + return strpos($file, $this->excludePath) === false; + }); // Fetch vendor files from filtered php files - $vendorFiles = array_diff( $phpFiles, $nonVendorFiles ); + $vendorFiles = array_diff($phpFiles, $nonVendorFiles); return [ - 'lang' => array_values( $nonVendorFiles ), - 'vendor' => array_values( $vendorFiles ), + 'lang' => array_values($nonVendorFiles), + 'vendor' => array_values($vendorFiles), ]; } @@ -143,13 +143,13 @@ public function toArray() * * @return array */ - public function toFlat( $prefix = '.' ) + public function toFlat($prefix = '.') { $results = []; - foreach ( $this->strings as $lang => $strings ) { - foreach ( $strings as $lang_array => $lang_messages ) { - $key = $lang . $prefix . $lang_array; - $results[ $key ] = $lang_messages; + foreach ($this->strings as $lang => $strings) { + foreach ($strings as $lang_array => $lang_messages) { + $key = $lang.$prefix.$lang_array; + $results[$key] = $lang_messages; } } @@ -163,7 +163,7 @@ public function toFlat( $prefix = '.' ) */ public function toCollection() { - return collect( $this->strings ); + return collect($this->strings); } /** @@ -171,21 +171,21 @@ public function toCollection() * * @param string $file */ - protected function parseLangFiles( $file ) + protected function parseLangFiles($file) { // Base package name without file ending - $packageName = basename( $file, '.php' ); + $packageName = basename($file, '.php'); // Get package, language and file contents from language file // //(.php - $language = explode( DIRECTORY_SEPARATOR, $file )[ 1 ]; - $fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file; + $language = explode(DIRECTORY_SEPARATOR, $file)[1]; + $fileContents = require resource_path('lang').DIRECTORY_SEPARATOR.$file; // Check if language already exists in array - if ( array_key_exists( $language, $this->strings ) ) { - $this->strings[ $language ][ $packageName ] = $fileContents; + if (array_key_exists($language, $this->strings)) { + $this->strings[$language][$packageName] = $fileContents; } else { - $this->strings[ $language ] = [ + $this->strings[$language] = [ $packageName => $fileContents, ]; } @@ -196,30 +196,27 @@ protected function parseLangFiles( $file ) * * @param string $file */ - protected function parseVendorFiles( $file ) + protected function parseVendorFiles($file) { // Base package name without file ending - $packageName = basename( $file, '.php' ); + $packageName = basename($file, '.php'); // Get package, language and file contents from language file // /vendor///.php - $package = explode( DIRECTORY_SEPARATOR, $file )[ 2 ]; - $language = explode( DIRECTORY_SEPARATOR, $file )[ 3 ]; - $fileContents = require resource_path( 'lang' ) . DIRECTORY_SEPARATOR . $file; + $package = explode(DIRECTORY_SEPARATOR, $file)[2]; + $language = explode(DIRECTORY_SEPARATOR, $file)[3]; + $fileContents = require resource_path('lang').DIRECTORY_SEPARATOR.$file; // Check if language already exists in array - if ( array_key_exists( $language, $this->strings ) ) { + if (array_key_exists($language, $this->strings)) { // Check if package already exists in language - if ( array_key_exists( $package, $this->strings[ $language ] ) ) { - - array_push( $this->strings[ $language ][ $package ], [ $packageName => $fileContents ] ); + if (array_key_exists($package, $this->strings[$language])) { + array_push($this->strings[$language][$package], [$packageName => $fileContents]); } else { - - $this->strings[ $language ][ $package ] = [ $packageName => $fileContents ]; + $this->strings[$language][$package] = [$packageName => $fileContents]; } - } else { - $this->strings[ $language ] = [ + $this->strings[$language] = [ $package => [