| Conditions | 18 |
| Paths | 9 |
| Total Lines | 84 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 36 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 37 | { |
||
| 38 | if (!is_file($input->getArgument('config'))) { |
||
| 39 | throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist'); |
||
| 40 | } |
||
| 41 | parse_str($input->getOption('params'), $params); |
||
| 42 | extract($params); |
||
| 43 | |||
| 44 | $checkDictionariesConfig = require $input->getArgument('config'); |
||
| 45 | if ($checkDictionariesConfig instanceof InvalidConfigInstanceReturnedException) { |
||
| 46 | throw $checkDictionariesConfig; |
||
| 47 | } |
||
| 48 | if (!$checkDictionariesConfig instanceof CheckDictionariesConfig) { |
||
| 49 | throw new InvalidConfigInstanceReturnedException('"' . (is_object($checkDictionariesConfig) ? get_class($checkDictionariesConfig) : $checkDictionariesConfig) . '" is not instance of ' . CheckDictionariesConfig::class); |
||
| 50 | } |
||
| 51 | |||
| 52 | $output->writeln(''); |
||
| 53 | $output->writeln('Loading dictionaries...'); |
||
| 54 | |||
| 55 | $dictionaries = $checkDictionariesConfig->load(); |
||
| 56 | $onlyOneLang = (count($dictionaries) === 1); |
||
| 57 | $errors = []; |
||
| 58 | $dirs = ['./app', './src']; |
||
| 59 | |||
| 60 | $exclude = json_decode($input->getOption('exclude') ?? '', true) ?? []; |
||
| 61 | $include = json_decode($input->getOption('include') ?? '', true) ?? []; |
||
| 62 | $this->processTranslationFindConfig($exclude, $include); |
||
| 63 | $results = (new CodeAnalyzer($dirs, $this->translationFindConfig))->analyzeDirectories(); |
||
| 64 | foreach ($results as $call) { |
||
| 65 | $key = $call['key']; |
||
| 66 | if ($key === 'dynamic_value' || !is_string($key)) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | if ($dictionaries === []) { |
||
| 70 | $errors[] = 'No dictionaries found.'; |
||
| 71 | break; |
||
| 72 | } |
||
| 73 | foreach ($dictionaries as $lang => $dictionary) { |
||
| 74 | $langText = !$onlyOneLang ? ' for language "' . $lang . '"' : ''; |
||
| 75 | if (!isset($dictionary[$key])) { |
||
| 76 | $errors[] = sprintf( |
||
| 77 | 'Missing translation for key "%s" ' . $langText . 'in file: %s:%s call: "%s"', |
||
| 78 | $key, |
||
| 79 | $call['file'], |
||
| 80 | $call['line'], |
||
| 81 | $call['call'] |
||
| 82 | ); |
||
| 83 | } else { |
||
| 84 | // find plural bad key |
||
| 85 | $dictionaryTranslate = $dictionary[$key]; |
||
| 86 | $pluralKey = $call['arg'] ?? null; |
||
| 87 | $pluralKeyInFile = $pluralKey ? '%' . $pluralKey . '%' : null; |
||
| 88 | if ($pluralKey && strpos($dictionaryTranslate, $pluralKeyInFile) === false) { |
||
|
|
|||
| 89 | $errors[] = sprintf( |
||
| 90 | 'Translation key "%s" ' . $langText . 'in file: %s:%s call: "%s" has bad plural key: %s for translation: "%s"', |
||
| 91 | $key, |
||
| 92 | $call['file'], |
||
| 93 | $call['line'], |
||
| 94 | $call['call'], |
||
| 95 | $pluralKeyInFile, |
||
| 96 | $dictionaryTranslate |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | if ($pluralKey === null && preg_match('/.*%.+%.*/', $dictionaryTranslate) === false) { |
||
| 100 | $errors[] = sprintf( |
||
| 101 | 'Translation key "%s" ' . $langText . 'in file: %s:%s call: "%s" has missing plural key for translation: "%s"', |
||
| 102 | $key, |
||
| 103 | $call['file'], |
||
| 104 | $call['line'], |
||
| 105 | $call['call'], |
||
| 106 | $dictionaryTranslate |
||
| 107 | ); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $output->writeln('', OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 113 | foreach (array_unique($errors) as $error) { |
||
| 114 | $output->writeln($error, OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
| 115 | } |
||
| 116 | |||
| 117 | $output->writeln(''); |
||
| 118 | $output->writeln('<comment>' . count($errors) . ' errors found</comment>'); |
||
| 119 | return count($errors); |
||
| 120 | } |
||
| 143 |