Completed
Push — master ( d8c240...9dd0a9 )
by Jared
02:18
created

Executable::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Operations;
12
13
trait Executable
14
{
15
    /**
16
     * @var PDO
17
     */
18
    protected $pdo;
19
20
    /**
21
     * @var int
22
     */
23
    protected $rowCount;
24
25
    /**
26
     * Sets the PDO instance used by this query.
27
     *
28
     * @param PDO $pdo
29
     *
30
     * @return self
31
     */
32
    public function setPDO($pdo)
33
    {
34
        $this->pdo = $pdo;
35
36
        return $this;
37
    }
38
39
    /** 
40
     * Gets the PDO instance used by this query.
41
     *
42
     * @return PDO
43
     */
44
    public function getPDO()
45
    {
46
        return $this->pdo;
47
    }
48
49
    /**
50
     * Executes a query.
51
     *
52
     * @return PDOStatement|bool result
53
     */
54
    public function execute()
55
    {
56
        $stmt = $this->pdo->prepare($this->build());
0 ignored issues
show
Bug introduced by
It seems like build() 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
58
        if ($stmt->execute($this->getValues())) {
0 ignored issues
show
Bug introduced by
It seems like getValues() 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...
59
            $this->rowCount = $stmt->rowCount();
60
61
            return $stmt;
62
        } else {
63
            return false;
64
        }
65
    }
66
67
    /**
68
     * Returns the number of rows affected by the last executed statement.
69
     *
70
     * @return int
71
     */
72
    public function rowCount()
73
    {
74
        return $this->rowCount;
75
    }
76
}
77