Passed
Push — master ( c89bf9...5eeb6f )
by Andrey
03:48
created

Search   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 29
c 3
b 2
f 0
dl 0
loc 87
ccs 32
cts 34
cp 0.9412
rs 10
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A filterNum() 0 4 1
A map() 0 5 1
A getId() 0 5 2
A filterSlugs() 0 4 1
A toArray() 0 9 3
A by() 0 19 5
A isArray() 0 3 1
A isArrayable() 0 3 2
1
<?php
2
3
namespace Helldar\Roles\Support\Database;
4
5
use Helldar\Roles\Models\BaseModel;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
11
class Search
12
{
13
    /**
14
     * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation  $builder
15
     * @param  \Helldar\Roles\Models\BaseModel|int|string  $values
16
     *
17
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation
18
     */
19 54
    public function by($builder, $values)
20
    {
21 54
        $value = $this->map($values);
22
23 54
        $nums  = $this->filterNum($value);
24 54
        $slugs = $this->filterSlugs($value);
25
26 54
        if ($nums && ! $slugs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slugs 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...
Bug Best Practice introduced by
The expression $nums 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 42
            return $builder->whereIn('id', $nums);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $builder->whereIn('id', $nums) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloq...uent\Relations\Relation.
Loading history...
28
        }
29
30 54
        if (! $nums && $slugs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slugs 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...
Bug Best Practice introduced by
The expression $nums 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...
31 54
            return $builder->whereIn('slug', $slugs);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $builder->whereIn('slug', $slugs) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloq...uent\Relations\Relation.
Loading history...
32
        }
33
34
        return $builder->where(function (Builder $builder) use ($nums, $slugs) {
35
            $builder
36 36
                ->whereIn('id', $nums)
37 36
                ->orWhereIn('slug', $slugs);
38 36
        });
39
    }
40
41
    /**
42
     * @param  \Helldar\Roles\Models\BaseModel|int|string  $value
43
     *
44
     * @return int|string
45
     */
46 54
    public function getId($value)
47
    {
48 54
        return $value instanceof BaseModel
49
            ? $value->id
50 54
            : $value;
51
    }
52
53
    /**
54
     * @param  array|Arrayable|Collection|int|string  $values
55
     *
56
     * @return array
57
     */
58 54
    protected function map($values): array
59
    {
60
        return array_map(function ($value) {
61 54
            return $this->getId($value);
62 54
        }, $this->toArray($values));
63
    }
64
65 54
    protected function filterNum(array $values): array
66
    {
67
        return array_filter($values, function ($value) {
68 54
            return is_numeric($value);
69 54
        });
70
    }
71
72 54
    protected function filterSlugs(array $values): array
73
    {
74
        return array_filter($values, function ($value) {
75 54
            return ! is_numeric($value);
76 54
        });
77
    }
78
79 54
    protected function toArray($values): array
80
    {
81 54
        if ($this->isArrayable($values)) {
82
            $values = $values->toArray();
83 54
        } elseif (! $this->isArray($values)) {
84 42
            $values = Arr::wrap($values);
85
        }
86
87 54
        return Arr::flatten($values);
88
    }
89
90 54
    protected function isArray($values): bool
91
    {
92 54
        return is_array($values);
93
    }
94
95 54
    protected function isArrayable($values): bool
96
    {
97 54
        return $values instanceof Arrayable || $values instanceof Collection;
98
    }
99
}
100