Failed Conditions
Pull Request — 2.6 (#7506)
by
unknown
09:52
created

GenerateRepositoriesCommand::execute()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 57
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7.355

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 57
ccs 25
cts 31
cp 0.8065
rs 8.5066
c 0
b 0
f 0
cc 7
nc 9
nop 2
crap 7.355

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
 * @deprecated 2.7 This class is being removed from the ORM and won't have any replacement
42
 */
43
class GenerateRepositoriesCommand extends Command
44
{
45
    /**
46
     * {@inheritdoc}
47
     */
48 3
    protected function configure()
49
    {
50 3
        $this->setName('orm:generate-repositories')
51 3
             ->setAliases(['orm:generate:repositories'])
52 3
             ->setDescription('Generate repository classes from your mapping information')
53 3
             ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.')
54 3
             ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
55 3
             ->setHelp('Generate repository classes from your mapping information.');
56 3
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 3
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63 3
        $ui = new SymfonyStyle($input, $output);
64 3
        $ui->warning('Command ' . $this->getName() . ' is deprecated and will be removed in Doctrine 3.0.');
65
66 3
        $em = $this->getHelper('em')->getEntityManager();
67
68 3
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
69 3
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
70
71 3
        $repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName();
72
73
        // Process destination directory
74 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

74
        $destPath = realpath(/** @scrutinizer ignore-type */ $input->getArgument('dest-path'));
Loading history...
75
76 3
        if ( ! file_exists($destPath)) {
77
            throw new \InvalidArgumentException(
78
                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

78
                sprintf("Entities destination directory '<info>%s</info>' does not exist.", /** @scrutinizer ignore-type */ $input->getArgument('dest-path'))
Loading history...
79
            );
80
        }
81
82 3
        if ( ! is_writable($destPath)) {
83
            throw new \InvalidArgumentException(
84
                sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
85
            );
86
        }
87
88 3
        if (empty($metadatas)) {
89 1
            $ui->success('No Metadata Classes to process.');
90 1
            return 0;
91
        }
92
93 2
        $numRepositories = 0;
94 2
        $generator       = new EntityRepositoryGenerator();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ORM\Tools\EntityRepositoryGenerator has been deprecated: 2.7 This class is being removed from the ORM and won't have any replacement ( Ignorable by Annotation )

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

94
        $generator       = /** @scrutinizer ignore-deprecated */ new EntityRepositoryGenerator();
Loading history...
95
96 2
        $generator->setDefaultRepositoryName($repositoryName);
97
98 2
        foreach ($metadatas as $metadata) {
99 2
            if ($metadata->customRepositoryClassName) {
100 2
                $ui->text(sprintf('Processing repository "<info>%s</info>"', $metadata->customRepositoryClassName));
101
102 2
                $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath);
103
104 2
                ++$numRepositories;
105
            }
106
        }
107
108 2
        if ($numRepositories === 0) {
109
            $ui->text('No Repository classes were found to be processed.');
110
            return 0;
111
        }
112
113
        // Outputting information message
114 2
        $ui->newLine();
115 2
        $ui->text(sprintf('Repository classes generated to "<info>%s</info>"', $destPath));
116
117 2
        return 0;
118
    }
119
}
120