Passed
Push — master ( 9db0c6...cf0e90 )
by Michal
18:17 queued 01:02
created

CheckDictionariesCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
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'))) {
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('config') can also be of type null and string[]; however, parameter $filename of is_file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        if (!is_file(/** @scrutinizer ignore-type */ $input->getArgument('config'))) {
Loading history...
28
            throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist');
0 ignored issues
show
Bug introduced by
Are you sure $input->getArgument('config') of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
            throw new InvalidArgumentException('File "' . /** @scrutinizer ignore-type */ $input->getArgument('config') . '" does not exist');
Loading history...
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
                $missingTranslations = array_diff(array_keys($dictionary1), array_keys($dictionary2));
49
                foreach ($missingTranslations as $missingTranslation) {
50
                    $errors[] = 'Missing translation for key ' . $missingTranslation . ' for language ' . $lang2;
51
                }
52
            }
53
        }
54
55
        $output->writeln("\n", OutputInterface::VERBOSITY_VERY_VERBOSE);
56
        foreach ($errors as $error) {
57
            $output->writeln($error, OutputInterface::VERBOSITY_VERY_VERBOSE);
58
        }
59
60
        $output->writeln("\n");
61
        $output->writeln('<comment>' . count($errors) . ' errors found</comment>');
62
        return count($errors);
63
    }
64
}
65