Passed
Pull Request — 2.6 (#7899)
by Andreas
07:29
created

GenerateRepositoriesCommand::execute()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.392

Importance

Changes 0
Metric Value
cc 7
eloc 29
c 0
b 0
f 0
nc 9
nop 2
dl 0
loc 56
ccs 24
cts 30
cp 0.8
crap 7.392
rs 8.5226

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 Doctrine\ORM\Tools\Console\MetadataFilter;
23
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Style\SymfonyStyle;
30
31
/**
32
 * Command to generate repository classes for mapping information.
33
 *
34
 * @link    www.doctrine-project.org
35
 * @since   2.0
36
 * @author  Benjamin Eberlei <[email protected]>
37
 * @author  Guilherme Blanco <[email protected]>
38
 * @author  Jonathan Wage <[email protected]>
39
 * @author  Roman Borschel <[email protected]>
40
 */
41
class GenerateRepositoriesCommand extends Command
42
{
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    protected function configure()
47
    {
48 3
        $this->setName('orm:generate-repositories')
49 3
             ->setAliases(['orm:generate:repositories'])
50 3
             ->setDescription('Generate repository classes from your mapping information')
51 3
             ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.')
52 3
             ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
53 3
             ->setHelp('Generate repository classes from your mapping information.');
54 3
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 3
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61 3
        $ui = new SymfonyStyle($input, $output);
62
63 3
        $em = $this->getHelper('em')->getEntityManager();
64
65 3
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
66 3
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
67
68 3
        $repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName();
69
70
        // Process destination directory
71 3
        $destPath = realpath($input->getArgument('dest-path'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('dest-path') can also be of type string[]; however, parameter $path of realpath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $destPath = realpath(/** @scrutinizer ignore-type */ $input->getArgument('dest-path'));
Loading history...
72
73 3
        if ( ! file_exists($destPath)) {
74
            throw new \InvalidArgumentException(
75
                sprintf("Entities destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('dest-path') can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
                sprintf("Entities destination directory '<info>%s</info>' does not exist.", /** @scrutinizer ignore-type */ $input->getArgument('dest-path'))
Loading history...
76
            );
77
        }
78
79 3
        if ( ! is_writable($destPath)) {
80
            throw new \InvalidArgumentException(
81
                sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
82
            );
83
        }
84
85 3
        if (empty($metadatas)) {
86 1
            $ui->success('No Metadata Classes to process.');
87 1
            return 0;
88
        }
89
90 2
        $numRepositories = 0;
91 2
        $generator       = new EntityRepositoryGenerator();
92
93 2
        $generator->setDefaultRepositoryName($repositoryName);
94
95 2
        foreach ($metadatas as $metadata) {
96 2
            if ($metadata->customRepositoryClassName) {
97 2
                $ui->text(sprintf('Processing repository "<info>%s</info>"', $metadata->customRepositoryClassName));
98
99 2
                $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath);
100
101 2
                ++$numRepositories;
102
            }
103
        }
104
105 2
        if ($numRepositories === 0) {
106
            $ui->text('No Repository classes were found to be processed.');
107
            return 0;
108
        }
109
110
        // Outputting information message
111 2
        $ui->newLine();
112 2
        $ui->text(sprintf('Repository classes generated to "<info>%s</info>"', $destPath));
113
114 2
        return 0;
115
    }
116
}
117