Passed
Push — master ( 43dc9a...7dcb7e )
by Anton
02:32
created

ResourceTrait::fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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\helpers\Inflector;
11
12
trait ResourceTrait
13
{
14
    /**
15
     * @return null|string
16
     */
17
    public function getId()
18
    {
19
        if ($this instanceof ActiveRecordInterface) {
20
            return (string) $this->getPrimaryKey();
21
        }
22
        return null;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getType()
29
    {
30
        $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...
31
        $className = $reflect->getShortName();
32
        return Inflector::pluralize(Inflector::camel2id($className));
33
    }
34
35
    /**
36
     * @param array $fields
37
     * @return array
38
     */
39
    public function getResourceAttributes(array $fields = [])
40
    {
41
        $attributes = [];
42
        if ($this instanceof Arrayable) {
43
            $fieldDefinitions = $this->fields();
44
        } else {
45
            $vars = array_keys(\Yii::getObjectVars($this));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 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...
46
            $fieldDefinitions = array_combine($vars, $vars);
47
        }
48
49 View Code Duplication
        foreach ($this->resolveFields($fieldDefinitions, $fields) as $name => $definition) {
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...
50
            $attributes[$name] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $name);
51
        }
52
        return $attributes;
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getResourceRelationships()
59
    {
60
        $relationships = [];
61
        $fields = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
62
        if ($this instanceof Arrayable) {
63
            $fields = $this->extraFields();
64
        }
65
66 View Code Duplication
        foreach ($this->resolveFields($fields) as $name => $definition) {
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...
67
            $relationships[$name] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $name);
68
        }
69
        return $relationships;
70
    }
71
72
    /**
73
     * @param string $name the case sensitive name of the relationship.
74
     * @param $relationship
75
     */
76
    public function setResourceRelationship($name, $relationship)
77
    {
78
        if (!is_array($relationship)) {
79
            $relationship = [$relationship];
80
        }
81
        foreach ($relationship as $key => $value) {
82
            if ($value instanceof ActiveRecordInterface) {
83
                $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...
84
            }
85
        }
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