Completed
Push — standalone ( 64a47f...7c3a2c )
by Philip
06:01 queued 28s
created

DoctrineDriver::loadMetadataForClass()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
ccs 20
cts 20
cp 1
rs 8.5806
cc 4
eloc 20
nc 6
nop 1
crap 4
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata\Driver;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata;
7
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata;
8
use Metadata\Driver\DriverInterface;
9
10
class DoctrineDriver implements DriverInterface
11
{
12
    /**
13
     * @var EntityManagerInterface
14
     */
15
    private $entityManager;
16
17 14
    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...
18
    {
19 14
        $this->entityManager = $entityManager;
20 14
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 14
    public function loadMetadataForClass(\ReflectionClass $class)
26
    {
27 14
        $doctrineClassMetadata = $this->entityManager->getClassMetadata($class->getName());
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
28 14
        $ddrRestClassMetadata = new ClassMetadata($class->getName());
1 ignored issue
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
29
30 14
        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...
31 14
            if (!array_key_exists('declared', $fieldMapping)) {
32 14
                $ddrRestPropertyMetadata = new PropertyMetadata(
33 14
                    $doctrineClassMetadata->getName(),
34 14
                    $fieldMapping['fieldName']
35
                );
36 14
                $ddrRestPropertyMetadata->setType($fieldMapping['type']);
37 14
                $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
38
            }
39
        }
40
41 14
        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...
42 2
            $ddrRestPropertyMetadata = new PropertyMetadata(
43 2
                $doctrineClassMetadata->getName(),
44 2
                $associationMapping['fieldName']
45
            );
46 2
            $ddrRestPropertyMetadata->setAssociation(true);
47 2
            $ddrRestPropertyMetadata->setTargetClass($associationMapping['targetEntity']);
48 2
            $ddrRestPropertyMetadata->setCollection(
49 2
                $doctrineClassMetadata->isCollectionValuedAssociation($associationMapping['fieldName'])
50
            );
51 2
            $ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata);
52
        }
53
54 14
        return $ddrRestClassMetadata;
55
    }
56
}
57