Conditions | 4 |
Paths | 3 |
Total Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 5 |
CRAP Score | 4.074 |
Changes | 0 |
1 | <?php |
||
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)) { |
|
|
|||
26 | 1 | return $this->relations[$key]; |
|
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); |
|
34 | } |
||
35 | } |
||
36 | } |
||
37 |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.