| Conditions | 7 |
| Paths | 4 |
| Total Lines | 19 |
| Code Lines | 11 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 12 |
| CRAP Score | 7 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 18 | 4 | public function fromMorph(string $key = '', ?Model $default = null, string $idKeyName = 'id', string $classKeyName = 'class'): ?Model |
|
| 19 | { |
||
| 20 | 4 | $id = $this->getAttribute($key ? "{$key}.{$idKeyName}" : $idKeyName); |
|
| 21 | 4 | $class = $this->getAttribute($key ? "{$key}.{$classKeyName}" : $classKeyName); |
|
| 22 | 4 | if (!$id || !$class) { |
|
| 23 | 4 | return $default; |
|
| 24 | } |
||
| 25 | |||
| 26 | 4 | $class = Relation::getMorphedModel($class) ?? $class; |
|
| 27 | 4 | if (!is_a($class, Model::class, true)) { |
|
| 28 | 2 | return $default; |
|
| 29 | } |
||
| 30 | |||
| 31 | 4 | $model = $class::find($id); |
|
| 32 | 4 | if (!$model) { |
|
| 33 | 2 | return $default; |
|
| 34 | } |
||
| 35 | |||
| 36 | 2 | return $model; |
|
| 37 | } |
||
| 39 |