TableNameBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 26
c 1
b 0
f 0
dl 0
loc 46
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C buildTableName() 0 32 12
1
<?php
2
namespace Kir\MySQL\Builder\Traits;
3
4
use Kir\MySQL\Builder\Internal\Types;
5
use Kir\MySQL\Database;
6
use Kir\MySQL\Tools\VirtualTable;
7
8
/**
9
 * @phpstan-import-type DBTableNameType from Types
10
 */
11
trait TableNameBuilder {
12
	use AbstractAliasReplacer;
13
14
	/**
15
	 * @param string|null $alias
16
	 * @param DBTableNameType $name
0 ignored issues
show
Bug introduced by
The type Kir\MySQL\Builder\Traits\DBTableNameType 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...
17
	 * @return string
18
	 */
19
	protected function buildTableName(?string $alias, $name): string {
20
		if(is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) {
21
			$name = (string) $name;
22
			$lines = explode("\n", $name);
23
			$lines = array_map(static fn(string $line) => "\t{$line}", $lines);
24
			$name = implode("\n", $lines);
25
			$name = '(' . trim(rtrim(trim($name), ';')) . ')';
26
		}
27
		if(is_array($name)) {
28
			$parts = [];
29
			foreach($name as /*$index => */$bucket) {
30
				if(is_scalar($bucket)/* && ctype_digit((string) $index)*/) {
31
					$parts[] = "SELECT {$this->db()->quote($bucket)} AS {$this->db()->quoteField('value')}";
32
				} else {
33
					$values = [];
34
					foreach($bucket as $field => $value) {
35
						$values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field));
36
					}
37
					$parts[] = sprintf("SELECT %s", implode(', ', $values));
38
				}
39
			}
40
			$name = '(' . implode("\n\tUNION ALL\n\t", $parts) . ')';
41
		}
42
		if((is_string($name) || $name instanceof VirtualTable) && $this->db()->getVirtualTables()->has($name)) {
43
			$select = (string) $this->db()->getVirtualTables()->get($name);
44
			$name = sprintf('(%s)', implode("\n\t", explode("\n", trim($select))));
45
		}
46
		$name = $this->aliasReplacer()->replace((string) $name);
47
		if($alias !== null) {
48
			return sprintf("%s %s", $name, $alias);
49
		}
50
		return $name;
51
	}
52
53
	/**
54
	 * @return Database
55
	 */
56
	abstract protected function db();
57
}
58