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

Bloodline::limit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 33
rs 9.7666
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Eloquent\Relations;
4
5
use Illuminate\Database\Query\Expression;
6
use Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Eloquent\Relations\Traits\HasEagerLimit;
7
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline as Base;
8
9
class Bloodline extends Base
10
{
11
    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...ent\Relations\Bloodline.
Loading history...
12
13
    /**
14
     * Set the "limit" value of the query.
15
     *
16
     * @param int $value
17
     * @return $this
18
     */
19
    public function limit($value)
20
    {
21
        if ($this->parent->exists) {
22
            $this->query->limit($value);
23
        } else {
24
            $grammar = $this->getEagerLimitGrammar();
25
26
	        $sql = $grammar->compileFirstPathSegment(
27
		        $this->related->qualifyColumn(
28
			        $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

28
			        /** @scrutinizer ignore-type */ $this->related->getPathName()
Loading history...
29
		        )
30
	        );
31
32
            $column = new Expression($sql);
33
34
            // TODO
35
            //		    if ($grammar instanceof MySqlGrammar && $grammar->useLegacyGroupLimit($this->query->getQuery())) {
36
            //			    $column = 'laravel_through_key';
37
            //		    }
38
39
            $this->query->groupLimit($value, $column);
40
41
	        $this->query->getQuery()->addBinding(
42
		        array_fill(
43
			        0,
44
			        substr_count($sql, '?'),
45
			        $this->related->getPathSeparator()
46
		        ),
47
		        'groupBy'
48
	        );
49
        }
50
51
        return $this;
52
    }
53
}
54