| Conditions | 6 |
| Paths | 5 |
| Total Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 42 |
| Changes | 0 | ||
| 1 | <?php |
||
| 25 | public function with(...$relations): self |
||
| 26 | { |
||
| 27 | foreach ($relations as $relation) { |
||
| 28 | if (\is_string($relation)) { |
||
| 29 | $this->add(new Relation($relation, $this)); |
||
|
|
|||
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | if (\is_array($relation)) { |
||
| 34 | foreach ($relation as $rel => $sub) { |
||
| 35 | \assert(\is_string($rel) && $sub instanceof \Closure); |
||
| 36 | |||
| 37 | $this->add(new Relation($rel, $this, $sub)); |
||
| 38 | } |
||
| 39 | continue; |
||
| 40 | } |
||
| 41 | |||
| 42 | $error = 'Relation should be string ("relation_name") '. |
||
| 43 | 'or array (["relation" => function]), ' . |
||
| 44 | 'but %s given'; |
||
| 45 | |||
| 46 | throw new \InvalidArgumentException(\sprintf($error, \gettype($relation))); |
||
| 47 | } |
||
| 48 | |||
| 49 | return $this; |
||
| 50 | } |
||
| 51 | } |
||
| 52 |
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
Idableprovides a methodequalsIdthat 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.