Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate function nodes to PHP 8 syntax #10214

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/Query/AST/Functions/AbsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ class AbsFunction extends FunctionNode
/** @var SimpleArithmeticExpression */
public $simpleArithmeticExpression;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 public properties are not migrated it seems

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is best because that would not even pass the test suite:

1) Doctrine\Tests\ORM\Functional\QueryDqlFunctionTest::testFunctionAbs
TypeError: Cannot assign Doctrine\ORM\Query\AST\ArithmeticTerm to property Doctrine\ORM\Query\AST\Functions\AbsFunction::$simpleArithmeticExpression of type Doctrine\ORM\Query\AST\SimpleArithmeticExpression

/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/AST/Functions/AbsFunction.php:33
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:3531
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:3475
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:2282
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:1238
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:927
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:896
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:306
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:406
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query.php:244
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query.php:253
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/AbstractQuery.php:931
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/AbstractQuery.php:887
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/AbstractQuery.php:687
/home/greg/dev/doctrine-orm-split/major/tests/Doctrine/Tests/ORM/Functional/QueryDqlFunctionTest.php:72

2) Doctrine\Tests\ORM\Functional\Ticket\GH7941Test::typesShouldBeConvertedForDQLFunctions
TypeError: Cannot assign Doctrine\ORM\Query\AST\PathExpression to property Doctrine\ORM\Query\AST\Functions\AbsFunction::$simpleArithmeticExpression of type Doctrine\ORM\Query\AST\SimpleArithmeticExpression

/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/AST/Functions/AbsFunction.php:33
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:3531
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:3475
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:2282
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:1233
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:927
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:896
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:306
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query/Parser.php:406
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query.php:244
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/Query.php:253
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/AbstractQuery.php:931
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/AbstractQuery.php:887
/home/greg/dev/doctrine-orm-split/major/lib/Doctrine/ORM/AbstractQuery.php:687
/home/greg/dev/doctrine-orm-split/major/tests/Doctrine/Tests/ORM/Functional/Ticket/GH7941Test.php:75

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I migrated all properties having Node as a type hint, and left most other properties alone. I think this will deserve a separate PR if we decide to do something about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a good call, I think. Many property types in the AST namespace are inaccurate. Migrating the straightforward ones first is probably the best we can do.


/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
return 'ABS(' . $sqlWalker->walkSimpleArithmeticExpression(
$this->simpleArithmeticExpression,
) . ')';
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Query/AST/Functions/AvgFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
*/
final class AvgFunction extends FunctionNode
{
/** @var AggregateExpression */
private $aggregateExpression;
private AggregateExpression $aggregateExpression;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I elected not to use AggregateExpression|null + assert inside getSql: although the property is not set in the constructor, it is always set in parse(). Otherwise, it can IMO stay uninitialized.


public function getSql(SqlWalker $sqlWalker): string
{
Expand Down
13 changes: 4 additions & 9 deletions lib/Doctrine/ORM/Query/AST/Functions/BitAndFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
*/
class BitAndFunction extends FunctionNode
{
/** @var Node */
public $firstArithmetic;
public Node $firstArithmetic;
public Node $secondArithmetic;

/** @var Node */
public $secondArithmetic;

/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
$platform = $sqlWalker->getConnection()->getDatabasePlatform();

Expand All @@ -33,8 +29,7 @@ public function getSql(SqlWalker $sqlWalker)
);
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
13 changes: 4 additions & 9 deletions lib/Doctrine/ORM/Query/AST/Functions/BitOrFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
*/
class BitOrFunction extends FunctionNode
{
/** @var Node */
public $firstArithmetic;
public Node $firstArithmetic;
public Node $secondArithmetic;

/** @var Node */
public $secondArithmetic;

/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
$platform = $sqlWalker->getConnection()->getDatabasePlatform();

Expand All @@ -33,8 +29,7 @@ public function getSql(SqlWalker $sqlWalker)
);
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
13 changes: 4 additions & 9 deletions lib/Doctrine/ORM/Query/AST/Functions/ConcatFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
*/
class ConcatFunction extends FunctionNode
{
/** @var Node */
public $firstStringPrimary;

/** @var Node */
public $secondStringPrimary;
public Node $firstStringPrimary;
public Node $secondStringPrimary;

/** @psalm-var list<Node> */
public $concatExpressions = [];

/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
$platform = $sqlWalker->getConnection()->getDatabasePlatform();

Expand All @@ -39,8 +35,7 @@ public function getSql(SqlWalker $sqlWalker)
return $platform->getConcatExpression(...$args);
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Query/AST/Functions/CountFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
final class CountFunction extends FunctionNode implements TypedExpression
{
/** @var AggregateExpression */
private $aggregateExpression;
private AggregateExpression $aggregateExpression;

public function getSql(SqlWalker $sqlWalker): string
{
Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/Query/AST/Functions/CurrentDateFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
class CurrentDateFunction extends FunctionNode
{
/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
return $sqlWalker->getConnection()->getDatabasePlatform()->getCurrentDateSQL();
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/Query/AST/Functions/CurrentTimeFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
class CurrentTimeFunction extends FunctionNode
{
/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
return $sqlWalker->getConnection()->getDatabasePlatform()->getCurrentTimeSQL();
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
class CurrentTimestampFunction extends FunctionNode
{
/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
return $sqlWalker->getConnection()->getDatabasePlatform()->getCurrentTimestampSQL();
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
98 changes: 38 additions & 60 deletions lib/Doctrine/ORM/Query/AST/Functions/DateAddFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,45 @@
*/
class DateAddFunction extends FunctionNode
{
/** @var Node */
public $firstDateExpression = null;
public Node $firstDateExpression;
public Node $intervalExpression;
public Node $unit;

/** @var Node */
public $intervalExpression = null;

/** @var Node */
public $unit = null;

/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
switch (strtolower($this->unit->value)) {
case 'second':
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
);

case 'minute':
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMinutesExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
);

case 'hour':
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
);

case 'day':
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
);

case 'week':
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddWeeksExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
);

case 'month':
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
);

case 'year':
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddYearsExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
);

default:
throw QueryException::semanticalError(
'DATE_ADD() only supports units of type second, minute, hour, day, week, month and year.',
);
}
return match (strtolower((string) $this->unit->value)) {
'second' => $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddSecondsExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
),
'minute' => $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMinutesExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
),
'hour' => $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddHourExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
),
'day' => $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddDaysExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
),
'week' => $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddWeeksExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
),
'month' => $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddMonthExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
),
'year' => $sqlWalker->getConnection()->getDatabasePlatform()->getDateAddYearsExpression(
$this->firstDateExpression->dispatch($sqlWalker),
$this->dispatchIntervalExpression($sqlWalker),
),
default => throw QueryException::semanticalError(
'DATE_ADD() only supports units of type second, minute, hour, day, week, month and year.',
),
};
}

/**
Expand All @@ -97,8 +76,7 @@ private function dispatchIntervalExpression(SqlWalker $sqlWalker)
return $sql;
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
13 changes: 4 additions & 9 deletions lib/Doctrine/ORM/Query/AST/Functions/DateDiffFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@
*/
class DateDiffFunction extends FunctionNode
{
/** @var Node */
public $date1;
public Node $date1;
public Node $date2;

/** @var Node */
public $date2;

/** @inheritdoc */
public function getSql(SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker): string
{
return $sqlWalker->getConnection()->getDatabasePlatform()->getDateDiffExpression(
$this->date1->dispatch($sqlWalker),
$this->date2->dispatch($sqlWalker),
);
}

/** @inheritdoc */
public function parse(Parser $parser)
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
Expand Down
Loading