Completed
Push — master ( f96c7c...8d3ead )
by Mathieu
02:17 queued 10s
created

ExtractCommand::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 49

Duplication

Lines 3
Ratio 6.12 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 3
loc 49
ccs 0
cts 37
cp 0
rs 8.8016
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
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\Bundle\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\Console\Style\SymfonyStyle;
20
use Symfony\Component\Finder\Finder;
21
use Translation\Bundle\Catalogue\CatalogueCounter;
22
use Translation\Bundle\Catalogue\CatalogueFetcher;
23
use Translation\Bundle\Catalogue\CatalogueWriter;
24
use Translation\Bundle\Model\Configuration;
25
use Translation\Bundle\Service\ConfigurationManager;
26
use Translation\Bundle\Service\Importer;
27
use Translation\Extractor\Model\Error;
28
29
/**
30
 * @author Tobias Nyholm <[email protected]>
31
 */
32
class ExtractCommand extends Command
33
{
34
    use BundleTrait;
35
36
    protected static $defaultName = 'translation:extract';
37
38
    /**
39
     * @var CatalogueFetcher
40
     */
41
    private $catalogueFetcher;
42
43
    /**
44
     * @var CatalogueWriter
45
     */
46
    private $catalogueWriter;
47
48
    /**
49
     * @var CatalogueCounter
50
     */
51
    private $catalogueCounter;
52
53
    /**
54
     * @var Importer
55
     */
56
    private $importer;
57
58
    /**
59
     * @var ConfigurationManager
60
     */
61
    private $configurationManager;
62
63
    public function __construct(
64
        CatalogueFetcher $catalogueFetcher,
65
        CatalogueWriter $catalogueWriter,
66
        CatalogueCounter $catalogueCounter,
67
        Importer $importer,
68
        ConfigurationManager $configurationManager
69
    ) {
70
        $this->catalogueFetcher = $catalogueFetcher;
71
        $this->catalogueWriter = $catalogueWriter;
72
        $this->catalogueCounter = $catalogueCounter;
73
        $this->importer = $importer;
74
        $this->configurationManager = $configurationManager;
75
76
        parent::__construct();
77
    }
78
79 View Code Duplication
    protected function configure(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
    {
81
        $this
82
            ->setName(self::$defaultName)
83
            ->setDescription('Extract translations from source code.')
84
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
85
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale to use. If omitted, we use all configured locales.', false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|array<integer,string>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
            ->addOption('hide-errors', null, InputOption::VALUE_NONE, 'If we should print error or not')
87
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want extract translations from.')
88
        ;
89
    }
90
91
    protected function execute(InputInterface $input, OutputInterface $output): void
92
    {
93
        $configName = $input->getArgument('configuration');
94 View Code Duplication
        if (null === $config = $this->configurationManager->getConfiguration($configName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
            throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName));
96
        }
97
98
        $locales = [];
99
        if ($inputLocale = $input->getArgument('locale')) {
100
            $locales = [$inputLocale];
101
        }
102
103
        $catalogues = $this->catalogueFetcher->getCatalogues($config, $locales);
104
        $this->configureBundleDirs($input, $config);
105
        $finder = $this->getConfiguredFinder($config);
106
107
        $result = $this->importer->extractToCatalogues($finder, $catalogues, [
108
            'blacklist_domains' => $config->getBlacklistDomains(),
109
            'whitelist_domains' => $config->getWhitelistDomains(),
110
            'project_root' => $config->getProjectRoot(),
111
        ]);
112
        $errors = $result->getErrors();
113
114
        $this->catalogueWriter->writeCatalogues($config, $result->getMessageCatalogues());
0 ignored issues
show
Documentation introduced by
$result->getMessageCatalogues() is of type array<integer,object<Sym...ageCatalogueInterface>>, but the function expects a array<integer,object<Sym...tion\MessageCatalogue>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
116
        $definedBefore = $this->catalogueCounter->getNumberOfDefinedMessages($catalogues[0]);
117
        $definedAfter = $this->catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]);
118
119
        /*
120
         * Print results
121
         */
122
        $io = new SymfonyStyle($input, $output);
123
        $io->table(['Type', 'Count'], [
124
            ['Total defined messages', $definedAfter],
125
            ['New messages', $definedAfter - $definedBefore],
126
            ['Errors', \count($errors)],
127
        ]);
128
129
        if (!$input->getOption('hide-errors')) {
130
            /** @var Error $error */
131
            foreach ($errors as $error) {
132
                $io->error(
133
                    \sprintf("%s\nLine: %s\nMessage: %s", $error->getPath(), $error->getLine(), $error->getMessage())
134
                );
135
            }
136
        }
137
138
        return 0;
139
    }
140
141
    private function getConfiguredFinder(Configuration $config): Finder
142
    {
143
        $finder = new Finder();
144
        $finder->in($config->getDirs());
145
146
        foreach ($config->getExcludedDirs() as $exclude) {
147
            $finder->notPath($exclude);
148
        }
149
150
        foreach ($config->getExcludedNames() as $exclude) {
151
            $finder->notName($exclude);
152
        }
153
154
        return $finder;
155
    }
156
}
157