Queryable::getQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Created by enea dhack - 29/12/2019 12:46.
4
 */
5
6
namespace Vaened\Searcher;
7
8
use Closure;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Support\Collection;
11
12
abstract class Queryable
13
{
14
    private ?Query $query = null;
15
16
    abstract protected function builder(): Builder;
17
18
    protected function apply(Constraint $constraint): void
19
    {
20
        $this->getQuery()->push($constraint);
21
    }
22
23
    protected function removeNamedBinding(string $name): void
24
    {
25
        $this->getQuery()->attach(fn(Builder $builder) => $builder->withoutGlobalScope($name));
26
    }
27
28
    protected function getQuery(): Query
29
    {
30
        return $this->query ??= $this->createQueryBuilder();
31
    }
32
33
    private function createQueryBuilder(): Query
34
    {
35
        $bindings = $this->getBootableTraitsBindings();
36
        return new Query($this->builder(), $bindings->merge($this->defaultBindings())->toArray());
0 ignored issues
show
Bug introduced by
$this->defaultBindings() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        return new Query($this->builder(), $bindings->merge(/** @scrutinizer ignore-type */ $this->defaultBindings())->toArray());
Loading history...
37
    }
38
39
    private function getBootableTraitsBindings(): Collection
40
    {
41
        $methods = $this->filterBootableTraits(static::class, fn(string $name) => "get{$name}DefaultBindings");
42
        return collect($methods)->map(fn(string $method): array => call_user_func([$this, $method]))->flatten();
0 ignored issues
show
Bug introduced by
$methods of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        return collect(/** @scrutinizer ignore-type */ $methods)->map(fn(string $method): array => call_user_func([$this, $method]))->flatten();
Loading history...
43
    }
44
45
    private function filterBootableTraits(string $class, Closure $buildFunctionName): array
46
    {
47
        $booted = [];
48
49
        foreach (class_uses_recursive($class) as $trait) {
50
            $method = $buildFunctionName(class_basename($trait));
51
            if (method_exists($class, $method) && ! in_array($method, $booted)) {
52
                $booted[] = $method;
53
            }
54
        }
55
56
        return $booted;
57
    }
58
59
    protected function defaultBindings(): array
60
    {
61
        return [];
62
    }
63
}
64