Completed
Push — 1.0.x ( bcb4f8...f4d4ca )
by Andreas
9s
created

ClassMetadata::__sleep()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 74
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 13.0352

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 46
cts 57
cp 0.807
rs 2.2446
c 0
b 0
f 0
cc 12
eloc 46
nc 2048
nop 0
crap 13.0352

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 799
    public function __construct($documentName)
64
    {
65 799
        parent::__construct($documentName);
66 799
        $this->reflClass = new \ReflectionClass($documentName);
67 799
        $this->namespace = $this->reflClass->getNamespaceName();
68 799
        $this->setCollection($this->reflClass->getShortName());
69 799
        $this->instantiator = new Instantiator();
70 799
    }
71
72
    /**
73
     * Map a field.
74
     *
75
     * @param array $mapping The mapping information.
76
     * @return void
77
     */
78 789
    public function mapField(array $mapping)
79
    {
80 789
        $mapping = parent::mapField($mapping);
81
82 788
        $reflProp = $this->reflClass->getProperty($mapping['fieldName']);
83 787
        $reflProp->setAccessible(true);
84 787
        $this->reflFields[$mapping['fieldName']] = $reflProp;
85 787
    }
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->isVersioned) {
146
            $serialized[] = 'isVersioned';
147
            $serialized[] = 'versionField';
148
        }
149
150 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...
151
            $serialized[] = 'lifecycleCallbacks';
152
        }
153
154 3
        if ($this->file) {
155 1
            $serialized[] = 'file';
156 1
        }
157
158 3
        if ($this->slaveOkay) {
159 1
            $serialized[] = 'slaveOkay';
160 1
        }
161
162 3
        if ($this->distance) {
163 1
            $serialized[] = 'distance';
164 1
        }
165
166 3
        if ($this->collectionCapped) {
167 1
            $serialized[] = 'collectionCapped';
168 1
            $serialized[] = 'collectionSize';
169 1
            $serialized[] = 'collectionMax';
170 1
        }
171
172 3
        return $serialized;
173
    }
174
175
    /**
176
     * Restores some state that can not be serialized/unserialized.
177
     *
178
     * @return void
179
     */
180 3
    public function __wakeup()
181
    {
182
        // Restore ReflectionClass and properties
183 3
        $this->reflClass = new \ReflectionClass($this->name);
184 3
        $this->instantiator = $this->instantiator ?: new Instantiator();
185
186 3
        foreach ($this->fieldMappings as $field => $mapping) {
187 3
            if (isset($mapping['declared'])) {
188 1
                $reflField = new \ReflectionProperty($mapping['declared'], $field);
189 1
            } else {
190 3
                $reflField = $this->reflClass->getProperty($field);
191
            }
192 3
            $reflField->setAccessible(true);
193 3
            $this->reflFields[$field] = $reflField;
194 3
        }
195 3
    }
196
197
    /**
198
     * Creates a new instance of the mapped class, without invoking the constructor.
199
     *
200
     * @return object
201
     */
202 371
    public function newInstance()
203
    {
204 371
        return $this->instantiator->instantiate($this->name);
205
    }
206
}
207