Conditions | 7 |
Paths | 16 |
Total Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Tests | 10 |
CRAP Score | 7 |
Changes | 0 |
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
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.