Completed
Pull Request — master (#119)
by Toby
02:45
created

ResourceObject   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 48.98 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 3
dl 24
loc 49
ccs 20
cts 24
cp 0.8333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttribute() 12 12 3
A jsonSerialize() 0 10 1
A validateField() 0 4 1
A setRelationship() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
use InvalidArgumentException;
15
use LogicException;
16
17
class ResourceObject extends ResourceIdentifier
18
{
19
    use LinksTrait, SelfLinkTrait;
20
21
    private $attributes = [];
22
    private $relationships = [];
23
24 6 View Code Duplication
    public function setAttribute($name, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26 6
        if (! $this->validateField($name)) {
27
            throw new InvalidArgumentException('Invalid attribute name');
28
        }
29
30 6
        if (isset($this->relationships[$name])) {
31
            throw new LogicException("Field $name already exists in relationships");
32
        }
33
34 6
        $this->attributes[$name] = $value;
35 6
    }
36
37 3 View Code Duplication
    public function setRelationship($name, Relationship $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39 3
        if (! $this->validateField($name)) {
40
            throw new InvalidArgumentException('Invalid relationship name');
41
        }
42
43 3
        if (isset($this->attributes[$name])) {
44
            throw new LogicException("Field $name already exists in attributes");
45
        }
46
47 3
        $this->relationships[$name] = $value;
48 3
    }
49
50 15
    public function jsonSerialize()
51
    {
52 15
        return array_filter(
53 15
            parent::jsonSerialize() + [
54 15
                'attributes' => $this->attributes,
55 15
                'relationships' => $this->relationships,
56 15
                'links' => $this->links
57 15
            ]
58 15
        );
59
    }
60
61 9
    private function validateField($name)
62
    {
63 9
        return ! in_array($name, ['id', 'type']);
64
    }
65
}
66