From 9f761b42eea994385c642dd96a0e3cf3253ee451 Mon Sep 17 00:00:00 2001 From: acoulton Date: Mon, 9 Dec 2024 17:09:10 +0000 Subject: [PATCH 1/2] change: Add hard parameter and return typehints to all classes --- CHANGELOG.md | 1 + src/Dependency/ConnectionConfigProvider.php | 37 +++++++---------- src/Dependency/DoctrineFactory.php | 46 +++++++-------------- src/Dependency/EventDispatchFactory.php | 12 ++---- src/ExplicitClasslistAttributeDriver.php | 4 +- 5 files changed, 37 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b51363..a5fede3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [BREAKING] Switch to using PSR-6 cache implementations for metadata, query and result caches: - drops the composer dependency on doctrine/cache in favour of symfony/cache - DoctrineCacheFactory methods are now hard-typehinted to return CacheItemPoolInterface +* [BREAKING] All classes and methods now have hard typehints and return values * Narrow supported dependency versions to the current latest minor of the suppoerted major version. * Drop support for PHP < 8.2 diff --git a/src/Dependency/ConnectionConfigProvider.php b/src/Dependency/ConnectionConfigProvider.php index 8472e11..bdefed9 100644 --- a/src/Dependency/ConnectionConfigProvider.php +++ b/src/Dependency/ConnectionConfigProvider.php @@ -12,45 +12,38 @@ class ConnectionConfigProvider { - /** - * @var array - */ - protected $config; + protected array $config; /** * - * @param array $config e.g. the connection group from the database connection config. + * @param ?array $config e.g. the connection group from the database connection config. * [NB] it is not expected to be valid for this to be empty at runtime, but allowing a null * value allows us to create an instance in development / test environments without full * config. */ - public function __construct(array $config = NULL) + public function __construct(?array $config = NULL) { - $this->config = \array_merge( - [ - 'type' => 'MySQL', - 'connection' => [ - 'hostname' => NULL, - 'database' => NULL, - 'username' => NULL, - 'password' => NULL, - ], - 'charset' => 'utf8', - 'timeout_seconds' => 5, + $this->config = [ + 'type' => 'MySQL', + 'connection' => [ + 'hostname' => NULL, + 'database' => NULL, + 'username' => NULL, + 'password' => NULL, ], - $config ?: [] - ); + 'charset' => 'utf8', + 'timeout_seconds' => 5, + ...($config ?? []), + ]; } /** * Convert the config to a doctrine connection array, including a NullPDO if there's no DB * - * @return array - * * @throws \InvalidArgumentException */ - public function getConnection() + public function getConnection(): array { if ($this->config['type'] !== 'MySQL') { throw new \InvalidArgumentException( diff --git a/src/Dependency/DoctrineFactory.php b/src/Dependency/DoctrineFactory.php index 8534df5..f564bd7 100644 --- a/src/Dependency/DoctrineFactory.php +++ b/src/Dependency/DoctrineFactory.php @@ -9,6 +9,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Ingenerator\KohanaDoctrine\ExplicitClasslistAttributeDriver; +use PDO; use Psr\Cache\CacheItemPoolInterface; class DoctrineFactory @@ -17,9 +18,8 @@ class DoctrineFactory /** * Core definitions for all the services required to run with doctrine in our default configuration * - * @return array */ - public static function definitions() + public static function definitions(): array { return [ 'doctrine' => [ @@ -136,11 +136,8 @@ public static function definitions() * Note also that subscribers CANNOT have any dependency on the doctrine.entity_manager as that will create circular * reference problems during construction of the entity manager. * - * @param array $subscribers - * - * @return array */ - public static function subscriberDefinitions(array $subscribers) + public static function subscriberDefinitions(array $subscribers): array { $defs = [ 'event_manager' => [ @@ -165,18 +162,13 @@ public static function subscriberDefinitions(array $subscribers) } /** - * @param ConnectionConfigProvider $conn - * @param Configuration $config - * @param EventManager $event_manager - * - * @return EntityManager * @throws \Doctrine\ORM\ORMException */ public static function buildEntityManager( ConnectionConfigProvider $conn, Configuration $config, EventManager $event_manager - ) { + ): EntityManager { return EntityManager::create( $conn->getConnection(), $config, @@ -187,7 +179,6 @@ public static function buildEntityManager( /** * Creates and configures the ORM config * - * @return Configuration * @throws \Doctrine\DBAL\DBALException */ public static function buildORMConfig( @@ -195,16 +186,14 @@ public static function buildORMConfig( CacheItemPoolInterface $compiler_cache, CacheItemPoolInterface $data_cache, ?array $config = NULL - ) { - $config = \array_merge( - [ - 'auto_gen_proxies' => \Kohana::$environment === \Kohana::DEVELOPMENT, - 'proxy_dir' => APPPATH.'/DoctrineEntityProxy', - 'proxy_namespace' => 'DoctrineEntityProxy', - 'custom_types' => [], - ], - $config ?? [] - ); + ):Configuration { + $config = [ + 'auto_gen_proxies' => \Kohana::$environment === \Kohana::DEVELOPMENT, + 'proxy_dir' => APPPATH.'/DoctrineEntityProxy', + 'proxy_namespace' => 'DoctrineEntityProxy', + 'custom_types' => [], + ...($config ?? []), + ]; $orm_cfg = new \Doctrine\ORM\Configuration; $orm_cfg->setMetadataDriverImpl($meta_driver); @@ -228,12 +217,7 @@ public static function buildORMConfig( return $orm_cfg; } - /** - * @param EntityManager $entityManager - * - * @return \Doctrine\DBAL\Driver\Connection - */ - public static function getRawPDO(EntityManager $entityManager) + public static function getRawPDO(EntityManager $entityManager): PDO { // NOTE: getNativeConnection() returns a raw PDO object, *not* a Doctrine extension of the PDO object // as in DBAL < 3. That is because in DBAL >= 3, the doctrine connection object is a *proxy* to the @@ -249,9 +233,9 @@ public static function getRawPDO(EntityManager $entityManager) // to couple things that currently know nothing about doctrine such as the MysqlSession handler from // php-utils. $driver = $entityManager->getConnection()->getNativeConnection(); - if ( ! $driver instanceof \PDO) { + if ( ! $driver instanceof PDO) { throw new \InvalidArgumentException( - 'Expected Doctrine connection to be instance of '.\PDO::class.', got '.\get_class($driver) + 'Expected Doctrine connection to be instance of '.PDO::class.', got '.\get_class($driver) ); } diff --git a/src/Dependency/EventDispatchFactory.php b/src/Dependency/EventDispatchFactory.php index 65aeea0..0f586da 100644 --- a/src/Dependency/EventDispatchFactory.php +++ b/src/Dependency/EventDispatchFactory.php @@ -9,19 +9,15 @@ class EventDispatchFactory { - /** - * @param EventSubscriber|NULL $subscriber,... - * - * @return EventManager - */ - public static function buildEventManagerWithSubscribers(EventSubscriber $subscriber = NULL) + + public static function buildEventManagerWithSubscribers(EventSubscriber ...$subscribers): EventManager { $manager = new EventManager; - foreach (\func_get_args() as $subscriber) { + foreach ($subscribers as $subscriber) { $manager->addEventSubscriber($subscriber); } return $manager; } -} \ No newline at end of file +} diff --git a/src/ExplicitClasslistAttributeDriver.php b/src/ExplicitClasslistAttributeDriver.php index 8da2d9e..cc32eba 100644 --- a/src/ExplicitClasslistAttributeDriver.php +++ b/src/ExplicitClasslistAttributeDriver.php @@ -19,7 +19,7 @@ public function __construct(?array $entity_classes = NULL) $this->classNames = $entity_classes ?? []; } - public function getAllClassNames() + public function getAllClassNames(): array { foreach ($this->classNames as $class_name) { if ( ! \class_exists($class_name)) { @@ -35,7 +35,7 @@ public function getPaths() throw new \BadMethodCallException(__CLASS__.' does not support access to entity paths'); } - public function addPaths(array $paths) + public function addPaths(array $paths): void { if ($paths === []) { // This is always called by the constructor as of doctrine/persistence@2.4.0 From 3cb447f27fd9b39a4027f9e92f49a12440d052b0 Mon Sep 17 00:00:00 2001 From: acoulton Date: Mon, 9 Dec 2024 17:11:49 +0000 Subject: [PATCH 2/2] chore: Migrate off deprecated way of constructing EntityManager --- src/Dependency/DoctrineFactory.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Dependency/DoctrineFactory.php b/src/Dependency/DoctrineFactory.php index f564bd7..03aae74 100644 --- a/src/Dependency/DoctrineFactory.php +++ b/src/Dependency/DoctrineFactory.php @@ -4,6 +4,7 @@ use Doctrine\Common\EventManager; +use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Types\Type; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManager; @@ -169,11 +170,9 @@ public static function buildEntityManager( Configuration $config, EventManager $event_manager ): EntityManager { - return EntityManager::create( - $conn->getConnection(), - $config, - $event_manager - ); + $connection = DriverManager::getConnection($conn->getConnection(), $config, $event_manager); + + return new EntityManager($connection, $config); } /**