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

DeleteObsoleteCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 9.17 %

Coupling/Cohesion

Components 2
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 11
dl 10
loc 109
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A configure() 10 10 1
C execute() 0 52 10

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\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