Completed
Push — master ( ac9ebe...25a070 )
by Iman
03:02
created

guessBelongsToManyRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Imanghafoori\Relativity;
4
5
trait BaseEloquentOverrides
6
{
7 1
    protected function guessBelongsToManyRelation()
8
    {
9 1
        $debug = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3);
10
11 1
        $name = $debug[1]['args'][8] ?? $debug[2]['function'];
12
13 1
        if ($name == "morphedByMany") {
14 1
            $name = $debug[2]['args'][7] ?? debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4)[3]['function'];
15
        }
16
17 1
        return $name;
18
    }
19
20 5
    public function getRelationValue($key)
21
    {
22
        // If the key already exists in the relationships array, it just means the
23
        // relationship has already been loaded, so we'll just return it out of
24
        // here because there is no need to query within the relations twice.
25 5
        if ($this->relationLoaded($key)) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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...
26 1
            return $this->relations[$key];
0 ignored issues
show
Bug introduced by
The property relations 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...
27
        }
28
29
        // If the "attribute" exists as a method on the model, we will just assume
30
        // it is a relationship and will load and return results from the query
31
        // and hydrate the relationship's value on the "relationships" array.
32 5
        if (method_exists($this, $key) or isset(static::$dynamicRelations[$key])) {
33 5
            return $this->getRelationshipFromMethod($key);
0 ignored issues
show
Bug introduced by
It seems like getRelationshipFromMethod() 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...
34
        }
35
    }
36
}
37