Skip to content

Commit d143a9b

Browse files
committed
style: Format code
1 parent b025df1 commit d143a9b

1 file changed

Lines changed: 60 additions & 80 deletions

File tree

‎system/Commands/Translation/LocalizationFinder.php‎

Lines changed: 60 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function run(array $params)
5757
$this->languagePath = $currentDir . 'Language';
5858

5959
if (ENVIRONMENT === 'testing') {
60-
$currentDir = SUPPORTPATH . 'Services/';
60+
$currentDir = SUPPORTPATH . 'Services' . DIRECTORY_SEPARATOR;
6161
$this->languagePath = SUPPORTPATH . 'Language';
6262
}
6363

@@ -99,6 +99,58 @@ public function run(array $params)
9999
return EXIT_SUCCESS;
100100
}
101101

102+
private function process(string $currentDir, string $currentLocale): void
103+
{
104+
$tableRows = [];
105+
$countNewKeys = 0;
106+
107+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($currentDir));
108+
$files = iterator_to_array($iterator, true);
109+
ksort($files);
110+
111+
[$foundLanguageKeys, $countFiles] = $this->findLanguageKeysInFiles($files);
112+
ksort($foundLanguageKeys);
113+
114+
$languageDiff = [];
115+
$languageFoundGroups = array_unique(array_keys($foundLanguageKeys));
116+
117+
foreach ($languageFoundGroups as $langFileName) {
118+
$languageStoredKeys = [];
119+
$languageFilePath = $this->languagePath . DIRECTORY_SEPARATOR . $currentLocale . DIRECTORY_SEPARATOR . $langFileName . '.php';
120+
121+
if (is_file($languageFilePath)) {
122+
// Load old localization
123+
$languageStoredKeys = require $languageFilePath;
124+
}
125+
126+
$languageDiff = ArrayHelper::recursiveDiff($foundLanguageKeys[$langFileName], $languageStoredKeys);
127+
$countNewKeys += ArrayHelper::recursiveCount($languageDiff);
128+
129+
if ($this->showNew) {
130+
$tableRows = array_merge($this->arrayToTableRows($langFileName, $languageDiff), $tableRows);
131+
} else {
132+
$newLanguageKeys = array_replace_recursive($foundLanguageKeys[$langFileName], $languageStoredKeys);
133+
134+
if ($languageDiff !== []) {
135+
if (false === file_put_contents($languageFilePath, $this->templateFile($newLanguageKeys))) {
136+
$this->writeIsVerbose('Lang file ' . $langFileName . ' (error write).', 'red');
137+
} else {
138+
exec('composer cs-fix --quiet ' . $this->languagePath);
139+
$this->writeIsVerbose('Lang file "' . $langFileName . '" successful updated!', 'green');
140+
}
141+
}
142+
}
143+
}
144+
145+
if ($this->showNew && $tableRows !== []) {
146+
sort($tableRows);
147+
CLI::table($tableRows, ['File', 'Key']);
148+
}
149+
150+
$this->writeIsVerbose('Files found: ' . $countFiles);
151+
$this->writeIsVerbose('New translates found: ' . $countNewKeys);
152+
}
153+
102154
/**
103155
* @param SplFileInfo|string $file
104156
*/
@@ -113,16 +165,14 @@ private function findTranslationsInFile($file): array
113165
$fileContent = file_get_contents($file->getRealPath());
114166
preg_match_all('/lang\(\'([._a-z0-9\-]+)\'\)/ui', $fileContent, $matches);
115167

116-
if ([] === $matches[1]) {
168+
if ($matches[1] === []) {
117169
return [];
118170
}
119171

120172
foreach ($matches[1] as $phraseKey) {
121173
$phraseKeys = explode('.', $phraseKey);
122174

123-
/**
124-
* Language key not have Filename or Lang key
125-
*/
175+
// Language key not have Filename or Lang key
126176
if (count($phraseKeys) < 2) {
127177
continue;
128178
}
@@ -136,16 +186,11 @@ private function findTranslationsInFile($file): array
136186
continue;
137187
}
138188

139-
/**
140-
* Language key as string
141-
*/
142189
if (count($phraseKeys) === 1) {
143-
/**
144-
* Add found language keys to temporary array
145-
*/
146190
$foundLanguageKeys[$languageFileName][$phraseKeys[0]] = $phraseKey;
147191
} else {
148-
$childKeys = $this->buildMultiArray($phraseKeys, $phraseKey);
192+
$childKeys = $this->buildMultiArray($phraseKeys, $phraseKey);
193+
149194
$foundLanguageKeys[$languageFileName] = array_replace_recursive($foundLanguageKeys[$languageFileName] ?? [], $childKeys);
150195
}
151196
}
@@ -159,13 +204,13 @@ private function isIgnoredFile(SplFileInfo $file): bool
159204
return true;
160205
}
161206

