From 79ad20b11af3617ead8e424f6838fbd41a38024d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Mon, 16 Jan 2023 15:51:11 +0100 Subject: [PATCH] Phpstan: fixes --- phpstan.neon | 38 +--------- src/DI/DbalConsoleExtension.php | 6 -- src/DI/DbalExtension.php | 21 +++--- src/Events/ContainerAwareEventManager.php | 91 ++++++++++------------- src/Events/DebugEventManager.php | 27 ++----- src/Tracy/BlueScreen/DbalBlueScreen.php | 4 +- 6 files changed, 58 insertions(+), 129 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index aab8ba4..a6c803b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,40 +10,4 @@ parameters: - src ignoreErrors: - - - message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#" - count: 1 - path: src/DI/DbalExtension.php - - - - message: "#^Parameter \\#1 \\$function of function call_user_func expects callable\\(\\)\\: mixed, array\\{object, 'getSubscribedEvents'\\} given\\.$#" - count: 1 - path: src/DI/DbalExtension.php - - - - message: "#^Method Nettrine\\\\DBAL\\\\Events\\\\ContainerAwareEventManager\\:\\:getInitializedListeners\\(\\) should return array\\ but returns array\\\\.$#" - count: 2 - path: src/Events/ContainerAwareEventManager.php - - - - message: "#^Fetching class constant class of deprecated class Doctrine\\\\DBAL\\\\Logging\\\\LoggerChain\\.$#" - count: 1 - path: src/DI/DbalExtension.php - - - - message: """ - #^Class Nettrine\\\\DBAL\\\\Logger\\\\AbstractLogger implements deprecated interface Doctrine\\\\DBAL\\\\Logging\\\\SQLLogger\\: - Use \\{@see \\\\Doctrine\\\\DBAL\\\\Logging\\\\Middleware\\} or implement - \\{@see \\\\Doctrine\\\\DBAL\\\\Driver\\\\Middleware\\} instead\\.$# - """ - count: 1 - path: src/Logger/AbstractLogger.php - - - - message: """ - #^Class Nettrine\\\\DBAL\\\\Logger\\\\PsrLogger implements deprecated interface Doctrine\\\\DBAL\\\\Logging\\\\SQLLogger\\: - Use \\{@see \\\\Doctrine\\\\DBAL\\\\Logging\\\\Middleware\\} or implement - \\{@see \\\\Doctrine\\\\DBAL\\\\Driver\\\\Middleware\\} instead\\.$# - """ - count: 1 - path: src/Logger/PsrLogger.php + - '#.*deprecated class Doctrine\\DBAL\\Logging\\LoggerChain|SQLLogger.*#' diff --git a/src/DI/DbalConsoleExtension.php b/src/DI/DbalConsoleExtension.php index 80ee798..3f1f608 100644 --- a/src/DI/DbalConsoleExtension.php +++ b/src/DI/DbalConsoleExtension.php @@ -2,7 +2,6 @@ namespace Nettrine\DBAL\DI; -use Doctrine\DBAL\Tools\Console\Command\ReservedWordsCommand; use Doctrine\DBAL\Tools\Console\Command\RunSqlCommand; use Doctrine\DBAL\Tools\Console\ConnectionProvider\SingleConnectionProvider; use Nette\DI\CompilerExtension; @@ -36,11 +35,6 @@ public function loadConfiguration(): void ->setAutowired(false); //Commands - $builder->addDefinition($this->prefix('reservedWordsCommand')) - ->setFactory(ReservedWordsCommand::class, [$connectionProvider]) - ->addTag('console.command', 'dbal:reserved-words') - ->setAutowired(false); - $builder->addDefinition($this->prefix('runSqlCommand')) ->setFactory(RunSqlCommand::class, [$connectionProvider]) ->addTag('console.command', 'dbal:run-sql') diff --git a/src/DI/DbalExtension.php b/src/DI/DbalExtension.php index e433be6..7253553 100644 --- a/src/DI/DbalExtension.php +++ b/src/DI/DbalExtension.php @@ -200,20 +200,19 @@ public function beforeCompile(): void { $builder = $this->getContainerBuilder(); - // Idea by @enumag - // https://github.com/Arachne/EventManager/blob/master/src/DI/EventManagerExtension.php - /** @var ServiceDefinition $eventManager */ $eventManager = $builder->getDefinition($this->prefix('eventManager')); + foreach ($builder->findByType(EventSubscriber::class) as $serviceName => $serviceDef) { - $eventManager->addSetup( - '?->addEventListener(?, ?)', - [ - '@self', - call_user_func([(new ReflectionClass((string) $serviceDef->getType()))->newInstanceWithoutConstructor(), 'getSubscribedEvents']), - $serviceName, // Intentionally without @ for laziness. - ] - ); + /** @var class-string $serviceClass */ + $serviceClass = (string) $serviceDef->getType(); + $rc = new ReflectionClass($serviceClass); + + /** @var EventSubscriber $subscriber */ + $subscriber = $rc->newInstanceWithoutConstructor(); + $events = $subscriber->getSubscribedEvents(); + + $eventManager->addSetup('?->addEventListener(?, ?)', ['@self', $events, $serviceName]); } } diff --git a/src/Events/ContainerAwareEventManager.php b/src/Events/ContainerAwareEventManager.php index 9f8e978..8d43ab3 100644 --- a/src/Events/ContainerAwareEventManager.php +++ b/src/Events/ContainerAwareEventManager.php @@ -24,74 +24,38 @@ public function __construct(Container $container) $this->container = $container; } - /** - * @param string $eventName - * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint - */ - public function dispatchEvent($eventName, ?EventArgs $eventArgs = null): void + public function dispatchEvent(string $eventName, ?EventArgs $eventArgs = null): void { $eventArgs = $eventArgs ?? EventArgs::getEmptyInstance(); - foreach ($this->getInitializedListeners($eventName) as $hash => $listener) { + foreach ($this->getInitializedListeners($eventName) as $listener) { $listener->$eventName($eventArgs); } } /** - * @param string|null $event - * @return object[]|object[][] - * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint + * @return array */ - public function getListeners($event = null): array + public function getListeners(string $event): array { - if ($event !== null) { - return $this->getInitializedListeners($event); - } - - $stack = []; - - foreach ($this->listeners as $eventName => $listeners) { - $stack[$eventName] = $this->getInitializedListeners($eventName); - } - - return $stack; + return $this->getInitializedListeners($event); } /** - * @return object[]|object[][] + * @return array> */ public function getAllListeners(): array { - return $this->getListeners(); - } - - /** - * @return object[] - */ - private function getInitializedListeners(string $event): array - { - $initialized = $this->initialized[$event] ?? false; + $allListeners = []; - if ($initialized) { - return $this->listeners[$event] ?? []; + foreach ($this->listeners as $event => $listeners) { + $allListeners[$event] = $this->getListeners($event); } - foreach ($this->listeners[$event] ?? [] as $hash => $listener) { - if (!is_object($listener)) { - $this->listeners[$event][$hash] = $this->container->getService($listener); - } - } - - $this->initialized[$event] = true; - - return $this->listeners[$event] ?? []; + return $allListeners; } - /** - * @param string $event - * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint - */ - public function hasListeners($event): bool + public function hasListeners(string $event): bool { return !empty($this->listeners[$event]); } @@ -100,7 +64,7 @@ public function hasListeners($event): bool * Adds an event listener that listens on the specified events. * * @param string|string[] $events The event(s) to listen on. - * @param string|object $listener The listener object. + * @param string|object $listener The listener object. * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ public function addEventListener($events, $listener): void @@ -126,15 +90,14 @@ public function addEventListener($events, $listener): void /** * Removes an event listener from the specified events. * - * @param string|string[] $events - * @param string|int|object $listener + * @param string|string[] $events + * @param string|object $listener * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ public function removeEventListener($events, $listener): void { - $hash = !is_object($listener) - ? 'service@' . $listener - : spl_object_hash($listener); + $hash = !is_object($listener) ? 'service@' . $listener : spl_object_hash($listener); + foreach ((array) $events as $event) { // Check if actually have this listener associated if (isset($this->listeners[$event][$hash])) { @@ -143,4 +106,26 @@ public function removeEventListener($events, $listener): void } } + /** + * @return array + */ + private function getInitializedListeners(string $event): array + { + $initialized = $this->initialized[$event] ?? false; + + if ($initialized) { + return $this->listeners[$event] ?? []; + } + + foreach ($this->listeners[$event] ?? [] as $hash => $listener) { + if (!is_object($listener)) { + $this->listeners[$event][$hash] = $this->container->getService($listener); + } + } + + $this->initialized[$event] = true; + + return $this->listeners[$event] ?? []; + } + } diff --git a/src/Events/DebugEventManager.php b/src/Events/DebugEventManager.php index 811e1bf..e331e75 100644 --- a/src/Events/DebugEventManager.php +++ b/src/Events/DebugEventManager.php @@ -17,38 +17,28 @@ public function __construct(EventManager $inner) $this->inner = $inner; } - /** - * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint - * @param string $eventName - */ - public function dispatchEvent($eventName, ?EventArgs $eventArgs = null): void + public function dispatchEvent(string $eventName, ?EventArgs $eventArgs = null): void { $this->inner->dispatchEvent($eventName, $eventArgs); } /** - * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint - * @param string|NULL $event - * @return object[]|object[][] + * @return array */ - public function getListeners($event = null): array + public function getListeners(string $event): array { return $this->inner->getListeners($event); } /** - * @return object[]|object[][] + * @return array> */ public function getAllListeners(): array { - return $this->getListeners(); + return $this->inner->getAllListeners(); } - /** - * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint - * @param string $event - */ - public function hasListeners($event): bool + public function hasListeners(string $event): bool { return $this->inner->hasListeners($event); } @@ -68,11 +58,10 @@ public function addEventListener($events, $listener): void /** * Removes an event listener from the specified events. * - * @param string|string $events - * @param object $listener + * @param string|string[] $events * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint */ - public function removeEventListener($events, $listener): void + public function removeEventListener($events, object $listener): void { $this->inner->removeEventListener($events, $listener); } diff --git a/src/Tracy/BlueScreen/DbalBlueScreen.php b/src/Tracy/BlueScreen/DbalBlueScreen.php index 14fe105..e3654ce 100644 --- a/src/Tracy/BlueScreen/DbalBlueScreen.php +++ b/src/Tracy/BlueScreen/DbalBlueScreen.php @@ -41,9 +41,7 @@ public static function renderException(?Throwable $e): ?array ]; } } elseif ($e instanceof PDOException) { - if (isset($e->queryString)) { - $sql = $e->queryString; - } elseif ($item = Helpers::findTrace($e->getTrace(), Connection::class . '::executeQuery')) { + if ($item = Helpers::findTrace($e->getTrace(), Connection::class . '::executeQuery')) { $sql = $item['args'][0]; } elseif ($item = Helpers::findTrace($e->getTrace(), PDO::class . '::query')) { $sql = $item['args'][0];