Passed
Push — master ( 48be7a...5232a2 )
by Ron
01:36
created

TableNameBuilder::buildTableName()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 34
rs 6.9666
c 0
b 0
f 0
cc 12
nc 16
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 Kir\MySQL\Databases\MySQL;
5
use Kir\MySQL\Tools\VirtualTable;
6
7
trait TableNameBuilder {
8
	use AbstractAliasReplacer;
9
10
	/**
11
	 * @param string $alias
12
	 * @param string|array|object $name
13
	 * @return string
14
	 */
15
	protected function buildTableName($alias, $name) {
16
		if(is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) {
17
			$name = (string) $name;
18
			$lines = explode("\n", $name);
19
			foreach($lines as &$line) {
20
				$line = "\t{$line}";
21
			}
22
			$name = join("\n", $lines);
23
			$name = '(' . trim(rtrim(trim($name), ';')) . ')';
24
		}
25
		if(is_array($name)) {
26
			$parts = [];
27
			foreach($name as $index => $bucket) {
28
				if(ctype_digit((string) $index) && is_scalar($bucket)) {
29
					$parts[] = "SELECT {$this->db()->quote($bucket)} AS {$this->db()->quoteField('value')}";
30
				} else {
31
					$values = [];
32
					foreach($bucket as $field => $value) {
33
						$values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field));
34
					}
35
					$parts[] = sprintf("SELECT %s", join(', ', $values));
36
				}
37
			}
38
			$name = '(' . join("\n\tUNION\n\t", $parts) . ')';
39
		}
40
		if($this->db()->getVirtualTables()->has($name)) {
41
			$select = (string) $this->db()->getVirtualTables()->get($name);
42
			$name = sprintf('(%s)', join("\n\t", explode("\n", trim($select))));
43
		}
44
		$name = $this->aliasReplacer()->replace($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type object; however, parameter $name of Kir\MySQL\Tools\AliasReplacer::replace() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
		$name = $this->aliasReplacer()->replace(/** @scrutinizer ignore-type */ $name);
Loading history...
45
		if($alias !== null) {
0 ignored issues
show
introduced by
The condition $alias !== null is always true.
Loading history...
46
			return sprintf("%s %s", $name, $alias);
47
		}
48
		return $name;
49
	}
50
	
51
	/**
52
	 * @return MySQL
53
	 */
54
	abstract protected function db();
55
}
56