162-
return 'php' !== $file->getExtension();
207+
return $file->getExtension() !== 'php';
163208
}
164209

165210
private function templateFile(array $language = []): string
166211
{
167-
if ([] !== $language) {
168-
$languageArrayString = $this->languageKeysDump($language);
212+
if ($language !== []) {
213+
$languageArrayString = var_export($language, true);
169214

170215
return <<<PHP
171216
<?php
@@ -224,11 +269,6 @@ private function arrayToTableRows(string $langFileName, array $array): array
224269
return $rows;
225270
}
226271

227-
private function languageKeysDump(array $inputArray): string
228-
{
229-
return var_export($inputArray, true);
230-
}
231-
232272
/**
233273
* Show details in the console if the flag is set
234274
*/
@@ -244,66 +284,6 @@ private function isSubDirectory(string $directory, string $rootDirectory): bool
244284
return 0 === strncmp($directory, $rootDirectory, strlen($directory));
245285
}
246286

247-
private function process(string $currentDir, string $currentLocale): void
248-
{
249-
$tableRows = [];
250-
$countNewKeys = 0;
251-
252-
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($currentDir));
253-
$files = iterator_to_array($iterator, true);
254-
ksort($files);
255-
256-
[$foundLanguageKeys, $countFiles] = $this->findLanguageKeysInFiles($files);
257-
ksort($foundLanguageKeys);
258-
259-
/**
260-
* New translates
261-
*/
262-
$languageDiff = [];
263-
$languageFoundGroups = array_unique(array_keys($foundLanguageKeys));
264-
265-
foreach ($languageFoundGroups as $langFileName) {
266-
$languageStoredKeys = [];
267-
$languageFilePath = $this->languagePath . DIRECTORY_SEPARATOR . $currentLocale . DIRECTORY_SEPARATOR . $langFileName . '.php';
268-
269-
if (is_file($languageFilePath)) {
270-
/**
271-
* Load old localization
272-
*/
273-
$languageStoredKeys = require $languageFilePath;
274-
}
275-
276-
$languageDiff = ArrayHelper::recursiveDiff($foundLanguageKeys[$langFileName], $languageStoredKeys);
277-
$countNewKeys += ArrayHelper::recursiveCount($languageDiff);
278-
279-
if ($this->showNew) {
280-
$tableRows = array_merge($this->arrayToTableRows($langFileName, $languageDiff), $tableRows);
281-
} else {
282-
$newLanguageKeys = array_replace_recursive($foundLanguageKeys[$langFileName], $languageStoredKeys);
283-
284-
/**
285-
* New translates exists
286-
*/
287-
if ($languageDiff !== []) {
288-
if (false === file_put_contents($languageFilePath, $this->templateFile($newLanguageKeys))) {
289-
$this->writeIsVerbose('Lang file ' . $langFileName . ' (error write).', 'red');
290-
} else {
291-
exec('composer cs-fix --quiet ' . $this->languagePath);
292-
$this->writeIsVerbose('Lang file "' . $langFileName . '" successful updated!', 'green');
293-
}
294-
}
295-
}
296-
}
297-
298-
if ($this->showNew && [] !== $tableRows) {
299-
sort($tableRows);
300-
CLI::table($tableRows, ['File', 'Key']);
301-
}
302-
303-
$this->writeIsVerbose('Files found: ' . $countFiles);
304-
$this->writeIsVerbose('New translates found: ' . $countNewKeys);
305-
}
306-
307287
/**
308288
* @param SplFileInfo[] $files
309289
*

0 commit comments

Comments
 (0)