Completed
Pull Request — master (#119)
by Toby
57:34 queued 55:19
created

AbstractResource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 8.57 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 6
loc 70
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A getAttributes() 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 AbstractResource implements ResourceInterface
17
{
18
    use LinksTrait;
19
    use MetaTrait;
20
21
    /**
22
     * The resource type.
23
     *
24
     * @var string
25
     */
26
    protected $type;
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 3
    public function getType()
32
    {
33 3
        return $this->type;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    public function getAttributes(array $fields = null)
40
    {
41 3
        return [];
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @throws \LogicException
48
     */
49 6
    public function getRelationship($name)
50
    {
51 6
        $method = $this->getRelationshipMethodName($name);
52
53 6
        if (method_exists($this, $method)) {
54 6
            $relationship = $this->$method();
55
56 6
            if ($relationship !== null && ! ($relationship instanceof Relationship)) {
57 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...
58
            }
59
60 3
            return $relationship;
61
        }
62
    }
63
64
    /**
65
     * Get the method name for the given relationship.
66
     *
67
     * snake_case and kebab-case are converted into camelCase.
68
     *
69
     * @param string $name
70
     *
71
     * @return string
72
     */
73 6
    private function getRelationshipMethodName($name)
74
    {
75 6 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...
76 3
            $name = lcfirst(implode('', array_map('ucfirst', explode('-', $name))));
77 3
        }
78
79 6 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...
80 3
            $name = lcfirst(implode('', array_map('ucfirst', explode('_', $name))));
81 3
        }
82
83 6
        return $name;
84
    }
85
}
86