Test Failed
Push — master ( ba1fe5...03a684 )
by Mikael
02:03
created

DatabaseQueryBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 6 1
A setDefaultsFromConfiguration() 0 9 2
A execute() 0 16 3
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 void
43
     */
44
    public function configure($what)
45
    {
46
        $this->loadConfiguration($what);
47
        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
        $this->setDefaultsFromConfiguration();
49
    }
50
51
52
53
    /**
54
     * Update builder settings from active configuration.
55
     *
56
     * @return void
57
     */
58
    private function setDefaultsFromConfiguration()
59
    {
60
        if ($this->options['dsn']) {
61
            $dsn = explode(':', $this->options['dsn']);
62
            $this->setSQLDialect($dsn[0]);
63
        }
64
65
        $this->setTablePrefix($this->options['table_prefix']);
66
    }
67
68
69
70
    /**
71
     * Execute a SQL-query and ignore the resultset.
72
     *
73
     * @param string $query  the SQL statement
74
     * @param array  $params the params array
75
     *
76
     * @return boolean returns TRUE on success or FALSE on failure.
77
     */
78
    public function execute($query = null, $params = [])
79
    {
80
        if (is_array($query)) {
81
            $params = $query;
82
            $query = null;
83
        }
84
85
        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...
86
            $query = $this->getSQL();
87
        }
88
89
        var_dump($query);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($query); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
90
        var_dump($params);
91
92
        return parent::execute($query, $params);
93
    }
94
}
95