Completed
Push — master ( 6c70a6...40fa3c )
by Mikael
01:27
created

DatabaseQueryBuilder::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Anax\Database;
4
5
use \Anax\Common\ConfigureInterface;
6
use \Anax\Common\ConfigureTrait;
7
use \Anax\Database\Exception\BuildException;
8
use \Anax\Database\QueryBuilderTrait;
9
10
/**
11
 * Build SQL queries by method calling.
12
 */
13
class DatabaseQueryBuilder extends Database implements ConfigureInterface
14
{
15
    use QueryBuilderTrait,
16
        ConfigureTrait {
17
        configure as protected loadConfiguration;
18
    }
19
20
21
22
    /**
23
     * Constructor creating a PDO object connecting to a choosen database.
24
     *
25
     * @param array $options containing details for connecting to the database.
26
     */
27
    public function __construct($options = [])
28
    {
29
        parent::__construct($options);
30
        $this->setDefaultsFromConfiguration();
31
    }
32
33
34
35
    /**
36
     * Load and apply configurations.
37
     *
38
     * @param array|string $what is an array with key/value config options
39
     *                           or a file to be included which returns such
40
     *                           an array.
41
     *
42
     * @return self
43
     */
44 1
    public function configure($what)
45
    {
46 1
        $this->loadConfiguration($what);
47 1
        parent::setOptions($this->config);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setOptions() instead of configure()). Are you sure this is correct? If so, you might want to change this to $this->setOptions().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
48 1
        $this->setDefaultsFromConfiguration();
49 1
        return $this;
50
    }
51
52
53
54
    /**
55
     * Update builder settings from active configuration.
56
     *
57
     * @return void
58
     */
59 1
    private function setDefaultsFromConfiguration()
60
    {
61 1
        if ($this->options['dsn']) {
62
            $dsn = explode(':', $this->options['dsn']);
63
            $this->setSQLDialect($dsn[0]);
64
        }
65
66 1
        $this->setTablePrefix($this->options['table_prefix']);
67 1
    }
68
69
70
71
    /**
72
     * Execute a SQL-query and ignore the resultset.
73
     *
74
     * @param string $query  the SQL statement
75
     * @param array  $params the params array
76
     *
77
     * @return boolean returns TRUE on success or FALSE on failure.
78
     */
79 3
    public function execute($query = null, $params = [])
80
    {
81 3
        if (is_array($query)) {
82 1
            $params = $query;
83 1
            $query = null;
84 1
        }
85
86 3
        if (!$query) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
87 3
            $query = $this->getSQL();
88 3
        }
89
90 3
        return parent::execute($query, $params);
91
    }
92
}
93