-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: [Commands] lang:find
show bad keys when scanning
#8149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,7 +111,7 @@ private function process(string $currentDir, string $currentLocale): void | |
$files = iterator_to_array($iterator, true); | ||
ksort($files); | ||
|
||
[$foundLanguageKeys, $countFiles] = $this->findLanguageKeysInFiles($files); | ||
[$foundLanguageKeys, $badLanguageKeys, $countFiles] = $this->findLanguageKeysInFiles($files); | ||
ksort($foundLanguageKeys); | ||
|
||
$languageDiff = []; | ||
|
@@ -155,14 +155,28 @@ private function process(string $currentDir, string $currentLocale): void | |
|
||
$this->writeIsVerbose('Files found: ' . $countFiles); | ||
$this->writeIsVerbose('New translates found: ' . $countNewKeys); | ||
$this->writeIsVerbose('Bad translates found: ' . count($badLanguageKeys)); | ||
|
||
if ($this->verbose && $badLanguageKeys !== []) { | ||
$tableBadRows = []; | ||
|
||
foreach ($badLanguageKeys as $key => $value) { | ||
$tableBadRows[] = [$key, $value]; | ||
} | ||
|
||
CLI::table($tableBadRows, ['№', 'Bad key']); | ||
} | ||
} | ||
|
||
/** | ||
* @param SplFileInfo|string $file | ||
* | ||
* @return array<int, array> | ||
*/ | ||
private function findTranslationsInFile($file): array | ||
{ | ||
$foundLanguageKeys = []; | ||
$badLanguageKeys = []; | ||
|
||
if (is_string($file) && is_file($file)) { | ||
$file = new SplFileInfo($file); | ||
|
@@ -172,14 +186,16 @@ private function findTranslationsInFile($file): array | |
preg_match_all('/lang\(\'([._a-z0-9\-]+)\'\)/ui', $fileContent, $matches); | ||
|
||
if ($matches[1] === []) { | ||
return []; | ||
return [[], []]; | ||
} | ||
|
||
foreach ($matches[1] as $phraseKey) { | ||
$phraseKeys = explode('.', $phraseKey); | ||
|
||
// Language key not have Filename or Lang key | ||
if (count($phraseKeys) < 2) { | ||
$badLanguageKeys[] = $phraseKey; | ||
|
||
continue; | ||
} | ||
|
||
|
@@ -189,6 +205,8 @@ private function findTranslationsInFile($file): array | |
|| ($languageFileName === '' && $phraseKeys[0] === ''); | ||
|
||
if ($isEmptyNestedArray) { | ||
$badLanguageKeys[] = $phraseKey; | ||
|
||
continue; | ||
} | ||
|
||
|
@@ -201,7 +219,7 @@ private function findTranslationsInFile($file): array | |
} | ||
} | ||
|
||
return $foundLanguageKeys; | ||
return [$foundLanguageKeys, $badLanguageKeys]; | ||
} | ||
|
||
private function isIgnoredFile(SplFileInfo $file): bool | ||
|
@@ -335,11 +353,11 @@ private function isSubDirectory(string $directory, string $rootDirectory): bool | |
* @param SplFileInfo[] $files | ||
* | ||
* @return array<int, array|int> | ||
* @phpstan-return list{0: array<string, array<string, string>>, 1: int} | ||
* @phpstan-return list{0: array<string, array<string, string>>, 1: array<string, array<string, string>>, 2: int} | ||
*/ | ||
private function findLanguageKeysInFiles(array $files): array | ||
{ | ||
$foundLanguageKeys = []; | ||
$foundLanguageKeys = [[], []]; | ||
$countFiles = 0; | ||
|
||
foreach ($files as $file) { | ||
|
@@ -349,9 +367,13 @@ private function findLanguageKeysInFiles(array $files): array | |
|
||
$this->writeIsVerbose('File found: ' . mb_substr($file->getRealPath(), mb_strlen(APPPATH))); | ||
$countFiles++; | ||
$foundLanguageKeys = array_replace_recursive($this->findTranslationsInFile($file), $foundLanguageKeys); | ||
|
||
$findInFile = $this->findTranslationsInFile($file); | ||
|
||
$foundLanguageKeys[0] = array_replace_recursive($findInFile[0], $foundLanguageKeys[0]); | ||
$foundLanguageKeys[1] = array_merge($findInFile[1], $foundLanguageKeys[1]); | ||
Comment on lines
+373
to
+374
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable names are difficult to understand. Don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good. this is because the function returns multiple values instead of one |
||
} | ||
|
||
return [$foundLanguageKeys, $countFiles]; | ||
return [$foundLanguageKeys[0], array_unique($foundLanguageKeys[1]), $countFiles]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you need the No. for? I want the file paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The number is just for understanding which line. I think file paths are not an option to show. Since one language key can be in ~1000 different files. How do you see it?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a bad key is used in 1000 files, then those 1000 locations should be shown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I'll try to do it.