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

AbstractResource::getRelationshipMethodName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 6
Ratio 50 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 6
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 3
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