AnnotationDriver::loadMetadataForClass()   B
last analyzed

Complexity

Conditions 10
Paths 80

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 80
nop 1
dl 0
loc 33
rs 7.6666
c 0
b 0
f 0

How to fix   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
namespace Novaway\Component\OpenGraph\Metadata\Driver;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Metadata\Driver\DriverInterface;
7
use Novaway\Component\OpenGraph\Annotation\GraphNode;
8
use Novaway\Component\OpenGraph\Annotation\NamespaceNode;
9
use Novaway\Component\OpenGraph\Metadata\ClassMetadata;
10
use Novaway\Component\OpenGraph\Metadata\MetadataValue;
11
use Novaway\Component\OpenGraph\Metadata\MethodMetadata;
12
use Novaway\Component\OpenGraph\Metadata\PropertyMetadata;
13
14
class AnnotationDriver implements DriverInterface
15
{
16
    /** @var Reader */
17
    private $reader;
18
19
20
    /**
21
     * Constructor
22
     *
23
     * @param Reader $reader
24
     */
25
    public function __construct(Reader $reader)
26
    {
27
        $this->reader = $reader;
28
    }
29
30
    /**
31
     * Load metadata class
32
     *
33
     * @param \ReflectionClass $class
34
     * @return ClassMetadata
35
     */
36
    public function loadMetadataForClass(\ReflectionClass $class)
37
    {
38
        $classMetadata = new ClassMetadata($class->name);
39
        $classMetadata->fileResources[] = $class->getFileName();
40
41
        foreach ($this->reader->getClassAnnotations($class) as $annotation) {
42
            if ($annotation instanceof NamespaceNode) {
43
                $classMetadata->addGraphNamespace($annotation);
44
            }
45
46
            if ($annotation instanceof GraphNode) {
47
                $classMetadata->addGraphMetadata($annotation, new MetadataValue($annotation->value));
48
            }
49
        }
50
51
        foreach ($class->getProperties() as $property) {
52
            foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
53
                if ($annotation instanceof GraphNode) {
54
                    $classMetadata->addGraphMetadata($annotation, new PropertyMetadata($class->name, $property->name));
55
                }
56
            }
57
        }
58
59
        foreach ($class->getMethods() as $method) {
60
            foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
61
                if ($annotation instanceof GraphNode) {
62
                    $classMetadata->addGraphMetadata($annotation, new MethodMetadata($class->name, $method->name));
63
                }
64
            }
65
        }
66
67
        return $classMetadata;
68
    }
69
}
70