Skip to content

Commit

Permalink
Decrease psalm level down to 2 (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster authored Apr 14, 2023
1 parent 3fd8360 commit dbb7f42
Show file tree
Hide file tree
Showing 31 changed files with 196 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
branches:
- master
- 2.0
- '*.*'

name: phpunit

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
branches:
- master
- 2.0
- '*.*'

name: static analysis

Expand Down
23 changes: 15 additions & 8 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?xml version="1.0"?>
<psalm
phpVersion="8.1"
errorLevel="4"
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"
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"
errorLevel="2"
resolveFromConfigFile="true"
findUnusedBaselineEntry="false"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
<directory name="src"/>
<ignoreFiles>
<directory name="vendor" />
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<issueHandlers>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<directory name="src/Console/Command"/>
</errorLevel>
</PropertyNotSetInConstructor>
<UndefinedAttributeClass>
<errorLevel type="suppress">
<referencedClass name="JetBrains\PhpStorm\ExpectedValues" />
<referencedClass name="JetBrains\PhpStorm\ExpectedValues"/>
</errorLevel>
</UndefinedAttributeClass>
<InvalidCatch>
Expand Down
3 changes: 3 additions & 0 deletions src/Bootloader/CacheBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ private function initStorageFactory(RPCInterface $rpc, SerializerInterface $seri
return new Factory($rpc, $serializer);
}

/**
* @param non-empty-string $driver
*/
private function initDefaultStorage(FactoryInterface $factory, string $driver): StorageInterface
{
return $factory->select($driver);
Expand Down
20 changes: 10 additions & 10 deletions src/Bootloader/CentrifugoBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
use Psr\Container\ContainerInterface;
use RoadRunner\Centrifugo\CentrifugoApiInterface;
use RoadRunner\Centrifugo\CentrifugoWorker;
use RoadRunner\Centrifugo\CentrifugoWorkerInterface;
use RoadRunner\Centrifugo\RPCCentrifugoApi;
use Spiral\Boot\AbstractKernel;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Broadcasting\Bootloader\BroadcastingBootloader;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Core\FactoryInterface;
use Spiral\RoadRunnerBridge\Centrifugo\Broadcast;
use Spiral\RoadRunnerBridge\Centrifugo\Dispatcher;
use Spiral\RoadRunnerBridge\Centrifugo\ErrorHandlerInterface;
use Spiral\RoadRunnerBridge\Centrifugo\Interceptor;
use Spiral\RoadRunnerBridge\Centrifugo\LogErrorHandler;
use Spiral\RoadRunnerBridge\Centrifugo\RegistryInterface;
use Spiral\RoadRunnerBridge\Centrifugo\ServiceRegistry;
use RoadRunner\Centrifugo\CentrifugoWorkerInterface;
use Spiral\Boot\AbstractKernel;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Broadcasting\Bootloader\BroadcastingBootloader;
use Spiral\RoadRunnerBridge\Centrifugo\Interceptor;
use Spiral\RoadRunnerBridge\Config\CentrifugoConfig;

final class CentrifugoBootloader extends Bootloader
Expand All @@ -34,7 +34,7 @@ final class CentrifugoBootloader extends Bootloader
];

public function __construct(
private readonly ConfiguratorInterface $config
private readonly ConfiguratorInterface $config,
) {
}

Expand All @@ -45,7 +45,7 @@ private function initConfig(): void
[
'services' => [],
'interceptors' => [],
]
],
);
}

Expand All @@ -66,16 +66,16 @@ public function boot(
private function initInterceptorRegistry(
CentrifugoConfig $config,
ContainerInterface $container,
FactoryInterface $factory
FactoryInterface $factory,
): Interceptor\RegistryInterface {
return new Interceptor\InterceptorRegistry($config->getInterceptors(), $container, $factory);
}

private function initServiceRegistry(
CentrifugoConfig $config,
ContainerInterface $container,
FactoryInterface $factory
) {
FactoryInterface $factory,
): RegistryInterface {
return new ServiceRegistry($config->getServices(), $container, $factory);
}
}
3 changes: 3 additions & 0 deletions src/Bootloader/RoadRunnerBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ private function initEnvironment(GlobalEnvironmentInterface $env): EnvironmentIn
return new Environment($env->getAll());
}

