Completed
Pull Request — master (#1263)
by Andreas
10:33
created

ClassMetadata   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 85.06%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 20
c 2
b 0
f 1
lcom 2
cbo 2
dl 0
loc 168
ccs 74
cts 87
cp 0.8506
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A mapField() 0 8 1
F __sleep() 0 78 13
A __wakeup() 0 16 4
A newInstance() 0 4 1
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\ODM\MongoDB\Mapping;
21
22
use Doctrine\Instantiator\Instantiator;
23
use Doctrine\ODM\MongoDB\LockException;
24
25
/**
26
 * A <tt>ClassMetadata</tt> instance holds all the object-document mapping metadata
27
 * of a document and it's references.
28
 *
29
 * Once populated, ClassMetadata instances are usually cached in a serialized form.
30
 *
31
 * <b>IMPORTANT NOTE:</b>
32
 *
33
 * The fields of this class are only public for 2 reasons:
34
 * 1) To allow fast READ access.
35
 * 2) To drastically reduce the size of a serialized instance (private/protected members
36
 *    get the whole class name, namespace inclusive, prepended to every property in
37
 *    the serialized representation).
38
 *
39
 * @since       1.0
40
 * @author      Jonathan H. Wage <[email protected]>
41
 * @author      Roman Borschel <[email protected]>
42
 */
43
class ClassMetadata extends ClassMetadataInfo
44
{
45
    /**
46
     * The ReflectionProperty instances of the mapped class.
47
     *
48
     * @var \ReflectionProperty[]
49
     */
50
    public $reflFields = array();
51
52
    /**
53
     * @var \Doctrine\Instantiator\InstantiatorInterface|null
54
     */
55
    private $instantiator;
56
57
    /**
58
     * Initializes a new ClassMetadata instance that will hold the object-document mapping
59
     * metadata of the class with the given name.
60
     *
61
     * @param string $documentName The name of the document class the new instance is used for.
62
     */
63 792
    public function __construct($documentName)
64
    {
65 792
        parent::__construct($documentName);
66 792
        $this->reflClass = new \ReflectionClass($documentName);
67 792
        $this->namespace = $this->reflClass->getNamespaceName();
68 792
        $this->setCollection($this->reflClass->getShortName());
69 792
        $this->instantiator = new Instantiator();
70 792
    }
71
72
    /**
73
     * Map a field.
74
     *
75
     * @param array $mapping The mapping information.
76
     * @return void
77
     */
78 782
    public function mapField(array $mapping)
79
    {
80 782
        $mapping = parent::mapField($mapping);
81
82 781
        $reflProp = $this->reflClass->getProperty($mapping['fieldName']);
83 780
        $reflProp->setAccessible(true);
84 780
        $this->reflFields[$mapping['fieldName']] = $reflProp;
85 780
    }
86
87
    /**
88
     * Determines which fields get serialized.
89
     *
90
     * It is only serialized what is necessary for best unserialization performance.
91
     * That means any metadata properties that are not set or empty or simply have
92
     * their default value are NOT serialized.
93
     *
94
     * Parts that are also NOT serialized because they can not be properly unserialized:
95
     *      - reflClass (ReflectionClass)
96
     *      - reflFields (ReflectionProperty array)
97
     *
98
     * @return array The names of all the fields that should be serialized.
99
     */
100 3
    public function __sleep()
101
    {
102
        // This metadata is always serialized/cached.
103
        $serialized = array(
104 3
            'fieldMappings',
105 3
            'associationMappings',
106 3
            'identifier',
107 3
            'name',
108 3
            'namespace', // TODO: REMOVE
109 3
            'db',
110 3
            'collection',
111 3
            'rootDocumentName',
112 3
            'generatorType',
113 3
            'generatorOptions',
114 3
            'idGenerator',
115 3
            'indexes',
116 3
        );
117
118
        // The rest of the metadata is only serialized if necessary.
119 3
        if ($this->changeTrackingPolicy != self::CHANGETRACKING_DEFERRED_IMPLICIT) {
120
            $serialized[] = 'changeTrackingPolicy';
121
        }
122
123 3
        if ($this->customRepositoryClassName) {
124 1
            $serialized[] = 'customRepositoryClassName';
125 1
        }
126
127 3
        if ($this->inheritanceType != self::INHERITANCE_TYPE_NONE) {
128 1
            $serialized[] = 'inheritanceType';
129 1
            $serialized[] = 'discriminatorField';
130 1
            $serialized[] = 'discriminatorValue';
131 1
            $serialized[] = 'discriminatorMap';
132 1
            $serialized[] = 'defaultDiscriminatorValue';
133 1
            $serialized[] = 'parentClasses';
134 1
            $serialized[] = 'subClasses';
135 1
        }
136
137 3
        if ($this->isMappedSuperclass) {
138
            $serialized[] = 'isMappedSuperclass';
139
        }
140
141 3
        if ($this->isEmbeddedDocument) {
142
            $serialized[] = 'isEmbeddedDocument';
143
        }
144
145 3
        if ($this->isAggregationResultDocument) {
146
            $serialized[] = 'isAggregationResultDocument';
147
        }
148
149 3
        if ($this->isVersioned) {
150
            $serialized[] = 'isVersioned';
151
            $serialized[] = 'versionField';
152
        }
153
154 3
        if ($this->lifecycleCallbacks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->lifecycleCallbacks of type array 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...
155
            $serialized[] = 'lifecycleCallbacks';
156
        }
157
158 3
        if ($this->file) {
159 1
            $serialized[] = 'file';
160 1
        }
161
162 3
        if ($this->slaveOkay) {
163 1
            $serialized[] = 'slaveOkay';
164 1
        }
165
166 3
        if ($this->distance) {
167 1
            $serialized[] = 'distance';
168 1
        }
169
170 3
        if ($this->collectionCapped) {
171 1
            $serialized[] = 'collectionCapped';
172 1
            $serialized[] = 'collectionSize';
173 1
            $serialized[] = 'collectionMax';
174 1
        }
175
176 3
        return $serialized;
177
    }
178
179
    /**
180
     * Restores some state that can not be serialized/unserialized.
181
     *
182
     * @return void
183
     */
184 3
    public function __wakeup()
185
    {
186
        // Restore ReflectionClass and properties
187 3
        $this->reflClass = new \ReflectionClass($this->name);
188 3
        $this->instantiator = $this->instantiator ?: new Instantiator();
189
190 3
        foreach ($this->fieldMappings as $field => $mapping) {
191 3
            if (isset($mapping['declared'])) {
192 1
                $reflField = new \ReflectionProperty($mapping['declared'], $field);
193 1
            } else {
194 3
                $reflField = $this->reflClass->getProperty($field);
195
            }
196 3
            $reflField->setAccessible(true);
197 3
            $this->reflFields[$field] = $reflField;
198 3
        }
199 3
    }
200
201
    /**
202
     * Creates a new instance of the mapped class, without invoking the constructor.
203
     *
204
     * @return object
205
     */
206 371
    public function newInstance()
207
    {
208 371
        return $this->instantiator->instantiate($this->name);
209
    }
210
}
211