Completed
Push — master ( 4e8bc9...f46df8 )
by Tobias
13:01 queued 05:03
created

ExtractCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 9
loc 84
ccs 50
cts 55
cp 0.9091
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 9 9 1
B execute() 0 50 4
A getConfiguredFinder() 0 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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\Model\Configuration;
22
use Translation\Extractor\Model\Error;
23
24
/**
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
class ExtractCommand extends ContainerAwareCommand
28
{
29 1 View Code Duplication
    protected function configure()
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...
30
    {
31 1
        $this
32 1
            ->setName('translation:extract')
33 1
            ->setDescription('Extract translations from source code.')
34 1
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
35 1
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false)
36 1
            ->addOption('hide-errors', null, InputOption::VALUE_NONE, 'If we should print error or not');
37 1
    }
38
39 1
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41 1
        $container = $this->getContainer();
42 1
        $importer = $container->get('php_translation.importer');
43 1
        $config = $container->get('php_translation.configuration_manager')
44 1
            ->getConfiguration($input->getArgument('configuration'));
45
46 1
        $locales = [];
47 1
        if ($inputLocale = $input->getArgument('locale')) {
48 1
            $locales = [$inputLocale];
49 1
        }
50
51 1
        $catalogues = $container->get('php_translation.catalogue_fetcher')
52 1
            ->getCatalogues($config, $locales);
53
54 1
        $finder = $this->getConfiguredFinder($config);
55
56 1
        $result = $importer->extractToCatalogues($finder, $catalogues, [
57 1
            'blacklist_domains' => $config->getBlacklistDomains(),
58 1
            'whitelist_domains' => $config->getWhitelistDomains(),
59 1
            'project_root' => $config->getProjectRoot(),
60 1
        ]);
61 1
        $errors = $result->getErrors();
62
63 1
        $container->get('php_translation.catalogue_writer')
64 1
            ->writeCatalogues($config, $result->getMessageCatalogues());
65
66 1
        $catalogueCounter = $container->get('php_translation.catalogue_counter');
67 1
        $definedBefore = $catalogueCounter->getNumberOfDefinedMessages($catalogues[0]);
68 1
        $definedAfter = $catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]);
69
70
        /*
71
         * Print results
72
         */
73 1
        $io = new SymfonyStyle($input, $output);
74 1
        $io->table(['Type', 'Count'], [
75 1
            ['Total defined messages', $definedAfter],
76 1
            ['New messages', $definedAfter - $definedBefore],
77 1
            ['Errors', count($errors)],
78 1
        ]);
79
80 1
        if (!$input->getOption('hide-errors')) {
81
            /** @var Error $error */
82 1
            foreach ($errors as $error) {
83
                $io->error(
84
                    sprintf("%s\nLine: %s\nMessage: %s", $error->getPath(), $error->getLine(), $error->getMessage())
85
                );
86 1
            }
87 1
        }
88 1
    }
89
90
    /**
91
     * @param Configuration $config
92
     *
93
     * @return Finder
94
     */
95 1
    private function getConfiguredFinder(Configuration $config)
96
    {
97 1
        $finder = new Finder();
98 1
        $finder->in($config->getDirs());
99
100 1
        foreach ($config->getExcludedDirs() as $exclude) {
101
            $finder->notPath($exclude);
102 1
        }
103
104 1
        foreach ($config->getExcludedNames() as $exclude) {
105
            $finder->notName($exclude);
106 1
        }
107
108 1
        return $finder;
109
    }
110
}
111