Skip to content

Commit 3bfdfdd

Browse files
committed
Expression: Handle null $subTree
1 parent bae0f92 commit 3bfdfdd

File tree

5 files changed

+8
-10
lines changed

5 files changed

+8
-10
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=7.4.0 || ^8.0",
16+
"php": "^7.4 || ^8.0",
1717
"mouf/utils.common.conditioninterface": "~2.0",
1818
"mouf/utils.value.value-interface": "~1.0",
1919
"mouf/utils.common.paginable-interface": "~1.0",

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ parameters:
9595
count: 1
9696
path: src/SQLParser/Node/NodeFactory.php
9797

98-
-
99-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, array\\<SQLParser\\\\Node\\\\NodeInterface\\>\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#"
100-
count: 1
101-
path: src/SQLParser/Node/NodeFactory.php
102-
10398
-
10499
message: "#^Parameter \\#2 \\.\\.\\.\\$args of function array_merge expects array, array\\<SQLParser\\\\Node\\\\NodeInterface\\>\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#"
105100
count: 1

src/SQLParser/Node/Expression.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public function setBaseExpression($baseExpression): void
6969
$this->baseExpression = $baseExpression;
7070
}
7171

72-
/** @var NodeInterface[]|NodeInterface */
72+
/** @var NodeInterface[]|NodeInterface|null */
7373
private $subTree;
7474

7575
/**
76-
* @return NodeInterface|NodeInterface[]
76+
* @return NodeInterface|NodeInterface[]|null
7777
*/
7878
public function getSubTree()
7979
{
@@ -263,7 +263,7 @@ public function walk(VisitorInterface $visitor)
263263
$this->subTree[$key] = $result2;
264264
}
265265
}
266-
} else {
266+
} elseif ($this->subTree instanceof NodeInterface) {
267267
$result2 = $this->subTree->walk($visitor);
268268
if ($result2 === NodeTraverser::REMOVE_NODE) {
269269
$this->subTree = [];

src/SQLParser/Node/NodeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ public static function simplify($nodes)
652652
if ($operand instanceof Expression) {
653653
if (empty($operand->getBaseExpression())) {
654654
$subTree = $operand->getSubTree();
655-
if (count($subTree) === 1) {
655+
if (is_array($subTree) && count($subTree) === 1) {
656656
$newNodes = array_merge($newNodes, self::simplify($subTree));
657657
} else {
658658
$newNodes[] = $operand;

tests/Mouf/Database/MagicQueryTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ public function testStandardSelect()
208208

209209
$sql = 'SELECT COUNT(*) AS cnt FROM (SELECT id FROM country) subquery';
210210
$this->assertEquals($sql, self::simplifySql($magicQuery->build($sql)));
211+
212+
$sql = "SELECT YEAR(register_date) AS year FROM users GROUP BY year";
213+
$this->assertEquals("SELECT YEAR(register_date) AS year FROM users GROUP BY year", self::simplifySql($magicQuery->build($sql)));
211214
}
212215

213216
public function testInNullException() {

0 commit comments

Comments
 (0)