Passed
Push — main ( 31aa9e...537bf7 )
by Jonas
11:44
created

Descendants::limit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 37
rs 9.6666
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Eloquent\Relations;
4
5
use Illuminate\Database\Query\Expression;
6
use RuntimeException;
7
use Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Eloquent\Relations\Traits\HasEagerLimit;
8
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants as Base;
9
10
class Descendants extends Base
11
{
12
    use HasEagerLimit;
0 ignored issues
show
Bug introduced by
The trait Staudenmeir\EloquentEage...ns\Traits\HasEagerLimit requires the property $exists which is not provided by Staudenmeir\EloquentEage...t\Relations\Descendants.
Loading history...
13
14
    /**
15
     * Set the "limit" value of the query.
16
     *
17
     * @param int $value
18
     * @return $this
19
     */
20
    public function limit($value)
21
    {
22
        if ($this->parent->exists) {
23
            $this->query->limit($value);
24
        } else {
25
            if ($this->andSelf) {
26
                $grammar = $this->getEagerLimitGrammar();
27
28
	            $sql = $grammar->compileFirstPathSegment(
29
		            $this->related->qualifyColumn(
30
			            $this->related->getPathName()
0 ignored issues
show
Bug introduced by
It seems like $this->related->getPathName() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $column of Illuminate\Database\Eloq...\Model::qualifyColumn() does only seem to accept 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

30
			            /** @scrutinizer ignore-type */ $this->related->getPathName()
Loading history...
31
		            )
32
	            );
33
34
	            $column = new Expression($sql);
35
36
                // TODO
37
                //		    if ($grammar instanceof MySqlGrammar && $grammar->useLegacyGroupLimit($this->query->getQuery())) {
38
                //			    $column = 'laravel_through_key';
39
                //		    }
40
41
                $this->query->groupLimit($value, $column);
42
43
	            $this->query->getQuery()->addBinding(
44
		            array_fill(
45
			            0,
46
			            substr_count($sql, '?'),
47
			            $this->related->getPathSeparator()
48
		            ),
49
		            'groupBy'
50
	            );
51
            } else {
52
                throw new RuntimeException('Eager loading limits are not supported on Descendants relationships.');
53
            }
54
        }
55
56
        return $this;
57
    }
58
}
59