Skip to content
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

v3: Add hard typehints throughout #22

Merged
merged 2 commits into from
Dec 10, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 15 additions & 22 deletions src/Dependency/ConnectionConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
55 changes: 19 additions & 36 deletions src/Dependency/DoctrineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@


use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAttributeDriver;
use PDO;
use Psr\Cache\CacheItemPoolInterface;

class DoctrineFactory
Expand All @@ -17,9 +19,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' => [
Expand Down Expand Up @@ -136,11 +137,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' => [
Expand All @@ -165,46 +163,36 @@ 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
) {
return EntityManager::create(
$conn->getConnection(),
$config,
$event_manager
);
): EntityManager {
$connection = DriverManager::getConnection($conn->getConnection(), $config, $event_manager);

return new EntityManager($connection, $config);
}

/**
* Creates and configures the ORM config
*
* @return Configuration
* @throws \Doctrine\DBAL\DBALException
*/
public static function buildORMConfig(
MappingDriver $meta_driver,
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);

Expand All @@ -228,12 +216,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
Expand All @@ -249,9 +232,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)
);
}

Expand Down
12 changes: 4 additions & 8 deletions src/Dependency/EventDispatchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
}
4 changes: 2 additions & 2 deletions src/ExplicitClasslistAttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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/[email protected]
Expand Down
Loading