Skip to content

Commit

Permalink
Expression: Handle null $subTree
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons committed Sep 17, 2021
1 parent bae0f92 commit 3bfdfdd
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": ">=7.4.0 || ^8.0",
"php": "^7.4 || ^8.0",
"mouf/utils.common.conditioninterface": "~2.0",
"mouf/utils.value.value-interface": "~1.0",
"mouf/utils.common.paginable-interface": "~1.0",
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ parameters:
count: 1
path: src/SQLParser/Node/NodeFactory.php

-
message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, array\\<SQLParser\\\\Node\\\\NodeInterface\\>\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#"
count: 1
path: src/SQLParser/Node/NodeFactory.php

-
message: "#^Parameter \\#2 \\.\\.\\.\\$args of function array_merge expects array, array\\<SQLParser\\\\Node\\\\NodeInterface\\>\\|SQLParser\\\\Node\\\\NodeInterface given\\.$#"
count: 1
Expand Down
6 changes: 3 additions & 3 deletions src/SQLParser/Node/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public function setBaseExpression($baseExpression): void
$this->baseExpression = $baseExpression;
}

/** @var NodeInterface[]|NodeInterface */
/** @var NodeInterface[]|NodeInterface|null */
private $subTree;

/**
* @return NodeInterface|NodeInterface[]
* @return NodeInterface|NodeInterface[]|null
*/
public function getSubTree()
{
Expand Down Expand Up @@ -263,7 +263,7 @@ public function walk(VisitorInterface $visitor)
$this->subTree[$key] = $result2;
}
}
} else {
} elseif ($this->subTree instanceof NodeInterface) {
$result2 = $this->subTree->walk($visitor);
if ($result2 === NodeTraverser::REMOVE_NODE) {
$this->subTree = [];
Expand Down
2 changes: 1 addition & 1 deletion src/SQLParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ public static function simplify($nodes)
if ($operand instanceof Expression) {
if (empty($operand->getBaseExpression())) {
$subTree = $operand->getSubTree();
if (count($subTree) === 1) {
if (is_array($subTree) && count($subTree) === 1) {
$newNodes = array_merge($newNodes, self::simplify($subTree));
} else {
$newNodes[] = $operand;
Expand Down
3 changes: 3 additions & 0 deletions tests/Mouf/Database/MagicQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ public function testStandardSelect()

$sql = 'SELECT COUNT(*) AS cnt FROM (SELECT id FROM country) subquery';
$this->assertEquals($sql, self::simplifySql($magicQuery->build($sql)));

$sql = "SELECT YEAR(register_date) AS year FROM users GROUP BY year";
$this->assertEquals("SELECT YEAR(register_date) AS year FROM users GROUP BY year", self::simplifySql($magicQuery->build($sql)));
}

public function testInNullException() {
Expand Down

0 comments on commit 3bfdfdd

Please sign in to comment.