ClassMetadataFactory::wakeupReflection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Doctrine\ODM\CouchDB\Mapping;
4
5
use Doctrine\ODM\CouchDB\DocumentManager,
6
    Doctrine\ODM\CouchDB\CouchDBException,
7
    Doctrine\ODM\CouchDB\Mapping\ClassMetadata,
8
    Doctrine\Common\Persistence\Mapping\Driver\MappingDriver,
9
    Doctrine\Common\Persistence\Mapping\ClassMetadata as ClassMetadataInterface,
10
    Doctrine\Common\Persistence\Mapping\ReflectionService,
11
    Doctrine\Common\Persistence\Mapping\RuntimeReflectionService,
12
    Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory;
13
14
/**
15
 * The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
16
 * metadata mapping information of a class which describes how a class should be mapped
17
 * to a document database.
18
19
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
20
 * @link        www.doctrine-project.com
21
 * @since       1.0
22
 * @author      Benjamin Eberlei <[email protected]>
23
 * @author      Lukas Kahwe Smith <[email protected]>
24
 */
25
class ClassMetadataFactory extends AbstractClassMetadataFactory
26
{
27
    /**
28
     * @var DocumentManager
29
     */
30
    private $dm;
31
32
    /**
33
     *  The used metadata driver.
34
     *
35
     * @var MappingDriver
36
     */
37
    private $driver;
38
39
    /**
40
     * Creates a new factory instance that uses the given DocumentManager instance.
41
     *
42
     * @param DocumentManager $dm The DocumentManager instance
43
     * @throws \RuntimeException
44
     */
45
    public function __construct(DocumentManager $dm)
46
    {
47
        $this->dm = $dm;
48
        $config = $this->dm->getConfiguration();
49
        $this->setCacheDriver($config->getMetadataCacheImpl());
50
        $this->driver = $config->getMetadataDriverImpl();
51
        if (!$this->driver) {
52
            throw new \RuntimeException('No metadata driver was configured.');
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents)
60
    {
61
        /** @var $parent ClassMetaData */
62
        if ($parent) {
63
            $this->addAssociationsMapping($class, $parent);
64
            $this->addFieldMapping($class, $parent);
65
            $this->addIndexes($class, $parent);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Couc...\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
66
            $parent->deriveChildMetadata($class);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Couc...\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
67
            $class->setParentClasses($nonSuperclassParents);
68
        }
69
70
        if ($this->getDriver()) {
71
            $this->getDriver()->loadMetadataForClass($class->getName(), $class);
72
        }
73
74
        $this->validateMapping($class);
75
    }
76
77
    /**
78
     * Check for any possible shortcomings in the class:
79
     *
80
     * The class must have an identifier field unless it's an embedded document or mapped superclass.
81
     */
82
    private function validateMapping(ClassMetadataInterface $class)
83
    {
84
        if (!$class->identifier && !$class->isEmbeddedDocument && !$class->isMappedSuperclass) {
0 ignored issues
show
Bug introduced by
Accessing identifier on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing isEmbeddedDocument on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing isMappedSuperclass on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
85
            throw new MappingException("An identifier (@Id) field is required in {$class->getName()}.");
86
        }
87
    }
88
89
    private function addFieldMapping(ClassMetadataInterface $class, ClassMetadataInterface $parent)
90
    {
91
        foreach ($parent->reflFields as $name => $field) {
0 ignored issues
show
Bug introduced by
Accessing reflFields on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
92
            $class->reflFields[$name] = $field;
0 ignored issues
show
Bug introduced by
Accessing reflFields on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
93
        }
94
95
        foreach ($parent->fieldMappings as $name => $field) {
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
96
            $class->fieldMappings[$name] = $field;
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
97
        }
98
99
        foreach ($parent->jsonNames as $name => $field) {
0 ignored issues
show
Bug introduced by
Accessing jsonNames on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
100
            $class->jsonNames[$name] = $field;
0 ignored issues
show
Bug introduced by
Accessing jsonNames on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
101
        }
102
103
        if ($parent->identifier) {
0 ignored issues
show
Bug introduced by
Accessing identifier on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
104
            $class->setIdentifier($parent->identifier);
0 ignored issues
show
Bug introduced by
Accessing identifier on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
105
        }
106
    }
107
108
    private function addIndexes(ClassMetadata $class, ClassMetadata $parent)
109
    {
110
        $class->indexes = $parent->indexes;
111
    }
112
113
    /**
114
     *
115
     * @param ClassMetadataInterface $class
116
     * @param ClassMetadataInterface $parent
117
     */
118
    private function addAssociationsMapping(ClassMetadataInterface $class, ClassMetadataInterface $parent)
119
    {
120
        foreach ($parent->associationsMappings as $name => $field) {
0 ignored issues
show
Bug introduced by
Accessing associationsMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
121
            $class->associationsMappings[$name] = $field;
0 ignored issues
show
Bug introduced by
Accessing associationsMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
122
        }
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    protected function getFqcnFromAlias($namespaceAlias, $simpleClassName)
129
    {
130
        return $this->dm->getConfiguration()->getDocumentNamespace($namespaceAlias) . '\\' . $simpleClassName;
131
    }
132
133
    /**
134
     * Forces the factory to load the metadata of all classes known to the underlying
135
     * mapping driver.
136
     *
137
     * @return array The ClassMetadata instances of all mapped classes.
138
     */
139
    public function getAllMetadata()
140
    {
141
        $metadata = array();
142
        foreach ($this->driver->getAllClassNames() as $className) {
143
            $metadata[] = $this->getMetadataFor($className);
144
        }
145
146
        return $metadata;
147
    }
148
149
    /**
150
     * Gets the class metadata descriptor for a class.
151
     *
152
     * @param string $className The name of the class.
153
     * @return ClassMetadata
154
     * @throws MappingException
155
     */
156
    public function getMetadataFor($className)
157
    {
158
        $metadata = parent::getMetadataFor($className);
159
160
        if ($metadata) {
161
            return $metadata;
162
        }
163
164
        throw MappingException::classNotMapped($className);
0 ignored issues
show
Unused Code introduced by
The call to MappingException::classNotMapped() has too many arguments starting with $className.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
165
    }
166
167
    /**
168
     * Loads the metadata of the class in question and all it's ancestors whose metadata
169
     * is still not loaded.
170
     *
171
     * @param string $className The name of the class for which the metadata should get loaded.
172
     * @return array
173
     * @throws MappingException
174
     */
175
    protected function loadMetadata($className)
176
    {
177
        if (class_exists($className)) {
178
            return parent::loadMetadata($className);
179
        }
180
        throw MappingException::classNotFound($className);
181
    }
182
183
    /**
184
     * Creates a new ClassMetadata instance for the given class name.
185
     *
186
     * @param string $className
187
     * @return ClassMetadata
188
     */
189
    protected function newClassMetadataInstance($className)
190
    {
191
        return new ClassMetadata($className);
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    protected function getDriver()
198
    {
199
        return $this->driver;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    protected function initialize()
206
    {
207
        $this->initialized = true;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService)
214
    {
215
        $class->initializeReflection($reflService);
216
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221
    protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService)
222
    {
223
        $class->wakeupReflection($reflService);
224
    }
225
226
    /**
227
     * {@inheritDoc}
228
     */
229
    protected function isEntity(ClassMetadataInterface $class)
230
    {
231
        return isset($class->isMappedSuperclass) && $class->isMappedSuperclass === false;
0 ignored issues
show
Bug introduced by
Accessing isMappedSuperclass on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
232
    }
233
}
234