Completed
Pull Request — master (#1385)
by Andreas
10:19
created

ShardCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 7.45 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 5
dl 7
loc 94
ccs 0
cts 51
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 7 22 4
A processDocumentIndex() 0 4 1
A processIndex() 0 4 1
A processDocumentCollection() 0 4 1
A processCollection() 0 4 1
A processDocumentDb() 0 4 1
A processDb() 0 4 1

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
 * 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
class ShardCommand extends AbstractCommand
28
{
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('odm:schema:shard')
33
            ->addOption('class', 'c', InputOption::VALUE_OPTIONAL, 'Document class to process (default: all classes)')
34
            ->setDescription('Enable sharding for selected documents')
35
        ;
36
    }
37
38
    /**
39
     * @param InputInterface $input
40
     * @param OutputInterface $output
41
     * @return int|null|void
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $class = $input->getOption('class');
46
47
        $sm = $this->getSchemaManager();
48
        $isErrored = false;
49
50
        try {
51 View Code Duplication
            if (isset($class)) {
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...
52
                $this->processDocumentIndex($sm, $class);
53
                $output->writeln(sprintf('Enabled sharding for <info>%s</info>', $class));
54
            } else {
55
                $this->processIndex($sm);
56
                $output->writeln('Enabled sharding for <info>all classes</info>');
57
            }
58
        } catch (\Exception $e) {
59
            $output->writeln('<error>' . $e->getMessage() . '</error>');
60
            $isErrored = true;
61
        }
62
63
        return $isErrored ? 255 : 0;
64
    }
65
66
    /**
67
     * @param SchemaManager $sm
68
     * @param object $document
69
     */
70
    protected function processDocumentIndex(SchemaManager $sm, $document)
71
    {
72
        $sm->ensureDocumentSharding($document);
0 ignored issues
show
Documentation introduced by
$document is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
    }
74
75
    /**
76
     * @param SchemaManager $sm
77
     */
78
    protected function processIndex(SchemaManager $sm)
79
    {
80
        $sm->ensureSharding();
81
    }
82
83
    /**
84
     * @param SchemaManager $sm
85
     * @param object $document
86
     * @throws \BadMethodCallException
87
     */
88
    protected function processDocumentCollection(SchemaManager $sm, $document)
89
    {
90
        throw new \BadMethodCallException('Cannot update a document collection');
91
    }
92
93
    /**
94
     * @param SchemaManager $sm
95
     * @throws \BadMethodCallException
96
     */
97
    protected function processCollection(SchemaManager $sm)
98
    {
99
        throw new \BadMethodCallException('Cannot update a collection');
100
    }
101
102
    /**
103
     * @param SchemaManager $sm
104
     * @param object $document
105
     * @throws \BadMethodCallException
106
     */
107
    protected function processDocumentDb(SchemaManager $sm, $document)
108
    {
109
        throw new \BadMethodCallException('Cannot update a document database');
110
    }
111
112
    /**
113
     * @param SchemaManager $sm
114
     * @throws \BadMethodCallException
115
     */
116
    protected function processDb(SchemaManager $sm)
117
    {
118
        throw new \BadMethodCallException('Cannot update a database');
119
    }
120
}
121