From 4d73e3ce7801d3bf3254257332e903d8ecea4096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sat, 12 Nov 2022 10:04:59 +0100 Subject: [PATCH] Migrate Expr and Lexer to PHP 8 syntax --- lib/Doctrine/ORM/Query/Expr.php | 165 ++++++--------------- lib/Doctrine/ORM/Query/Expr/Andx.php | 9 +- lib/Doctrine/ORM/Query/Expr/Base.php | 39 ++--- lib/Doctrine/ORM/Query/Expr/Comparison.php | 40 ++--- lib/Doctrine/ORM/Query/Expr/Composite.php | 8 +- lib/Doctrine/ORM/Query/Expr/From.php | 27 ++-- lib/Doctrine/ORM/Query/Expr/Func.php | 21 +-- lib/Doctrine/ORM/Query/Expr/GroupBy.php | 11 +- lib/Doctrine/ORM/Query/Expr/Join.php | 12 +- lib/Doctrine/ORM/Query/Expr/Literal.php | 11 +- lib/Doctrine/ORM/Query/Expr/Math.php | 27 ++-- lib/Doctrine/ORM/Query/Expr/OrderBy.php | 49 +++--- lib/Doctrine/ORM/Query/Expr/Orx.php | 9 +- lib/Doctrine/ORM/Query/Expr/Select.php | 13 +- lib/Doctrine/ORM/Query/Lexer.php | 160 ++++++++++---------- 15 files changed, 239 insertions(+), 362 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Expr.php b/lib/Doctrine/ORM/Query/Expr.php index 18f988b1067..1b3372914fa 100644 --- a/lib/Doctrine/ORM/Query/Expr.php +++ b/lib/Doctrine/ORM/Query/Expr.php @@ -36,7 +36,7 @@ class Expr * but requires at least one defined * when converting to string. */ - public function andX(...$x): Expr\Andx + public function andX(Expr\Comparison|Expr\Func|Expr\Andx|Expr\Orx|string ...$x): Expr\Andx { return new Expr\Andx($x); } @@ -54,31 +54,23 @@ public function andX(...$x): Expr\Andx * but requires at least one defined * when converting to string. */ - public function orX(...$x): Expr\Orx + public function orX(Expr\Comparison|Expr\Func|Expr\Andx|Expr\Orx|string ...$x): Expr\Orx { return new Expr\Orx($x); } /** * Creates an ASCending order expression. - * - * @param mixed $expr - * - * @return Expr\OrderBy */ - public function asc($expr) + public function asc(mixed $expr): Expr\OrderBy { return new Expr\OrderBy($expr, 'ASC'); } /** * Creates a DESCending order expression. - * - * @param mixed $expr - * - * @return Expr\OrderBy */ - public function desc($expr) + public function desc(mixed $expr): Expr\OrderBy { return new Expr\OrderBy($expr, 'DESC'); } @@ -95,10 +87,8 @@ public function desc($expr) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Comparison */ - public function eq($x, $y) + public function eq(mixed $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, Expr\Comparison::EQ, $y); } @@ -114,10 +104,8 @@ public function eq($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Comparison */ - public function neq($x, $y) + public function neq(mixed $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, Expr\Comparison::NEQ, $y); } @@ -133,10 +121,8 @@ public function neq($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Comparison */ - public function lt($x, $y) + public function lt(mixed $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, Expr\Comparison::LT, $y); } @@ -152,10 +138,8 @@ public function lt($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Comparison */ - public function lte($x, $y) + public function lte(mixed $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, Expr\Comparison::LTE, $y); } @@ -171,10 +155,8 @@ public function lte($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Comparison */ - public function gt($x, $y) + public function gt(mixed $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, Expr\Comparison::GT, $y); } @@ -190,10 +172,8 @@ public function gt($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Comparison */ - public function gte($x, $y) + public function gte(mixed $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, Expr\Comparison::GTE, $y); } @@ -202,10 +182,8 @@ public function gte($x, $y) * Creates an instance of AVG() function, with the given argument. * * @param mixed $x Argument to be used in AVG() function. - * - * @return Expr\Func */ - public function avg($x) + public function avg(mixed $x): Expr\Func { return new Expr\Func('AVG', [$x]); } @@ -214,10 +192,8 @@ public function avg($x) * Creates an instance of MAX() function, with the given argument. * * @param mixed $x Argument to be used in MAX() function. - * - * @return Expr\Func */ - public function max($x) + public function max(mixed $x): Expr\Func { return new Expr\Func('MAX', [$x]); } @@ -226,10 +202,8 @@ public function max($x) * Creates an instance of MIN() function, with the given argument. * * @param mixed $x Argument to be used in MIN() function. - * - * @return Expr\Func */ - public function min($x) + public function min(mixed $x): Expr\Func { return new Expr\Func('MIN', [$x]); } @@ -238,10 +212,8 @@ public function min($x) * Creates an instance of COUNT() function, with the given argument. * * @param mixed $x Argument to be used in COUNT() function. - * - * @return Expr\Func */ - public function count($x) + public function count(mixed $x): Expr\Func { return new Expr\Func('COUNT', [$x]); } @@ -250,10 +222,8 @@ public function count($x) * Creates an instance of COUNT(DISTINCT) function, with the given argument. * * @param mixed ...$x Argument to be used in COUNT(DISTINCT) function. - * - * @return string */ - public function countDistinct(mixed ...$x) + public function countDistinct(mixed ...$x): string { return 'COUNT(DISTINCT ' . implode(', ', $x) . ')'; } @@ -262,10 +232,8 @@ public function countDistinct(mixed ...$x) * Creates an instance of EXISTS() function, with the given DQL Subquery. * * @param mixed $subquery DQL Subquery to be used in EXISTS() function. - * - * @return Expr\Func */ - public function exists($subquery) + public function exists(mixed $subquery): Expr\Func { return new Expr\Func('EXISTS', [$subquery]); } @@ -274,10 +242,8 @@ public function exists($subquery) * Creates an instance of ALL() function, with the given DQL Subquery. * * @param mixed $subquery DQL Subquery to be used in ALL() function. - * - * @return Expr\Func */ - public function all($subquery) + public function all(mixed $subquery): Expr\Func { return new Expr\Func('ALL', [$subquery]); } @@ -286,10 +252,8 @@ public function all($subquery) * Creates a SOME() function expression with the given DQL subquery. * * @param mixed $subquery DQL Subquery to be used in SOME() function. - * - * @return Expr\Func */ - public function some($subquery) + public function some(mixed $subquery): Expr\Func { return new Expr\Func('SOME', [$subquery]); } @@ -298,10 +262,8 @@ public function some($subquery) * Creates an ANY() function expression with the given DQL subquery. * * @param mixed $subquery DQL Subquery to be used in ANY() function. - * - * @return Expr\Func */ - public function any($subquery) + public function any(mixed $subquery): Expr\Func { return new Expr\Func('ANY', [$subquery]); } @@ -310,10 +272,8 @@ public function any($subquery) * Creates a negation expression of the given restriction. * * @param mixed $restriction Restriction to be used in NOT() function. - * - * @return Expr\Func */ - public function not($restriction) + public function not(mixed $restriction): Expr\Func { return new Expr\Func('NOT', [$restriction]); } @@ -322,21 +282,16 @@ public function not($restriction) * Creates an ABS() function expression with the given argument. * * @param mixed $x Argument to be used in ABS() function. - * - * @return Expr\Func */ - public function abs($x) + public function abs(mixed $x): Expr\Func { return new Expr\Func('ABS', [$x]); } /** * Creates a MOD($x, $y) function expression to return the remainder of $x divided by $y. - * - * @param mixed $x - * @param mixed $y */ - public function mod($x, $y): Expr\Func + public function mod(mixed $x, mixed $y): Expr\Func { return new Expr\Func('MOD', [$x, $y]); } @@ -353,10 +308,8 @@ public function mod($x, $y): Expr\Func * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Math */ - public function prod($x, $y) + public function prod(mixed $x, mixed $y): Expr\Math { return new Expr\Math($x, '*', $y); } @@ -372,10 +325,8 @@ public function prod($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Math */ - public function diff($x, $y) + public function diff(mixed $x, mixed $y): Expr\Math { return new Expr\Math($x, '-', $y); } @@ -391,10 +342,8 @@ public function diff($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Math */ - public function sum($x, $y) + public function sum(mixed $x, mixed $y): Expr\Math { return new Expr\Math($x, '+', $y); } @@ -410,10 +359,8 @@ public function sum($x, $y) * * @param mixed $x Left expression. * @param mixed $y Right expression. - * - * @return Expr\Math */ - public function quot($x, $y) + public function quot(mixed $x, mixed $y): Expr\Math { return new Expr\Math($x, '/', $y); } @@ -422,10 +369,8 @@ public function quot($x, $y) * Creates a SQRT() function expression with the given argument. * * @param mixed $x Argument to be used in SQRT() function. - * - * @return Expr\Func */ - public function sqrt($x) + public function sqrt(mixed $x): Expr\Func { return new Expr\Func('SQRT', [$x]); } @@ -435,10 +380,8 @@ public function sqrt($x) * * @param string $x Field in string format to be restricted by IN() function. * @param mixed $y Argument to be used in IN() function. - * - * @return Expr\Func */ - public function in($x, $y) + public function in(string $x, mixed $y): Expr\Func { if (is_iterable($y)) { if ($y instanceof Traversable) { @@ -460,10 +403,8 @@ public function in($x, $y) * * @param string $x Field in string format to be restricted by NOT IN() function. * @param mixed $y Argument to be used in NOT IN() function. - * - * @return Expr\Func */ - public function notIn($x, $y) + public function notIn(string $x, mixed $y): Expr\Func { if (is_iterable($y)) { if ($y instanceof Traversable) { @@ -484,10 +425,8 @@ public function notIn($x, $y) * Creates an IS NULL expression with the given arguments. * * @param string $x Field in string format to be restricted by IS NULL. - * - * @return string */ - public function isNull($x) + public function isNull(string $x): string { return $x . ' IS NULL'; } @@ -496,10 +435,8 @@ public function isNull($x) * Creates an IS NOT NULL expression with the given arguments. * * @param string $x Field in string format to be restricted by IS NOT NULL. - * - * @return string */ - public function isNotNull($x) + public function isNotNull(string $x): string { return $x . ' IS NOT NULL'; } @@ -509,10 +446,8 @@ public function isNotNull($x) * * @param string $x Field in string format to be inspected by LIKE() comparison. * @param mixed $y Argument to be used in LIKE() comparison. - * - * @return Expr\Comparison */ - public function like($x, $y) + public function like(string $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, 'LIKE', $y); } @@ -522,10 +457,8 @@ public function like($x, $y) * * @param string $x Field in string format to be inspected by LIKE() comparison. * @param mixed $y Argument to be used in LIKE() comparison. - * - * @return Expr\Comparison */ - public function notLike($x, $y) + public function notLike(string $x, mixed $y): Expr\Comparison { return new Expr\Comparison($x, 'NOT LIKE', $y); } @@ -534,10 +467,8 @@ public function notLike($x, $y) * Creates a CONCAT() function expression with the given arguments. * * @param mixed ...$x Arguments to be used in CONCAT() function. - * - * @return Expr\Func */ - public function concat(mixed ...$x) + public function concat(mixed ...$x): Expr\Func { return new Expr\Func('CONCAT', $x); } @@ -548,10 +479,8 @@ public function concat(mixed ...$x) * @param mixed $x Argument to be used as string to be cropped by SUBSTRING() function. * @param int $from Initial offset to start cropping string. May accept negative values. * @param int|null $len Length of crop. May accept negative values. - * - * @return Expr\Func */ - public function substring($x, $from, $len = null) + public function substring(mixed $x, int $from, $len = null): Expr\Func { $args = [$x, $from]; if ($len !== null) { @@ -568,7 +497,7 @@ public function substring($x, $from, $len = null) * * @return Expr\Func A LOWER function expression. */ - public function lower($x) + public function lower(mixed $x): Expr\Func { return new Expr\Func('LOWER', [$x]); } @@ -580,7 +509,7 @@ public function lower($x) * * @return Expr\Func An UPPER function expression. */ - public function upper($x) + public function upper(mixed $x): Expr\Func { return new Expr\Func('UPPER', [$x]); } @@ -592,7 +521,7 @@ public function upper($x) * * @return Expr\Func A LENGTH function expression. */ - public function length($x) + public function length(mixed $x): Expr\Func { return new Expr\Func('LENGTH', [$x]); } @@ -601,10 +530,8 @@ public function length($x) * Creates a literal expression of the given argument. * * @param scalar $literal Argument to be converted to literal. - * - * @return Expr\Literal */ - public function literal($literal) + public function literal(mixed $literal): Expr\Literal { return new Expr\Literal($this->quoteLiteral($literal)); } @@ -614,7 +541,7 @@ public function literal($literal) * * @param scalar $literal The literal value. */ - private function quoteLiteral($literal): string + private function quoteLiteral(mixed $literal): string { if (is_int($literal) || is_float($literal)) { return (string) $literal; @@ -636,7 +563,7 @@ private function quoteLiteral($literal): string * * @return string A BETWEEN expression. */ - public function between($val, $x, $y) + public function between(mixed $val, int|string $x, int|string $y): string { return $val . ' BETWEEN ' . $x . ' AND ' . $y; } @@ -648,7 +575,7 @@ public function between($val, $x, $y) * * @return Expr\Func a TRIM expression. */ - public function trim($x) + public function trim(mixed $x): Expr\Func { return new Expr\Func('TRIM', $x); } @@ -658,10 +585,8 @@ public function trim($x) * * @param string $x Value to be checked * @param string $y Value to be checked against - * - * @return Expr\Comparison */ - public function isMemberOf($x, $y) + public function isMemberOf(string $x, string $y): Expr\Comparison { return new Expr\Comparison($x, 'MEMBER OF', $y); } @@ -671,10 +596,8 @@ public function isMemberOf($x, $y) * * @param string $x Value to be checked * @param string $y Value to be checked against - * - * @return Expr\Comparison */ - public function isInstanceOf($x, $y) + public function isInstanceOf(string $x, string $y): Expr\Comparison { return new Expr\Comparison($x, 'INSTANCE OF', $y); } diff --git a/lib/Doctrine/ORM/Query/Expr/Andx.php b/lib/Doctrine/ORM/Query/Expr/Andx.php index 229a4567963..a20bcef098f 100644 --- a/lib/Doctrine/ORM/Query/Expr/Andx.php +++ b/lib/Doctrine/ORM/Query/Expr/Andx.php @@ -11,11 +11,10 @@ */ class Andx extends Composite { - /** @var string */ - protected $separator = ' AND '; + protected string $separator = ' AND '; /** @var string[] */ - protected $allowedClasses = [ + protected array $allowedClasses = [ Comparison::class, Func::class, Orx::class, @@ -23,10 +22,10 @@ class Andx extends Composite ]; /** @psalm-var list */ - protected $parts = []; + protected array $parts = []; /** @psalm-return list */ - public function getParts() + public function getParts(): array { return $this->parts; } diff --git a/lib/Doctrine/ORM/Query/Expr/Base.php b/lib/Doctrine/ORM/Query/Expr/Base.php index 6fe3e347d5a..6fd5b82b923 100644 --- a/lib/Doctrine/ORM/Query/Expr/Base.php +++ b/lib/Doctrine/ORM/Query/Expr/Base.php @@ -8,7 +8,6 @@ use Stringable; use function count; -use function get_class; use function get_debug_type; use function implode; use function in_array; @@ -20,25 +19,19 @@ * * @link www.doctrine-project.org */ -abstract class Base +abstract class Base implements Stringable { - /** @var string */ - protected $preSeparator = '('; - - /** @var string */ - protected $separator = ', '; - - /** @var string */ - protected $postSeparator = ')'; + protected string $preSeparator = '('; + protected string $separator = ', '; + protected string $postSeparator = ')'; /** @var list */ - protected $allowedClasses = []; + protected array $allowedClasses = []; /** @var list */ - protected $parts = []; + protected array $parts = []; - /** @param mixed $args */ - public function __construct($args = []) + public function __construct(mixed $args = []) { $this->addMultiple($args); } @@ -49,7 +42,7 @@ public function __construct($args = []) * * @return $this */ - public function addMultiple($args = []) + public function addMultiple($args = []): static { foreach ((array) $args as $arg) { $this->add($arg); @@ -59,17 +52,15 @@ public function addMultiple($args = []) } /** - * @param mixed $arg - * * @return $this * * @throws InvalidArgumentException */ - public function add($arg) + public function add(mixed $arg): static { if ($arg !== null && (! $arg instanceof self || $arg->count() > 0)) { // If we decide to keep Expr\Base instances, we can use this check - if (! is_string($arg) && ! in_array(get_class($arg), $this->allowedClasses, true)) { + if (! is_string($arg) && ! in_array($arg::class, $this->allowedClasses, true)) { throw new InvalidArgumentException(sprintf( "Expression of type '%s' not allowed in this context.", get_debug_type($arg), @@ -82,17 +73,13 @@ public function add($arg) return $this; } - /** - * @return int - * @psalm-return 0|positive-int - */ - public function count() + /** @psalm-return 0|positive-int */ + public function count(): int { return count($this->parts); } - /** @return string */ - public function __toString() + public function __toString(): string { if ($this->count() === 1) { return (string) $this->parts[0]; diff --git a/lib/Doctrine/ORM/Query/Expr/Comparison.php b/lib/Doctrine/ORM/Query/Expr/Comparison.php index 152b6c98174..ec8ef21b2d9 100644 --- a/lib/Doctrine/ORM/Query/Expr/Comparison.php +++ b/lib/Doctrine/ORM/Query/Expr/Comparison.php @@ -4,51 +4,43 @@ namespace Doctrine\ORM\Query\Expr; +use Stringable; + /** * Expression class for DQL comparison expressions. * * @link www.doctrine-project.org */ -class Comparison +class Comparison implements Stringable { - public const EQ = '='; - public const NEQ = '<>'; - public const LT = '<'; - public const LTE = '<='; - public const GT = '>'; - public const GTE = '>='; - - /** - * Creates a comparison expression with the given arguments. - * - * @param mixed $leftExpr - * @param string $operator - * @param mixed $rightExpr - */ - public function __construct(protected $leftExpr, protected $operator, protected $rightExpr) + final public const EQ = '='; + final public const NEQ = '<>'; + final public const LT = '<'; + final public const LTE = '<='; + final public const GT = '>'; + final public const GTE = '>='; + + /** Creates a comparison expression with the given arguments. */ + public function __construct(protected mixed $leftExpr, protected string $operator, protected mixed $rightExpr) { } - /** @return mixed */ - public function getLeftExpr() + public function getLeftExpr(): mixed { return $this->leftExpr; } - /** @return string */ - public function getOperator() + public function getOperator(): string { return $this->operator; } - /** @return mixed */ - public function getRightExpr() + public function getRightExpr(): mixed { return $this->rightExpr; } - /** @return string */ - public function __toString() + public function __toString(): string { return $this->leftExpr . ' ' . $this->operator . ' ' . $this->rightExpr; } diff --git a/lib/Doctrine/ORM/Query/Expr/Composite.php b/lib/Doctrine/ORM/Query/Expr/Composite.php index 2270fbca34b..f3007a77534 100644 --- a/lib/Doctrine/ORM/Query/Expr/Composite.php +++ b/lib/Doctrine/ORM/Query/Expr/Composite.php @@ -4,6 +4,8 @@ namespace Doctrine\ORM\Query\Expr; +use Stringable; + use function implode; use function is_object; use function preg_match; @@ -15,8 +17,7 @@ */ class Composite extends Base { - /** @return string */ - public function __toString() + public function __toString(): string { if ($this->count() === 1) { return (string) $this->parts[0]; @@ -31,8 +32,7 @@ public function __toString() return implode($this->separator, $components); } - /** @param string|object $part */ - private function processQueryPart($part): string + private function processQueryPart(string|Stringable $part): string { $queryPart = (string) $part; diff --git a/lib/Doctrine/ORM/Query/Expr/From.php b/lib/Doctrine/ORM/Query/Expr/From.php index 1157eb93478..21af0786e27 100644 --- a/lib/Doctrine/ORM/Query/Expr/From.php +++ b/lib/Doctrine/ORM/Query/Expr/From.php @@ -4,42 +4,43 @@ namespace Doctrine\ORM\Query\Expr; +use Stringable; + /** * Expression class for DQL from. * * @link www.doctrine-project.org */ -class From +class From implements Stringable { /** - * @param class-string $from The class name. - * @param string $alias The alias of the class. - * @param string $indexBy The index for the from. + * @param class-string $from The class name. + * @param string $alias The alias of the class. */ - public function __construct(protected $from, protected $alias, protected $indexBy = null) - { + public function __construct( + protected string $from, + protected string $alias, + protected string|null $indexBy = null, + ) { } /** @return class-string */ - public function getFrom() + public function getFrom(): string { return $this->from; } - /** @return string */ - public function getAlias() + public function getAlias(): string { return $this->alias; } - /** @return string|null */ - public function getIndexBy() + public function getIndexBy(): string|null { return $this->indexBy; } - /** @return string */ - public function __toString() + public function __toString(): string { return $this->from . ' ' . $this->alias . ($this->indexBy ? ' INDEX BY ' . $this->indexBy : ''); diff --git a/lib/Doctrine/ORM/Query/Expr/Func.php b/lib/Doctrine/ORM/Query/Expr/Func.php index 7c77979903e..1ecb8e25fae 100644 --- a/lib/Doctrine/ORM/Query/Expr/Func.php +++ b/lib/Doctrine/ORM/Query/Expr/Func.php @@ -4,6 +4,8 @@ namespace Doctrine\ORM\Query\Expr; +use Stringable; + use function implode; /** @@ -11,37 +13,36 @@ * * @link www.doctrine-project.org */ -class Func +class Func implements Stringable { /** @var mixed[] */ - protected $arguments; + protected array $arguments; /** * Creates a function, with the given argument. * - * @param string $name * @param mixed[]|mixed $arguments * @psalm-param list|mixed $arguments */ - public function __construct(protected $name, $arguments) - { + public function __construct( + protected string $name, + $arguments, + ) { $this->arguments = (array) $arguments; } - /** @return string */ - public function getName() + public function getName(): string { return $this->name; } /** @psalm-return list */ - public function getArguments() + public function getArguments(): array { return $this->arguments; } - /** @return string */ - public function __toString() + public function __toString(): string { return $this->name . '(' . implode(', ', $this->arguments) . ')'; } diff --git a/lib/Doctrine/ORM/Query/Expr/GroupBy.php b/lib/Doctrine/ORM/Query/Expr/GroupBy.php index 178a48b231b..fa4625a6355 100644 --- a/lib/Doctrine/ORM/Query/Expr/GroupBy.php +++ b/lib/Doctrine/ORM/Query/Expr/GroupBy.php @@ -11,17 +11,14 @@ */ class GroupBy extends Base { - /** @var string */ - protected $preSeparator = ''; - - /** @var string */ - protected $postSeparator = ''; + protected string $preSeparator = ''; + protected string $postSeparator = ''; /** @psalm-var list */ - protected $parts = []; + protected array $parts = []; /** @psalm-return list */ - public function getParts() + public function getParts(): array { return $this->parts; } diff --git a/lib/Doctrine/ORM/Query/Expr/Join.php b/lib/Doctrine/ORM/Query/Expr/Join.php index 906af5640d4..c3b6dc9dd00 100644 --- a/lib/Doctrine/ORM/Query/Expr/Join.php +++ b/lib/Doctrine/ORM/Query/Expr/Join.php @@ -4,6 +4,8 @@ namespace Doctrine\ORM\Query\Expr; +use Stringable; + use function strtoupper; /** @@ -11,13 +13,13 @@ * * @link www.doctrine-project.org */ -class Join +class Join implements Stringable { - public const INNER_JOIN = 'INNER'; - public const LEFT_JOIN = 'LEFT'; + final public const INNER_JOIN = 'INNER'; + final public const LEFT_JOIN = 'LEFT'; - public const ON = 'ON'; - public const WITH = 'WITH'; + final public const ON = 'ON'; + final public const WITH = 'WITH'; /** * @psalm-param self::INNER_JOIN|self::LEFT_JOIN $joinType diff --git a/lib/Doctrine/ORM/Query/Expr/Literal.php b/lib/Doctrine/ORM/Query/Expr/Literal.php index d3930fcb97d..0c130309c45 100644 --- a/lib/Doctrine/ORM/Query/Expr/Literal.php +++ b/lib/Doctrine/ORM/Query/Expr/Literal.php @@ -11,17 +11,14 @@ */ class Literal extends Base { - /** @var string */ - protected $preSeparator = ''; - - /** @var string */ - protected $postSeparator = ''; + protected string $preSeparator = ''; + protected string $postSeparator = ''; /** @psalm-var list */ - protected $parts = []; + protected array $parts = []; /** @psalm-return list */ - public function getParts() + public function getParts(): array { return $this->parts; } diff --git a/lib/Doctrine/ORM/Query/Expr/Math.php b/lib/Doctrine/ORM/Query/Expr/Math.php index 42b165aa708..05e0b39b6b8 100644 --- a/lib/Doctrine/ORM/Query/Expr/Math.php +++ b/lib/Doctrine/ORM/Query/Expr/Math.php @@ -4,44 +4,41 @@ namespace Doctrine\ORM\Query\Expr; +use Stringable; + /** * Expression class for DQL math statements. * * @link www.doctrine-project.org */ -class Math +class Math implements Stringable { /** * Creates a mathematical expression with the given arguments. - * - * @param mixed $leftExpr - * @param string $operator - * @param mixed $rightExpr */ - public function __construct(protected $leftExpr, protected $operator, protected $rightExpr) - { + public function __construct( + protected mixed $leftExpr, + protected string $operator, + protected mixed $rightExpr, + ) { } - /** @return mixed */ - public function getLeftExpr() + public function getLeftExpr(): mixed { return $this->leftExpr; } - /** @return string */ - public function getOperator() + public function getOperator(): string { return $this->operator; } - /** @return mixed */ - public function getRightExpr() + public function getRightExpr(): mixed { return $this->rightExpr; } - /** @return string */ - public function __toString() + public function __toString(): string { // Adjusting Left Expression $leftExpr = (string) $this->leftExpr; diff --git a/lib/Doctrine/ORM/Query/Expr/OrderBy.php b/lib/Doctrine/ORM/Query/Expr/OrderBy.php index dc6cfbd78ee..ac9e1601563 100644 --- a/lib/Doctrine/ORM/Query/Expr/OrderBy.php +++ b/lib/Doctrine/ORM/Query/Expr/OrderBy.php @@ -4,6 +4,8 @@ namespace Doctrine\ORM\Query\Expr; +use Stringable; + use function count; use function implode; @@ -12,63 +14,46 @@ * * @link www.doctrine-project.org */ -class OrderBy +class OrderBy implements Stringable { - /** @var string */ - protected $preSeparator = ''; - - /** @var string */ - protected $separator = ', '; - - /** @var string */ - protected $postSeparator = ''; + protected string $preSeparator = ''; + protected string $separator = ', '; + protected string $postSeparator = ''; /** @var string[] */ - protected $allowedClasses = []; + protected array $allowedClasses = []; /** @psalm-var list */ - protected $parts = []; + protected array $parts = []; - /** - * @param string|null $sort - * @param string|null $order - */ - public function __construct($sort = null, $order = null) - { + public function __construct( + string|null $sort = null, + string|null $order = null, + ) { if ($sort) { $this->add($sort, $order); } } - /** - * @param string $sort - * @param string|null $order - * - * @return void - */ - public function add($sort, $order = null) + public function add(string $sort, string|null $order = null): void { $order = ! $order ? 'ASC' : $order; $this->parts[] = $sort . ' ' . $order; } - /** - * @return int - * @psalm-return 0|positive-int - */ - public function count() + /** @psalm-return 0|positive-int */ + public function count(): int { return count($this->parts); } /** @psalm-return list */ - public function getParts() + public function getParts(): array { return $this->parts; } - /** @return string */ - public function __toString() + public function __toString(): string { return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator; } diff --git a/lib/Doctrine/ORM/Query/Expr/Orx.php b/lib/Doctrine/ORM/Query/Expr/Orx.php index 2d3ae0df00d..2ae23320168 100644 --- a/lib/Doctrine/ORM/Query/Expr/Orx.php +++ b/lib/Doctrine/ORM/Query/Expr/Orx.php @@ -11,11 +11,10 @@ */ class Orx extends Composite { - /** @var string */ - protected $separator = ' OR '; + protected string $separator = ' OR '; /** @var string[] */ - protected $allowedClasses = [ + protected array $allowedClasses = [ Comparison::class, Func::class, Andx::class, @@ -23,10 +22,10 @@ class Orx extends Composite ]; /** @psalm-var list */ - protected $parts = []; + protected array $parts = []; /** @psalm-return list */ - public function getParts() + public function getParts(): array { return $this->parts; } diff --git a/lib/Doctrine/ORM/Query/Expr/Select.php b/lib/Doctrine/ORM/Query/Expr/Select.php index a3929fb08ad..91b0b600949 100644 --- a/lib/Doctrine/ORM/Query/Expr/Select.php +++ b/lib/Doctrine/ORM/Query/Expr/Select.php @@ -11,20 +11,17 @@ */ class Select extends Base { - /** @var string */ - protected $preSeparator = ''; - - /** @var string */ - protected $postSeparator = ''; + protected string $preSeparator = ''; + protected string $postSeparator = ''; /** @var string[] */ - protected $allowedClasses = [Func::class]; + protected array $allowedClasses = [Func::class]; /** @psalm-var list */ - protected $parts = []; + protected array $parts = []; /** @psalm-return list */ - public function getParts() + public function getParts(): array { return $this->parts; } diff --git a/lib/Doctrine/ORM/Query/Lexer.php b/lib/Doctrine/ORM/Query/Lexer.php index 651c0d0dec7..1732f4465a9 100644 --- a/lib/Doctrine/ORM/Query/Lexer.php +++ b/lib/Doctrine/ORM/Query/Lexer.php @@ -23,88 +23,88 @@ class Lexer extends AbstractLexer { // All tokens that are not valid identifiers must be < 100 - public const T_NONE = 1; - public const T_INTEGER = 2; - public const T_STRING = 3; - public const T_INPUT_PARAMETER = 4; - public const T_FLOAT = 5; - public const T_CLOSE_PARENTHESIS = 6; - public const T_OPEN_PARENTHESIS = 7; - public const T_COMMA = 8; - public const T_DIVIDE = 9; - public const T_DOT = 10; - public const T_EQUALS = 11; - public const T_GREATER_THAN = 12; - public const T_LOWER_THAN = 13; - public const T_MINUS = 14; - public const T_MULTIPLY = 15; - public const T_NEGATE = 16; - public const T_PLUS = 17; - public const T_OPEN_CURLY_BRACE = 18; - public const T_CLOSE_CURLY_BRACE = 19; + final public const T_NONE = 1; + final public const T_INTEGER = 2; + final public const T_STRING = 3; + final public const T_INPUT_PARAMETER = 4; + final public const T_FLOAT = 5; + final public const T_CLOSE_PARENTHESIS = 6; + final public const T_OPEN_PARENTHESIS = 7; + final public const T_COMMA = 8; + final public const T_DIVIDE = 9; + final public const T_DOT = 10; + final public const T_EQUALS = 11; + final public const T_GREATER_THAN = 12; + final public const T_LOWER_THAN = 13; + final public const T_MINUS = 14; + final public const T_MULTIPLY = 15; + final public const T_NEGATE = 16; + final public const T_PLUS = 17; + final public const T_OPEN_CURLY_BRACE = 18; + final public const T_CLOSE_CURLY_BRACE = 19; // All tokens that are identifiers or keywords that could be considered as identifiers should be >= 100 - public const T_FULLY_QUALIFIED_NAME = 101; - public const T_IDENTIFIER = 102; + final public const T_FULLY_QUALIFIED_NAME = 101; + final public const T_IDENTIFIER = 102; // All keyword tokens should be >= 200 - public const T_ALL = 200; - public const T_AND = 201; - public const T_ANY = 202; - public const T_AS = 203; - public const T_ASC = 204; - public const T_AVG = 205; - public const T_BETWEEN = 206; - public const T_BOTH = 207; - public const T_BY = 208; - public const T_CASE = 209; - public const T_COALESCE = 210; - public const T_COUNT = 211; - public const T_DELETE = 212; - public const T_DESC = 213; - public const T_DISTINCT = 214; - public const T_ELSE = 215; - public const T_EMPTY = 216; - public const T_END = 217; - public const T_ESCAPE = 218; - public const T_EXISTS = 219; - public const T_FALSE = 220; - public const T_FROM = 221; - public const T_GROUP = 222; - public const T_HAVING = 223; - public const T_HIDDEN = 224; - public const T_IN = 225; - public const T_INDEX = 226; - public const T_INNER = 227; - public const T_INSTANCE = 228; - public const T_IS = 229; - public const T_JOIN = 230; - public const T_LEADING = 231; - public const T_LEFT = 232; - public const T_LIKE = 233; - public const T_MAX = 234; - public const T_MEMBER = 235; - public const T_MIN = 236; - public const T_NEW = 237; - public const T_NOT = 238; - public const T_NULL = 239; - public const T_NULLIF = 240; - public const T_OF = 241; - public const T_OR = 242; - public const T_ORDER = 243; - public const T_OUTER = 244; - public const T_PARTIAL = 245; - public const T_SELECT = 246; - public const T_SET = 247; - public const T_SOME = 248; - public const T_SUM = 249; - public const T_THEN = 250; - public const T_TRAILING = 251; - public const T_TRUE = 252; - public const T_UPDATE = 253; - public const T_WHEN = 254; - public const T_WHERE = 255; - public const T_WITH = 256; + final public const T_ALL = 200; + final public const T_AND = 201; + final public const T_ANY = 202; + final public const T_AS = 203; + final public const T_ASC = 204; + final public const T_AVG = 205; + final public const T_BETWEEN = 206; + final public const T_BOTH = 207; + final public const T_BY = 208; + final public const T_CASE = 209; + final public const T_COALESCE = 210; + final public const T_COUNT = 211; + final public const T_DELETE = 212; + final public const T_DESC = 213; + final public const T_DISTINCT = 214; + final public const T_ELSE = 215; + final public const T_EMPTY = 216; + final public const T_END = 217; + final public const T_ESCAPE = 218; + final public const T_EXISTS = 219; + final public const T_FALSE = 220; + final public const T_FROM = 221; + final public const T_GROUP = 222; + final public const T_HAVING = 223; + final public const T_HIDDEN = 224; + final public const T_IN = 225; + final public const T_INDEX = 226; + final public const T_INNER = 227; + final public const T_INSTANCE = 228; + final public const T_IS = 229; + final public const T_JOIN = 230; + final public const T_LEADING = 231; + final public const T_LEFT = 232; + final public const T_LIKE = 233; + final public const T_MAX = 234; + final public const T_MEMBER = 235; + final public const T_MIN = 236; + final public const T_NEW = 237; + final public const T_NOT = 238; + final public const T_NULL = 239; + final public const T_NULLIF = 240; + final public const T_OF = 241; + final public const T_OR = 242; + final public const T_ORDER = 243; + final public const T_OUTER = 244; + final public const T_PARTIAL = 245; + final public const T_SELECT = 246; + final public const T_SET = 247; + final public const T_SOME = 248; + final public const T_SUM = 249; + final public const T_THEN = 250; + final public const T_TRAILING = 251; + final public const T_TRUE = 252; + final public const T_UPDATE = 253; + final public const T_WHEN = 254; + final public const T_WHERE = 255; + final public const T_WITH = 256; /** * Creates a new query scanner object. @@ -119,7 +119,7 @@ public function __construct($input) /** * {@inheritdoc} */ - protected function getCatchablePatterns() + protected function getCatchablePatterns(): array { return [ '[a-z_][a-z0-9_]*\:[a-z_][a-z0-9_]*(?:\\\[a-z_][a-z0-9_]*)*', // aliased name @@ -133,7 +133,7 @@ protected function getCatchablePatterns() /** * {@inheritdoc} */ - protected function getNonCatchablePatterns() + protected function getNonCatchablePatterns(): array { return ['\s+', '--.*', '(.)']; }