Completed
Push — master ( c0227e...7c8e25 )
by smiley
02:55
created

StatementAbstract::execute()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 8
nop 3
1
<?php
2
/**
3
 * Class StatementAbstract
4
 *
5
 * @filesource   StatementAbstract.php
6
 * @created      03.06.2017
7
 * @package      chillerlan\Database\Query
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database\Query;
14
15
use chillerlan\Database\Drivers\DBDriverInterface;
16
17
abstract class StatementAbstract implements StatementInterface{
18
19
	/**
20
	 * @var \chillerlan\Database\Drivers\DBDriverInterface
21
	 */
22
	protected $DBDriver;
23
24
	/**
25
	 * @var string
26
	 */
27
	protected $dialect;
28
29
	/**
30
	 * @var \string[]
31
	 */
32
	protected $quotes;
33
34
	/**
35
	 * @var int
36
	 */
37
	protected $limit;
38
39
	/**
40
	 * @var int
41
	 */
42
	protected $offset;
43
44
	/**
45
	 * @var array
46
	 */
47
	protected $bindValues = [];
48
49
	/**
50
	 * @var bool
51
	 */
52
	protected $offsetlimit_bound = false;
53
54
	/**
55
	 * @var bool
56
	 */
57
	protected $multi = false;
58
59
	/**
60
	 * StatementAbstract constructor.
61
	 *
62
	 * @param \chillerlan\Database\Drivers\DBDriverInterface $DBDriver
63
	 * @param string                                         $dialect
64
	 */
65
	public function __construct(DBDriverInterface $DBDriver, string $dialect){
66
		$this->DBDriver = $DBDriver;
67
		$this->dialect  = $dialect;
68
		$this->quotes   = $this->DBDriver->quotes;
0 ignored issues
show
Bug introduced by
Accessing quotes on the interface chillerlan\Database\Drivers\DBDriverInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
	}
70
71
	/**
72
	 * @return string
73
	 */
74
	public function sql():string {
75
		return '--NOT IMPLEMENTED';
76
	}
77
78
	/**
79
	 * @return array
80
	 */
81
	public function bindValues():array{
82
83
		if(!$this->offsetlimit_bound){
84
			if($this->offset){
85
				$this->bindValues[] = $this->offset;
86
			}
87
88
			if($this->limit){
89
				$this->bindValues[] = $this->limit;
90
			}
91
92
			$this->offsetlimit_bound = true;
93
		}
94
95
		return $this->bindValues;
96
	}
97
98
	/**
99
	 * @param string|null   $index
100
	 * @param array|null    $values
101
	 * @param callable|null $callback
102
	 *
103
	 * @return bool|\chillerlan\Database\DBResult
104
	 */
105
	public function execute(string $index = null, array $values = null, $callback = null){
106
107
		return $this->multi || !is_null($values)
108
			? is_callable($callback)
109
				? $this->DBDriver->multi_callback($this->sql(), $values, $callback)
0 ignored issues
show
Bug introduced by
It seems like $values defined by parameter $values on line 105 can also be of type null; however, chillerlan\Database\Driv...rface::multi_callback() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
110
				: $this->DBDriver->multi($this->sql(), !empty($values) ? $values : $this->bindValues())
111
			: $this->DBDriver->prepared($this->sql(), $this->bindValues(), $index);
112
113
	}
114
115
	/**
116
	 * @param string $str
117
	 * @todo ...
118
	 * @return string
119
	 */
120
	protected function quote(string $str):string{
121
		$x = explode('.', $str);
122
123
		$str = $this->quotes[0].implode($this->quotes[1].'.'.$this->quotes[0], $x).$this->quotes[1];
124
		return $str;
125
	}
126
127
	/**
128
	 * @param string $name
129
	 *
130
	 * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
131
	 */
132
	protected function getStatementClass(string $name){
133
		$class = $this->dialect.'\\'.$name;
134
135
		return new $class($this->DBDriver, $this->dialect);
136
	}
137
138
}
139