ConditionBuilder::build()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 24
rs 9.1111
c 1
b 0
f 0
cc 6
nc 4
nop 4
1
<?php
2
3
namespace Kir\MySQL\Builder\Internal;
4
5
use Kir\MySQL\Database;
6
use Stringable;
7
8
/**
9
 * @phpstan-import-type DBParameterValueType from Types
10
 * @phpstan-import-type DBWhereExpressionType from Types
11
 */
12
final class ConditionBuilder {
13
	/**
14
	 * @param Database $db
15
	 * @param string $query
16
	 * @param array<int, array{DBWhereExpressionType, list<DBParameterValueType>}> $conditions
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, array{DBWhere...DBParameterValueType>}> at position 6 could not be parsed: Expected ':' at position 6, but found 'DBWhereExpressionType'.
Loading history...
17
	 * @param string $token
18
	 * @return string
19
	 */
20
	public static function build(Database $db, string $query, array $conditions, string $token): string {
21
		if(!count($conditions)) {
22
			return $query;
23
		}
24
		$query .= "{$token}\n";
25
		$arr = [];
26
		foreach($conditions as [$expression, $arguments]) {
27
			if(is_array($expression)) {
28
				foreach($expression as $key => $value) {
29
					$key = self::formatKey($key);
30
					if($value === null) {
31
						$arr = self::buildCondition($arr, "ISNULL({$key})", [$value], $db);
32
					} else {
33
						$arr = self::buildCondition($arr, "{$key}=?", [$value], $db);
34
					}
35
				}
36
			} else {
37
				/** @var Stringable|string $expression */
38
				$arr = self::buildCondition($arr, (string) $expression, $arguments, $db);
39
			}
40
		}
41
		$query .= implode("\n\tAND\n", $arr);
42
43
		return "{$query}\n";
44
	}
45
46
	/**
47
	 * @param string[] $conditions
48
	 * @param string $expression
49
	 * @param list<DBParameterValueType> $arguments
0 ignored issues
show
Bug introduced by
The type Kir\MySQL\Builder\Internal\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
	 * @param Database $db
51
	 * @return string[]
52
	 */
53
	private static function buildCondition(array $conditions, string $expression, array $arguments, Database $db): array {
54
		$expr = $db->quoteExpression($expression, $arguments);
55
		$conditions[] = "\t({$expr})";
56
57
		return $conditions;
58
	}
59
60
	/**
61
	 * @param string $key
62
	 * @return string
63
	 */
64
	private static function formatKey(string $key): string {
65
		if(strpos($key, '`') !== false || strpos($key, '(') !== false) {
66
			return $key;
67
		}
68
		$keyParts = explode('.', $key);
69
		$fn = static fn(string $part) => "`{$part}`";
70
		$enclosedKeyParts = array_map($fn, $keyParts);
71
72
		return implode('.', $enclosedKeyParts);
73
	}
74
}
75