DoctrineMongoDBTypeGuesser::getMetadata()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Doctrine MongoDBBundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Saxulum\DoctrineMongodbOdmManagerRegistry\Form;
16
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use Doctrine\Common\Persistence\Mapping\MappingException;
19
use Symfony\Component\Form\FormTypeGuesserInterface;
20
use Symfony\Component\Form\Guess\Guess;
21
use Symfony\Component\Form\Guess\TypeGuess;
22
use Symfony\Component\Form\Guess\ValueGuess;
23
24
/**
25
 * Tries to guess form types according to ODM mappings
26
 *
27
 * @author Thibault Duplessis <[email protected]>
28
 */
29
class DoctrineMongoDBTypeGuesser implements FormTypeGuesserInterface
30
{
31
    protected $registry;
32
33
    private $cache = array();
34
35
    public function __construct(ManagerRegistry $registry)
36
    {
37
        $this->registry = $registry;
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function guessType($class, $property)
44
    {
45
        if (!$ret = $this->getMetadata($class)) {
46
            return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
47
        }
48
49
        list($metadata, $name) = $ret;
0 ignored issues
show
Unused Code introduced by
The assignment to $name is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
50
51
        if ($metadata->hasAssociation($property)) {
52
            $multiple = $metadata->isCollectionValuedAssociation($property);
53
            $mapping = $metadata->getFieldMapping($property);
54
55
            return new TypeGuess(
56
                'document',
57
                array(
58
                    'class' => $mapping['targetDocument'],
59
                    'multiple' => $multiple,
60
                    'expanded' => $multiple
61
                ),
62
                Guess::HIGH_CONFIDENCE
63
            );
64
        }
65
66
        $fieldMapping = $metadata->getFieldMapping($property);
67
        switch ($fieldMapping['type'])
68
        {
69
            case 'collection':
70
                return new TypeGuess(
71
                    'Collection',
72
                    array(),
73
                    Guess::MEDIUM_CONFIDENCE
74
                );
75
            case 'boolean':
76
                return new TypeGuess(
77
                    'checkbox',
78
                    array(),
79
                    Guess::HIGH_CONFIDENCE
80
                );
81
            case 'date':
82
            case 'timestamp':
83
                return new TypeGuess(
84
                    'datetime',
85
                    array(),
86
                    Guess::HIGH_CONFIDENCE
87
                );
88
            case 'float':
89
                return new TypeGuess(
90
                    'number',
91
                    array(),
92
                    Guess::MEDIUM_CONFIDENCE
93
                );
94
            case 'int':
95
                return new TypeGuess(
96
                    'integer',
97
                    array(),
98
                    Guess::MEDIUM_CONFIDENCE
99
                );
100
            case 'string':
101
                return new TypeGuess(
102
                    'text',
103
                    array(),
104
                    Guess::MEDIUM_CONFIDENCE
105
                );
106
        }
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function guessRequired($class, $property)
113
    {
114
        $ret = $this->getMetadata($class);
115
        if ($ret && $ret[0]->hasField($property)) {
116
            if (!$ret[0]->isNullable($property)) {
117
                return new ValueGuess(
118
                    true,
119
                    Guess::HIGH_CONFIDENCE
120
                );
121
            }
122
123
            return new ValueGuess(
124
                false,
125
                Guess::MEDIUM_CONFIDENCE
126
            );
127
        }
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function guessMaxLength($class, $property)
134
    {
135
        $ret = $this->getMetadata($class);
136
        if ($ret && $ret[0]->hasField($property) && !$ret[0]->hasAssociation($property)) {
137
            $mapping = $ret[0]->getFieldMapping($property);
138
139
            if (isset($mapping['length'])) {
140
                return new ValueGuess($mapping['length'], Guess::HIGH_CONFIDENCE);
141
            }
142
143
            if ('float' === $mapping['type']) {
144
                return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
145
            }
146
        }
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152
    public function guessMinLength($class, $property)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
    {
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    public function guessPattern($class, $property)
160
    {
161
        $ret = $this->getMetadata($class);
162
        if ($ret && $ret[0]->hasField($property) && !$ret[0]->hasAssociation($property)) {
163
            $mapping = $ret[0]->getFieldMapping($property);
164
165
            if ('float' === $mapping['type']) {
166
                return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
167
            }
168
        }
169
    }
170
171
    protected function getMetadata($class)
172
    {
173
        if (array_key_exists($class, $this->cache)) {
174
            return $this->cache[$class];
175
        }
176
177
        $this->cache[$class] = null;
178
        foreach ($this->registry->getManagers() as $name => $dm) {
179
            try {
180
                return $this->cache[$class] = array($dm->getClassMetadata($class), $name);
181
            } catch (MappingException $e) {
182
                // not an entity or mapped super class
183
            }
184
        }
185
    }
186
}
187