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

Ancestors::limit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
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 34
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\EloquentEagerLimitXLaravelAdjacencyList\Query\Grammars\MySqlGrammar;
8
use Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Query\Grammars\PostgresGrammar;
9
use Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Query\Grammars\SQLiteGrammar;
10
use Staudenmeir\EloquentEagerLimitXLaravelAdjacencyList\Query\Grammars\SqlServerGrammar;
11
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors as Base;
12
13
class Ancestors extends Base
14
{
15
    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\Ancestors.
Loading history...
16
17
    /**
18
     * Set the "limit" value of the query.
19
     *
20
     * @param int $value
21
     * @return $this
22
     */
23
    public function limit($value)
24
    {
25
        if ($this->parent->exists) {
26
            $this->query->limit($value);
27
        } else {
28
            $grammar = $this->getEagerLimitGrammar();
29
30
			// TODO
31
			$sql = $grammar->compileFirstPathSegment(
32
				$this->related->qualifyColumn(
33
					$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

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