Failed Conditions
Pull Request — master (#6546)
by Jáchym
11:13
created

GenerateRepositoriesCommand::execute()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.3112

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 22
cts 27
cp 0.8148
rs 7.2396
c 0
b 0
f 0
cc 7
eloc 28
nc 9
nop 2
crap 7.3112

How to fix   Long Method   

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\ORM\Tools\Console\Command;
21
22
use Symfony\Component\Console\Input\InputArgument;
23
use Symfony\Component\Console\Input\InputOption;
24
use Doctrine\ORM\Tools\Console\MetadataFilter;
25
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Command\Command;
29
30
/**
31
 * Command to generate repository classes for mapping information.
32
 *
33
 * @link    www.doctrine-project.org
34
 * @since   2.0
35
 * @author  Benjamin Eberlei <[email protected]>
36
 * @author  Guilherme Blanco <[email protected]>
37
 * @author  Jonathan Wage <[email protected]>
38
 * @author  Roman Borschel <[email protected]>
39
 */
40
class GenerateRepositoriesCommand extends Command
41
{
42
    /**
43
     * {@inheritdoc}
44
     */
45 2 View Code Duplication
    protected function configure()
46
    {
47
        $this
48 2
        ->setName('orm:generate-repositories')
49 2
        ->setAliases(['orm:generate:repositories'])
50 2
        ->setDescription('Generate repository classes from your mapping information.')
51 2
        ->setDefinition(
52
            [
53 2
                new InputOption(
54 2
                    'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
55 2
                    'A string pattern used to match entities that should be processed.'
56
                ),
57 2
                new InputArgument(
58 2
                    'dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.'
59
                )
60
            ]
61
        )
62 2
        ->setHelp(<<<EOT
63 2
Generate repository classes from your mapping information.
64
EOT
65
        );
66 2
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73 2
        $em = $this->getHelper('em')->getEntityManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method getEntityManager() does only exist in the following implementations of said interface: Doctrine\ORM\Tools\Conso...per\EntityManagerHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
74
75 2
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
76 2
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
77
78 2
        $repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName();
79
80
        // Process destination directory
81 2
        $destPath = realpath($input->getArgument('dest-path'));
82
83 2
        if ( ! file_exists($destPath)) {
84
            throw new \InvalidArgumentException(
85
                sprintf("Entities destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
86
            );
87
        }
88
89 2
        if ( ! is_writable($destPath)) {
90
            throw new \InvalidArgumentException(
91
                sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
92
            );
93
        }
94
95 2
        if (count($metadatas)) {
96 2
            $numRepositories = 0;
97 2
            $generator = new EntityRepositoryGenerator();
98
99 2
            $generator->setDefaultRepositoryName($repositoryName);
100
101 2
            foreach ($metadatas as $metadata) {
102 2
                if ($metadata->customRepositoryClassName) {
103 2
                    $output->writeln(
104 2
                        sprintf('Processing repository "<info>%s</info>"', $metadata->customRepositoryClassName)
105
                    );
106
107 2
                    $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath);
108
109 2
                    $numRepositories++;
110
                }
111
            }
112
113 2
            if ($numRepositories) {
114
                // Outputting information message
115 2
                $output->writeln(PHP_EOL . sprintf('Repository classes generated to "<info>%s</INFO>"', $destPath));
116
            } else {
117 2
                $output->writeln('No Repository classes were found to be processed.');
118
            }
119
        } else {
120
            $output->writeln('No Metadata Classes to process.');
121
        }
122 2
    }
123
}
124