ImportMappingDoctrineCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 9.248
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\Command;
4
5
use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
6
use Doctrine\ORM\Tools\Console\MetadataFilter;
7
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
8
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
9
use Doctrine\Persistence\ManagerRegistry;
10
use InvalidArgumentException;
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
16
/**
17
 * Import Doctrine ORM metadata mapping information from an existing database.
18
 *
19
 * @deprecated
20
 *
21
 * @final
22
 */
23
class ImportMappingDoctrineCommand extends DoctrineCommand
24
{
25
    /** @var string[] */
26
    private $bundles;
27
28
    /**
29
     * @param string[] $bundles
30
     */
31
    public function __construct(ManagerRegistry $doctrine, array $bundles)
32
    {
33
        parent::__construct($doctrine);
34
35
        $this->bundles = $bundles;
36
    }
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('doctrine:mapping:import')
45
            ->addArgument('name', InputArgument::REQUIRED, 'The bundle or namespace to import the mapping information to')
46
            ->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the imported mapping information to')
47
            ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
48
            ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command')
49
            ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be mapped.')
50
            ->addOption('force', null, InputOption::VALUE_NONE, 'Force to overwrite existing mapping files.')
51
            ->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path where the files would be generated (not used when a bundle is passed).')
52
            ->setDescription('Imports mapping information from an existing database')
53
            ->setHelp(<<<EOT
54
The <info>%command.name%</info> command imports mapping information
55
from an existing database:
56
57
Generate annotation mappings into the src/ directory using App as the namespace:
58
<info>php %command.full_name% App\\\Entity annotation --path=src/Entity</info>
59
60
Generate xml mappings into the config/doctrine/ directory using App as the namespace:
61
<info>php %command.full_name% App\\\Entity xml --path=config/doctrine</info>
62
63
Generate XML mappings into a bundle:
64
<info>php %command.full_name% "MyCustomBundle" xml</info>
65
66
You can also optionally specify which entity manager to import from with the
67
<info>--em</info> option:
68
69
<info>php %command.full_name% "MyCustomBundle" xml --em=default</info>
70
71
If you don't want to map every entity that can be found in the database, use the
72
<info>--filter</info> option. It will try to match the targeted mapped entity with the
73
provided pattern string.
74
75
<info>php %command.full_name% "MyCustomBundle" xml --filter=MyMatchedEntity</info>
76
77
Use the <info>--force</info> option, if you want to override existing mapping files:
78
79
<info>php %command.full_name% "MyCustomBundle" xml --force</info>
80
EOT
81
        );
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        $type = $input->getArgument('mapping-type') ?: 'xml';
90
        if ($type === 'yaml') {
91
            $type = 'yml';
92
        }
93
94
        $namespaceOrBundle = $input->getArgument('name');
95
        if (isset($this->bundles[$namespaceOrBundle])) {
96
            $bundle    = $this->getApplication()->getKernel()->getBundle($namespaceOrBundle);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
97
            $namespace = $bundle->getNamespace() . '\Entity';
98
99
            $destPath = $bundle->getPath();
100
            if ($type === 'annotation') {
101
                $destPath .= '/Entity';
102
            } else {
103
                $destPath .= '/Resources/config/doctrine';
104
            }
105
        } else {
106
            // assume a namespace has been passed
107
            $namespace = $namespaceOrBundle;
108
            $destPath  = $input->getOption('path');
109
            if ($destPath === null) {
110
                throw new InvalidArgumentException('The --path option is required when passing a namespace (e.g. --path=src). If you intended to pass a bundle name, check your spelling.');
111
            }
112
        }
113
114
        $cme      = new ClassMetadataExporter();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\ORM\Tools\Export\ClassMetadataExporter has been deprecated with message: 2.7 This class is being removed from the ORM and won't have any replacement

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...
115
        $exporter = $cme->getExporter($type);
0 ignored issues
show
Bug introduced by
It seems like $type defined by $input->getArgument('mapping-type') ?: 'xml' on line 89 can also be of type array<integer,string>; however, Doctrine\ORM\Tools\Expor...Exporter::getExporter() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
        $exporter->setOverwriteExistingFiles($input->getOption('force'));
117
118
        if ($type === 'annotation') {
119
            $entityGenerator = $this->getEntityGenerator();
120
            $exporter->setEntityGenerator($entityGenerator);
121
        }
122
123
        $em = $this->getEntityManager($input->getOption('em'), $input->getOption('shard'));
124
125
        $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
126
        $em->getConfiguration()->setMetadataDriverImpl($databaseDriver);
127
128
        $emName = $input->getOption('em');
129
        $emName = $emName ? $emName : 'default';
130
131
        $cmf = new DisconnectedClassMetadataFactory();
132
        $cmf->setEntityManager($em);
133
        $metadata = $cmf->getAllMetadata();
134
        $metadata = MetadataFilter::filter($metadata, $input->getOption('filter'));
135
        if ($metadata) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $metadata of type Doctrine\Persistence\Mapping\ClassMetadata[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
136
            $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
137
            foreach ($metadata as $class) {
138
                $className   = $class->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
139
                $class->name = $namespace . '\\' . $className;
0 ignored issues
show
Bug introduced by
Accessing name on the interface Doctrine\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
140
                if ($type === 'annotation') {
141
                    $path = $destPath . '/' . str_replace('\\', '.', $className) . '.php';
142
                } else {
143
                    $path = $destPath . '/' . str_replace('\\', '.', $className) . '.orm.' . $type;
144
                }
145
                $output->writeln(sprintf('  > writing <comment>%s</comment>', $path));
146
                $code = $exporter->exportClassMetadata($class);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Persiste...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ORM\Mapping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
147
                $dir  = dirname($path);
148
                if (! is_dir($dir)) {
149
                    mkdir($dir, 0775, true);
150
                }
151
                file_put_contents($path, $code);
152
                chmod($path, 0664);
153
            }
154
155
            return 0;
156
        }
157
158
        $output->writeln('Database does not have any mapping information.');
159
        $output->writeln('');
160
161
        return 1;
162
    }
163
}
164