Column::getParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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