Failed Conditions
Pull Request — master (#7842)
by
unknown
09:15
created

RunDqlCommand::execute()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 55
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 29
c 0
b 0
f 0
nc 13
nop 2
dl 0
loc 55
rs 8.2114
ccs 0
cts 30
cp 0
crap 72

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
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools\Console\Command;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use LogicException;
9
use RuntimeException;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
use Symfony\Component\VarDumper\Cloner\VarCloner;
17
use Symfony\Component\VarDumper\Dumper\CliDumper;
18
use function constant;
19
use function defined;
20
use function is_numeric;
21
use function sprintf;
22
use function str_replace;
23
use function strtoupper;
24
25
/**
26
 * Command to execute DQL queries in a given EntityManager.
27
 */
28
class RunDqlCommand extends Command
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    protected function configure()
34
    {
35 1
        $this->setName('orm:run-dql')
36 1
             ->setDescription('Executes arbitrary DQL directly from the command line')
37 1
             ->addArgument('dql', InputArgument::REQUIRED, 'The DQL to execute.')
38 1
             ->addOption('hydrate', null, InputOption::VALUE_REQUIRED, 'Hydration mode of result set. Should be either: object, array, scalar or single-scalar.', 'object')
39 1
             ->addOption('first-result', null, InputOption::VALUE_REQUIRED, 'The first result in the result set.')
40 1
             ->addOption('max-result', null, InputOption::VALUE_REQUIRED, 'The maximum number of results in the result set.')
41 1
             ->addOption('show-sql', null, InputOption::VALUE_NONE, 'Dump generated SQL instead of executing query')
42 1
             ->setHelp('Executes arbitrary DQL directly from the command line.');
43 1
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $ui = new SymfonyStyle($input, $output);
51
52
        /** @var EntityManagerInterface $em */
53
        $em  = $this->getHelper('em')->getEntityManager();
54
        $dql = $input->getArgument('dql');
55
56
        if ($dql === null) {
57
            throw new RuntimeException("Argument 'dql' is required in order to execute this command correctly.");
58
        }
59
60
        $hydrationModeName = $input->getOption('hydrate');
61
        $hydrationMode     = 'Doctrine\ORM\Query::HYDRATE_' . strtoupper(str_replace('-', '_', $hydrationModeName));
62
63
        if (! defined($hydrationMode)) {
64
            throw new RuntimeException(sprintf(
65
                "Hydration mode '%s' does not exist. It should be either: object. array, scalar or single-scalar.",
66
                $hydrationModeName
0 ignored issues
show
Bug introduced by
It seems like $hydrationModeName 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

66
                /** @scrutinizer ignore-type */ $hydrationModeName
Loading history...
67
            ));
68
        }
69
70
        $query       = $em->createQuery($dql);
0 ignored issues
show
Bug introduced by
It seems like $dql can also be of type string[]; however, parameter $dql of Doctrine\ORM\EntityManagerInterface::createQuery() 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

70
        $query       = $em->createQuery(/** @scrutinizer ignore-type */ $dql);
Loading history...
71
        $firstResult = $input->getOption('first-result');
72
73
        if ($firstResult !== null) {
74
            if (! is_numeric($firstResult)) {
75
                throw new LogicException("Option 'first-result' must contain an integer value");
76
            }
77
78
            $query->setFirstResult((int) $firstResult);
79
        }
80
81
        $maxResult = $input->getOption('max-result');
82
83
        if ($maxResult !== null) {
84
            if (! is_numeric($maxResult)) {
85
                throw new LogicException("Option 'max-result' must contain an integer value");
86
            }
87
88
            $query->setMaxResults((int) $maxResult);
89
        }
90
91
        if ($input->getOption('show-sql')) {
92
            $ui->text($query->getSQL());
93
94
            return;
95
        }
96
97
        $resultSet = $query->execute([], constant($hydrationMode));
98
99
        $dumper = new CliDumper(static function (string $payload) use ($output) : void {
100
            $output->write($payload);
101
        });
102
        $dumper->dump((new VarCloner())->cloneVar($resultSet));
103
    }
104
}
105