Skip to content

Commit

Permalink
install psalm in level 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed Aug 14, 2021
1 parent cb68515 commit 7d19177
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 98 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"phpstan/phpstan": "^0.11.5 || ^0.12.0",
"phpunit/phpunit": "^8.0 || ^9.0",
"squizlabs/php_codesniffer": "^3.4",
"symfony/var-dumper": "^4.2 || ^5.0"
"symfony/var-dumper": "^4.2 || ^5.0",
"vimeo/psalm": "^4.9"
},
"autoload": {
"psr-4": {
Expand Down
53 changes: 53 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
errorLevel="1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->

<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />
<DeprecatedConstant errorLevel="info" />
<DeprecatedInterface errorLevel="info" />
<DeprecatedTrait errorLevel="info" />

<InternalMethod errorLevel="info" />
<InternalProperty errorLevel="info" />
<InternalClass errorLevel="info" />

<MissingClosureReturnType errorLevel="info" />
<MissingReturnType errorLevel="info" />
<MissingPropertyType errorLevel="info" />
<InvalidDocblock errorLevel="info" />

<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor errorLevel="info" />
<MissingClosureParamType errorLevel="info" />
<MissingParamType errorLevel="info" />

<RedundantCondition errorLevel="info" />

<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />

<UnresolvableInclude errorLevel="info" />

<RawObjectIteration errorLevel="info" />

<InvalidStringClass errorLevel="info" />
</issueHandlers>
</psalm>
10 changes: 7 additions & 3 deletions src/ActionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Throwable;
use function array_key_exists;

class ActionContainer implements ContainerInterface
final class ActionContainer implements ContainerInterface
{
/** @var array<callable> */
private array $actions;
Expand All @@ -23,7 +23,7 @@ public function __construct(array $actions)
$this->actions = $actions;
}

public function get($id): callable
public function get(string $id): callable
{
if (false === $this->has($id)) {
throw ActionNotFoundException::withId($id);
Expand All @@ -35,10 +35,14 @@ public function get($id): callable
throw ActionContainerException::withIdAndPreviousException($id, $exception);
}

if (false === is_callable($action)) {
throw ActionContainerException::withIdAndInvalidCallable($id);
}

return $action;
}

public function has($id): bool
public function has(string $id): bool
{
return array_key_exists($id, $this->actions);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Cli/StartQueueConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Antidot\Queue\Cli;

use Antidot\Queue\Event\QueueConsumerStarted;
use Antidot\Queue\MessageProcessor;
use Enqueue\Consumption\QueueConsumerInterface;
use Enqueue\Consumption\Result;
use Interop\Queue\Context;
use Interop\Queue\Message;
use Interop\Queue\Processor;
Expand Down Expand Up @@ -56,9 +58,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new InvalidArgumentException(self::INVALID_NAME_MESSAGE);
}
$this->eventDispatcher->dispatch(QueueConsumerStarted::occur($queue));
/** @var MessageProcessor $processor */
$processor = $this->processor;
$context = $this->context;
$callback = static fn(Message $message) => $processor->process($message, $context);
$callback = static fn(Message $message): Result => $processor->process($message, $context);
$this->consumer->bindCallback($queue, $callback);
$this->consumer->consume();

Expand Down
17 changes: 13 additions & 4 deletions src/Container/ActionContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ public function __invoke(
ContainerInterface $container,
string $contextName = ConfigProvider::DEFAULT_CONTEXT
): ActionContainer {
$contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
foreach ($contextConfig[ConfigProvider::MESSAGE_TYPES_KEY] ?? [] as $messageType => $action) {
$actions[$messageType] = static fn() => $container->get($action);
/** @var array<string, array<string, mixed>> $config */
$config = $container->get(ConfigProvider::CONFIG_KEY);
$contextConfig = ConfigProvider::getContextConfig($contextName, $config);
$actions = [];
/** @var array<string, string> $messages */
$messages = $contextConfig[ConfigProvider::MESSAGE_TYPES_KEY] ?? [];
foreach ($messages as $messageType => $action) {
$actions[$messageType] = static function () use ($container, $action): callable {
/** @var callable $actionCallable */
$actionCallable = $container->get($action);
return $actionCallable;
};
}

return new ActionContainer($actions ?? []);
return new ActionContainer($actions);
}
}
5 changes: 3 additions & 2 deletions src/Container/Config/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ public function __invoke(): array
}

/**
* @param array<mixed> $config
* @return array<mixed>
* @param array<string, array<string, mixed>> $config
* @return array<string, mixed>
* @throws AssertionFailedException
*/
public static function getContextConfig(string $contextName, array $config): array
{
ConfigProvider::validate($config);

/** @var array<string, array<string, mixed>> $contextsConfig */
$contextsConfig = $config[ConfigProvider::QUEUES_KEY][ConfigProvider::CONTEXTS_KEY];
if (false === array_key_exists($contextName, $contextsConfig)) {
throw new InvalidArgumentException(sprintf(ConfigProvider::INVALID_CONTEXT_MESSAGE, $contextName));
Expand Down
28 changes: 20 additions & 8 deletions src/Container/ContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Antidot\Queue\Container\Config\ConfigProvider;
use Assert\Assertion;
use Doctrine\DBAL\Connection;
use Enqueue\Dbal\DbalContext;
use Enqueue\Fs\FsConnectionFactory;
use Enqueue\Null\NullContext;
Expand All @@ -31,32 +32,37 @@ public function __invoke(
ContainerInterface $container,
string $contextName = ConfigProvider::DEFAULT_CONTEXT
): Context {
$contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
/** @var array<string, array<string, mixed>> $config */
$config = $container->get(ConfigProvider::CONFIG_KEY);
$contextConfig = ConfigProvider::getContextConfig($contextName, $config);

/** @var string $contextType */
$contextType = $contextConfig[ConfigProvider::CONTEXTS_TYPE_KEY];
if (self::NULL === $contextType) {
return new NullContext();
}

Assertion::keyExists($contextConfig, 'context_params');
/** @var array<string, mixed> $contextParams */
$contextParams = $contextConfig['context_params'];
if (self::FILESYSTEM === $contextType) {
return $this->createFilesystemContext($contextConfig['context_params']);
return $this->createFilesystemContext($contextParams);
}

if (self::DBAL === $contextType) {
return $this->createDBALContext($container, $contextConfig['context_params']);
return $this->createDBALContext($container, $contextParams);
}

if (self::REDIS === $contextType) {
return $this->createRedisContext($contextConfig['context_params']);
return $this->createRedisContext($contextParams);
}

if (self::BEANSTALK === $contextType) {
return $this->createBeanstalkContext($contextConfig['context_params']);
return $this->createBeanstalkContext($contextParams);
}

if (self::SQS === $contextType) {
return $this->createSQSContext($contextConfig['context_params']);
return $this->createSQSContext($contextParams);
}

throw new InvalidArgumentException(sprintf('There is not implementation for given context %s.', $contextType));
Expand All @@ -73,8 +79,10 @@ private function createFilesystemContext(array $contextConfig): Context
'Install "enqueue/fs" package to run filesystem context.'
);
Assertion::keyExists($contextConfig, 'path', 'Absolute "path" is required to run filesystem context.');
$contextPath = $contextConfig['path'];
Assertion::string($contextPath, 'Absolute "path" must be string.');

return (new FsConnectionFactory($contextConfig['path']))->createContext();
return (new FsConnectionFactory($contextPath))->createContext();
}

/**
Expand All @@ -92,7 +100,11 @@ private function createDBALContext(ContainerInterface $container, array $context
'connection',
'The "connection" service name is required to run dbal context.'
);
$context = new DbalContext($container->get($contextConfig['connection']));
/** @var string $connectionName */
$connectionName = $contextConfig['connection'];
/** @var Connection $connection */
$connection = $container->get($connectionName);
$context = new DbalContext($connection);
$context->createDataBaseTable();

return $context;
Expand Down
5 changes: 4 additions & 1 deletion src/Container/LoggerExtensionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class LoggerExtensionFactory
{
public function __invoke(ContainerInterface $container): LoggerExtension
{
return new LoggerExtension($container->get(LoggerInterface::class));
/** @var LoggerInterface $logger */
$logger = $container->get(LoggerInterface::class);

return new LoggerExtension($logger);
}
}
14 changes: 11 additions & 3 deletions src/Container/MessageProcessorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Antidot\Queue\Container;

use Antidot\Queue\ActionContainer;
use Antidot\Queue\Container\Config\ConfigProvider;
use Antidot\Queue\MessageProcessor;
use Psr\Container\ContainerInterface;
Expand All @@ -15,9 +16,16 @@ public function __invoke(
ContainerInterface $container,
string $contextName = ConfigProvider::DEFAULT_CONTEXT
): MessageProcessor {
$contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
$actionContainer = $container->get($contextConfig[ConfigProvider::CONTAINER_KEY]);
/** @var array<string, array<string, mixed>> $config */
$config = $container->get(ConfigProvider::CONFIG_KEY);
$contextConfig = ConfigProvider::getContextConfig($contextName, $config);
/** @var string $actionContainerName */
$actionContainerName = $contextConfig[ConfigProvider::CONTAINER_KEY];
/** @var ActionContainer $actionContainer */
$actionContainer = $container->get($actionContainerName);
/** @var EventDispatcherInterface $eventDispatcher */
$eventDispatcher = $container->get(EventDispatcherInterface::class);

return new MessageProcessor($actionContainer, $container->get(EventDispatcherInterface::class));
return new MessageProcessor($actionContainer, $eventDispatcher);
}
}
10 changes: 8 additions & 2 deletions src/Container/ProducerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Antidot\Queue\Container\Config\ConfigProvider;
use Antidot\Queue\JobProducer;
use Antidot\Queue\Producer;
use Interop\Queue\Context;
use Psr\Container\ContainerInterface;

class ProducerFactory
Expand All @@ -15,8 +16,13 @@ public function __invoke(
ContainerInterface $container,
string $contextName = ConfigProvider::DEFAULT_CONTEXT
): Producer {
$contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
$context = $container->get($contextConfig[ConfigProvider::CONTEXT_SERVICE_KEY]);
/** @var array<string, array<string, mixed>> $config */
$config = $container->get(ConfigProvider::CONFIG_KEY);
$contextConfig = ConfigProvider::getContextConfig($contextName, $config);
/** @var string $contaxtServiceName */
$contaxtServiceName = $contextConfig[ConfigProvider::CONTEXT_SERVICE_KEY];
/** @var Context $context */
$context = $container->get($contaxtServiceName);

return new JobProducer($context);
}
Expand Down
44 changes: 35 additions & 9 deletions src/Container/QueueConsumerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,55 @@

use Antidot\Queue\Container\Config\ConfigProvider;
use Enqueue\Consumption\ChainExtension;
use Enqueue\Consumption\EndExtensionInterface;
use Enqueue\Consumption\ExtensionInterface;
use Enqueue\Consumption\InitLoggerExtensionInterface;
use Enqueue\Consumption\MessageReceivedExtensionInterface;
use Enqueue\Consumption\MessageResultExtensionInterface;
use Enqueue\Consumption\PostConsumeExtensionInterface;
use Enqueue\Consumption\PostMessageReceivedExtensionInterface;
use Enqueue\Consumption\PreConsumeExtensionInterface;
use Enqueue\Consumption\PreSubscribeExtensionInterface;
use Enqueue\Consumption\ProcessorExceptionExtensionInterface;
use Enqueue\Consumption\QueueConsumer;
use Enqueue\Consumption\QueueConsumerInterface;
use Enqueue\Consumption\StartExtensionInterface;
use Interop\Queue\Context;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;

class QueueConsumerFactory
{
public function __invoke(
ContainerInterface $container,
string $contextName = ConfigProvider::DEFAULT_CONTEXT
): QueueConsumerInterface {
$contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
/** @var array<string, array<string, mixed>> $config */
$config = $container->get(ConfigProvider::CONFIG_KEY);
$contextConfig = ConfigProvider::getContextConfig($contextName, $config);

/** @var array<string> $contextExtensions */
$contextExtensions = $contextConfig['extensions'] ?? [];
$extensions = [];
foreach ($contextConfig['extensions'] ?? [] as $extension) {
$extensions[] = $container->get($extension);
foreach ($contextExtensions as $extension) {
/**
* @var ExtensionInterface|StartExtensionInterface|PreSubscribeExtensionInterface
* |PreConsumeExtensionInterface|MessageReceivedExtensionInterface
* |PostMessageReceivedExtensionInterface|MessageResultExtensionInterface
* |ProcessorExceptionExtensionInterface|PostConsumeExtensionInterface|EndExtensionInterface
* |InitLoggerExtensionInterface $extensionService
*/
$extensionService = $container->get($extension);
$extensions[] = $extensionService;
}
/** @var string $contextServiceName */
$contextServiceName = $contextConfig[ConfigProvider::CONTEXT_SERVICE_KEY];
/** @var Context $contextService */
$contextService = $container->get($contextServiceName);
/** @var LoggerInterface $logger */
$logger = $container->get(LoggerInterface::class);

return new QueueConsumer(
$container->get($contextConfig[ConfigProvider::CONTEXT_SERVICE_KEY]),
new ChainExtension($extensions),
[],
$container->get(LoggerInterface::class)
);
return new QueueConsumer($contextService, new ChainExtension($extensions), [], $logger);
}
}

0 comments on commit 7d19177

Please sign in to comment.