Passed
Pull Request — master (#33)
by Mark van den
16:57 queued 06:50
created

Builder::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.9
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimit;
4
5
use Illuminate\Database\Query\Builder as Base;
6
7
class Builder extends Base
8
{
9
    /**
10
     * The maximum number of records to return per group.
11
     *
12
     * @var array
13
     */
14
    public $groupLimit;
15
16
    /**
17
     * Add a "group limit" clause to the query.
18
     *
19
     * @param int $value
20
     * @param string $column
21
     * @return $this
22
     */
23 116
    public function groupLimit($value, $column)
24
    {
25 116
        if ($value >= 0) {
26 116
            $this->groupLimit = compact('value', 'column');
27
        }
28
29 116
        return $this;
30
    }
31
32
    /**
33
     * Execute the query as a "select" statement.
34
     *
35
     * @param array $columns
36
     * @return \Illuminate\Support\Collection
37
     */
38 128
    public function get($columns = ['*'])
39
    {
40 128
        $items = parent::get($columns);
41
42 128
        if (!$this->groupLimit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->groupLimit of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43 128
            return $items;
44
        }
45
46 96
        $column = last(explode('.', $this->groupLimit['column']));
47
48
        $keys = [
49 96
            'laravel_row',
50 96
            '@laravel_partition := '.$this->grammar->wrap($column),
51 96
            '@laravel_partition := '.$this->grammar->wrap('pivot_'.$column),
52
        ];
53
54 96
        foreach ($items as $item) {
55 96
            unset($item->{$keys[0]}, $item->{$keys[1]}, $item->{$keys[2]});
56
        }
57
58 96
        return $items;
59
    }
60
}
61