AttributeMetadata::getAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of AppBundle the package.
4
 *
5
 * (c) Ruslan Muriev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace RonteLtd\JsonApiBundle\Serializer\Mapping;
12
13
use RonteLtd\JsonApiBundle\Annotation\Attribute;
14
use RonteLtd\JsonApiBundle\Annotation\Relationship;
15
16
/**
17
 * Class AttributeMetadata
18
 *
19
 * @package RonteLtd\Bundle\Serializer\Mapping
20
 * @author Ruslan Muriev <[email protected]>
21
 */
22
class AttributeMetadata implements AttributeMetadataInterface
23
{
24
    /**
25
     * @var string
26
     *
27
     * @internal This property is public in order to reduce the size of the
28
     *           class' serialized representation. Do not access it. Use
29
     *           {@link getName()} instead.
30
     */
31
    public $name;
32
33
    /**
34
     * @var Attribute
35
     */
36
    public $attribute;
37
38
    /**
39
     * @var Relationship
40
     */
41
    public $relationship;
42
43
    /**
44
     * Constructs a metadata for the given attribute.
45
     *
46
     * @param string $name
47
     */
48
    public function __construct($name)
49
    {
50
        $this->name = $name;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getName()
57
    {
58
        return $this->name;
59
    }
60
61
    /**
62
     * @return Attribute
63
     */
64
    public function getAttribute()
65
    {
66
        return $this->attribute;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->attribute; (RonteLtd\JsonApiBundle\Annotation\Attribute) is incompatible with the return type declared by the interface RonteLtd\JsonApiBundle\S...Interface::getAttribute of type array.

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...
67
    }
68
69
    /**
70
     * @param Attribute $attribute
71
     */
72
    public function setAttribute($attribute)
73
    {
74
        $this->attribute = $attribute;
75
    }
76
77
    /**
78
     * @return Relationship
79
     */
80
    public function getRelationship()
81
    {
82
        return $this->relationship;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->relationship; (RonteLtd\JsonApiBundle\Annotation\Relationship) is incompatible with the return type declared by the interface RonteLtd\JsonApiBundle\S...erface::getRelationship of type array.

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...
83
    }
84
85
    /**
86
     * @param Relationship $relationship
87
     */
88
    public function setRelationship($relationship)
89
    {
90
        $this->relationship = $relationship;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function merge(AttributeMetadataInterface $attributeMetadata)
97
    {
98
        // Overwrite only if not defined
99
        if (null === $this->attribute) {
100
            $this->attribute = $attributeMetadata->getAttribute();
0 ignored issues
show
Documentation Bug introduced by
It seems like $attributeMetadata->getAttribute() of type array is incompatible with the declared type object<RonteLtd\JsonApiB...e\Annotation\Attribute> of property $attribute.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
101
        }
102
103
        // Overwrite only if not defined
104
        if (null === $this->relationship) {
105
            $this->relationship = $attributeMetadata->getRelationship();
0 ignored issues
show
Documentation Bug introduced by
It seems like $attributeMetadata->getRelationship() of type array is incompatible with the declared type object<RonteLtd\JsonApiB...nnotation\Relationship> of property $relationship.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
        }
107
    }
108
109
    /**
110
     * Returns the names of the properties that should be serialized.
111
     *
112
     * @return string[]
113
     */
114
    public function __sleep()
115
    {
116
        return array('name', 'attribute', 'relationship');
117
    }
118
}