ConvertCommand   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 11
dl 0
loc 72
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
B execute() 0 25 6
B getReader() 0 25 11
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Converter\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Translation\Loader;
20
use Translation\Converter\Reader\JmsReader;
21
use Translation\Converter\Service\Converter;
22
23
/**
24
 * @author Tobias Nyholm <[email protected]>
25
 */
26
class ConvertCommand extends Command
27
{
28
    const SUPPORTED_FORMATS = ['jms', 'csv', 'ini', 'json', 'mo', 'php', 'yml', 'xlf'];
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setName('translation:convert')
34
            ->setDescription('Convert your existing translation files to Xliff')
35
            ->addArgument('input_dir', InputArgument::REQUIRED, 'Where are your existing translations?')
36
            ->addArgument('output_dir', InputArgument::REQUIRED, 'Where should we put the new translations?')
37
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The format to convert from. Supported formats are: '.implode(', ', self::SUPPORTED_FORMATS))
38
        ;
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $files = scandir($input->getArgument('input_dir'));
44
45
        $formats = [];
46
        $locales = [];
47
        foreach ($files as $file) {
48
            if (preg_match('|.+\.(.+)\.(.+)|', $file, $matches)) {
49
                $locales[] = $matches[1];
50
                $formats[] = $matches[2];
51
            }
52
        }
53
54
        if (null === $format = $input->getOption('format')) {
55
            $formats = array_unique($formats);
56
            if (1 !== count($formats)) {
57
                throw new \InvalidArgumentException('More than one format found. Please specify a format with --format=xxx');
58
            }
59
            $format = reset($formats);
60
        }
61
62
        $reader = $this->getReader($format);
63
        $converter = new Converter($reader, 'jms' === $format ? ['xlf', 'xliff'] : $format);
64
        $converter->convert($input->getArgument('input_dir'), $input->getArgument('output_dir'), array_unique($locales));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('input_dir') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Translation\Converter\Service\Converter::convert() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $input->getArgument('output_dir') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Translation\Converter\Service\Converter::convert() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
65
    }
66
67
    /**
68
     * @param string $format
69
     *
70
     * @return Loader\LoaderInterface
71
     */
72
    private function getReader($format)
73
    {
74
        switch ($format) {
75
            case 'jms':
76
                return new JmsReader();
77
            case 'csv':
78
                return new Loader\CsvFileLoader();
79
            case 'ini':
80
                return new Loader\IniFileLoader();
81
            case 'json':
82
                return new Loader\JsonFileLoader();
83
            case 'mo':
84
                return new Loader\MoFileLoader();
85
            case 'php':
86
                return new Loader\PhpFileLoader();
87
            case 'yml':
88
            case 'yaml':
89
                return new Loader\YamlFileLoader();
90
            case 'xlf':
91
            case 'xliff':
92
                return new Loader\XliffFileLoader();
93
            default:
94
                throw new \InvalidArgumentException(sprintf('Format "%s" is not a valid format', $format));
95
        }
96
    }
97
}
98