Skip to content

Commit

Permalink
Phpstan: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 3, 2023
1 parent 427f2ee commit 79ad20b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 129 deletions.
38 changes: 1 addition & 37 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,4 @@ parameters:
- src

ignoreErrors:
-
message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|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\\<object\\> but returns array\\<object\\|string\\>\\.$#"
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.*#'
6 changes: 0 additions & 6 deletions src/DI/DbalConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand Down
21 changes: 10 additions & 11 deletions src/DI/DbalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
91 changes: 38 additions & 53 deletions src/Events/ContainerAwareEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<object|string>
*/
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<string, array<object|string>>
*/
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]);
}
Expand All @@ -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
Expand All @@ -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])) {
Expand All @@ -143,4 +106,26 @@ public function removeEventListener($events, $listener): void
}
}

/**
* @return array<object|string>
*/
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] ?? [];
}

}
27 changes: 8 additions & 19 deletions src/Events/DebugEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>
*/
public function getListeners($event = null): array
public function getListeners(string $event): array
{
return $this->inner->getListeners($event);
}

/**
* @return object[]|object[][]
* @return array<string, array<object>>
*/
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);
}
Expand All @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Tracy/BlueScreen/DbalBlueScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 79ad20b

Please sign in to comment.