Completed
Pull Request — 1.0.x (#1416)
by Maciej
09:32
created

DropCommand::execute()   C

Complexity

Conditions 12
Paths 84

Size

Total Lines 36
Code Lines 23

Duplication

Lines 18
Ratio 50 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 18
loc 36
ccs 0
cts 31
cp 0
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 23
nc 84
nop 2
crap 156

How to fix   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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;
21
22
use Doctrine\ODM\MongoDB\SchemaManager;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Input\InputOption;
25
use Symfony\Component\Console\Output\OutputInterface;
26
27
/**
28
 * @author Bulat Shakirzyanov <[email protected]>
29
 */
30
class DropCommand extends AbstractCommand
31
{
32
    private $dropOrder = array(self::INDEX, self::COLLECTION, self::DB);
33
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('odm:schema:drop')
38
            ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)')
39
            ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Drop databases')
40
            ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Drop collections')
41
            ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Drop indexes')
42
            ->setDescription('Drop databases, collections and indexes for your documents')
43
        ;
44
    }
45
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        foreach ($this->dropOrder as $option) {
49
            if ($input->getOption($option)) {
50
                $drop[] = $option;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$drop was never initialized. Although not strictly required by PHP, it is generally a good practice to add $drop = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
51
            }
52
        }
53
54
        // Default to the full drop order if no options were specified
55
        $drop = empty($drop) ? $this->dropOrder : $drop;
56
57
        $class = $input->getOption('class');
58
        $sm = $this->getSchemaManager();
59
        $isErrored = false;
60
61 View Code Duplication
        foreach ($drop as $option) {
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...
62
            try {
63
                if (isset($class)) {
64
                    $this->{'processDocument' . ucfirst($option)}($sm, $class);
65
                } else {
66
                    $this->{'process' . ucfirst($option)}($sm);
67
                }
68
                $output->writeln(sprintf(
69
                    'Dropped <comment>%s%s</comment> for <info>%s</info>',
70
                    $option,
71
                    (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')),
72
                    (isset($class) ? $class : 'all classes')
73
                ));
74
            } catch (\Exception $e) {
75
                $output->writeln('<error>' . $e->getMessage() . '</error>');
76
                $isErrored = true;
77
            }
78
        }
79
80
        return ($isErrored) ? 255 : 0;
81
    }
82
83
    protected function processDocumentCollection(SchemaManager $sm, $document)
84
    {
85
        $sm->dropDocumentCollection($document);
86
    }
87
88
    protected function processCollection(SchemaManager $sm)
89
    {
90
        $sm->dropCollections();
91
    }
92
93
    protected function processDocumentDb(SchemaManager $sm, $document)
94
    {
95
        $sm->dropDocumentDatabase($document);
96
    }
97
98
    protected function processDb(SchemaManager $sm)
99
    {
100
        $sm->dropDatabases();
101
    }
102
103
    protected function processDocumentIndex(SchemaManager $sm, $document)
104
    {
105
        $sm->deleteDocumentIndexes($document);
106
    }
107
108
    protected function processIndex(SchemaManager $sm)
109
    {
110
        $sm->deleteIndexes();
111
    }
112
}
113