Skip to content

Commit

Permalink
Merge pull request #10227 from greg0ire/php8-migration-expr
Browse files Browse the repository at this point in the history
Migrate Expr and Lexer to PHP 8 syntax
  • Loading branch information
greg0ire authored Nov 28, 2022
2 parents 5afa360 + 4d73e3c commit 7433c3a
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 362 deletions.
165 changes: 44 additions & 121 deletions lib/Doctrine/ORM/Query/Expr.php

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions lib/Doctrine/ORM/Query/Expr/Andx.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
*/
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,
self::class,
];

/** @psalm-var list<string|Comparison|Func|Orx|self> */
protected $parts = [];
protected array $parts = [];

/** @psalm-return list<string|Comparison|Func|Orx|self> */
public function getParts()
public function getParts(): array
{
return $this->parts;
}
Expand Down
39 changes: 13 additions & 26 deletions lib/Doctrine/ORM/Query/Expr/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<class-string> */
protected $allowedClasses = [];
protected array $allowedClasses = [];

/** @var list<string|Stringable> */
protected $parts = [];
protected array $parts = [];

/** @param mixed $args */
public function __construct($args = [])
public function __construct(mixed $args = [])
{
$this->addMultiple($args);
}
Expand All @@ -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);
Expand All @@ -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),
Expand All @@ -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];
Expand Down
40 changes: 16 additions & 24 deletions lib/Doctrine/ORM/Query/Expr/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Doctrine/ORM/Query/Expr/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\ORM\Query\Expr;

use Stringable;

use function implode;
use function is_object;
use function preg_match;
Expand All @@ -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];
Expand All @@ -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;

Expand Down
27 changes: 14 additions & 13 deletions lib/Doctrine/ORM/Query/Expr/From.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 : '');
Expand Down
21 changes: 11 additions & 10 deletions lib/Doctrine/ORM/Query/Expr/Func.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,45 @@

namespace Doctrine\ORM\Query\Expr;

use Stringable;

use function implode;

/**
* Expression class for generating DQL functions.
*
* @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>|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<mixed> */
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) . ')';
}
Expand Down
11 changes: 4 additions & 7 deletions lib/Doctrine/ORM/Query/Expr/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> */
protected $parts = [];
protected array $parts = [];

/** @psalm-return list<string> */
public function getParts()
public function getParts(): array
{
return $this->parts;
}
Expand Down
12 changes: 7 additions & 5 deletions lib/Doctrine/ORM/Query/Expr/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

namespace Doctrine\ORM\Query\Expr;

use Stringable;

use function strtoupper;

/**
* Expression class for DQL join.
*
* @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
Expand Down
11 changes: 4 additions & 7 deletions lib/Doctrine/ORM/Query/Expr/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> */
protected $parts = [];
protected array $parts = [];

/** @psalm-return list<string> */
public function getParts()
public function getParts(): array
{
return $this->parts;
}
Expand Down
Loading

0 comments on commit 7433c3a

Please sign in to comment.