ReversesRelationships   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 26
dl 0
loc 80
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hasManyDeepFromReverse() 0 3 1
A hasOneOrManyDeepFromReverseThroughClass() 0 23 5
A hasOneOrManyDeepFromReverse() 0 19 2
A hasOneDeepFromReverse() 0 3 1
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\Pivot;
7
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
8
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
9
10
trait ReversesRelationships
11
{
12
    /**
13
     * Define a has-many-deep relationship by reversing an existing deep relationship.
14
     *
15
     * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation
16
     * @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep
17
     */
18
    public function hasManyDeepFromReverse(HasManyDeep $relation): HasManyDeep
19
    {
20
        return $this->hasManyDeep(...$this->hasOneOrManyDeepFromReverse($relation));
0 ignored issues
show
Bug introduced by
The method hasManyDeep() does not exist on Staudenmeir\EloquentHasM...s\ReversesRelationships. Did you maybe mean hasManyDeepFromReverse()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return $this->/** @scrutinizer ignore-call */ hasManyDeep(...$this->hasOneOrManyDeepFromReverse($relation));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
21
    }
22
23
    /**
24
     * Define a has-one-deep relationship by reversing an existing deep relationship.
25
     *
26
     * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation
27
     * @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep
28
     */
29
    public function hasOneDeepFromReverse(HasManyDeep $relation): HasOneDeep
30
    {
31
        return $this->hasOneDeep(...$this->hasOneOrManyDeepFromReverse($relation));
0 ignored issues
show
Bug introduced by
The method hasOneDeep() does not exist on Staudenmeir\EloquentHasM...s\ReversesRelationships. Did you maybe mean hasOneDeepFromReverse()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        return $this->/** @scrutinizer ignore-call */ hasOneDeep(...$this->hasOneOrManyDeepFromReverse($relation));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
    }
33
34
    /**
35
     * Prepare a has-one-deep or has-many-deep relationship by reversing an existing deep relationship.
36
     *
37
     * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation
38
     * @return array
39
     */
40
    protected function hasOneOrManyDeepFromReverse(HasManyDeep $relation): array
41
    {
42
        $related = $relation->getFarParent()::class;
43
44
        $through = [];
45
46
        foreach (array_reverse($relation->getThroughParents()) as $throughParent) {
47
            $through[] = $this->hasOneOrManyDeepFromReverseThroughClass($throughParent);
48
        }
49
50
        $foreignKeys = array_reverse(
51
            $relation->getLocalKeys()
52
        );
53
54
        $localKeys = array_reverse(
55
            $relation->getForeignKeys()
56
        );
57
58
        return [$related, $through, $foreignKeys, $localKeys];
59
    }
60
61
    /**
62
     * Prepare a has-one-deep or has-many-deep relationship through class.
63
     *
64
     * @param \Illuminate\Database\Eloquent\Model $throughParent
65
     * @return string
66
     */
67
    protected function hasOneOrManyDeepFromReverseThroughClass(Model $throughParent): string
68
    {
69
        $table = $throughParent->getTable();
70
71
        $segments = preg_split('/\s+as\s+/i', $table);
72
73
        if ($throughParent instanceof Pivot) {
74
            if (isset($segments[1])) {
75
                $class = $throughParent::class . " as $segments[1]";
76
            } else {
77
                $class = $table;
78
            }
79
        } else {
80
            $class = $throughParent::class;
81
82
            if (isset($segments[1])) {
83
                $class .= " as $segments[1]";
84
            } elseif ($table !== (new $throughParent())->getTable()) {
85
                $class .= " from $table";
86
            }
87
        }
88
89
        return $class;
90
    }
91
}
92