Completed
Push — master ( c35f40...b4f983 )
by Nazar
03:58
created

SQLite::s_internal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs\DB;
9
class SQLite extends _Abstract {
10
	/**
11
	 * @var \SQLite3 Instance of DB connection
12
	 */
13
	protected $instance;
14
	/**
15
	 * @param string $database Ignored for SQLite
16
	 * @param string $user     Ignored for SQLite
17
	 * @param string $password Ignored for SQLite
18
	 * @param string $host     Path to database file, relatively to website root or absolute
19
	 * @param string $charset  Ignored for SQLite
20
	 * @param string $prefix
21
	 */
22
	function __construct ($database, $user = '', $password = '', $host = '', $charset = '', $prefix = '') {
23
		$start = microtime(true);
24
		try {
25
			$this->instance        = @new \SQLite3($host);
26
			$this->database        = $database;
27
			$this->connected       = true;
28
			$this->connecting_time = microtime(true) - $start;
29
			$this->db_type         = 'sqlite';
30
			$this->prefix          = $prefix;
31
		} catch (\Exception $e) {
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
32
		}
33
	}
34
	function q ($query, $params = [], $_ = null) {
35
		// Hack to convert small subset of MySQL queries into SQLite-compatible syntax
36
		$query = str_replace('INSERT IGNORE', 'INSERT OR IGNORE', $query);
37
		return call_user_func_array([__NAMESPACE__.'\\_Abstract', 'q'], [$query] + func_get_args());
38
	}
39
	/**
40
	 * @inheritdoc
41
	 *
42
	 * @return bool|\SQLite3Result
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use false|\SQLite3Result.

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...
43
	 */
44
	protected function q_internal ($query) {
45
		if (!$query) {
46
			return false;
47
		}
48
		return $this->instance->query($query);
49
	}
50
	/**
51
	 * @inheritdoc
52
	 */
53
	protected function q_multi_internal ($query) {
54
		$result = true;
55
		foreach ($query as $q) {
56
			$result = $result && $this->q_internal($q);
57
		}
58
		return $result;
59
	}
60
	/**
61
	 * @inheritdoc
62
	 */
63
	function n ($query_result) {
64
		if (is_object($query_result)) {
65
			/**
66
			 * @var \SQLite3Result $query_result
67
			 */
68
			$n = 0;
69
			$query_result->reset();
70
			while ($query_result->fetchArray()) {
71
				++$n;
72
			}
73
			$query_result->reset();
74
			return $n;
75
		} else {
76
			return false;
77
		}
78
	}
79
	/**
80
	 * @inheritdoc
81
	 *
82
	 * @param false|\SQLite3Result $query_result
83
	 */
84
	function f ($query_result, $single_column = false, $array = false, $indexed = false) {
85
		if (!is_object($query_result)) {
86
			return false;
87
		}
88
		$result_type = $single_column || $indexed ? SQLITE3_NUM : SQLITE3_ASSOC;
89
		if ($array) {
90
			$result = [];
91
			while ($current = $query_result->fetchArray($result_type)) {
92
				$result[] = $single_column ? $current[0] : $current;
93
			}
94
			$this->free($query_result);
95
			return $result;
96
		}
97
		$result = $query_result->fetchArray($result_type);
98
		return $single_column && $result ? $result[0] : $result;
99
	}
100
	/**
101
	 * @inheritdoc
102
	 */
103
	function id () {
104
		return $this->instance->lastInsertRowID();
105
	}
106
	/**
107
	 * @inheritdoc
108
	 */
109
	function affected () {
110
		return $this->instance->changes();
111
	}
112
	/**
113
	 * @inheritdoc
114
	 *
115
	 * @param false|\SQLite3Result $query_result
116
	 */
117
	function free ($query_result) {
118
		if (is_object($query_result)) {
119
			$query_result->finalize();
120
		}
121
		return true;
122
	}
123
	/**
124
	 * @inheritdoc
125
	 */
126
	function columns ($table, $like = false) {
127
		if (!$table) {
128
			return false;
129
		}
130
		$columns = $this->qfa("PRAGMA table_info(`$table`)");
131
		foreach ($columns as &$column) {
0 ignored issues
show
Bug introduced by
The expression $columns of type false|array<integer,arra...g|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
132
			$column = $column['name'];
133
		}
134
		/**
135
		 * Only support the most common cases
136
		 */
137
		if ($like) {
138
			if (substr($like, -1) == '%') {
139
				$like = substr($like, 0, -1);
140
				return array_filter(
141
					$columns,
142
					function ($column) use ($like) {
143
						return strpos($column, $like) === 0;
144
					}
145
				);
146
			} else {
147
				trigger_error("Can't get columns like $like, SQLite engine doesn't support such conditions", E_USER_WARNING);
148
				return [];
149
			}
150
		}
151
		return $columns;
152
	}
153
	/**
154
	 * @inheritdoc
155
	 */
156
	function tables ($like = false) {
157
		if ($like) {
158
			$like = $this->s($like);
1 ignored issue
show
Documentation introduced by
$like is of type boolean, but the function expects a string|array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
			return $this->qfas(
160
				"SELECT `name`
161
				FROM `sqlite_master`
162
				WHERE
163
					`type` = 'table' AND
164
					`name` != 'sqlite_sequence' AND
165
					`name` LIKE $like"
166
			);
167
		} else {
168
			return $this->qfas(
169
				"SELECT `name`
170
				FROM `sqlite_master`
171
				WHERE
172
					`type` = 'table' AND
173
					`name` != 'sqlite_sequence'"
174
			);
175
		}
176
	}
177
	/**
178
	 * @inheritdoc
179
	 */
180
	protected function s_internal ($string, $single_quotes_around) {
181
		$return = \SQLite3::escapeString($string);
182
		return $single_quotes_around ? "'$return'" : $return;
183
	}
184
	/**
185
	 * @inheritdoc
186
	 */
187
	function server () {
188
		return \SQLite3::version()['versionString'];
189
	}
190
	/**
191
	 * @inheritdoc
192
	 */
193
	function __destruct () {
194
		if ($this->connected && is_object($this->instance)) {
195
			$this->instance->close();
196
			$this->connected = false;
197
		}
198
	}
199
}
200