DateFilter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A withPropertyConstraint() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder\Filters;
6
7
use DateTimeInterface;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Support\Facades\DB;
10
11
class DateFilter extends ExactFilter
12
{
13
    /**
14
     * @param mixed $value
15
     * @param \Illuminate\Database\Query\Expression|string $property
16
     */
17 1
    protected function withPropertyConstraint(Builder $query, $value, $property): Builder
18
    {
19 1
        $formatter = function ($value) {
20 1
            return $value instanceof DateTimeInterface ? $value->format('Y-m-d') : $value;
21
        };
22 1
        if (\is_array($value)) {
23 1
            $value = array_map($formatter, $value);
24
25 1
            return $query->whereIn(DB::raw(sprintf('date(%s)', $property)), $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereIn(I...', $property)), $value) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
26
        }
27
28 1
        $value = $formatter($value);
29
30 1
        return $query->whereDate($property, $value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereDate($property, $value) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
31
    }
32
}
33