Completed
Push — master ( bb7d96...d4201d )
by Tomáš
01:30
created

Helper::dumpSql()   C

Complexity

Conditions 14
Paths 1

Size

Total Lines 67
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 59.2805

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 17
cts 44
cp 0.3864
rs 5.8135
c 0
b 0
f 0
cc 14
eloc 49
nc 1
nop 3
crap 59.2805

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Portiny\Doctrine\Adapter\Nette\Tracy;
4
5
use Doctrine\DBAL\Connection;
6
use Nette\Utils\Strings;
7
8
final class Helper
9
{
10
	/**
11
	 * Returns syntax highlighted SQL command.
12
	 */
13 2
	public static function dumpSql(string $sql, ?array $params = NULL, ?Connection $connection = NULL): string
14
	{
15 2
		static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|'
16
			. 'DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|'
17
			. 'INNER\s+JOIN|TRUNCATE';
18 2
		static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|[RI]?LIKE|'
19
			. 'REGEXP|TRUE|FALSE';
20
21
		// insert new lines
22 2
		$sql = " ${sql} ";
23
		$sql = preg_replace("#(?<=[\\s,(])(${keywords1})(?=[\\s,)])#i", "\n\$1", $sql);
24
		// reduce spaces
25 2
		$sql = preg_replace('#[ \t]{2,}#', ' ', $sql);
26 2
		$sql = wordwrap($sql, 100);
27 2
		$sql = preg_replace('#([ \t]*\r?\n){2,}#', "\n", $sql);
28
		// syntax highlight
29 2
		$sql = htmlspecialchars($sql, ENT_IGNORE, 'UTF-8');
30
		$closure = function ($matches) {
31 2
			if (! empty($matches[1])) { // comment
32
				return '<em style="color:gray">' . $matches[1] . '</em>';
33 2
			} elseif (! empty($matches[2])) { // error
34
				return '<strong style="color:red">' . $matches[2] . '</strong>';
35 2
			} elseif (! empty($matches[3])) { // most important keywords
36 2
				return '<strong style="color:blue">' . $matches[3] . '</strong>';
37
			} elseif (! empty($matches[4])) { // other keywords
38
				return '<strong style="color:green">' . $matches[4] . '</strong>';
39
			}
40 2
		};
41 2
		$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])(${keywords1})(?=[\\s,)])|'
42 2
			. '(?<=[\\s,(=])(${keywords2})(?=[\\s,)=])#is", $closure, $sql);
43
44
		// parameters
45
		$sql = preg_replace_callback('#\?#', function () use ($params, $connection) {
46
			static $i = 0;
47
			if (! isset($params[$i])) {
48
				return '?';
49
			}
50
51
			$param = $params[$i++];
52
			if (is_string($param)
53
				&& (preg_match('#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u', $param) || preg_last_error())
54
			) {
55
				return '<i title="Length ' . strlen($param) . ' bytes">&lt;binary&gt;</i>';
56
			} elseif (is_string($param)) {
57
				$length = Strings::length($param);
58
				$truncated = Strings::truncate($param, 120);
59
				$text = htmlspecialchars(
60
					$connection ? $connection->quote($truncated) : '\'' . $truncated . '\'',
61
					ENT_NOQUOTES,
62
					'UTF-8'
63
				);
64
				return '<span title="Length ' . $length . ' characters">' . $text . '</span>';
65
			} elseif (is_resource($param)) {
66
				$type = get_resource_type($param);
67
				if ($type === 'stream') {
68
					$info = stream_get_meta_data($param);
69
					return '<i' . (isset($info['uri']) ? ' title="' .
70
							htmlspecialchars($info['uri'], ENT_NOQUOTES, 'UTF-8') . '"' : NULL)
71
						. '>&lt;' . htmlspecialchars($type, ENT_NOQUOTES, 'UTF-8') . ' resource&gt;</i> ';
72
				}
73
			}
74
75
			return htmlspecialchars((string) $param, ENT_NOQUOTES, 'UTF-8');
76 2
		}, $sql);
77
78 2
		return '<pre class="dump">' . trim($sql) . "</pre>\n";
79
	}
80
}
81