Skip to content

Commit

Permalink
Migrate more classes to PHP 8 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Feb 6, 2023
1 parent de8b444 commit 941292f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 71 deletions.
29 changes: 9 additions & 20 deletions lib/Doctrine/ORM/Event/OnClassMetadataNotFoundEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,30 @@
*/
class OnClassMetadataNotFoundEventArgs extends ManagerEventArgs
{
/** @var string */
private $className;

/** @var ClassMetadata|null */
private $foundMetadata;

/**
* @param string $className
* @param EntityManagerInterface $objectManager
*/
public function __construct($className, ObjectManager $objectManager)
{
$this->className = (string) $className;
private ClassMetadata|null $foundMetadata = null;

/** @param EntityManagerInterface $objectManager */
public function __construct(
private readonly string $className,
ObjectManager $objectManager,
) {
parent::__construct($objectManager);
}

/** @return void */
public function setFoundMetadata(ClassMetadata|null $classMetadata)
public function setFoundMetadata(ClassMetadata|null $classMetadata): void
{
$this->foundMetadata = $classMetadata;
}

/** @return ClassMetadata|null */
public function getFoundMetadata()
public function getFoundMetadata(): ClassMetadata|null
{
return $this->foundMetadata;
}

/**
* Retrieve class name for which a failed metadata fetch attempt was executed
*
* @return string
*/
public function getClassName()
public function getClassName(): string
{
return $this->className;
}
Expand Down
19 changes: 9 additions & 10 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
*/
private string|null $backRefFieldName = null;

/**
* The class descriptor of the collection's entity type.
*/
private ClassMetadata|null $typeClass = null;

/**
* Whether the collection is dirty and needs to be synchronized with the database
* when the UnitOfWork that manages its persistent state commits.
Expand All @@ -83,18 +78,17 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
/**
* Creates a new persistent collection.
*
* @param EntityManagerInterface $em The EntityManager the collection will be associated with.
* @param ClassMetadata $class The class descriptor of the entity type of this collection.
* @param EntityManagerInterface $em The EntityManager the collection will be associated with.
* @param ClassMetadata $typeClass The class descriptor of the entity type of this collection.
* @psalm-param Collection<TKey, T>&Selectable<TKey, T> $collection The collection elements.
*/
public function __construct(
EntityManagerInterface $em,
ClassMetadata $class,
private readonly ClassMetadata|null $typeClass,
Collection $collection,
) {
$this->collection = $collection;
$this->em = $em;
$this->typeClass = $class;
$this->initialized = true;
}

Expand Down Expand Up @@ -523,6 +517,11 @@ public function __sleep(): array
return ['collection', 'initialized'];
}

public function __wakeup(): void
{
$this->em = null;
}

/**
* Extracts a slice of $length elements starting at position $offset from the Collection.
*
Expand Down Expand Up @@ -614,7 +613,7 @@ public function matching(Criteria $criteria): Collection
*
* @return Collection<TKey, T>&Selectable<TKey, T>
*/
public function unwrap(): Collection&Selectable
public function unwrap(): Selectable&Collection
{
assert($this->collection instanceof Collection);
assert($this->collection instanceof Selectable);
Expand Down
38 changes: 10 additions & 28 deletions lib/Doctrine/ORM/Query/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,16 @@
*/
class Printer
{
/**
* Current indentation level
*
* @var int
*/
protected $_indent = 0;

/**
* Defines whether parse tree is printed (default, false) or not (true).
*
* @var bool
*/
protected $_silent;
/** Current indentation level */
protected int $indent = 0;

/**
* Constructs a new parse tree printer.
*
* @param bool $silent Parse tree will not be printed if true.
*/
public function __construct($silent = false)
public function __construct(protected bool $silent = false)
{
$this->_silent = $silent;
}

/**
Expand All @@ -44,39 +32,33 @@ public function __construct($silent = false)
* This method is called before executing a production.
*
* @param string $name Production name.
*
* @return void
*/
public function startProduction($name)
public function startProduction(string $name): void
{
$this->println('(' . $name);
$this->_indent++;
$this->indent++;
}

/**
* Decreases indentation level by one and prints a closing parenthesis.
*
* This method is called after executing a production.
*
* @return void
*/
public function endProduction()
public function endProduction(): void
{
$this->_indent--;
$this->indent--;
$this->println(')');
}

/**
* Prints text indented with spaces depending on current indentation level.
*
* @param string $str The text.
*
* @return void
*/
public function println($str)
public function println(string $str): void
{
if (! $this->_silent) {
echo str_repeat(' ', $this->_indent), $str, "\n";
if (! $this->silent) {
echo str_repeat(' ', $this->indent), $str, "\n";
}
}
}
16 changes: 8 additions & 8 deletions lib/Doctrine/ORM/Query/SqlWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public function walkPathExpression(AST\PathExpression $pathExpr): string
public function walkSelectClause(AST\SelectClause $selectClause): string
{
$sql = 'SELECT ' . ($selectClause->isDistinct ? 'DISTINCT ' : '');
$sqlSelectExpressions = array_filter(array_map([$this, 'walkSelectExpression'], $selectClause->selectExpressions));
$sqlSelectExpressions = array_filter(array_map($this->walkSelectExpression(...), $selectClause->selectExpressions));

if ($this->query->getHint(Query::HINT_INTERNAL_ITERATION) === true && $selectClause->isDistinct) {
$this->query->setHint(self::HINT_DISTINCT, true);
Expand Down Expand Up @@ -1043,7 +1043,7 @@ public function walkFunction(AST\Functions\FunctionNode $function): string
*/
public function walkOrderByClause(AST\OrderByClause $orderByClause): string
{
$orderByItems = array_map([$this, 'walkOrderByItem'], $orderByClause->orderByItems);
$orderByItems = array_map($this->walkOrderByItem(...), $orderByClause->orderByItems);

$collectionOrderByItems = $this->generateOrderedCollectionOrderByItems();
if ($collectionOrderByItems !== '') {
Expand Down Expand Up @@ -1677,7 +1677,7 @@ public function walkUpdateClause(AST\UpdateClause $updateClause): string
$this->setSQLTableAlias($tableName, $tableName, $updateClause->aliasIdentificationVariable);
$this->rootAliases[] = $updateClause->aliasIdentificationVariable;

return $sql . ' SET ' . implode(', ', array_map([$this, 'walkUpdateItem'], $updateClause->updateItems));
return $sql . ' SET ' . implode(', ', array_map($this->walkUpdateItem(...), $updateClause->updateItems));
}

/**
Expand Down Expand Up @@ -1756,7 +1756,7 @@ public function walkConditionalExpression(
return $this->walkConditionalTerm($condExpr);
}

return implode(' OR ', array_map([$this, 'walkConditionalTerm'], $condExpr->conditionalTerms));
return implode(' OR ', array_map($this->walkConditionalTerm(...), $condExpr->conditionalTerms));
}

/**
Expand All @@ -1771,7 +1771,7 @@ public function walkConditionalTerm(
return $this->walkConditionalFactor($condTerm);
}

return implode(' AND ', array_map([$this, 'walkConditionalFactor'], $condTerm->conditionalFactors));
return implode(' AND ', array_map($this->walkConditionalFactor(...), $condTerm->conditionalFactors));
}

/**
Expand Down Expand Up @@ -1953,7 +1953,7 @@ public function walkInListExpression(AST\InListExpression $inExpr): string
{
return $this->walkArithmeticExpression($inExpr->expression)
. ($inExpr->not ? ' NOT' : '') . ' IN ('
. implode(', ', array_map([$this, 'walkInParameter'], $inExpr->literals))
. implode(', ', array_map($this->walkInParameter(...), $inExpr->literals))
. ')';
}

Expand Down Expand Up @@ -2134,7 +2134,7 @@ public function walkSimpleArithmeticExpression(AST\Node|string $simpleArithmetic
return $this->walkArithmeticTerm($simpleArithmeticExpr);
}

return implode(' ', array_map([$this, 'walkArithmeticTerm'], $simpleArithmeticExpr->arithmeticTerms));
return implode(' ', array_map($this->walkArithmeticTerm(...), $simpleArithmeticExpr->arithmeticTerms));
}

/**
Expand All @@ -2154,7 +2154,7 @@ public function walkArithmeticTerm(AST\Node|string $term): string
return $this->walkArithmeticFactor($term);
}

return implode(' ', array_map([$this, 'walkArithmeticFactor'], $term->arithmeticFactors));
return implode(' ', array_map($this->walkArithmeticFactor(...), $term->arithmeticFactors));
}

/**
Expand Down
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,6 @@
<code>list&lt;T&gt;</code>
</MoreSpecificReturnType>
</file>
<file src="lib/Doctrine/ORM/Event/OnClassMetadataNotFoundEventArgs.php">
<RedundantCastGivenDocblockType>
<code>(string) $className</code>
</RedundantCastGivenDocblockType>
</file>
<file src="lib/Doctrine/ORM/Id/AssignedGenerator.php">
<PossiblyNullArgument>
<code>$entity</code>
Expand Down

0 comments on commit 941292f

Please sign in to comment.