Skip to content

Commit 0990f0e

Browse files
Merge pull request #102 from neo4j-php/78-insert-parentheses-based-on-precedence
Insert parentheses based on precedence
2 parents 1e2a892 + a24d889 commit 0990f0e

86 files changed

Lines changed: 868 additions & 661 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog], and this project adheres to
1313
- Changed the minimum required PHP version to 8.1.
1414
- Changed the signature of many functions to use PHP 8 union types.
1515
- Changed the `Relationship::DIR_*` to the `Direction` enum.
16+
- Remove `$insertParentheses` from all methods, and automatically insert them based on precedence.
1617

1718
### Removed
1819

src/Expressions/Exists.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,15 @@ final class Exists implements BooleanType
3434
private ?WhereClause $where;
3535

3636
/**
37-
* @var bool Whether to insert parentheses around the expression
38-
*/
39-
private bool $insertParentheses;
40-
41-
/**
42-
* @param MatchClause $match The MATCH part of the EXISTS expression
43-
* @param null|WhereClause $where The optional WHERE part of the EXISTS expression
44-
* @param bool $insertParentheses Whether to insert parentheses around the expression
37+
* @param MatchClause $match The MATCH part of the EXISTS expression
38+
* @param null|WhereClause $where The optional WHERE part of the EXISTS expression
4539
*
4640
* @internal This function is not covered by the backwards compatibility guarantee of php-cypher-dsl
4741
*/
48-
public function __construct(MatchClause $match, ?WhereClause $where = null, bool $insertParentheses = false)
42+
public function __construct(MatchClause $match, ?WhereClause $where = null)
4943
{
5044
$this->match = $match;
5145
$this->where = $where;
52-
$this->insertParentheses = $insertParentheses;
5346
}
5447

5548
/**
@@ -68,27 +61,19 @@ public function getWhere(): ?WhereClause
6861
return $this->where;
6962
}
7063

71-
/**
72-
* Returns whether it inserts parentheses around the expression.
73-
*/
74-
public function insertsParentheses(): bool
75-
{
76-
return $this->insertParentheses;
77-
}
78-
7964
/**
8065
* @inheritDoc
8166
*/
8267
public function toQuery(): string
8368
{
8469
if (isset($this->where)) {
8570
return sprintf(
86-
$this->insertParentheses ? "(EXISTS { %s %s })" : "EXISTS { %s %s }",
71+
"EXISTS { %s %s }",
8772
$this->match->toQuery(),
8873
$this->where->toQuery()
8974
);
9075
}
9176

92-
return sprintf($this->insertParentheses ? "(EXISTS { %s })" : "EXISTS { %s }", $this->match->toQuery());
77+
return sprintf("EXISTS { %s }", $this->match->toQuery());
9378
}
9479
}

src/Expressions/Operators/Addition.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ protected function getOperator(): string
2323
{
2424
return "+";
2525
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function getPrecedence(): Precedence
31+
{
32+
return Precedence::ADDITIVE;
33+
}
2634
}

src/Expressions/Operators/BinaryOperator.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,11 @@ abstract class BinaryOperator extends Operator
2727
private AnyType $right;
2828

2929
/**
30-
* @inheritDoc
31-
*
3230
* @param AnyType $left The left-hand of the expression
3331
* @param AnyType $right The right-hand of the expression
3432
*/
35-
public function __construct(AnyType $left, AnyType $right, bool $insertParentheses = true)
33+
public function __construct(AnyType $left, AnyType $right)
3634
{
37-
parent::__construct($insertParentheses);
38-
3935
$this->left = $left;
4036
$this->right = $right;
4137
}
@@ -59,9 +55,17 @@ public function getRight(): AnyType
5955
/**
6056
* @inheritDoc
6157
*/
62-
protected function toInner(): string
58+
public function toQuery(): string
6359
{
64-
return sprintf("%s %s %s", $this->left->toQuery(), $this->getOperator(), $this->right->toQuery());
60+
$left = $this->shouldInsertParentheses($this->left) ?
61+
"({$this->left->toQuery()})" :
62+
$this->left->toQuery();
63+
64+
$right = $this->shouldInsertParentheses($this->right) ?
65+
"({$this->right->toQuery()})" :
66+
$this->right->toQuery();
67+
68+
return sprintf("%s %s %s", $left, $this->getOperator(), $right);
6569
}
6670

6771
/**

src/Expressions/Operators/BooleanBinaryOperator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ abstract class BooleanBinaryOperator extends BinaryOperator implements BooleanTy
3131
* @param BooleanType $left The left-hand of the boolean operator
3232
* @param BooleanType $right The right-hand of the boolean operator
3333
*/
34-
public function __construct(BooleanType $left, BooleanType $right, bool $insertParentheses = true)
34+
public function __construct(BooleanType $left, BooleanType $right)
3535
{
36-
parent::__construct($left, $right, $insertParentheses);
36+
parent::__construct($left, $right);
3737
}
3838
}

src/Expressions/Operators/BooleanUnaryOperator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ abstract class BooleanUnaryOperator extends UnaryOperator implements BooleanType
2828
*
2929
* @param BooleanType $expression The unary expression
3030
*/
31-
public function __construct(BooleanType $expression, bool $insertParentheses = true)
31+
public function __construct(BooleanType $expression)
3232
{
33-
parent::__construct($expression, $insertParentheses);
33+
parent::__construct($expression);
3434
}
3535
}

src/Expressions/Operators/ComparisonBinaryOperator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ abstract class ComparisonBinaryOperator extends BinaryOperator implements Boolea
4141
* @param AnyType $left The left-hand of the comparison operator
4242
* @param AnyType $right The right-hand of the comparison operator
4343
*/
44-
public function __construct(AnyType $left, AnyType $right, bool $insertParentheses = true)
44+
public function __construct(AnyType $left, AnyType $right)
4545
{
46-
parent::__construct($left, $right, $insertParentheses);
46+
parent::__construct($left, $right);
4747
}
4848
}

src/Expressions/Operators/ComparisonUnaryOperator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ abstract class ComparisonUnaryOperator extends UnaryOperator implements BooleanT
3030
*
3131
* @param AnyType $expression The unary expression
3232
*/
33-
public function __construct(AnyType $expression, bool $insertParentheses = true)
33+
public function __construct(AnyType $expression)
3434
{
35-
parent::__construct($expression, $insertParentheses);
35+
parent::__construct($expression);
3636
}
3737
}

src/Expressions/Operators/Conjunction.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ protected function getOperator(): string
2323
{
2424
return "AND";
2525
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function getPrecedence(): Precedence
31+
{
32+
return Precedence::CONJUNCTION;
33+
}
2634
}

src/Expressions/Operators/Contains.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ protected function getOperator(): string
2323
{
2424
return "CONTAINS";
2525
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
protected function getPrecedence(): Precedence
31+
{
32+
return Precedence::FUNCTIONAL;
33+
}
2634
}

0 commit comments

Comments
 (0)