Completed
Pull Request — 2.x (#210)
by Christian
09:33
created

DumpMappingCommand::getDoctrineRegistry()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\EasyExtendsBundle\Command;
15
16
use Doctrine\Bundle\DoctrineBundle\Registry;
17
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
18
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Generate Application entities from bundle entities.
25
 *
26
 * NEXT_MAJOR: stop extending ContainerAwareCommand.
27
 *
28
 * @author Thomas Rabaix <[email protected]>
29
 */
30
class DumpMappingCommand extends ContainerAwareCommand
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...d\ContainerAwareCommand has been deprecated with message: since Symfony 4.2, use {@see Command} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
31
{
32
    /**
33
     * @var Registry|null
34
     */
35
    private $registry;
36
37
    public function __construct(string $name = null, Registry $registry = null)
38
    {
39
        parent::__construct($name);
40
41
        if (null === $registry) {
42
            @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
                'Not providing a registry to "%s" is deprecated since 2.x and will no longer be possible in 3.0',
44
                \get_class($this)
45
            ), E_USER_DEPRECATED);
46
        }
47
48
        $this->registry = $registry;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected function configure()
55
    {
56
        $this->setName('sonata:easy-extends:dump-mapping');
57
        $this->setDescription('Dump some mapping information (debug only)');
58
59
        $this->addArgument('manager', InputArgument::OPTIONAL, 'The manager name to use', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|array<integer,string>|null.

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...
60
        $this->addArgument('model', InputArgument::OPTIONAL, 'The class to dump', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|array<integer,string>|null.

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...
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output): ?int
67
    {
68
        $factory = $this->getDoctrineRegistry()->getManager($input->getArgument('manager'))->getMetadataFactory();
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('manager') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string>; however, Doctrine\Common\Persiste...rRegistry::getManager() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
69
70
        $metadata = $factory->getMetadataFor($input->getArgument('model'));
71
72
        $cme = new ClassMetadataExporter();
73
        $exporter = $cme->getExporter('php');
74
75
        $output->writeln($exporter->exportClassMetadata($metadata));
76
        $output->writeln('Done!');
77
78
        return 0;
79
    }
80
81
    private function getDoctrineRegistry(): Registry
82
    {
83
        if (null === $this->registry) {
84
            return $this->getContainer()->get('doctrine');
85
        }
86
87
        return $this->registry;
88
    }
89
}
90