Completed
Push — master ( 7c2774...f18b9d )
by Tobias
14:09 queued 06:53
created

DeleteObsoleteCommand::getConfiguredFinder()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
crap 12
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\Helper\ProgressBar;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
use Symfony\Component\Finder\Finder;
22
use Translation\Bundle\Service\StorageService;
23
24
class DeleteObsoleteCommand extends ContainerAwareCommand
25
{
26
    /**
27
     * @var ContainerInterface
28
     */
29
    private $container;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $container is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31 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...
32
    {
33
        $this
34
            ->setName('translation:delete-obsolete')
35
            ->setDescription('Delete all translations marked as obsolete.')
36
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
37
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null);
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $configName = $input->getArgument('configuration');
43
        $config = $this->getContainer()->get('php_translation.configuration_manager')->getConfiguration($configName);
44 View Code Duplication
        if (null !== $inputLocale = $input->getArgument('locale', null)) {
0 ignored issues
show
Unused Code introduced by
The call to InputInterface::getArgument() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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...
45
            $locales = [$inputLocale];
46
        } else {
47
            $locales = $this->getContainer()->getParameter('php_translation.locales');
48
        }
49
50
        $transPaths = array_merge($config['external_translations_dirs'], [$config['output_dir']]);
51
        $catalogueManager = $this->getContainer()->get('php_translation.catalogue_manager');
52
        $catalogueManager->load($this->getContainer()->get('php_translation.catalogue_fetcher')->getCatalogues($locales, $transPaths));
53
54
        /** @var StorageService $storage */
55
        $storage = $this->getContainer()->get('php_translation.storage.'.$configName);
56
        $messages = $catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
57
58
        $helper = $this->getHelper('question');
59
        $question = new ConfirmationQuestion(sprintf('You are about to remove %d translations. Do you wish to continue?', count($messages)), false);
60
        if (!$helper->ask($input, $output, $question)) {
61
            return;
62
        }
63
64
        $progress = new ProgressBar($output, count($messages));
65
        foreach ($messages as $message) {
66
            $storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
67
            $progress->advance();
68
        }
69
        $progress->finish();
70
    }
71
72
    /**
73
     * @param array $configuration
0 ignored issues
show
Documentation introduced by
There is no parameter named $configuration. Did you maybe mean $config?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
74
     *
75
     * @return Finder
76
     */
77 View Code Duplication
    private function getConfiguredFinder(array $config)
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...
78
    {
79
        // 'dirs', 'excluded_dirs', 'excluded_names'
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
81
        $finder = new Finder();
82
        $finder->in($config['dirs']);
83
84
        foreach ($config['excluded_dirs'] as $exclude) {
85
            $finder->notPath($exclude);
86
        }
87
88
        foreach ($config['excluded_names'] as $exclude) {
89
            $finder->notName($exclude);
90
        }
91
92
        return $finder;
93
    }
94
}
95