Column   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 46
ccs 15
cts 15
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParams() 0 7 2
A __construct() 0 4 1
A __toString() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace QB\Generic\Clause;
6
7
use QB\Generic\IQueryPart;
8
use QB\Generic\Statement\Select;
9
10
class Column implements IColumn
11
{
12
    protected IQueryPart|string $expr;
13
14
    protected ?string $alias = null;
15
16
    /**
17
     * Expr constructor.
18
     *
19
     * @param IQueryPart|string $expr column name or expression to be used
20
     * @param string|null       $alias
21
     */
22 47
    public function __construct(IQueryPart|string $expr, ?string $alias = null)
23
    {
24 47
        $this->expr  = $expr;
25 47
        $this->alias = $alias;
26 47
    }
27
28
    /**
29
     * @return string
30
     */
31 37
    public function __toString(): string
32
    {
33 37
        $expr = is_string($this->expr) ? $this->expr : (string)$this->expr;
34
35 37
        if ($this->expr instanceof Select) {
36 4
            $expr = '(' . str_replace(PHP_EOL, ' ', $expr) . ')';
37
        }
38
39 37
        if ($this->alias === null) {
40 32
            return $expr;
41
        }
42
43 10
        return sprintf('%s AS %s', $expr, $this->alias);
44
    }
45
46
    /**
47
     * @return array
48
     */
49 8
    public function getParams(): array
50
    {
51 8
        if (is_string($this->expr)) {
52 1
            return [];
53
        }
54
55 7
        return $this->expr->getParams();
56
    }
57
}
58