Passed
Pull Request — master (#211)
by Arman
04:55 queued 02:00
created

Criteria::sanitizeValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.6
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\Exceptions\DatabaseException;
18
use Quantum\Libraries\Database\Contracts\DbalInterface;
19
20
/**
21
 * Trait Criteria
22
 * @package Quantum\Libraries\Database
23
 */
24
trait Criteria
25
{
26
27
    /**
28
     * @inheritDoc
29
     * @throws DatabaseException
30
     */
31
    public function criteria(string $column, string $operator, $value = null): DbalInterface
32
    {
33
        if (!in_array($operator, $this->operators)) {
34
            throw DatabaseException::operatorNotSupported($operator);
35
        }
36
37
        $this->criterias[] = [$column, $operator, $this->sanitizeValue($value)];
0 ignored issues
show
Bug Best Practice introduced by
The property criterias does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
39
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...kdb\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
40
    }
41
42
    /**
43
     * @inheritDoc
44
     * @throws DatabaseException
45
     */
46
    public function criterias(...$criterias): DbalInterface
47
    {
48
        foreach ($criterias as $criteria) {
49
            if (isset($criteria[0]) && is_array($criteria[0])) {
50
                $this->orCriteria($criteria);
51
            } else {
52
                $this->criteria(...$criteria);
0 ignored issues
show
Bug introduced by
The call to Quantum\Libraries\Databa...ts\Criteria::criteria() has too few arguments starting with operator. ( Ignorable by Annotation )

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

52
                $this->/** @scrutinizer ignore-call */ 
53
                       criteria(...$criteria);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
53
            }
54
        }
55
56
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...kdb\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
57
    }
58
59
    /**
60
     * @inheritDoc
61
     * @throws DatabaseException
62
     */
63
    public function having(string $column, string $operator, string $value = null): DbalInterface
64
    {
65
        if (!in_array($operator, $this->operators)) {
66
            throw DatabaseException::operatorNotSupported($operator);
67
        }
68
69
        $this->havings[] = [$column, $operator, $value];
0 ignored issues
show
Bug Best Practice introduced by
The property havings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
71
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...kdb\Statements\Criteria which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
72
    }
73
74
    /**
75
     * Adds one or more OR criteria in brackets
76
     * @param array $orCriterias
77
     * @throws DatabaseException
78
     */
79
    protected function orCriteria(array $orCriterias)
80
    {
81
        foreach ($orCriterias as $index => $criteria) {
82
            $this->criteria(...$criteria);
0 ignored issues
show
Bug introduced by
The call to Quantum\Libraries\Databa...ts\Criteria::criteria() has too few arguments starting with operator. ( Ignorable by Annotation )

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

82
            $this->/** @scrutinizer ignore-call */ 
83
                   criteria(...$criteria);

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
83
84
            if ($index != array_key_last($orCriterias)) {
85
                $this->criterias[] = 'OR';
0 ignored issues
show
Bug Best Practice introduced by
The property criterias does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
            }
87
        }
88
    }
89
90
    /**
91
     * @param $value
92
     * @return mixed|string|null
93
     */
94
    protected function sanitizeValue($value)
95
    {
96
        if (is_array($value)) {
97
            return array_map(function ($v) {
98
                return is_string($v) ? preg_quote($v, '/') : $v;
99
            }, $value);
100
        }
101
102
        return is_string($value) ? preg_quote($value, '/') : $value;
103
    }
104
}