Failed Conditions
Pull Request — develop (#6873)
by
unknown
112:44 queued 47:41
created

getEntityGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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\ConvertDoctrine1Schema;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Tools\ConvertDoctrine1Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Doctrine\ORM\Tools\EntityGenerator;
24
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
/**
32
 * Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
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 ConvertDoctrine1SchemaCommand extends Command
42
{
43
    /**
44
     * @var EntityGenerator|null
45
     */
46
    private $entityGenerator = null;
47
48
    /**
49
     * @var ClassMetadataExporter|null
50
     */
51
    private $metadataExporter = null;
52
53
    /**
54
     * @return EntityGenerator
55
     */
56
    public function getEntityGenerator()
57
    {
58
        if ($this->entityGenerator == null) {
59
            $this->entityGenerator = new EntityGenerator();
60
        }
61
62
        return $this->entityGenerator;
63
    }
64
65
    /**
66
     * @param EntityGenerator $entityGenerator
67
     *
68
     * @return void
69
     */
70
    public function setEntityGenerator(EntityGenerator $entityGenerator)
71
    {
72
        $this->entityGenerator = $entityGenerator;
73
    }
74
75
    /**
76
     * @return ClassMetadataExporter
77
     */
78
    public function getMetadataExporter()
79
    {
80
        if ($this->metadataExporter == null) {
81
            $this->metadataExporter = new ClassMetadataExporter();
82
        }
83
84
        return $this->metadataExporter;
85
    }
86
87
    /**
88
     * @param ClassMetadataExporter $metadataExporter
89
     *
90
     * @return void
91
     */
92
    public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
93
    {
94
        $this->metadataExporter = $metadataExporter;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function configure()
101
    {
102
        $this->setName('orm:convert-d1-schema')
103
             ->setAliases(['orm:convert:d1-schema'])
104
             ->setDescription('Converts Doctrine 1.x schema into a Doctrine 2.x schema')
105
             ->addArgument('from-path', InputArgument::REQUIRED, 'The path of Doctrine 1.X schema information.')
106
             ->addArgument('to-type', InputArgument::REQUIRED, 'The destination Doctrine 2.X mapping type.')
107
             ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your Doctrine 2.X mapping information.')
108
             ->addOption('from', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Optional paths of Doctrine 1.X schema information.', [])
109
             ->addOption('extend', null, InputOption::VALUE_OPTIONAL, 'Defines a base class to be extended by generated entity classes.')
110
             ->addOption('num-spaces', null, InputOption::VALUE_OPTIONAL, 'Defines the number of indentation spaces', 4)
111
             ->setHelp('Converts Doctrine 1.x schema into a Doctrine 2.x schema.');
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected function execute(InputInterface $input, OutputInterface $output)
118
    {
119
        // Process source directories
120
        $fromPaths = array_merge([$input->getArgument('from-path')], $input->getOption('from'));
121
122
        // Process destination directory
123
        $destPath = realpath($input->getArgument('dest-path'));
124
125
        $toType = $input->getArgument('to-type');
126
        $extend = $input->getOption('extend');
127
        $numSpaces = $input->getOption('num-spaces');
128
129
        $this->convertDoctrine1Schema($fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
0 ignored issues
show
Bug introduced by
It seems like $destPath can also be of type false; however, parameter $destPath of Doctrine\ORM\Tools\Conso...onvertDoctrine1Schema() 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

129
        $this->convertDoctrine1Schema($fromPaths, /** @scrutinizer ignore-type */ $destPath, $toType, $numSpaces, $extend, $output);
Loading history...
130
    }
131
132
    /**
133
     * @param array           $fromPaths
134
     * @param string          $destPath
135
     * @param string          $toType
136
     * @param int             $numSpaces
137
     * @param string|null     $extend
138
     * @param OutputInterface $output
139
     *
140
     * @throws \InvalidArgumentException
141
     */
142
    public function convertDoctrine1Schema(array $fromPaths, $destPath, $toType, $numSpaces, $extend, OutputInterface $output)
143
    {
144
        foreach ($fromPaths as &$dirName) {
145
            $dirName = realpath($dirName);
146
147
            if ( ! file_exists($dirName)) {
0 ignored issues
show
Bug introduced by
It seems like $dirName can also be of type false; however, parameter $filename of file_exists() 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

147
            if ( ! file_exists(/** @scrutinizer ignore-type */ $dirName)) {
Loading history...
148
                throw new \InvalidArgumentException(
149
                    sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)
0 ignored issues
show
Bug introduced by
It seems like $dirName can also be of type false; 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

149
                    sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", /** @scrutinizer ignore-type */ $dirName)
Loading history...
150
                );
151
            }
152
153
            if ( ! is_readable($dirName)) {
0 ignored issues
show
Bug introduced by
It seems like $dirName can also be of type false; however, parameter $filename of is_readable() 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

153
            if ( ! is_readable(/** @scrutinizer ignore-type */ $dirName)) {
Loading history...
154
                throw new \InvalidArgumentException(
155
                    sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)
156
                );
157
            }
158
        }
159
160
        if ( ! file_exists($destPath)) {
161
            throw new \InvalidArgumentException(
162
                sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
163
            );
164
        }
165
166
        if ( ! is_writable($destPath)) {
167
            throw new \InvalidArgumentException(
168
                sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)
169
            );
170
        }
171
172
        $cme = $this->getMetadataExporter();
173
        $exporter = $cme->getExporter($toType, $destPath);
174
175
        if (strtolower($toType) === 'annotation') {
176
            $entityGenerator = $this->getEntityGenerator();
177
            $exporter->setEntityGenerator($entityGenerator);
0 ignored issues
show
Bug introduced by
The method setEntityGenerator() does not exist on Doctrine\ORM\Tools\Export\Driver\AbstractExporter. It seems like you code against a sub-type of Doctrine\ORM\Tools\Export\Driver\AbstractExporter such as Doctrine\ORM\Tools\Expor...iver\AnnotationExporter. ( Ignorable by Annotation )

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

177
            $exporter->/** @scrutinizer ignore-call */ 
178
                       setEntityGenerator($entityGenerator);
Loading history...
178
179
            $entityGenerator->setNumSpaces($numSpaces);
180
181
            if ($extend !== null) {
182
                $entityGenerator->setClassToExtend($extend);
183
            }
184
        }
185
186
        $converter = new ConvertDoctrine1Schema($fromPaths);
187
        $metadata = $converter->getMetadata();
188
189
        if ($metadata) {
190
            $output->writeln('');
191
192
            foreach ($metadata as $class) {
193
                $output->writeln(sprintf('Processing entity "<info>%s</info>"', $class->name));
194
            }
195
196
            $exporter->setMetadata($metadata);
197
            $exporter->export();
198
199
            $output->writeln(PHP_EOL . sprintf(
200
                'Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath
201
            ));
202
        } else {
203
            $output->writeln('No Metadata Classes to process.');
204
        }
205
    }
206
}
207