RelationshipsTrait::relationships()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 15
c 2
b 2
f 0
dl 0
loc 27
rs 8.8333
cc 7
nc 6
nop 0
1
<?php
2
3
namespace LWS\Import\Traits;
4
5
use ErrorException;
6
use ReflectionClass;
7
use ReflectionMethod;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
10
trait RelationshipsTrait
11
{
12
    public function relationships()
13
    {
14
        $model = new static;
15
16
        $relationships = [];
17
18
        foreach ((new ReflectionClass($model))->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
19
            if ($method->class != get_class($model) ||
20
                ! empty($method->getParameters()) ||
21
                $method->getName() == __FUNCTION__) {
22
                continue;
23
            }
24
25
            try {
26
                $return = $method->invoke($model);
27
28
                if ($return instanceof Relation) {
29
                    $relationships[$method->getName()] = [
30
                        'type' => (new ReflectionClass($return))->getShortName(),
31
                        'model' => (new ReflectionClass($return->getRelated()))->getName(),
32
                    ];
33
                }
34
            } catch (ErrorException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
35
            }
36
        }
37
38
        return $relationships;
39
    }
40
}
41