Expression   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 26
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A getParams() 0 3 1
A __construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Expression;
6
7
use Stringable;
8
use Yiisoft\Db\Connection\ConnectionInterface;
9
10
/**
11
 * Represents a DB expression that doesn't need escaping or quoting.
12
 *
13
 * When an Expression object is embedded within a SQL statement or fragment, it will be replaced with the
14
 * {@see expression} property value without any DB escaping or quoting.
15
 *
16
 * For example,
17
 *
18
 * ```php
19
 * $expression = new Expression('NOW()');
20
 * $now = (new \Yiisoft\Db\Query\Query)->select($expression)->scalar();  // SELECT NOW();
21
 * echo $now; // prints the current date
22
 * ```
23
 *
24
 * Expression objects are mainly created for passing raw SQL expressions to methods of
25
 * {@see \Yiisoft\Db\Query\QueryInterface} and related classes.
26
 *
27 202
 * @psalm-import-type ParamsType from ConnectionInterface
28
 */
29 202
class Expression implements ExpressionInterface, Stringable
30 202
{
31 202
    /**
32
     * @psalm-param ParamsType $params
33
     */
34
    public function __construct(private string $expression, private array $params = [])
35
    {
36
    }
37
38 369
    /**
39
     * @return string The expression.
40 369
     */
41
    public function __toString(): string
42
    {
43
        return $this->expression;
44
    }
45
46
    /**
47
     * @return array List of parameters to bind to this expression. The keys are placeholders appearing in
48
     * {@see expression} and the values are the corresponding parameter values.
49
     *
50 333
     * @psalm-return ParamsType
51
     */
52 333
    public function getParams(): array
53
    {
54
        return $this->params;
55
    }
56
}
57