Completed
Push — standalone ( 57a4d8...49bbc6 )
by Philip
10:02
created

DoctrineDriver::loadMetadataForClass()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7.0753

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 41
ccs 23
cts 26
cp 0.8846
rs 6.7272
c 2
b 0
f 0
cc 7
eloc 27
nc 13
nop 1
crap 7.0753
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Mapping\MappingException;
7
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
8
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
9
use Metadata\Driver\DriverInterface;
10
11
class DoctrineDriver implements DriverInterface
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $entityManager;
17
18 39
    function __construct(EntityManagerInterface $entityManager)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20 39
        $this->entityManager = $entityManager;
21 39
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 39
    public function loadMetadataForClass(\ReflectionClass $class)
27
    {
28 39
        $ddrRestClassMetadata = new ClassMetadata($class->getName());
29
        try {
30 39
            $doctrineClassMetadata = $this->entityManager->getClassMetadata($class->getName());
31 1
        } catch (MappingException $e) {
32 1
            return $ddrRestClassMetadata;
33
        }
34
35 39
        foreach ($doctrineClassMetadata->embeddedClasses as $fieldName => $embeddedClass) {
0 ignored issues
show
Bug introduced by
Accessing embeddedClasses 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...
36
            $ddrRestPropertyMetadata = new PropertyMetadata($doctrineClassMetadata->getName(), $fieldName);
37
            $ddrRestPropertyMetadata->setType($embeddedClass['class']);
38
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
39
        }
40
41 19
        foreach ($doctrineClassMetadata->fieldMappings as $fieldMapping) {
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...
42 19
            if (!array_key_exists('declared', $fieldMapping) && !array_key_exists('declaredField', $fieldMapping)) {
43 19
                $ddrRestPropertyMetadata = new PropertyMetadata(
44 19
                    $doctrineClassMetadata->getName(),
45 19
                    $fieldMapping['fieldName']
46
                );
47 19
                $ddrRestPropertyMetadata->setType($fieldMapping['type']);
48 19
                $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
49
            }
50
        }
51
52 19
        foreach ($doctrineClassMetadata->associationMappings as $associationMapping) {
0 ignored issues
show
Bug introduced by
Accessing associationMappings 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...
53 13
            $ddrRestPropertyMetadata = new PropertyMetadata(
54 13
                $doctrineClassMetadata->getName(),
55 13
                $associationMapping['fieldName']
56
            );
57 13
            $ddrRestPropertyMetadata->setAssociation(true);
58 13
            $ddrRestPropertyMetadata->setTargetClass($associationMapping['targetEntity']);
59 13
            $ddrRestPropertyMetadata->setCollection(
60 13
                $doctrineClassMetadata->isCollectionValuedAssociation($associationMapping['fieldName'])
61
            );
62 13
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
63
        }
64
65 19
        return $ddrRestClassMetadata;
66
    }
67
}
68