CheckDictionariesCommand::execute()   B
last analyzed

Complexity

Conditions 10
Paths 13

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 41
rs 7.6666
cc 10
nc 13
nop 2

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\Command\CheckDictionaries;
4
5
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException;
6
use Efabrica\TranslationsAutomatization\Tokenizer\Token;
7
use InvalidArgumentException;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class CheckDictionariesCommand extends Command
16
{
17
    protected function configure()
18
    {
19
        $this->setName('check:dictionaries')
20
            ->setDescription('Compare all dictionaries for all languages if there are all translation keys in all of them')
21
            ->addArgument('config', InputArgument::REQUIRED, 'Path to config file. Instance of ' . CheckDictionariesConfig::class  . ' have to be returned')
22
            ->addOption('params', null, InputOption::VALUE_REQUIRED, 'Params for config in format --params="a=b&c=d"');
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        if (!is_file($input->getArgument('config'))) {
28
            throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist');
29
        }
30
        parse_str($input->getOption('params'), $params);
31
        extract($params);
32
33
        $checkDictionariesConfig = require_once $input->getArgument('config');
34
        if ($checkDictionariesConfig instanceof InvalidConfigInstanceReturnedException) {
35
            throw $checkDictionariesConfig;
36
        }
37
        if (!$checkDictionariesConfig instanceof CheckDictionariesConfig) {
38
            throw new InvalidConfigInstanceReturnedException('"' . (is_object($checkDictionariesConfig) ? get_class($checkDictionariesConfig) : $checkDictionariesConfig) . '" is not instance of ' . CheckDictionariesConfig::class);
39
        }
40
41
        $output->writeln('');
42
        $output->writeln('Loading dictionaries...');
43
        $dictionaries = $checkDictionariesConfig->load();
44
45
        $errors = [];
46
        foreach ($dictionaries as $lang1 => $dictionary1) {
47
            foreach ($dictionaries as $lang2 => $dictionary2) {
48
                if ($lang1 === $lang2) {
49
                    continue;
50
                }
51
                $missingTranslations = array_diff(array_keys($dictionary1), array_keys($dictionary2));
52
                foreach ($missingTranslations as $missingTranslation) {
53
                    $errors[] = 'Missing translation for key ' . $missingTranslation . ' for language ' . $lang2;
54
                }
55
            }
56
        }
57
58
        $output->writeln('', OutputInterface::VERBOSITY_VERY_VERBOSE);
59
        foreach (array_unique($errors) as $error) {
60
            $output->writeln($error, OutputInterface::VERBOSITY_VERY_VERBOSE);
61
        }
62
63
        $output->writeln('');
64
        $output->writeln('<comment>' . count($errors) . ' errors found</comment>');
65
        return count($errors);
66
    }
67
}
68