Completed
Pull Request — master (#4329)
by Craig
05:18
created

TranslationKeyToValueCommand::execute()   C

Complexity

Conditions 11
Paths 140

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 11
eloc 33
c 1
b 0
f 1
nc 140
nop 2
dl 0
loc 49
rs 6.9833

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 Zikula\Bundle\CoreBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Exception\InvalidArgumentException;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\HttpKernel\KernelInterface;
14
use Symfony\Component\Yaml\Yaml;
15
16
class TranslationKeyToValueCommand extends Command
17
{
18
    protected static $defaultName = 'zikula:translation:keytovalue';
19
20
    private $defaultTransPath;
21
22
    public function __construct(string $defaultTransPath = null)
23
    {
24
        parent::__construct();
25
        $this->defaultTransPath = $defaultTransPath;
26
    }
27
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setDescription('update translation files to remove null values and replace them with the key')
33
            ->addArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages')
34
            ->setHelp(<<<'EOF'
35
The <info>%command.name%</info> command transforms translation strings of a given
36
bundle or the default translations directory. It sets null messages to the value of the key.
37
38
It is recommended to run <info>php bin/console translation:extract</info> first.
39
40
Example running against a Bundle (AcmeBundle)
41
42
  <info>php %command.full_name% AcmeBundle</info>
43
44
Example running against default messages directory
45
46
  <info>php %command.full_name%</info>
47
EOF
48
            )
49
        ;
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): int
53
    {
54
        $io = new SymfonyStyle($input, $output);
55
56
        /** @var KernelInterface $kernel */
57
        $kernel = $this->getApplication()->getKernel();
0 ignored issues
show
Bug introduced by
The method getKernel() does not exist on Symfony\Component\Console\Application. It seems like you code against a sub-type of Symfony\Component\Console\Application such as Symfony\Bundle\FrameworkBundle\Console\Application. ( Ignorable by Annotation )

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

57
        $kernel = $this->getApplication()->/** @scrutinizer ignore-call */ getKernel();
Loading history...
58
        $transPaths = [];
59
        if ($this->defaultTransPath) {
60
            $transPaths[] = $this->defaultTransPath;
61
        }
62
        $currentName = 'default directory';
63
        // Override with provided Bundle info (this section copied from symfony's translation:update command)
64
        if (null !== $input->getArgument('bundle')) {
65
            try {
66
                $foundBundle = $kernel->getBundle($input->getArgument('bundle'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('bundle') can also be of type null and string[]; however, parameter $name of Symfony\Component\HttpKe...lInterface::getBundle() 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

66
                $foundBundle = $kernel->getBundle(/** @scrutinizer ignore-type */ $input->getArgument('bundle'));
Loading history...
67
                $bundleDir = $foundBundle->getPath();
68
                $transPaths = [is_dir($bundleDir . '/Resources/translations') ? $bundleDir . '/Resources/translations' : $bundleDir . '/translations'];
69
                if ($this->defaultTransPath) {
70
                    $transPaths[] = $this->defaultTransPath;
71
                }
72
                $currentName = $foundBundle->getName();
73
            } catch (\InvalidArgumentException $e) {
74
                // such a bundle does not exist, so treat the argument as path
75
                $path = $input->getArgument('bundle');
76
                $transPaths = [$path . '/translations'];
0 ignored issues
show
Bug introduced by
Are you sure $path 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

76
                $transPaths = [/** @scrutinizer ignore-type */ $path . '/translations'];
Loading history...
77
                if (!is_dir($transPaths[0]) && !isset($transPaths[1])) {
78
                    throw new InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0]));
79
                }
80
            }
81
        }
82
        $io->title('Translation Messages Key to Value Transformer');
83
        $io->comment(sprintf('Transforming translation files for "<info>%s</info>"', $currentName));
84
        $finder = new Finder();
85
        $fs = new Filesystem();
86
        foreach ($finder->files()->in($transPaths)->name(['*.yaml', '*.yml']) as $file) {
87
            $io->text(sprintf('<comment>Parsing %s</comment>', $file->getBasename()));
88
            $messages = Yaml::parseFile($file->getRealPath());
89
            foreach ($messages as $key => $message) {
90
                if (null === $message) {
91
                    $messages[$key] = $key;
92
                }
93
            }
94
            $io->text(sprintf('<info>Dumping %s</info>', $file->getBasename()));
95
            $fs->dumpFile($file->getRealPath(), Yaml::dump($messages));
96
        }
97
98
        $io->success('Success!');
99
100
        return Command::SUCCESS;
101
    }
102
}
103