GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3684b5...a74bc8 )
by Sergey
03:58
created

ResourceMetadata   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 167
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getAttributes() 0 4 1
A addAttribute() 0 6 1
A getRelationships() 0 4 1
A addRelationship() 0 6 1
A getDiscriminatorField() 0 4 1
A setDiscriminatorField() 0 6 1
A setDiscriminatorMap() 0 6 1
A getDiscriminatorClass() 0 11 2
A mergeMetadata() 0 16 3
1
<?php
2
/*
3
 * This file is part of the reva2/jsonapi.
4
 *
5
 * (c) Sergey Revenko <[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
12
namespace Reva2\JsonApi\Decoders\Mapping;
13
14
use Reva2\JsonApi\Contracts\Decoders\Mapping\AttributeMetadataInterface;
15
use Reva2\JsonApi\Contracts\Decoders\Mapping\RelationshipMetadataInterface;
16
use Reva2\JsonApi\Contracts\Decoders\Mapping\ResourceMetadataInterface;
17
18
/**
19
 * JSON API resource metadata
20
 *
21
 * @package Reva2\JsonApi\Decoders\Mapping
22
 * @author Sergey Revenko <[email protected]>
23
 */
24
class ResourceMetadata extends GenericMetadata implements ResourceMetadataInterface
25
{
26
    /**
27
     * @var string
28
     * @internal
29
     */
30
    public $name;
31
32
    /**
33
     * @var AttributeMetadataInterface[]
34
     * @internal
35
     */
36
    public $attributes = [];
37
38
    /**
39
     * @var RelationshipMetadataInterface[]
40
     * @internal
41
     */
42
    public $relationships = [];
43
44
    /***
45
     * @var string|null
46
     * @internal
47
     */
48
    public $discField;
49
50
    /**
51
     * @var array
52
     * @internal
53
     */
54
    public $discMap;
55
56
    /**
57
     * Constructor
58
     *
59
     * @param string $name
60
     * @param $className
61
     */
62
    public function __construct($name, $className)
63
    {
64
        parent::__construct($className);
65
66
        $this->name = $name;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getName()
73
    {
74
        return $this->name;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function getAttributes()
81
    {
82
        return $this->attributes;
83
    }
84
85
    /**
86
     * Add resource attribute metadata
87
     *
88
     * @param AttributeMetadataInterface $attribute
89
     * @return $this
90
     */
91
    public function addAttribute(AttributeMetadataInterface $attribute)
92
    {
93
        $this->attributes[$attribute->getPropertyName()] = $attribute;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function getRelationships()
102
    {
103
        return $this->relationships;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->relationships; (Reva2\JsonApi\Contracts\...shipMetadataInterface[]) is incompatible with the return type declared by the interface Reva2\JsonApi\Contracts\...rface::getRelationships of type Reva2\JsonApi\Contracts\...onshipMetadataInterface.

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...
104
    }
105
106
    /**
107
     * Add resource relationship metadata
108
     *
109
     * @param RelationshipMetadataInterface $relationship
110
     * @return $this
111
     */
112
    public function addRelationship(RelationshipMetadataInterface $relationship)
113
    {
114
        $this->relationships[$relationship->getPropertyName()] = $relationship;
115
116
        return $this;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122
    public function getDiscriminatorField()
123
    {
124
        return $this->discField;
125
    }
126
127
    /**
128
     * Sets discriminator field
129
     *
130
     * @param string|null $field
131
     * @return $this
132
     */
133
    public function setDiscriminatorField($field = null)
134
    {
135
        $this->discField = $field;
136
137
        return $this;
138
    }
139
140
    /**
141
     * Sets discriminator map
142
     *
143
     * @param array $map
144
     * @return $this
145
     */
146
    public function setDiscriminatorMap(array $map)
147
    {
148
        $this->discMap = $map;
149
150
        return $this;
151
    }
152
153
    /**
154
     * @inheritdoc
155
     */
156
    public function getDiscriminatorClass($value)
157
    {
158
        if (!array_key_exists($value, $this->discMap)) {
159
            throw new \InvalidArgumentException(sprintf(
160
                "Discriminator class for value '%s' not specified",
161
                $value
162
            ));
163
        }
164
165
        return $this->discMap[$value];
166
    }
167
168
    /**
169
     * Merge resource metadata
170
     *
171
     * @param mixed $metadata
172
     * @return $this
173
     */
174
    public function mergeMetadata($metadata)
175
    {
176
        if (null === $metadata) {
177
            return $this;
178
        } elseif (!$metadata instanceof ResourceMetadataInterface) {
179
            throw new \InvalidArgumentException(sprintf(
180
                "Couldn't merge metadata from %s instance",
181
                get_class($metadata)
182
            ));
183
        }
184
185
        $this->attributes = array_merge($this->attributes, $metadata->getAttributes());
186
        $this->relationships = array_merge($this->relationships, $metadata->getRelationships());
187
188
        return $this;
189
    }
190
}