TableNameBuilder   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 28
c 1
b 0
f 0
dl 0
loc 47
rs 10

1 Method

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