Skip to content

Commit

Permalink
Beautify codestyle to PSR-12
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Barášek committed May 11, 2021
1 parent e26b033 commit db51412
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 152 deletions.
5 changes: 4 additions & 1 deletion src/Cache/SQLite3Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public function __construct(string $cachePath, int $ttl = 25)
$cache = new \SQLite3($cachePath);
$cache->enableExceptions(true);
if ($cache->busyTimeout(60_000) === false) {
throw new \RuntimeException('SQLite3 cache: Can not set busy timeout: ' . $cache->lastErrorMsg(), $cache->lastErrorCode());
throw new \RuntimeException(
'SQLite3 cache: Can not set busy timeout: ' . $cache->lastErrorMsg(),
$cache->lastErrorCode(),
);
}
do {
try {
Expand Down
173 changes: 109 additions & 64 deletions src/DatabaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ final class DatabaseExtension extends CompilerExtension
{
public const TAG_DOCTRINE_SUBSCRIBER = 'doctrine.subscriber';

/** @var string[] (type => typeClass) */
/** @var array<string, string> (type => typeClass) */
private static array $customTypes = [
'uuid' => UuidType::class,
'uuid-binary' => UuidBinaryType::class,
];

/** @var string[] (name => typeClass) */
/** @var array<string, string> (name => typeClass) */
private static array $customNumericFunctions = [
'RAND' => RandFunction::class,
'ROUND' => RoundFunction::class,
Expand Down Expand Up @@ -78,47 +78,57 @@ public static function addCustomNumericFunction(string $name, string $type): voi

public function getConfigSchema(): Schema
{
return Expect::structure([
'connection' => Expect::structure([
'host' => Expect::string()->required(),
'dbname' => Expect::string()->required(),
'user' => Expect::string()->required(),
'password' => Expect::string()->nullable(),
'url' => Expect::string()->nullable(),
'pdo' => Expect::string()->nullable(),
'memory' => Expect::string()->nullable(),
'driver' => Expect::string('pdo_mysql'),
'driverClass' => Expect::string()->nullable(),
'driverOptions' => Expect::array(),
'unix_socket' => Expect::string()->nullable(),
'port' => Expect::int()->nullable(),
'servicename' => Expect::string()->nullable(),
'charset' => Expect::string('UTF8'),
'portability' => Expect::int(PortabilityConnection::PORTABILITY_ALL),
'fetchCase' => Expect::int(\PDO::CASE_LOWER),
'persistent' => Expect::bool(true),
'types' => Expect::array(),
'typesMapping' => Expect::array(),
'wrapperClass' => Expect::string()->nullable(),
])->castTo('array')->required(),
'configuration' => Expect::structure([
'sqlLogger' => Expect::string()->nullable(),
'resultCacheImpl' => Expect::string()->nullable(),
'filterSchemaAssetsExpression' => Expect::string()->nullable(),
'autoCommit' => Expect::bool()->default(true),
])->castTo('array'),
'cache' => Expect::string(),
'types' => Expect::arrayOf(Expect::string())->default([]),
'customNumericFunctions' => Expect::arrayOf(Expect::string()),
'propertyIgnoreAnnotations' => Expect::arrayOf(Expect::string())->default([]),
])->castTo('array')->otherItems(Expect::mixed());
return Expect::structure(
[
'connection' => Expect::structure(
[
'host' => Expect::string()->required(),
'dbname' => Expect::string()->required(),
'user' => Expect::string()->required(),
'password' => Expect::string()->nullable(),
'url' => Expect::string()->nullable(),
'pdo' => Expect::string()->nullable(),
'memory' => Expect::string()->nullable(),
'driver' => Expect::string('pdo_mysql'),
'driverClass' => Expect::string()->nullable(),
'driverOptions' => Expect::array(),
'unix_socket' => Expect::string()->nullable(),
'port' => Expect::int()->nullable(),
'servicename' => Expect::string()->nullable(),
'charset' => Expect::string('UTF8'),
'portability' => Expect::int(PortabilityConnection::PORTABILITY_ALL),
'fetchCase' => Expect::int(\PDO::CASE_LOWER),
'persistent' => Expect::bool(true),
'types' => Expect::array(),
'typesMapping' => Expect::array(),
'wrapperClass' => Expect::string()->nullable(),
]
)->castTo('array')->required(),
'configuration' => Expect::structure(
[
'sqlLogger' => Expect::string()->nullable(),
'resultCacheImpl' => Expect::string()->nullable(),
'filterSchemaAssetsExpression' => Expect::string()->nullable(),
'autoCommit' => Expect::bool()->default(true),
]
)->castTo('array'),
'cache' => Expect::string(),
'types' => Expect::arrayOf(Expect::string())->default([]),
'customNumericFunctions' => Expect::arrayOf(Expect::string()),
'propertyIgnoreAnnotations' => Expect::arrayOf(Expect::string())->default([]),
]
)->castTo('array')->otherItems(Expect::mixed());
}


public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
OrmAnnotationsExtension::addAnnotationPathToManager($builder, 'Baraja\Doctrine\Entity', __DIR__ . '/Entity');
OrmAnnotationsExtension::addAnnotationPathToManager(
$builder,
'Baraja\Doctrine\Entity',
__DIR__ . '/Entity',
);

$this->loadDoctrineConfiguration();
$this->loadConnectionConfiguration();
Expand All @@ -143,16 +153,25 @@ public function beforeCompile(): void
$eventManager = $builder->getDefinition($this->prefix('eventManager'));
foreach (array_keys($builder->findByTag(self::TAG_DOCTRINE_SUBSCRIBER)) as $serviceName) {
$class = $builder->getDefinition($serviceName)->getType();

if ($class === null || !is_subclass_of($class, EventSubscriber::class)) {
throw new \RuntimeException('Subscriber "' . $serviceName . '" does not implement "' . EventSubscriber::class . '".');
throw new \RuntimeException(
'Subscriber "' . $serviceName . '" does not implement "' . EventSubscriber::class . '".'
);
}
try {
$eventManager->addSetup('?->addEventListener(?, ?)', [
'@self',
call_user_func([(new \ReflectionClass($class))->newInstanceWithoutConstructor(), 'getSubscribedEvents']),
$serviceName, // Intentionally without @ for laziness.
]);
$eventManager->addSetup(
'?->addEventListener(?, ?)',
[
'@self',
call_user_func(
[
(new \ReflectionClass($class))->newInstanceWithoutConstructor(),
'getSubscribedEvents',
]
),
$serviceName, // Intentionally without @ for laziness.
]
);
} catch (\ReflectionException $e) {
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -161,18 +180,29 @@ public function beforeCompile(): void
$types = [];
foreach (array_merge(self::$customTypes, $config['types'] ?? []) as $type => $typeClass) {
if (\class_exists($typeClass) === false) {
throw new ConfiguratorException('Doctrine type "' . $type . '" does not exist, because class "' . $typeClass . '" is not defined.');
throw new ConfiguratorException(
'Doctrine type "' . $type . '" does not exist, because class "' . $typeClass . '" is not defined.',
);
}
$types[$type] = $typeClass;
}

$functionsCode = '';
foreach (array_merge(self::$customNumericFunctions, $config['customNumericFunctions'] ?? []) as $functionName => $functionType) {
foreach (
array_merge(
self::$customNumericFunctions,
$config['customNumericFunctions'] ?? [],
) as $functionName => $functionType
) {
if (\class_exists($functionType) === false) {
throw new ConfiguratorException('Doctrine function definition "' . $functionName . '" does not exist, because class "' . $functionType . '" is not defined.');
throw new ConfiguratorException(
'Doctrine function definition "' . $functionName . '" does not exist, '
. 'because class "' . $functionType . '" is not defined.',
);
}
$functionsCode .= ($functionsCode ? "\n\t" : '')
. '$entityManager->getConfiguration()->addCustomNumericFunction(\'' . strtoupper($functionName) . '\', ' . $functionType . '::class);';
. '$entityManager->getConfiguration()->addCustomNumericFunction(\'' . strtoupper($functionName) . '\', '
. $functionType . '::class);';
}

if (interface_exists(ProjectEntityRepository::class)) {
Expand Down Expand Up @@ -203,11 +233,14 @@ public function beforeCompile(): void
[
'@self',
$types,
array_merge([
'sample',
'endpointName',
'editable',
], $config['propertyIgnoreAnnotations'] ?? []),
array_merge(
[
'sample',
'endpointName',
'editable',
],
$config['propertyIgnoreAnnotations'] ?? [],
),
],
);
}
Expand Down Expand Up @@ -298,7 +331,10 @@ private function loadDoctrineConfiguration(): void
$configuration->addSetup('setResultCacheImpl', [$config['configuration']['resultCacheImpl']]);
}
if ($config['configuration']['filterSchemaAssetsExpression'] !== null) { // FilterSchemaAssetsExpression
$configuration->addSetup('setFilterSchemaAssetsExpression', [$config['configuration']['filterSchemaAssetsExpression']]);
$configuration->addSetup(
'setFilterSchemaAssetsExpression',
[$config['configuration']['filterSchemaAssetsExpression']]
);
}

// AutoCommit
Expand All @@ -320,8 +356,10 @@ private function loadConnectionConfiguration(): void
) {
throw new \RuntimeException(
'Connection port (suffix included in host string) and given port are different.' . "\n"
. 'Given host "' . $config['connection']['host'] . '" contains port "' . $hostParser[2] . '", but given port is "' . $config['connection']['port'] . '".' . "\n"
. 'To solve this issue: Change "host" string to "' . $hostParser[1] . '" (without ":' . $hostParser[2] . '") or change port to "' . $config['connection']['port'] . '".',
. 'Given host "' . $config['connection']['host'] . '" contains port "' . $hostParser[2] . '", '
. 'but given port is "' . $config['connection']['port'] . '".' . "\n"
. 'To solve this issue: Change "host" string to "' . $hostParser[1] . '" '
. '(without ":' . $hostParser[2] . '") or change port to "' . $config['connection']['port'] . '".',
);
}
$config['connection']['host'] = $hostParser[1];
Expand All @@ -332,8 +370,9 @@ private function loadConnectionConfiguration(): void
&& preg_match('/^.+?\.ondigitalocean\.com$/', $config['connection']['host'])
) { // DigitalOcean managed database support
throw new \RuntimeException(
'In case of DigitalOcean (host is "' . $config['connection']['host'] . '") you must define port (as integer) in your NEON configuration, but NULL given.' . "\n"
. 'Hint: Check if your current IP "' . Utils::userIp() . '" is allowed for connection.',
'In case of DigitalOcean (host is "' . $config['connection']['host'] . '") '
. 'you must define port (as integer) in your NEON configuration, but NULL given.'
. "\n" . 'Hint: Check if your current IP "' . Utils::userIp() . '" is allowed for connection.',
);
}

Expand All @@ -348,15 +387,21 @@ private function loadConnectionConfiguration(): void
}

$builder->addDefinition($this->prefix('connectionFactory'))
->setFactory(ConnectionFactory::class, [$config['connection']['types'], $config['connection']['typesMapping']]);
->setFactory(
ConnectionFactory::class,
[$config['connection']['types'], $config['connection']['typesMapping']]
);

$builder->addDefinition($this->prefix('connection'))
->setFactory(Connection::class)
->setFactory('@' . $this->prefix('connectionFactory') . '::createConnection', [
$config['connection'],
'@' . $this->prefix('configuration'),
$builder->getDefinitionByType(EventManager::class),
]);
->setFactory(
'@' . $this->prefix('connectionFactory') . '::createConnection',
[
$config['connection'],
'@' . $this->prefix('configuration'),
$builder->getDefinitionByType(EventManager::class),
]
);
}


Expand Down
16 changes: 4 additions & 12 deletions src/Dbal/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@

class ConnectionFactory
{

/** @var mixed[] */
private array $typesConfig;

/** @var mixed[] */
private array $typesMapping;

/** @var mixed[] */
private array $commentedTypes = [];

Expand All @@ -33,10 +26,10 @@ class ConnectionFactory
* @param mixed[] $typesConfig
* @param mixed[] $typesMapping
*/
public function __construct(array $typesConfig = [], array $typesMapping = [])
{
$this->typesConfig = $typesConfig;
$this->typesMapping = $typesMapping;
public function __construct(
private array $typesConfig = [],
private array $typesMapping = [],
) {
}


Expand Down Expand Up @@ -115,7 +108,6 @@ private function initializeTypes(): void
} else {
Type::addType($type, $typeConfig['class']);
}

if ($typeConfig['commented']) {
$this->commentedTypes[] = $type;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Dbal/Events/ContainerAwareEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@

final class ContainerAwareEventManager extends DoctrineEventManager
{
protected Container $container;

/** @var bool[] */
protected array $initialized = [];

/** @var EventSubscriber[][]|mixed[][] */
protected array $listeners = [];


public function __construct(Container $container)
{
$this->container = $container;
public function __construct(
protected Container $container,
) {
}


Expand Down
9 changes: 3 additions & 6 deletions src/Dbal/Events/DebugEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@

final class DebugEventManager extends DoctrineEventManager
{
private EventManager $inner;


public function __construct(EventManager $inner)
{
$this->inner = $inner;
public function __construct(
private EventManager $inner,
) {
}


Expand Down
Loading

0 comments on commit db51412

Please sign in to comment.