Skip to content

Commit

Permalink
Migrate tools namespace to PHP8 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Jul 15, 2022
1 parent 94b7914 commit 37edd89
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,12 @@ private function getClassMetadata(
): ClassMetadata {
try {
return $entityManager->getClassMetadata($entityName);
} catch (MappingException $e) {
} catch (MappingException) {
}

$matches = array_filter(
$this->getMappedEntities($entityManager),
static function ($mappedEntity) use ($entityName) {
return preg_match('{' . preg_quote($entityName) . '}', $mappedEntity);
}
static fn ($mappedEntity) => preg_match('{' . preg_quote($entityName) . '}', $mappedEntity)
);

if (! $matches) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/Console/MetadataFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MetadataFilter extends FilterIterator implements Countable
*
* @return ClassMetadata[]
*/
public static function filter(array $metadatas, array|string $filter)
public static function filter(array $metadatas, array|string $filter): array
{
$metadatas = new MetadataFilter(new ArrayIterator($metadatas), $filter);

Expand Down
20 changes: 3 additions & 17 deletions lib/Doctrine/ORM/Tools/Event/GenerateSchemaEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,16 @@
*/
class GenerateSchemaEventArgs extends EventArgs
{
/** @var EntityManagerInterface */
private $em;

/** @var Schema */
private $schema;

public function __construct(EntityManagerInterface $em, Schema $schema)
public function __construct(private EntityManagerInterface $em, private Schema $schema)
{
$this->em = $em;
$this->schema = $schema;
}

/**
* @return EntityManagerInterface
*/
public function getEntityManager()
public function getEntityManager(): EntityManagerInterface
{
return $this->em;
}

/**
* @return Schema
*/
public function getSchema()
public function getSchema(): Schema
{
return $this->schema;
}
Expand Down
29 changes: 4 additions & 25 deletions lib/Doctrine/ORM/Tools/Event/GenerateSchemaTableEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,21 @@
*/
class GenerateSchemaTableEventArgs extends EventArgs
{
/** @var ClassMetadata */
private $classMetadata;

/** @var Schema */
private $schema;

/** @var Table */
private $classTable;

public function __construct(ClassMetadata $classMetadata, Schema $schema, Table $classTable)
public function __construct(private ClassMetadata $classMetadata, private Schema $schema, private Table $classTable)
{
$this->classMetadata = $classMetadata;
$this->schema = $schema;
$this->classTable = $classTable;
}

/**
* @return ClassMetadata
*/
public function getClassMetadata()
public function getClassMetadata(): ClassMetadata
{
return $this->classMetadata;
}

/**
* @return Schema
*/
public function getSchema()
public function getSchema(): Schema
{
return $this->schema;
}

/**
* @return Table
*/
public function getClassTable()
public function getClassTable(): Table
{
return $this->classTable;
}
Expand Down
11 changes: 3 additions & 8 deletions lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@
*/
class CountOutputWalker extends SqlWalker
{
/** @var AbstractPlatform */
private $platform;
private ?AbstractPlatform $platform;

/** @var ResultSetMapping */
private $rsm;
private ?ResultSetMapping $rsm;

/**
* Stores various parameters that are otherwise unavailable
Expand All @@ -62,10 +60,7 @@ public function __construct($query, $parserResult, array $queryComponents)
parent::__construct($query, $parserResult, $queryComponents);
}

/**
* {@inheritdoc}
*/
public function walkSelectStatement(SelectStatement $AST)
public function walkSelectStatement(SelectStatement $AST): string
{
if ($this->platform instanceof SQLServerPlatform) {
$AST->orderByClause = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Tools/Pagination/CountWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CountWalker extends TreeWalkerAdapter
*/
public const HINT_DISTINCT = 'doctrine_paginator.distinct';

public function walkSelectStatement(SelectStatement $AST)
public function walkSelectStatement(SelectStatement $AST): void
{
if ($AST->havingClause) {
throw new RuntimeException('Cannot count query that uses a HAVING clause. Use the output walkers for pagination');
Expand Down
44 changes: 14 additions & 30 deletions lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,29 @@ class LimitSubqueryOutputWalker extends SqlWalker
{
private const ORDER_BY_PATH_EXPRESSION = '/(?<![a-z0-9_])%s\.%s(?![a-z0-9_])/i';

/** @var AbstractPlatform */
private $platform;
private ?AbstractPlatform $platform;

/** @var ResultSetMapping */
private $rsm;
private ?ResultSetMapping $rsm;

/** @var int */
private $firstResult;
private ?int $firstResult;

/** @var int */
private $maxResults;
private ?int $maxResults;

/** @var EntityManagerInterface */
private $em;
private ?EntityManagerInterface $em;

/**
* The quote strategy.
*
* @var QuoteStrategy
*/
private $quoteStrategy;
private ?QuoteStrategy $quoteStrategy;

/** @var list<PathExpression> */
private $orderByPathExpressions = [];
private array $orderByPathExpressions = [];

/**
* @var bool We don't want to add path expressions from sub-selects into the select clause of the containing query.
* This state flag simply keeps track on whether we are walking on a subquery or not
*/
private $inSubSelect = false;
private bool $inSubSelect = false;

/**
* Stores various parameters that are otherwise unavailable
Expand Down Expand Up @@ -152,10 +145,7 @@ private function rebuildOrderByForRowNumber(SelectStatement $AST): void
$AST->orderByClause = null;
}

/**
* {@inheritdoc}
*/
public function walkSelectStatement(SelectStatement $AST)
public function walkSelectStatement(SelectStatement $AST): string
{
if ($this->platformSupportsRowNumber()) {
return $this->walkSelectStatementWithRowNumber($AST);
Expand All @@ -168,11 +158,9 @@ public function walkSelectStatement(SelectStatement $AST)
* Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT.
* This method is for use with platforms which support ROW_NUMBER.
*
* @return string
*
* @throws RuntimeException
*/
public function walkSelectStatementWithRowNumber(SelectStatement $AST)
public function walkSelectStatementWithRowNumber(SelectStatement $AST): string
{
$hasOrderBy = false;
$outerOrderBy = ' ORDER BY dctrn_minrownum ASC';
Expand Down Expand Up @@ -224,13 +212,9 @@ public function walkSelectStatementWithRowNumber(SelectStatement $AST)
* Walks down a SelectStatement AST node, wrapping it in a SELECT DISTINCT.
* This method is for platforms which DO NOT support ROW_NUMBER.
*
* @param bool $addMissingItemsFromOrderByToSelect
*
* @return string
*
* @throws RuntimeException
*/
public function walkSelectStatementWithoutRowNumber(SelectStatement $AST, $addMissingItemsFromOrderByToSelect = true)
public function walkSelectStatementWithoutRowNumber(SelectStatement $AST, bool $addMissingItemsFromOrderByToSelect = true): string
{
// We don't want to call this recursively!
if ($AST->orderByClause instanceof OrderByClause && $addMissingItemsFromOrderByToSelect) {
Expand Down Expand Up @@ -459,7 +443,7 @@ private function generateSqlAliasReplacements(): array
*
* @return list<PathExpression>
*/
public function getOrderByPathExpressions()
public function getOrderByPathExpressions(): array
{
return $this->orderByPathExpressions;
}
Expand Down Expand Up @@ -550,7 +534,7 @@ private function getSQLIdentifier(SelectStatement $AST): array
/**
* {@inheritdoc}
*/
public function walkPathExpression($pathExpr)
public function walkPathExpression($pathExpr): string
{
if (! $this->inSubSelect && ! $this->platformSupportsRowNumber() && ! in_array($pathExpr, $this->orderByPathExpressions, true)) {
$this->orderByPathExpressions[] = $pathExpr;
Expand All @@ -562,7 +546,7 @@ public function walkPathExpression($pathExpr)
/**
* {@inheritdoc}
*/
public function walkSubSelect($subselect)
public function walkSubSelect($subselect): string
{
$this->inSubSelect = true;

Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@ class LimitSubqueryWalker extends TreeWalkerAdapter

/**
* Counter for generating unique order column aliases.
*
* @var int
*/
private $_aliasCounter = 0;
private int $_aliasCounter = 0;

public function walkSelectStatement(SelectStatement $AST)
public function walkSelectStatement(SelectStatement $AST): void
{
$queryComponents = $this->_getQueryComponents();
// Get the root entity and alias from the AST fromClause
Expand Down
38 changes: 11 additions & 27 deletions lib/Doctrine/ORM/Tools/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ class Paginator implements Countable, IteratorAggregate
{
use SQLResultCasing;

/** @var Query */
private $query;
private Query $query;

/** @var bool */
private $fetchJoinCollection;
private bool $fetchJoinCollection;

/** @var bool|null */
private $useOutputWalkers;
private ?bool $useOutputWalkers = null;

/** @var int */
private $count;
private ?int $count = null;

/**
* @param Query|QueryBuilder $query A Doctrine ORM query or query builder.
Expand All @@ -59,10 +55,8 @@ public function __construct($query, $fetchJoinCollection = true)

/**
* Returns the query.
*
* @return Query
*/
public function getQuery()
public function getQuery(): Query
{
return $this->query;
}
Expand All @@ -72,48 +66,39 @@ public function getQuery()
*
* @return bool Whether the query joins a collection.
*/
public function getFetchJoinCollection()
public function getFetchJoinCollection(): bool
{
return $this->fetchJoinCollection;
}

/**
* Returns whether the paginator will use an output walker.
*
* @return bool|null
*/
public function getUseOutputWalkers()
public function getUseOutputWalkers(): ?bool
{
return $this->useOutputWalkers;
}

/**
* Sets whether the paginator will use an output walker.
*
* @param bool|null $useOutputWalkers
*
* @return $this
* @psalm-return static<T>
*/
public function setUseOutputWalkers($useOutputWalkers)
public function setUseOutputWalkers(?bool $useOutputWalkers): static
{
$this->useOutputWalkers = $useOutputWalkers;

return $this;
}

/**
* {@inheritdoc}
*
* @return int
*/
#[ReturnTypeWillChange]
public function count()
public function count(): int
{
if ($this->count === null) {
try {
$this->count = (int) array_sum(array_map('current', $this->getCountQuery()->getScalarResult()));
} catch (NoResultException $e) {
} catch (NoResultException) {
$this->count = 0;
}
}
Expand All @@ -124,11 +109,10 @@ public function count()
/**
* {@inheritdoc}
*
* @return ArrayIterator
* @psalm-return ArrayIterator<array-key, T>
*/
#[ReturnTypeWillChange]
public function getIterator()
public function getIterator(): ArrayIterator
{
$offset = $this->query->getFirstResult();
$length = $this->query->getMaxResults();
Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/Tools/Pagination/WhereInWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class WhereInWalker extends TreeWalkerAdapter
*/
public const PAGINATOR_ID_ALIAS = 'dpid';

public function walkSelectStatement(SelectStatement $AST)
public function walkSelectStatement(SelectStatement $AST): void
{
$queryComponents = $this->_getQueryComponents();
// Get the root entity and alias from the AST fromClause
Expand Down Expand Up @@ -147,8 +147,6 @@ private function convertWhereInIdentifiersToDatabaseValue(string $type): void
->getEntityManager()
->getConnection();

$query->setParameter(self::PAGINATOR_ID_ALIAS, array_map(static function ($id) use ($connection, $type) {
return $connection->convertToDatabaseValue($id, $type);
}, $identifiers));
$query->setParameter(self::PAGINATOR_ID_ALIAS, array_map(static fn ($id) => $connection->convertToDatabaseValue($id, $type), $identifiers));
}
}

0 comments on commit 37edd89

Please sign in to comment.