AbstractSerializer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 6.59 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 6
loc 91
ccs 23
cts 26
cp 0.8846
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A getId() 0 4 1
A getAttributes() 0 4 1
A getLinks() 0 4 1
A getMeta() 0 4 1
A getRelationship() 0 14 4
A getRelationshipMethodName() 6 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 LogicException;
15
16
abstract class AbstractSerializer implements SerializerInterface
17
{
18
    /**
19
     * The type.
20
     *
21
     * @var string
22
     */
23
    protected $type;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 42
    public function getType($model)
29
    {
30 42
        return $this->type;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 42
    public function getId($model)
37
    {
38 42
        return $model->id;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getAttributes($model, array $fields = null)
45
    {
46
        return [];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 24
    public function getLinks($model)
53
    {
54 24
        return [];
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 24
    public function getMeta($model)
61
    {
62 24
        return [];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @throws \LogicException
69
     */
70 24
    public function getRelationship($model, $name)
71
    {
72 24
        $method = $this->getRelationshipMethodName($name);
73
74 24
        if (method_exists($this, $method)) {
75 24
            $relationship = $this->$method($model);
76
77 24
            if ($relationship !== null && ! ($relationship instanceof Relationship)) {
78 3
                throw new LogicException('Relationship method must return null or an instance of Tobscure\JsonApi\Relationship');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
79
            }
80
81 21
            return $relationship;
82
        }
83
    }
84
85
    /**
86
     * Get the serializer method name for the given relationship.
87
     *
88
     * snake_case and kebab-case are converted into camelCase.
89
     *
90
     * @param string $name
91
     *
92
     * @return string
93
     */
94 24
    private function getRelationshipMethodName($name)
95
    {
96 24 View Code Duplication
        if (stripos($name, '-')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97 3
            $name = lcfirst(implode('', array_map('ucfirst', explode('-', $name))));
98 3
        }
99
100 24 View Code Duplication
        if (stripos($name, '_')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
101 3
            $name = lcfirst(implode('', array_map('ucfirst', explode('_', $name))));
102 3
        }
103
104 24
        return $name;
105
    }
106
}
107