Passed
Push — master ( dec884...1fd155 )
by Bernardette
02:01
created

SimpleQueryColorizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Rostenkowski\Doctrine\Debugger;
4
5
6
class SimpleQueryColorizer
7
{
8
9
	private static $keywords = [
10
		'DROP TABLE',
11
		'CREATE TABLE',
12
		'PRAGMA',
13
		'SELECT',
14
		'FROM',
15
		'WHERE',
16
		'ORDER BY',
17
		'GROUP BY',
18
		'LEFT JOIN',
19
		'INNER JOIN',
20
		'UNION ALL',
21
		'AND',
22
		'OR',
23
		'UPDATE',
24
		'INSERT',
25
		'DELETE',
26
		'START TRANSACTION',
27
		'COMMIT',
28
		'INTO',
29
		'SET',
30
		'VALUES',
31
		'DEFAULT',
32
		'PRIMARY KEY',
33
		'VARCHAR',
34
		'INTEGER',
35
		'TEXT',
36
		'IN',
37
		'IS',
38
		'NULL',
39
		'NOT NULL',
40
	];
41
42
	private static $keywordRegex;
43
44
45
	public function __construct()
46
	{
47
		self::$keywordRegex = '/\b(' . implode('|', self::$keywords) . ')\b/';
48
	}
49
50
51
	public function colorize(string $sql): string
52
	{
53
		// determine specific query type
54
		$type = strtolower(substr($sql, 0, 6));
55
		if (!in_array($type, ['select', 'update', 'delete', 'insert'])) {
56
			$type = '';
57
		}
58
		$template = '<span class="reserved">$1</span>';
59
		$sql = preg_replace(self::$keywordRegex, $template, $sql);
60
61
		return "<div class='query $type'>$sql</div>";
62
	}
63
}
64