These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; |
||
4 | |||
5 | use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
||
6 | use Doctrine\ODM\MongoDB\SchemaManager; |
||
7 | use Symfony\Component\Console\Input\InputInterface; |
||
8 | use Symfony\Component\Console\Input\InputOption; |
||
9 | use Symfony\Component\Console\Output\OutputInterface; |
||
10 | |||
11 | class CreateCommand extends AbstractCommand |
||
12 | { |
||
13 | private $createOrder = array(self::DB, self::COLLECTION, self::INDEX); |
||
14 | |||
15 | private $timeout; |
||
16 | |||
17 | protected function configure() |
||
18 | { |
||
19 | $this |
||
20 | ->setName('odm:schema:create') |
||
21 | ->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
||
22 | ->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation') |
||
23 | ->addOption(self::DB, null, InputOption::VALUE_NONE, 'Create databases') |
||
24 | ->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections') |
||
25 | ->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes') |
||
26 | ->setDescription('Create databases, collections and indexes for your documents') |
||
27 | ; |
||
28 | } |
||
29 | |||
30 | protected function execute(InputInterface $input, OutputInterface $output) |
||
31 | { |
||
32 | if ($input->getOption(self::DB)) { |
||
33 | @trigger_error('The ' . self::DB . ' option is deprecated and will be removed in ODM 2.0', E_USER_DEPRECATED); |
||
34 | } |
||
35 | |||
36 | foreach ($this->createOrder as $option) { |
||
37 | if ($input->getOption($option)) { |
||
38 | $create[] = $option; |
||
0 ignored issues
–
show
|
|||
39 | } |
||
40 | } |
||
41 | |||
42 | // Default to the full creation order if no options were specified |
||
43 | $create = empty($create) ? $this->createOrder : $create; |
||
44 | |||
45 | $class = $input->getOption('class'); |
||
46 | |||
47 | $timeout = $input->getOption('timeout'); |
||
48 | $this->timeout = isset($timeout) ? (int) $timeout : null; |
||
49 | |||
50 | $sm = $this->getSchemaManager(); |
||
51 | $isErrored = false; |
||
52 | |||
53 | View Code Duplication | foreach ($create as $option) { |
|
54 | try { |
||
55 | if (isset($class)) { |
||
56 | $this->{'processDocument' . ucfirst($option)}($sm, $class); |
||
57 | } else { |
||
58 | $this->{'process' . ucfirst($option)}($sm); |
||
59 | } |
||
60 | $output->writeln(sprintf( |
||
61 | 'Created <comment>%s%s</comment> for <info>%s</info>', |
||
62 | $option, |
||
63 | (isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
||
64 | ($class ?? 'all classes') |
||
65 | )); |
||
66 | } catch (\Exception $e) { |
||
67 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
68 | $isErrored = true; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $isErrored ? 255 : 0; |
||
73 | } |
||
74 | |||
75 | protected function processDocumentCollection(SchemaManager $sm, $document) |
||
76 | { |
||
77 | $sm->createDocumentCollection($document); |
||
78 | } |
||
79 | |||
80 | protected function processCollection(SchemaManager $sm) |
||
81 | { |
||
82 | $sm->createCollections(); |
||
83 | } |
||
84 | |||
85 | protected function processDocumentDb(SchemaManager $sm, $document) |
||
86 | { |
||
87 | $sm->createDocumentDatabase($document); |
||
88 | } |
||
89 | |||
90 | protected function processDb(SchemaManager $sm) |
||
91 | { |
||
92 | $sm->createDatabases(); |
||
93 | } |
||
94 | |||
95 | protected function processDocumentIndex(SchemaManager $sm, $document) |
||
96 | { |
||
97 | $sm->ensureDocumentIndexes($document, $this->timeout); |
||
98 | } |
||
99 | |||
100 | protected function processIndex(SchemaManager $sm) |
||
101 | { |
||
102 | $sm->ensureIndexes($this->timeout); |
||
103 | } |
||
104 | |||
105 | protected function processDocumentProxy(SchemaManager $sm, $document) |
||
106 | { |
||
107 | $classMetadata = $this->getMetadataFactory()->getMetadataFor($document); |
||
108 | |||
109 | if (!$classMetadata->isEmbeddedDocument && !$classMetadata->isMappedSuperclass && !$classMetadata->isQueryResultDocument) { |
||
110 | $this->getDocumentManager()->getProxyFactory()->generateProxyClasses(array($classMetadata)); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | protected function processProxy(SchemaManager $sm) |
||
115 | { |
||
116 | $classes = array_filter($this->getMetadataFactory()->getAllMetadata(), function (ClassMetadata $classMetadata) { |
||
117 | return !$classMetadata->isEmbeddedDocument && !$classMetadata->isMappedSuperclass && !$classMetadata->isQueryResultDocument; |
||
118 | }); |
||
119 | |||
120 | $this->getDocumentManager()->getProxyFactory()->generateProxyClasses($classes); |
||
121 | } |
||
122 | } |
||
123 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.