TableNameBuilder::buildTableName()   C
last analyzed

Complexity

Conditions 15
Paths 21

Size

Total Lines 38
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
c 2
b 0
f 0
dl 0
loc 38
rs 5.9166
cc 15
nc 21
nop 2

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