Completed
Push — master ( d58822...10e381 )
by Jonas
15:56
created

hasOneOrManyDeepFromReverseThroughClass()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 1
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 5
rs 9.4888
c 0
b 0
f 0
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 2
    public function hasManyDeepFromReverse(HasManyDeep $relation): HasManyDeep
19
    {
20 2
        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 3
    public function hasOneDeepFromReverse(HasManyDeep $relation): HasOneDeep
30
    {
31 3
        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 5
    protected function hasOneOrManyDeepFromReverse(HasManyDeep $relation): array
41
    {
42 5
        $related = $relation->getFarParent()::class;
43
44 5
        $through = [];
45
46 5
        foreach (array_reverse($relation->getThroughParents()) as $throughParent) {
47 5
            $through[] = $this->hasOneOrManyDeepFromReverseThroughClass($throughParent);
48
        }
49
50 5
        $foreignKeys = array_reverse(
51 5
            $relation->getLocalKeys()
52
        );
53
54 5
        $localKeys = array_reverse(
55 5
            $relation->getForeignKeys()
56
        );
57
58 5
        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 5
    protected function hasOneOrManyDeepFromReverseThroughClass(Model $throughParent): string
68
    {
69 5
        $table = $throughParent->getTable();
70
71 5
        $segments = preg_split('/\s+as\s+/i', $table);
72
73 5
        if ($throughParent instanceof Pivot) {
74 2
            if (isset($segments[1])) {
75 1
                $class = $throughParent::class . " as $segments[1]";
76
            } else {
77 2
                $class = $table;
78
            }
79
        } else {
80 5
            $class = $throughParent::class;
81
82 5
            if (isset($segments[1])) {
83 1
                $class .= " as $segments[1]";
84 4
            } elseif ($table !== (new $throughParent())->getTable()) {
85 1
                $class .= " from $table";
86
            }
87
        }
88
89 5
        return $class;
90
    }
91
}
92