Passed
Pull Request — master (#3)
by
unknown
02:52 queued 53s
created

ResourceTrait::getResourceAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi;
7
8
use yii\base\Arrayable;
9
use yii\db\ActiveRecordInterface;
10
use yii\db\BaseActiveRecord;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Inflector;
13
14
trait ResourceTrait
15
{
16
    /**
17
     * @return null|string
18
     */
19
    public function getId()
20
    {
21
        if ($this instanceof ActiveRecordInterface) {
22
            return (string) $this->getPrimaryKey();
23
        }
24
        return null;
25
    }
26
27
    /**
28
     * @return string
29
     */
30
    public function getType()
31
    {
32
        $reflect = new \ReflectionClass($this);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33
        $className = $reflect->getShortName();
34
        return Inflector::pluralize(Inflector::camel2id($className));
35
    }
36
37
    /**
38
     * @param array $fields
39
     * @return array
40
     */
41 View Code Duplication
    public function getResourceAttributes(array $fields = [])
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...
42
    {
43
        $attributes = [];
44
45
        foreach ($this->resolveFields($this->fields(), $fields) as $name => $definition) {
0 ignored issues
show
Bug introduced by
The method fields() does not exist on tuyakhov\jsonapi\ResourceTrait. Did you maybe mean extraFields()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
46
            $attributes[$name] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $name);
47
        }
48
        return $attributes;
49
    }
50
51
    /**
52
     * @return array
53
     */
54 View Code Duplication
    public function getResourceRelationships()
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...
55
    {
56
        $relationships = [];
57
58
        foreach ($this->resolveFields($this->extraFields()) as $name => $definition) {
59
            $relationships[$name] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $name);
60
        }
61
        return $relationships;
62
    }
63
64
    /**
65
     * @param string $name the case sensitive name of the relationship.
66
     * @param $relationship
67
     */
68
    public function setResourceRelationship($name, $relationship)
69
    {
70
        if (!is_array($relationship)) {
71
            $relationship = [$relationship];
72
        }
73
        foreach ($relationship as $key => $value) {
74
            if ($value instanceof ActiveRecordInterface) {
75
                $this->link($name, $value);
0 ignored issues
show
Bug introduced by
It seems like link() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
            }
77
        }
78
    }    
79
80
    /**
81
     * @return array
82
     */
83
    public function extraFields()
84
    {
85
        return [];
86
    }
87
88
    /**
89
     * @param array $fields
90
     * @param array $fieldSet
91
     * @return array
92
     */
93
    protected function resolveFields(array $fields, array $fieldSet = [])
94
    {
95
        $result = [];
96
97
        foreach ($fields as $field => $definition) {
98
            if (is_int($field)) {
99
                $field = $definition;
100
            }
101
            if (empty($fieldSet) || in_array($field, $fieldSet, true)) {
102
                $result[$field] = $definition;
103
            }
104
        }
105
106
        return $result;
107
    }
108
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
109