Completed
Push — master ( ce8c34...f078de )
by Andreas
49:52 queued 47:00
created

Tools/Console/Command/Schema/DropCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema;
4
5
use Doctrine\ODM\MongoDB\SchemaManager;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class DropCommand extends AbstractCommand
11
{
12
    private $dropOrder = array(self::INDEX, self::COLLECTION, self::DB);
13
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('odm:schema:drop')
18
            ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)')
19
            ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Drop databases')
20
            ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Drop collections')
21
            ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Drop indexes')
22
            ->setDescription('Drop databases, collections and indexes for your documents')
23
        ;
24
    }
25
26
    protected function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        foreach ($this->dropOrder as $option) {
29
            if ($input->getOption($option)) {
30
                $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...
31
            }
32
        }
33
34
        // Default to the full drop order if no options were specified
35
        $drop = empty($drop) ? $this->dropOrder : $drop;
36
37
        $class = $input->getOption('class');
38
        $sm = $this->getSchemaManager();
39
        $isErrored = false;
40
41 View Code Duplication
        foreach ($drop as $option) {
42
            try {
43
                if (isset($class)) {
44
                    $this->{'processDocument' . ucfirst($option)}($sm, $class);
45
                } else {
46
                    $this->{'process' . ucfirst($option)}($sm);
47
                }
48
                $output->writeln(sprintf(
49
                    'Dropped <comment>%s%s</comment> for <info>%s</info>',
50
                    $option,
51
                    (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')),
52
                    ($class ?? 'all classes')
53
                ));
54
            } catch (\Exception $e) {
55
                $output->writeln('<error>' . $e->getMessage() . '</error>');
56
                $isErrored = true;
57
            }
58
        }
59
60
        return $isErrored ? 255 : 0;
61
    }
62
63
    protected function processDocumentCollection(SchemaManager $sm, $document)
64
    {
65
        $sm->dropDocumentCollection($document);
66
    }
67
68
    protected function processCollection(SchemaManager $sm)
69
    {
70
        $sm->dropCollections();
71
    }
72
73
    protected function processDocumentDb(SchemaManager $sm, $document)
74
    {
75
        $sm->dropDocumentDatabase($document);
76
    }
77
78
    protected function processDb(SchemaManager $sm)
79
    {
80
        $sm->dropDatabases();
81
    }
82
83
    protected function processDocumentIndex(SchemaManager $sm, $document)
84
    {
85
        $sm->deleteDocumentIndexes($document);
86
    }
87
88
    protected function processIndex(SchemaManager $sm)
89
    {
90
        $sm->deleteIndexes();
91
    }
92
}
93