Skip to content

Commit 13e8b4d

Browse files
authored
Refactor Expression class (#1058)
1 parent 8207ca1 commit 13e8b4d

4 files changed

Lines changed: 13 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@
147147
- Chg #1054: Rename `ArrayExpression` to `ArrayValue`, `JsonExpression` to `JsonValue`,
148148
`StructuredExpression` to `StructuredValue` (@Tigrov)
149149
- Chg #1056: Remove unused exceptions (@Tigrov)
150+
- Chg #1058: Refactor `Expression` class: declare `$expression` and `$params` as public readonly properties, remove
151+
`getParams()` method (@vjik)
150152

151153
## 1.3.0 March 21, 2024
152154

UPGRADE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,5 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
280280
- Rename `StructuredExpressionBuilder` to `StructuredValueBuilder` and move it to `Yiisoft\Db\Expression\Value\Builder`
281281
namespace;
282282
- Remove `StaleObjectException`, `UnknownMethodException`, `UnknownPropertyException` classes;
283+
- Remove `Expression::getParams()` method, use `$params` property instead;
283284

src/Expression/Expression.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@
2929
final class Expression implements ExpressionInterface, Stringable
3030
{
3131
/**
32+
* @param string $expression The DB expression.
33+
* @param array $params List of parameters to bind to this expression. The keys are placeholders appearing in
34+
* {@see expression} and the values are the corresponding parameter values.
35+
*
3236
* @psalm-param ParamsType $params
3337
*/
34-
public function __construct(private readonly string $expression, private readonly array $params = [])
35-
{
38+
public function __construct(
39+
public readonly string $expression,
40+
public readonly array $params = [],
41+
) {
3642
}
3743

3844
/**
@@ -42,15 +48,4 @@ public function __toString(): string
4248
{
4349
return $this->expression;
4450
}
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-
* @psalm-return ParamsType
51-
*/
52-
public function getParams(): array
53-
{
54-
return $this->params;
55-
}
5651
}

src/Expression/ExpressionBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public function __construct(private readonly QueryBuilderInterface $queryBuilder
4444
*/
4545
public function build(ExpressionInterface $expression, array &$params = []): string
4646
{
47-
$sql = $expression->__toString();
48-
$expressionParams = $expression->getParams();
47+
$sql = $expression->expression;
48+
$expressionParams = $expression->params;
4949

5050
if (empty($expressionParams)) {
5151
return $sql;

0 commit comments

Comments
 (0)