AnnotationDriver::getCascadeMode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Doctrine\ODM\CouchDB\Mapping\Driver;
4
5
use Doctrine\Common\Annotations\AnnotationReader,
6
    Doctrine\Common\Annotations\AnnotationRegistry,
7
    Doctrine\Common\Annotations\Reader,
8
    Doctrine\Common\Persistence\Mapping\ClassMetadata,
9
    Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver,
10
    Doctrine\ODM\CouchDB\Mapping\Annotations as ODM,
11
    Doctrine\ODM\CouchDB\Mapping\MappingException;
12
13
/**
14
 * The AnnotationDriver reads the mapping metadata from docblock annotations.
15
 *
16
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
17
 * @link        www.doctrine-project.org
18
 * @since       1.0
19
 * @author      Jonathan H. Wage <[email protected]>
20
 * @author      Roman Borschel <[email protected]>
21
 */
22
class AnnotationDriver extends AbstractAnnotationDriver
23
{
24
    protected $entityAnnotationClasses = array(
25
        'Doctrine\ODM\CouchDB\Mapping\Annotations\Document' => true,
26
        'Doctrine\ODM\CouchDB\Mapping\Annotations\EmbeddedDocument' => true,
27
        'Doctrine\ODM\CouchDB\Mapping\Annotations\MappedSuperclass' => true,
28
    );
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function loadMetadataForClass($className, ClassMetadata $class)
34
    {
35
        $reflClass = $class->getReflectionClass();
36
37
        $isValidDocument  = false;
38
        $classAnnotations = $this->reader->getClassAnnotations($reflClass);
39
40
        foreach ($classAnnotations AS $classAnnotation) {
41
            if ($classAnnotation instanceof ODM\Document) {
42
                if ($classAnnotation->indexed) {
43
                    $class->indexed = true;
0 ignored issues
show
Bug introduced by
Accessing indexed 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...
44
                }
45
                $class->setCustomRepositoryClass($classAnnotation->repositoryClass);
46
                $isValidDocument = true;
47
            } elseif ($classAnnotation instanceof ODM\EmbeddedDocument) {
48
                $class->isEmbeddedDocument = true;
0 ignored issues
show
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...
49
                $isValidDocument = true;
50
            } else if ($classAnnotation instanceof ODM\MappedSuperclass) {
51
                $class->isMappedSuperclass = true;
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...
52
                $isValidDocument = true;
53
            } else if ($classAnnotation instanceof ODM\Index) {
54
                $class->indexed = true;
0 ignored issues
show
Bug introduced by
Accessing indexed 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...
55
            } else if ($classAnnotation instanceof ODM\InheritanceRoot) {
56
                $class->markInheritanceRoot();
57
            }
58
        }
59
60
        if ( ! $isValidDocument) {
61
            throw MappingException::classIsNotAValidDocument($className);
62
        }
63
64
        foreach ($reflClass->getProperties() as $property) {
65
            if ($class->isInheritedAssociation($property->name) || $class->isInheritedField($property->name)) {
66
                continue;
67
            }
68
69
            $mapping = array();
70
            $mapping['fieldName'] = $property->name;
71
72
            if ($this->reader->getPropertyAnnotation($property, 'Doctrine\ODM\CouchDB\Mapping\Annotations\Index')) {
73
                $mapping['indexed'] = true;
74
            }
75
76
            foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) {
77
                if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Field) {
78
                    if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Version) {
79
                        $mapping['isVersionField'] = true;
80
                    }
81
82
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
83
                    unset($mapping['value']);
84
                    $class->mapField($mapping);
85
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceOne) {
86
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
87
                    $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
88
                    unset($mapping['value']);
89
                    $class->mapManyToOne($mapping);
90
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\ReferenceMany) {
91
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
92
                    $mapping['cascade'] = $this->getCascadeMode($fieldAnnot->cascade);
93
                    unset($mapping['value']);
94
                    $class->mapManyToMany($mapping);
95
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\Attachments) {
96
                    $class->mapAttachments($mapping['fieldName']);
97
                } else if ($fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedOne || $fieldAnnot instanceof \Doctrine\ODM\CouchDB\Mapping\Annotations\EmbedMany) {
98
                    $mapping = array_merge($mapping, (array) $fieldAnnot);
99
                    unset($mapping['value']);
100
                    $class->mapEmbedded($mapping);
101
                }
102
            }
103
        }
104
    }
105
106
    /**
107
     * Gathers a list of cascade options found in the given cascade element.
108
     *
109
     * @param array $cascadeList cascade list
110
     * @return integer a bitmask of cascade options.
111
     * @throws MappingException
112
     */
113 View Code Duplication
    private function getCascadeMode(array $cascadeList)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $cascade = 0;
116
        foreach ($cascadeList as $cascadeMode) {
117
            $constantName = 'Doctrine\ODM\CouchDB\Mapping\ClassMetadata::CASCADE_' . strtoupper($cascadeMode);
118
            if (!defined($constantName)) {
119
                throw new MappingException("Cascade mode '$cascadeMode' not supported.");
120
            }
121
            $cascade |= constant($constantName);
122
        }
123
        return $cascade;
124
    }
125
}
126