8000 [DoctrineBridge] Spread some PHP 8 love by derrabus · Pull Request #52044 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DoctrineBridge] Spread some PHP 8 love #52044

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

Merged
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
8 changes: 3 additions & 5 deletions src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
*/
class ProxyCacheWarmer implements CacheWarmerInterface
{
private ManagerRegistry $registry;

public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
public function __construct(
private readonly ManagerRegistry $registry,
) {
}

/**
Expand Down
12 changes: 5 additions & 7 deletions src/Symfony/Bridge/Doctrine/DataCollector/ObjectParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@

final class ObjectParameter
{
private object $object;
private ?\Throwable $error;
private bool $stringable;
private string $class;

public function __construct(object $object, ?\Throwable $error)
{
$this->object = $object;
$this->error = $error;
$this->stringable = \is_callable([$object, '__toString']);
public function __construct(
private readonly object $object,
private readonly ?\Throwable $error,
) {
$this->stringable = $this->object instanceof \Stringable;
$this->class = $object::class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
*/
class ContainerAwareLoader extends Loader
{
private ContainerInterface $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
public function __construct(
private readonly ContainerInterface $container,
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
*/
class DoctrineValidationPass implements CompilerPassInterface
{
private string $managerType;

public function __construct(string $managerType)
{
$this->managerType = $managerType;
public function __construct(
private readonly string $managerType,
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,23 @@
*/
class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
{
private string $connectionsParameter;
private array $connections;

/**
* @var array<string, Definition>
*/
private array $eventManagers = [];

private string $managerTemplate;
private string $tagPrefix;

/**
* @param string $managerTemplate sprintf() template for generating the event
* manager's service ID for a connection name
* @param string $tagPrefix Tag prefix for listeners and subscribers
*/
public function __construct(string $connectionsParameter, string $managerTemplate, string $tagPrefix)
{
$this->connectionsParameter = $connectionsParameter;
$this->managerTemplate = $managerTemplate;
$this->tagPrefix = $tagPrefix;
public function __construct(
private readonly string $connectionsParameter,
private readonly string $managerTemplate,
private readonly string $tagPrefix,
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,6 @@ abstract class RegisterMappingsPass implements CompilerPassInterface
*/
protected $enabledParameter;

/**
* Naming pattern for the configuration service id, for example
* 'doctrine.orm.%s_configuration'.
*/
private string $configurationPattern;

/**
* Method name to call on the configuration service. This depends on the
* Doctrine implementation. For example addEntityNamespace.
*/
private string $registerAliasMethodName;

/**
* Map of alias to namespace.
*
* @var string[]
*/
private array $aliasMap;

/**
* The $managerParameters is an ordered list of container parameters that could provide the
* name of the manager to register these namespaces and alias on. The first non-empty name
Expand All @@ -108,24 +89,32 @@ abstract class RegisterMappingsPass implements CompilerPassInterface
* @param string|false $enabledParameter Service container parameter that must be
* present to enable the mapping. Set to false
* to not do any check, optional.
* @param string $configurationPattern Pattern for the Configuration service name
* @param string $registerAliasMethodName Name of Configuration class method to
* register alias
* @param string $configurationPattern Pattern for the Configuration service name,
* for example 'doctrine.orm.%s_configuration'.
* @param string $registerAliasMethodName Method name to call on the configuration service. This
* depends on the Doctrine implementation.
* For example addEntityNamespace.
* @param string[] $aliasMap Map of alias to namespace
*/
public function __construct(Definition|Reference $driver, array $namespaces, array $managerParameters, string $driverPattern, string|false $enabledParameter = false, string $configurationPattern = '', string $registerAliasMethodName = '', array $aliasMap = [])
{
public function __construct(
Definition|Reference $driver,
array $namespaces,
array $managerParameters,
string $driverPattern,
string|false $enabledParameter = false,
private readonly string $configurationPattern = '',
private readonly string $registerAliasMethodName = '',
private readonly array $aliasMap = [],
) {
$this->driver = $driver;
$this->namespaces = $namespaces;
$this->managerParameters = $managerParameters;
$this->driverPattern = $driverPattern;
$this->enabledParameter = $enabledParameter;
if (\count($aliasMap) && (!$configurationPattern || !$registerAliasMethodName)) {

if ($aliasMap && (!$configurationPattern || !$registerAliasMethodName)) {
throw new \InvalidArgumentException('configurationPattern and registerAliasMethodName are required to register namespace alias.');
}
$this->configurationPattern = $configurationPattern;
$this->registerAliasMethodName = $registerAliasMethodName;
$this->aliasMap = $aliasMap;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
*/
class EntityFactory implements UserProviderFactoryInterface
{
private string $key;
private string $providerId;

public function __construct(string $key, string $providerId)
{
$this->key = $key;
$this->providerId = $providerId;
public function __construct(
private readonly string $key,
private readonly string $providerId,
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
*/
class DoctrineChoiceLoader extends AbstractChoiceLoader
{
private ObjectManager $manager;
private string $class;
private ?IdReader $idReader;
private ?EntityLoaderInterface $objectLoader;
/** @var class-string */
private readonly string $class;

/**
* Creates a new choice loader.
Expand All @@ -36,18 +34,17 @@ class DoctrineChoiceLoader extends AbstractChoiceLo 1CF5 ader
*
* @param string $class The class name of the loaded objects
*/
public function __construct(ObjectManager $manager, string $class, IdReader $idReader = null, EntityLoaderInterface $objectLoader = n F438 ull)
{
$classMetadata = $manager->getClassMetadata($class);

public function __construct(
private readonly ObjectManager $manager,
string $class,
private readonly ?IdReader $idReader = null,
private readonly ?EntityLoaderInterface $objectLoader = null,
) {
if ($idReader && !$idReader->isSingleId()) {
throw new \InvalidArgumentException(sprintf('The second argument "$idReader" of "%s" must be null when the query cannot be optimized because of composite id fields.', __METHOD__));
}

$this->manager = $manager;
$this->class = $classMetadata->getName();
$this->idReader = $idReader;
$this->objectLoader = $objectLoader;
$this->class = $manager->getClassMetadata($class)->getName();
}

protected function loadChoices(): iterable
Expand Down
32 changes: 17 additions & 15 deletions src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,35 @@
*/
class IdReader
{
private ObjectManager $om;
private ClassMetadata $classMetadata;
private bool $singleId;
private bool $intId;
private string $idField;
private ?self $associationIdReader = null;

public function __construct(ObjectManager $om, ClassMetadata $classMetadata)
{
private readonly bool $singleId;
private readonly bool $intId;
private readonly string $idField;
private readonly ?self $associationIdReader;

public function __construct(
private readonly ObjectManager $om,
private readonly ClassMetadata $classMetadata,
) {
$ids = $classMetadata->getIdentifierFieldNames();
$idType = $classMetadata->getTypeOfField(current($ids));

$this->om = $om;
$this->classMetadata = $classMetadata;
$this->singleId = 1 === \count($ids);
$this->intId = $this->singleId && \in_array($idType, ['integer', 'smallint', 'bigint']);
$singleId = 1 === \count($ids);
$this->idField = current($ids);

// single field association are resolved, since the schema column could be an int
if ($this->singleId && $classMetadata->hasAssociation($this->idField)) {
if ($singleId && $classMetadata->hasAssociation($this->idField)) {
$this->associationIdReader = new self($om, $om->getClassMetadata(
$classMetadata->getAssociationTargetClass($this->idField)
));

$this->singleId = $this->associationIdReader->isSingleId();
$singleId = $this->associationIdReader->isSingleId();
$this->intId = $this->associationIdReader->isIntId();
} else {
$this->intId = $singleId && \in_array($idType, ['integer', 'smallint', 'bigint']);
$this->associationIdReader = null;
}

$this->singleId = $singleId;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,9 @@
*/
class ORMQueryBuilderLoader implements EntityLoaderInterface
{
/**
* Contains the query builder that builds the query for fetching the
* entities.
*
* This property should only be accessed through queryBuilder.
*/
private QueryBuilder $queryBuilder;

public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;
public function __construct(
private readonly QueryBuilder $queryBuilder,
) {
}

public function getEntities(): array
Expand Down
8 changes: 3 additions & 5 deletions src/Symfony/Bridge/Doctrine/IdGenerator/UlidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

final class UlidGenerator extends AbstractIdGenerator
{
private ?UlidFactory $factory;

public function __construct(UlidFactory $factory = null)
{
$this->factory = $factory;
public function __construct(
private readonly ?UlidFactory $factory = null
) {
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Doctrine/IdGenerator/UuidGenerator.php 10000
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

final class UuidGenerator extends AbstractIdGenerator
{
private UuidFactory $protoFactory;
private readonly UuidFactory $protoFactory;
private UuidFactory|NameBasedUuidFactory|RandomBasedUuidFactory|TimeBasedUuidFactory $factory;
private ?string $entityGetter = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
*/
class DoctrineClearEntityManagerWorkerSubscriber implements EventSubscriberInterface
{
private ManagerRegistry $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
{
$this->managerRegistry = $managerRegistry;
public function __construct(
private readonly ManagerRegistry $managerRegistry,
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
*/
class DoctrineOpenTransactionLoggerMiddleware extends AbstractDoctrineMiddleware
{
private ?LoggerInterface $logger;

public function __construct(ManagerRegistry $managerRegistry, string $entityManagerName = null, LoggerInterface $logger = null)
{
public function __construct(
ManagerRegistry $managerRegistry,
string $entityManagerName = null,
private readonly ?LoggerInterface $logger = null,
) {
parent::__construct($managerRegistry, $entityManagerName);

$this->logger = $logger;
}

protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Bridge/Doctrine/Middleware/Debug/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ final class Connection extends AbstractConnectionMiddleware
{
public function __construct(
ConnectionInterface $connection,
private DebugDataHolder $debugDataHolder,
private ?Stopwatch $stopwatch,
private string $connectionName,
private readonly DebugDataHolder $debugDataHolder,
private readonly ?Stopwatch $stopwatch,
private readonly string $connectionName,
) {
parent::__construct($connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ final class Connection extends AbstractConnectionMiddleware

public function __construct(
ConnectionInterface $connection,
private DebugDataHolder $debugDataHolder,
private ?Stopwatch $stopwatch,
private string $connectionName,
private readonly DebugDataHolder $debugDataHolder,
private readonly ?Stopwatch $stopwatch,
private readonly string $connectionName,
) {
parent::__construct($connection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
*/
final class Statement extends AbstractStatementMiddleware
{
private Query $query;
private readonly Query $query;

public function __construct(
StatementInterface $statement,
private DebugDataHolder $debugDataHolder,
private string $connectionName,
private readonly DebugDataHolder $debugDataHolder,
private readonly string $connectionName,
string $sql,
private ?Stopwatch $stopwatch = null,
private readonly ?Stopwatch $stopwatch = null,
) {
$this->query = new Query($sql);

Expand Down
Loading
0