get()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
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 27
rs 9.2222
cc 6
nc 7
nop 1
1
<?php
2
3
namespace Mpyw\ComposhipsEagerLimit\Database\Query\Concerns;
4
5
use Staudenmeir\EloquentEagerLimit\Traits\BuildsGroupLimitQueries;
6
7
/**
8
 * Trait BuildsGroupLimitQueriesByMultipleColumnPartition
9
 *
10
 * @mixin \Illuminate\Database\Query\Builder
11
 */
12
trait BuildsGroupLimitQueriesByMultipleColumnPartition
13
{
14
    use BuildsGroupLimitQueries;
15
16
    /**
17
     * Execute the query as a "select" statement.
18
     *
19
     * @param array $columns
20
     * @return \Illuminate\Support\Collection
21
     */
22
    public function get($columns = ['*'])
23
    {
24
        $items = parent::get($columns);
25
26
        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...
27
            return $items;
28
        }
29
30
        $keys = ['laravel_row'];
31
32
        if (is_array($this->groupLimit['column'])) {
33
            foreach ($this->groupLimit['column'] as $i => $column) {
34
                $keys[] = "@laravel_partition_$i := " . $this->grammar->wrap(last(explode('.', $column)));
35
                $keys[] = "@laravel_partition_$i := " . $this->grammar->wrap('pivot_' . last(explode('.', $column)));
36
            }
37
        } else {
38
            $keys[] = '@laravel_partition := ' . $this->grammar->wrap(last(explode('.', $this->groupLimit['column'])));
39
            $keys[] = '@laravel_partition := ' . $this->grammar->wrap('pivot_' . last(explode('.', $this->groupLimit['column'])));
40
        }
41
42
        foreach ($items as $item) {
43
            foreach ($keys as $key) {
44
                unset($item->$key);
45
            }
46
        }
47
48
        return $items;
49
    }
50
}