Passed
Push — master ( 06fb8e...d2fcb1 )
by Jonas
03:54
created

IsRecursiveRelation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 24
c 1
b 0
f 0
dl 0
loc 95
ccs 27
cts 27
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildDictionary() 0 5 1
A __construct() 0 5 1
A getQualifiedLocalKeyName() 0 3 1
A replaceTableHash() 0 11 1
A __call() 0 19 2
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Query\Expression;
9
10
trait IsRecursiveRelation
11
{
12
    /**
13
     * Whether to include the parent model.
14
     *
15
     * @var bool
16
     */
17
    protected $andSelf;
18
19
    /**
20
     * Create a new recursive relationship instance.
21
     *
22
     * @param \Illuminate\Database\Eloquent\Builder $query
23
     * @param \Illuminate\Database\Eloquent\Model $parent
24
     * @param string $foreignKey
25
     * @param string $localKey
26
     * @param bool $andSelf
27
     * @return void
28
     */
29 231
    public function __construct(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
30
    {
31 231
        $this->andSelf = $andSelf;
32
33 231
        parent::__construct($query, $parent, $foreignKey, $localKey);
34
    }
35
36
    /**
37
     * Build model dictionary.
38
     *
39
     * @param \Illuminate\Database\Eloquent\Collection $results
40
     * @return array
41
     */
42 50
    protected function buildDictionary(Collection $results)
43
    {
44 50
        return $results->mapToDictionary(function (Model $result) {
45 50
            return [$result->getFirstPathSegment() => $result];
46 50
        })->all();
47
    }
48
49
    /**
50
     * Get the fully qualified local key name.
51
     *
52
     * @return string
53
     */
54 149
    public function getQualifiedLocalKeyName()
55
    {
56 149
        return $this->qualifyColumn($this->localKey);
0 ignored issues
show
Bug introduced by
The method qualifyColumn() does not exist on Staudenmeir\LaravelAdjac...its\IsRecursiveRelation. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

56
        return $this->/** @scrutinizer ignore-call */ qualifyColumn($this->localKey);
Loading history...
57
    }
58
59
    /**
60
     * Handle dynamic method calls to the relationship.
61
     *
62
     * @param string $method
63
     * @param array $parameters
64
     * @return mixed
65
     */
66 191
    public function __call($method, $parameters)
67
    {
68 191
        $methods = ['update', 'increment', 'decrement', 'delete', 'forceDelete'];
69
70 191
        if (in_array($method, $methods)) {
71 24
            $expression = $this->query->getQuery()->from;
72
73 24
            $table = $this->parent->getTable();
74
75 24
            $this->query->getQuery()->from = $table;
76
77 24
            $this->query->getModel()->setTable($table);
78
79 24
            $keys = $this->query->getQuery()->newQuery()->from($expression)->select($this->localKey);
80
81 24
            return $this->query->whereIn($this->getQualifiedLocalKeyName(), $keys)->$method(...$parameters);
82
        }
83
84 191
        return parent::__call($method, $parameters);
85
    }
86
87
    /**
88
     * Replace table hash with expression name in self-relation aggregate queries.
89
     *
90
     * @param \Illuminate\Database\Eloquent\Builder $query
91
     * @param \Illuminate\Database\Query\Expression $expression
92
     * @return \Illuminate\Database\Query\Expression
93
     */
94 21
    protected function replaceTableHash(Builder $query, Expression $expression)
95
    {
96 21
        return new Expression(
97 21
            str_replace(
98 21
                $query->getGrammar()->wrap(
0 ignored issues
show
Bug introduced by
It seems like $query->getGrammar()->wr...lationCountHash(false)) can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Grammars\Grammar; however, parameter $search of str_replace() does only seem to accept string|string[], maybe add an additional type check? ( Ignorable by Annotation )

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

98
                /** @scrutinizer ignore-type */ $query->getGrammar()->wrap(
Loading history...
99 21
                    $this->getRelationCountHash(false)
0 ignored issues
show
Bug introduced by
The method getRelationCountHash() does not exist on Staudenmeir\LaravelAdjac...its\IsRecursiveRelation. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

99
                    $this->/** @scrutinizer ignore-call */ 
100
                           getRelationCountHash(false)
Loading history...
100
                ),
101 21
                $query->getGrammar()->wrap(
0 ignored issues
show
Bug introduced by
It seems like $query->getGrammar()->wr...)->getExpressionName()) can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Grammars\Grammar; however, parameter $replace of str_replace() does only seem to accept string|string[], maybe add an additional type check? ( Ignorable by Annotation )

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

101
                /** @scrutinizer ignore-type */ $query->getGrammar()->wrap(
Loading history...
102 21
                    $query->getModel()->getExpressionName()
103
                ),
104 21
                $expression->getValue(),
105
            )
106
        );
107
    }
108
}
109