Completed
Pull Request — master (#42)
by Jose Manuel
01:40
created

JsonPathAccessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 26
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A get() 0 23 4
1
<?php
2
3
namespace Softonic\GraphQL\Traits;
4
5
trait JsonPathAccessor
6
{
7 102
    public function get(string $path)
8
    {
9 102
        if ($path == '.') {
10 98
            return $this;
11
        }
12
13 100
        $attributes = explode('.', $path);
14 100
        unset($attributes[0]);
15
16 100
        $attribute = array_shift($attributes);
17
18
        /**
19
         * This condition is needed because a child could have the same name than a MutationTypeConfig attribute
20
         * (type, linksTo, children).
21
         */
22 100
        $value = $this->hasChild($attribute) ? $this->children[$attribute] : $this->{$attribute};
0 ignored issues
show
Bug introduced by
The property children does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like hasChild() 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...
23
24 100
        if (!empty($attributes)) {
25 74
            $value = $value->get('.' . implode('.', $attributes));
26
        }
27
28 100
        return $value;
29
    }
30
}
31