Passed
Push — master ( 9c833b...4586fd )
by Zing
09:40 queued 04:04
created

DateFilter::withPropertyConstraint()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 3
nop 3
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 4
rs 9.9
c 0
b 0
f 0
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
        if (is_array($value)) {
20 1
            $value = array_map(
21 1
                function ($value) {
22 1
                    if ($value instanceof DateTimeInterface) {
23 1
                        return $value->format('Y-m-d');
24
                    }
25
26 1
                    return $value;
27 1
                },
28
                $value
29
            );
30
31 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...
32
        }
33
34 1
        if ($value instanceof DateTimeInterface) {
35 1
            $value = $value->format('Y-m-d');
36
        }
37
38 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...
39
    }
40
}
41