Passed
Push — master ( 81d2d7...2ca0db )
by Jonas
10:40
created

ReversesRelationships   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 75
ccs 29
cts 29
cp 1
rs 10
c 1
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B hasOneOrManyDeepFromReverse() 0 45 8
A hasManyDeepFromReverse() 0 3 1
A hasOneDeepFromReverse() 0 3 1
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\Pivot;
7
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
8
9
trait ReversesRelationships
10
{
11
    /**
12
     * Define a has-many-deep relationship by reversing an existing deep relationship.
13
     *
14
     * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation
15
     * @return \Staudenmeir\EloquentHasManyDeep\HasManyDeep
16
     */
17 2
    public function hasManyDeepFromReverse(HasManyDeep $relation)
18
    {
19 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

19
        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...
20
    }
21
22
    /**
23
     * Define a has-one-deep relationship by reversing an existing deep relationship.
24
     *
25
     * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation
26
     * @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep
27
     */
28 3
    public function hasOneDeepFromReverse(HasManyDeep $relation)
29
    {
30 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

30
        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...
31
    }
32
33
    /**
34
     * Prepare a has-one-deep or has-many-deep relationship by reversing an existing deep relationship.
35
     *
36
     * @param \Staudenmeir\EloquentHasManyDeep\HasManyDeep $relation
37
     * @return array
38
     */
39 5
    protected function hasOneOrManyDeepFromReverse(HasManyDeep $relation): array
40
    {
41 5
        $related = $relation->getFarParent()::class;
42
43 5
        $through = [];
44
45 5
        foreach (array_reverse($relation->getThroughParents()) as $throughParent) {
46 5
            $table = $throughParent->getTable();
47
48 5
            $segments = preg_split('/\s+as\s+/i', $table);
49
50 5
            if ($throughParent instanceof Pivot) {
51 2
                if (isset($segments[1])) {
52 1
                    $class = $throughParent::class . " as $segments[1]";
53
                } else {
54 2
                    $class = $table;
55
                }
56
            } else {
57 5
                $class = $throughParent::class;
58
59 5
                if (isset($segments[1])) {
60 1
                    $class .= " as $segments[1]";
61 4
                } elseif ($table !== (new $throughParent())->getTable()) {
62 1
                    $class .= " from $table";
63
                }
64
            }
65
66 5
            $through[] = $class;
67
        }
68
69 5
        $foreignKeys = array_map(
70 5
            fn ($key) => is_string($key) ? end(...[explode('.', $key)]) : $key,
0 ignored issues
show
Bug introduced by
array(explode('.', $key)) is expanded, but the parameter $array of end() does not expect variable arguments. ( Ignorable by Annotation )

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

70
            fn ($key) => is_string($key) ? end(/** @scrutinizer ignore-type */ ...[explode('.', $key)]) : $key,
Loading history...
71 5
            array_reverse(
72 5
                $relation->getLocalKeys()
73
            )
74
        );
75
76 5
        $localKeys = array_map(
77 5
            fn ($key) => is_string($key) ? end(...[explode('.', $key)]) : $key,
0 ignored issues
show
Bug introduced by
array(explode('.', $key)) is expanded, but the parameter $array of end() does not expect variable arguments. ( Ignorable by Annotation )

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

77
            fn ($key) => is_string($key) ? end(/** @scrutinizer ignore-type */ ...[explode('.', $key)]) : $key,
Loading history...
78 5
            array_reverse(
79 5
                $relation->getForeignKeys()
80
            )
81
        );
82
83 5
        return [$related, $through, $foreignKeys, $localKeys];
84
    }
85
}
86