Completed
Pull Request — 3.x (#165)
by
unknown
02:18
created

WhereTrait::extractNameOrThrow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2.032
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 */
9
namespace Aura\SqlQuery\Common;
10
11
use Aura\SqlQuery\Exception;
12
13
/**
14
 *
15
 * Common code for WHERE clauses.
16
 *
17
 * @package Aura.SqlQuery
18
 *
19
 */
20
trait WhereTrait
21
{
22
    /**
23
     *
24
     * Adds a WHERE condition to the query by AND.
25
     *
26
     * @param string $cond The WHERE condition.
27
     *
28
     * @param array $bind Values to be bound to placeholders
29
     *
30
     * @return $this
31
     *
32
     */
33 65
    public function where($cond, array $bind = [])
34
    {
35 65
        $this->addClauseCondWithBind('where', 'AND', $cond, $bind);
0 ignored issues
show
Bug introduced by
It seems like addClauseCondWithBind() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36 65
        return $this;
37
    }
38
39
    /**
40
     *
41
     * Adds a WHERE condition to the query by OR. If the condition has
42
     * ?-placeholders, additional arguments to the method will be bound to
43
     * those placeholders sequentially.
44
     *
45
     * @param string $cond The WHERE condition.
46
     *
47
     * @param array $bind Values to be bound to placeholders
48
     *
49
     * @return $this
50
     *
51
     * @see where()
52
     *
53
     */
54 34
    public function orWhere($cond, array $bind = [])
55
    {
56 34
        $this->addClauseCondWithBind('where', 'OR', $cond, $bind);
0 ignored issues
show
Bug introduced by
It seems like addClauseCondWithBind() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57 34
        return $this;
58
    }
59
60
    /**
61
     *
62
     * Adds a WHERE condition with a prepared statement placeholder and value to the query by AND.
63
     *
64
     * @param string $cond the first part of the WHERE condition without the placeholder. e.g. "name = " or "name IN"
65
     *
66
     * @param string $placeholder the placeholder as a string. e.g. ":NAME" or "(:NAMES)"
67
     *
68
     * @param (string|int|float|array) $value the value to be bound to the placeholder. e.g. "John" or ["John", "Eric", "Michael", "Terry"]
69
     *
70
     * @return $this
71
     *
72
     * @throws Exception
73
     *
74
     */
75 5
    public function whereBoundValue(string $cond, string $placeholder, $value)
76
    {
77 5
        $name = $this->extractNameOrThrow($placeholder);
78 5
        $this->addClauseCondWithBind('where', 'AND', $cond.$placeholder, [ $name => $value ] );
0 ignored issues
show
Bug introduced by
It seems like addClauseCondWithBind() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79 5
        return $this;
80
    }
81
82
    /**
83
     *
84
     * Adds a WHERE condition with a prepared statement placeholder and value to the query by OR.
85
     *
86
     * @param string $cond the first part of the WHERE condition without the placeholder. e.g. "name = " or "name IN"
87
     *
88
     * @param string $placeholder the placeholder as a string. e.g. ":NAME" or "(:NAMES)"
89
     *
90
     * @param (string|int|float|array) $value the value to be bound to the placeholder. e.g. "John" or ["John", "Eric", "Michael", "Terry"]
91
     *
92
     * @return $this
93
     *
94
     * @throws Exception
95
     *
96
     * @see whereBoundValue()
97
     *
98
     */
99
    public function orWhereBoundValue(string $cond, string $placeholder, $value)
100
    {
101
        $name = $this->extractNameOrThrow($placeholder);
102
        $this->addClauseCondWithBind('where', 'OR', $cond.$placeholder, [ $name => $value ] );
0 ignored issues
show
Bug introduced by
It seems like addClauseCondWithBind() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
103
        return $this;
104
    }
105
106
    /**
107
     * Extract the name of PDO placeholders (e.g. "P")  of the form ":P" for simple values and "(:P)" for array values.
108
     *
109
     * @param string $placeholder the placeholder specification
110
     *
111
     * @return the placeholder name as string
112
     *
113
     * @throws Exception
114
     *
115
     */
116 5
    protected static function extractNameOrThrow(string $placeholder) {
117 5
        $name = preg_replace( '/^\(?:([^\)]+)\)?$/', '\1', $placeholder);
118
        // XXX add type checks
119 5
        if (strlen($name)===strlen($placeholder)) {
120
            throw new Exception("Bad placeholder \"$name\"");
121
        }
122 5
        return $name;
123
    }
124
}
125