Passed
Push — main ( 2a9e98...85995a )
by Sammy
18:18
created

Where::andIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace HexMakina\Crudites\Grammar\Clause;
4
5
use HexMakina\Crudites\Grammar\Predicate;
6
7
class Where extends Clause
8
{
9
    protected array $and = [];
10
11
    public function __construct(array $predicates = null)
12
    {
13
        if ($predicates !== null) {
14
            foreach ($predicates as $predicate) {
15
                if (is_string($predicate))
16
                    $this->and($predicate);
17
                else
18
                    $this->and($predicate, $predicate->bindings());
19
            }
20
        }
21
    }
22
23
    public function __toString(): string
24
    {
25
        if (empty($this->and)) {
26
            return '';
27
        }
28
29
        return 'WHERE ' . implode(' AND ', $this->and);
30
    }
31
32
    public function name(): string
33
    {
34
        return self::WHERE;
35
    }
36
37
    public function and(string $predicate, $bindings = [])
38
    {
39
        $this->and[] = $predicate;
40
41
        if (!empty($bindings)) {
42
            $this->bindings = array_merge($this->bindings, $bindings);
43
        }
44
45
        return $this;
46
    }
47
48
    public function andPredicate(Predicate $predicate)
49
    {
50
        return $this->and($predicate->__toString(), $predicate->bindings());
51
    }
52
53
54
    public function andIsNull($expression)
55
    {
56
        return $this->and(new Predicate($expression, 'IS NULL'));
57
    }
58
59
    public function andFields(array $assoc_data, $table_name = null, $operator = '=')
60
    {
61
        foreach ($assoc_data as $field => $value) {
62
            $column = $table_name === null ? [$field] : [$table_name, $field];
63
            $predicate = (new Predicate($column, $operator))->withValue($value, __FUNCTION__ .'_'.implode('_', $column));
64
65
            $this->and($predicate, $predicate->bindings());
66
        }
67
68
        return $this;
69
    }
70
71
    public function andValue($expression, $operator, $value, $bind_label = null)
72
    {
73
        return $this->andPredicate((new Predicate($expression, $operator))->withValue($value, $bind_label), $bind_label);
0 ignored issues
show
Unused Code introduced by
The call to HexMakina\Crudites\Gramm...e\Where::andPredicate() has too many arguments starting with $bind_label. ( Ignorable by Annotation )

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

73
        return $this->/** @scrutinizer ignore-call */ andPredicate((new Predicate($expression, $operator))->withValue($value, $bind_label), $bind_label);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74
    }
75
76
    public function andIn($expression, array $values)
77
    {
78
        return $this->andPredicate((new Predicate($expression, 'IN'))->withValues($values, __FUNCTION__));
79
    }
80
81
    public function andLike($expression, string $value)
82
    {
83
        return $this->andPredicate((new Predicate($expression, 'LIKE'))->withValue($value, __FUNCTION__));
84
    }
85
}
86