Completed
Push — master ( 356e08...d08624 )
by Tobias
09:26
created

DeleteObsoleteCommand::execute()   C

Complexity

Conditions 10
Paths 44

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 0
cts 44
cp 0
rs 6.2553
c 0
b 0
f 0
cc 10
eloc 33
nc 44
nop 2
crap 110

How to fix   Long Method    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
/*
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\Helper\ProgressBar;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Question\ConfirmationQuestion;
21
use Translation\Bundle\Catalogue\CatalogueFetcher;
22
use Translation\Bundle\Catalogue\CatalogueManager;
23
use Translation\Bundle\Service\ConfigurationManager;
24
use Translation\Bundle\Service\StorageManager;
25
26
/**
27
 * @author Tobias Nyholm <[email protected]>
28
 */
29
class DeleteObsoleteCommand extends Command
30
{
31
    use BundleTrait;
32
33
    protected static $defaultName = 'translation:delete-obsolete';
34
35
    /**
36
     * @var StorageManager
37
     */
38
    private $storageManager;
39
40
    /**
41
     * @var ConfigurationManager
42
     */
43
    private $configurationManager;
44
45
    /**
46
     * @var CatalogueManager
47
     */
48
    private $catalogueManager;
49
50
    /**
51
     * @var CatalogueFetcher
52
     */
53
    private $catalogueFetcher;
54
55
    /**
56
     * @param StorageManager       $storageManager
57
     * @param ConfigurationManager $configurationManager
58
     * @param CatalogueManager     $catalogueManager
59
     * @param CatalogueFetcher     $catalogueFetcher
60
     */
61
    public function __construct(
62
        StorageManager $storageManager,
63
        ConfigurationManager $configurationManager,
64
        CatalogueManager $catalogueManager,
65
        CatalogueFetcher $catalogueFetcher
66
    ) {
67
        $this->storageManager = $storageManager;
68
        $this->configurationManager = $configurationManager;
69
        $this->catalogueManager = $catalogueManager;
70
        $this->catalogueFetcher = $catalogueFetcher;
71
        parent::__construct();
72
    }
73
74 View Code Duplication
    protected function configure()
1 ignored issue
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...
75
    {
76
        $this
77
            ->setName(self::$defaultName)
78
            ->setDescription('Delete all translations marked as obsolete.')
79
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
80
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null)
81
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want remove translations from.')
82
        ;
83
    }
84
85
    protected function execute(InputInterface $input, OutputInterface $output)
86
    {
87
        $configName = $input->getArgument('configuration');
88
        $locales = [];
89
        if (null !== $inputLocale = $input->getArgument('locale')) {
90
            $locales = [$inputLocale];
91
        }
92
93
        $config = $this->configurationManager->getConfiguration($configName);
94
        $this->configureBundleDirs($input, $config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->configurationMana...figuration($configName) on line 93 can be null; however, Translation\Bundle\Comma...::configureBundleDirs() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
95
        $this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->configurationMana...figuration($configName) on line 93 can be null; however, Translation\Bundle\Catal...etcher::getCatalogues() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
96
97
        $storage = $this->storageManager->getStorage($configName);
98
        $messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
99
100
        $messageCount = count($messages);
101
        if (0 === $messageCount) {
102
            $output->writeln('No messages are obsolete');
103
104
            return;
105
        }
106
107
        $helper = $this->getHelper('question');
108
        $question = new ConfirmationQuestion(sprintf('You are about to remove %d translations. Do you wish to continue? (y/N) ', $messageCount), false);
109
        if (!$helper->ask($input, $output, $question)) {
110
            return;
111
        }
112
113
        $progress = null;
114
        if (OutputInterface::VERBOSITY_NORMAL === $output->getVerbosity() && OutputInterface::VERBOSITY_QUIET !== $output->getVerbosity()) {
115
            $progress = new ProgressBar($output, $messageCount);
116
        }
117
        foreach ($messages as $message) {
118
            $storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
119
            if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
120
                $output->writeln(sprintf(
121
                    'Deleted obsolete message "<info>%s</info>" from domain "<info>%s</info>" and locale "<info>%s</info>"',
122
                    $message->getKey(),
123
                    $message->getDomain(),
124
                    $message->getLocale()
125
                ));
126
            }
127
128
            if ($progress) {
129
                $progress->advance();
130
            }
131
        }
132
133
        if ($progress) {
134
            $progress->finish();
135
        }
136
    }
137
}
138