Passed
Push — master ( 9e2434...486cd6 )
by Ron
01:59
created

MySQLValueQuoter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 16
c 1
b 0
f 0
dl 0
loc 35
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B quote() 0 29 9
1
<?php
2
namespace Kir\MySQL\Databases\MySQL;
3
4
use Kir\MySQL\Builder\DBExpr;
5
use Kir\MySQL\Builder\Select;
6
use PDO;
7
use phpDocumentor\Reflection\Types\Scalar;
8
9
class MySQLValueQuoter {
10
	/**
11
	 * @param PDO $pdo
12
	 * @param null|scalar|array<int, null|scalar>|DBExpr|Select $value
13
	 * @return string
14
	 */
15
	public static function quote(PDO $pdo, $value): string {
16
		if(is_null($value)) {
17
			return 'NULL';
18
		}
19
20
		if(is_bool($value)) {
0 ignored issues
show
introduced by
The condition is_bool($value) is always false.
Loading history...
21
			return $value ? '1' : '0';
22
		}
23
24
		if(is_array($value)) {
25
			$fn = static function ($value) use ($pdo) {
26
				return self::quote($pdo, $value);
27
			};
28
			return implode(', ', array_map($fn, $value));
29
		}
30
31
		if($value instanceof DBExpr) {
32
			return $value->getExpression();
33
		}
34
35
		if($value instanceof Select) {
36
			return sprintf('(%s)', (string) $value);
37
		}
38
39
		if(is_int($value) || is_float($value)) {
0 ignored issues
show
introduced by
The condition is_float($value) is always false.
Loading history...
40
			return (string) $value;
41
		}
42
43
		return $pdo->quote($value);
44
	}
45
}
46