TableNameBuilder::buildTableName()   C
last analyzed

Complexity

Conditions 14
Paths 17

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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