/**
* @psalm-suppress ArgumentTypeCoercion
*/
private function initRPC(EnvironmentInterface $env): RPCInterface
{
return new RPC(Relay::create($env->getRPCAddress()));
Expand Down
2 changes: 1 addition & 1 deletion src/Bootloader/TcpBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function init(EnvironmentInterface $environment): void
$this->initTcpConfig($environment);
}

public function boot(KernelInterface $kernel, FactoryInterface $factory)
public function boot(KernelInterface $kernel, FactoryInterface $factory): void
{
$kernel->addDispatcher($factory->make(Dispatcher::class));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Centrifugo/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function __construct(

public function publish(iterable|\Stringable|string $topics, iterable|string $messages): void
{
/** @var non-empty-string[] $topics */
$topics = $this->formatTopics($this->toArray($topics));

/** @var string $message */
foreach ($this->toArray($messages) as $message) {
\assert(\is_string($message), 'Message argument must be a type of string');
$this->api->broadcast($topics, $message);
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/Centrifugo/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class Dispatcher implements DispatcherInterface
public function __construct(
private readonly ContainerInterface $container,
private readonly FinalizerInterface $finalizer,
private readonly RoadRunnerMode $mode
private readonly RoadRunnerMode $mode,
) {
}

Expand All @@ -37,7 +37,11 @@ public function serve(): void
{
/** @var CentrifugoWorker $worker */
$worker = $this->container->get(CentrifugoWorker::class);
/** @var ScopeInterface $scope */
/**
* @var ScopeInterface $scope
*
* @psalm-suppress DeprecatedInterface
*/
$scope = $this->container->get(ScopeInterface::class);
/** @var Interceptor\RegistryInterface $registry */
$registry = $this->container->get(Interceptor\RegistryInterface::class);
Expand All @@ -52,7 +56,7 @@ public function serve(): void
$service = $this->getService($handler, $registry, $type);
$scope->runScope([
RequestInterface::class => $request,
], static fn () => $service->callAction($request::class, 'handle', [
], static fn (): mixed => $service->callAction($request::class, 'handle', [
'type' => $type,
'request' => $request,
]));
Expand All @@ -67,7 +71,7 @@ public function serve(): void
public function getService(
RequestHandler $handler,
Interceptor\RegistryInterface $registry,
RequestType $type
RequestType $type,
): InterceptableCore {
if (isset($this->services[$type->value])) {
return $this->services[$type->value];
Expand Down
9 changes: 6 additions & 3 deletions src/Centrifugo/Interceptor/InterceptorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ final class InterceptorRegistry implements RegistryInterface
private const INTERCEPTORS_FOR_ALL_SERVICES = '*';

/** @var array<string, CoreInterceptorInterface[]> */
private array $interceptors;
private array $interceptors = [];

/**
* @param array<non-empty-string, list<TInterceptor>> $interceptors
* @param array<non-empty-string, TInterceptor|TInterceptor[]> $interceptors
*/
public function __construct(
array $interceptors,
Expand Down Expand Up @@ -53,11 +53,14 @@ public function register(string $type, Autowire|CoreInterceptorInterface|string
));
}

$this->interceptors[$type][] = match (true) {
/** @var CoreInterceptorInterface $object */
$object = match (true) {
$interceptor instanceof CoreInterceptorInterface => $interceptor,
$interceptor instanceof Autowire => $interceptor->resolve($this->factory),
default => $this->container->get($interceptor)
};

$this->interceptors[$type][] = $object;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Centrifugo/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
final class RequestHandler implements CoreInterface
{
public function __construct(
private readonly RegistryInterface $registry
private readonly RegistryInterface $registry,
) {
}

public function callAction(string $controller, string $action, array $parameters = []): mixed
public function callAction(string $controller, string $action, array $parameters = []): bool
{
\assert($parameters['request'] instanceof RequestInterface);
\assert($parameters['type'] instanceof RequestType);

$service = $this->registry->getService($parameters['type']);

if ($service === null) {
return false;
}

$service->handle($parameters['request']);

return true;
Expand Down
3 changes: 3 additions & 0 deletions src/Centrifugo/ServiceRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function hasService(RequestType $requestType): bool
return isset($this->services[$requestType->value]);
}

/**
* @psalm-suppress LessSpecificReturnStatement
*/
private function createService(Autowire|ServiceInterface|string $service): ServiceInterface
{
return match (true) {
Expand Down
2 changes: 2 additions & 0 deletions src/Config/GRPCConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function getBinaryPath(): ?string

/**
* Path, where generated DTO files should be stored.
*
* @return non-empty-string|null
*/
public function getGeneratedPath(): ?string
{
Expand Down
12 changes: 12 additions & 0 deletions src/Console/Command/GRPC/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function perform(
return self::FAILURE;
}

\assert($binaryPath !== null);

$compiler = new ProtoCompiler(
$this->getPath($kernel, $config->getGeneratedPath()),
$this->getNamespace($kernel, $config->getNamespace()),
Expand Down Expand Up @@ -84,6 +86,7 @@ public function perform(
}

foreach ($generatorRegistry->getGenerators() as $generator) {
/** @psalm-suppress ArgumentTypeCoercion */
$generator->run(
$compiled,
$this->getPath($kernel, $config->getGeneratedPath()),
Expand All @@ -96,6 +99,10 @@ public function perform(

/**
* Get or detect base source code path. By default fallbacks to kernel location.
*
* @param non-empty-string|null $generatedPath
*
* @return non-empty-string
*/
protected function getPath(KernelInterface $kernel, ?string $generatedPath): string
{
Expand All @@ -110,11 +117,16 @@ protected function getPath(KernelInterface $kernel, ?string $generatedPath): str

$r = new \ReflectionObject($kernel);

/** @psalm-suppress LessSpecificReturnStatement */
return \dirname($r->getFileName());
}

/**
* Get or detect base namespace. By default fallbacks to kernel namespace.
*
* @return non-empty-string
*
* @psalm-suppress LessSpecificReturnStatement
*/
protected function getNamespace(KernelInterface $kernel, ?string $protoNamespace): string
{
Expand Down
13 changes: 7 additions & 6 deletions src/Console/Command/Queue/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#[AsCommand(
name: 'rr:jobs:list',
description: 'Displays a list of available job pipelines for the RoadRunner.'
)
]
)]
final class ListCommand extends Command
{
/**
Expand All @@ -37,10 +36,12 @@ public function perform(JobsInterface $jobs): int
$queues = \array_map(static function (QueueInterface $queue): array {
\assert($queue instanceof Queue);
$stat = $queue->getPipelineStat();
\assert($stat !== null);

$fontColor = $stat->getReady() ? 'green' : 'gray';
$defaultColor = $stat->getReady() ? 'default' : 'gray';
$activeFont = $stat->getReady() ? 'bold' : '';
$isReady = $stat->getReady();
$fontColor = $isReady ? 'green' : 'gray';
$defaultColor = $isReady ? 'default' : 'gray';
$activeFont = $isReady ? 'bold' : '';

return [
'name' => new TableCell($stat->getPipeline(), [
Expand Down Expand Up @@ -76,7 +77,7 @@ public function perform(JobsInterface $jobs): int
}, $queues);

\ksort($queues);

\assert($this->output !== null);
$table = new Table($this->output);

$table->setHeaders(
Expand Down
20 changes: 14 additions & 6 deletions src/Console/Command/Queue/PauseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,37 @@

namespace Spiral\RoadRunnerBridge\Console\Command\Queue;

use Spiral\Console\Attribute\Argument;
use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Attribute\Question;
use Spiral\Console\Command;
use Spiral\RoadRunner\Jobs\Exception\JobsException;
use Spiral\RoadRunner\Jobs\JobsInterface;

#[AsCommand(
name: 'rr:jobs:pause',
description: 'Pauses the consumption of jobs for the specified pipeline in the RoadRunner.',
)]
final class PauseCommand extends Command
{
protected const SIGNATURE = 'rr:jobs:pause {pipeline : Pipeline name}';
protected const DESCRIPTION = 'Pauses the consumption of jobs for the specified pipeline in the RoadRunner.';
#[Argument(description: 'Pipeline name')]
#[Question('Provide pipeline name to resume')]
public string $pipeline;

/**
* @throws JobsException
*/
public function perform(JobsInterface $jobs): int
{
$name = $this->argument('pipeline');
\assert($this->pipeline !== '');

if ($this->isVerbose()) {
$this->info(\sprintf('Pausing pipeline [%s]...', $name));
$this->info(\sprintf('Pausing pipeline [%s]...', $this->pipeline));
}

$jobs->pause($name);
$jobs->pause($this->pipeline);

$this->info(\sprintf('Pipeline [%s] has been paused.', $name));
$this->info(\sprintf('Pipeline [%s] has been paused.', $this->pipeline));

return self::SUCCESS;
}
Expand Down
Loading

0 comments on commit dbb7f42

Please sign in to comment.