ReflectionClassMetadataLoader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 62
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A loadProperty() 0 11 2
A loadPropertyType() 0 21 4
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping\Loader;
13
14
use Ivory\Serializer\Type\Parser\TypeParserInterface;
15
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class ReflectionClassMetadataLoader extends AbstractReflectionClassMetadataLoader
21
{
22
    /**
23
     * @var PropertyInfoExtractorInterface|null
24
     */
25
    private $extractor;
26
27
    /**
28
     * @param PropertyInfoExtractorInterface|null $extractor
29
     * @param TypeParserInterface|null            $typeParser
30
     */
31 1660
    public function __construct(
32
        PropertyInfoExtractorInterface $extractor = null,
33
        TypeParserInterface $typeParser = null
34
    ) {
35 1660
        parent::__construct($typeParser);
36
37 1660
        $this->extractor = $extractor;
38 1660
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 1200
    protected function loadProperty(\ReflectionProperty $property)
44
    {
45 1200
        $result = [];
46 1200
        $type = $this->loadPropertyType($property);
47
48 1200
        if ($type !== null) {
49 1144
            $result['type'] = $type;
50
        }
51
52 1200
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type declared by the abstract method Ivory\Serializer\Mapping...ataLoader::loadProperty of type array|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
53
    }
54
55
    /**
56
     * @param \ReflectionProperty $property
57
     *
58
     * @return string|null
59
     */
60 1200
    private function loadPropertyType(\ReflectionProperty $property)
61
    {
62 1200
        if ($this->extractor === null) {
63 56
            return;
64
        }
65
66 1144
        $types = $this->extractor->getTypes($property->class, $property->name);
67
68 1144
        if (empty($types)) {
69 32
            return;
70
        }
71
72 1144
        $extractedType = current($types);
73 1144
        $type = $extractedType->getBuiltinType();
74
75 1144
        if ($type === 'object') {
76 268
            $type = $extractedType->getClassName();
77
        }
78
79 1144
        return $type;
80
    }
81
}
82