Descendants::limit()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 48
ccs 36
cts 36
cp 1
rs 9.456
cc 3
nc 3
nop 1
crap 3
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\Descendants as Base;
8
9
class Descendants 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...t\Relations\Descendants.
Loading history...
12
13
    /**
14
     * Set the "limit" value of the query.
15
     *
16
     * @param int $value
17
     * @return $this
18
     */
19 20
    public function limit($value)
20
    {
21 20
        if ($this->parent->exists) {
22 4
            $this->query->limit($value);
23
        } else {
24 16
            if ($this->andSelf) {
25 8
                $this->addGroupLimit($value);
26
            } else {
27 8
                $grammar = $this->getEagerLimitGrammar();
28
29 8
                $firstPathSegment = $grammar->compileFirstPathSegment(
30 8
                    $this->related->qualifyColumn(
31 8
                        $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 and Illuminate\Database\Eloq...gHasThroughRelationship; 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

31
                        /** @scrutinizer ignore-type */ $this->related->getPathName()
Loading history...
32 8
                    )
33 8
                );
34
35 8
                $parentKeyQuery = $this->related->newModelQuery()
36 8
                                                ->select($this->getForeignKeyName())
37 8
                                                ->from($this->related->getTable(), 'laravel_alias')
38 8
                                                ->where($this->localKey, new Expression($firstPathSegment))
39 8
                                                ->limit(1);
40
41 8
                $sql = $grammar->compileParentKeyOfFirstPathSegment(
42 8
                    $this->related->qualifyColumn(
43 8
                        $this->related->getPathName()
44 8
                    ),
45 8
                    $this->related->qualifyColumn(
46 8
                        $this->related->getParentKeyName()
47 8
                    ),
48 8
                    $parentKeyQuery->getQuery()
49 8
                );
50
51 8
                $column = new Expression($sql);
52
53 8
                $this->query->groupLimit($value, $column);
54
55 8
                $this->query->getQuery()->addBinding(
56 8
                    array_fill(
57 8
                        0,
58 8
                        substr_count($sql, '?'),
59 8
                        $this->related->getPathSeparator()
60 8
                    ),
61 8
                    'select'
62 8
                );
63
            }
64
        }
65
66 20
        return $this;
67
    }
68
}
69