Skip to content

Commit

Permalink
Codestyle fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Feb 5, 2024
1 parent 83911d6 commit a5524ad
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 77 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
->setFinder(PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->notContains(['final class Configuration implements ConfigurationInterface'])
)
->setRules($rules)
->setRiskyAllowed(true)
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
"Luzrain\\PhpRunnerBundle\\Test\\": "tests/"
}
},
"scripts": {
"test": [
"vendor/bin/php-cs-fixer fix -v --dry-run",
"vendor/bin/psalm",
"vendor/bin/phpunit"
]
},
"config": {
"sort-packages": true,
"allow-plugins": {
Expand Down
9 changes: 8 additions & 1 deletion src/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ConfigLoader implements CacheWarmerInterface
{
private array $config;
Expand Down Expand Up @@ -40,7 +43,7 @@ public function warmUpInFork(KernelFactory $kernelFactory): void
$kernelFactory->createKernel()->boot();
exit;
} else {
pcntl_wait($status);
\pcntl_wait($status);
unset($status);
}
}
Expand All @@ -50,6 +53,10 @@ public function isFresh(): bool
return $this->cache->isFresh();
}

/**
* @psalm-suppress UnresolvableInclude
* @psalm-suppress RedundantPropertyInitializationCheck
*/
private function getConfigCache(): array
{
return $this->config ??= require $this->cache->getPath();
Expand Down
70 changes: 35 additions & 35 deletions src/DependencyInjection/CompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,41 @@ public function process(ContainerBuilder $container): void
//$tasks = array_map(fn(array $a) => $a[0], $container->findTaggedServiceIds('phprunner.task'));
//$processes = array_map(fn(array $a) => $a[0], $container->findTaggedServiceIds('phprunner.process'));

// $container
// ->getDefinition('phprunner.config_loader')
// ->addMethodCall('setProcessConfig', [$processes])
// ->addMethodCall('setSchedulerConfig', [$tasks])
// ;

// $container
// ->register('phprunner.task_locator', ServiceLocator::class)
// ->addTag('container.service_locator')
// ->setArguments([$this->referenceMap($tasks)])
// ;
//
// $container
// ->register('phprunner.process_locator', ServiceLocator::class)
// ->addTag('container.service_locator')
// ->setArguments([$this->referenceMap($processes)])
// ;

// $container
// ->register('phprunner.task_handler', TaskHandler::class)
// ->setPublic(true)
// ->setArguments([
// new Reference('phprunner.task_locator'),
// new Reference(EventDispatcherInterface::class),
// ])
// ;
//
// $container
// ->register('phprunner.process_handler', ProcessHandler::class)
// ->setPublic(true)
// ->setArguments([
// new Reference('phprunner.process_locator'),
// new Reference(EventDispatcherInterface::class),
// ])
// ;
// $container
// ->getDefinition('phprunner.config_loader')
// ->addMethodCall('setProcessConfig', [$processes])
// ->addMethodCall('setSchedulerConfig', [$tasks])
// ;

// $container
// ->register('phprunner.task_locator', ServiceLocator::class)
// ->addTag('container.service_locator')
// ->setArguments([$this->referenceMap($tasks)])
// ;
//
// $container
// ->register('phprunner.process_locator', ServiceLocator::class)
// ->addTag('container.service_locator')
// ->setArguments([$this->referenceMap($processes)])
// ;

// $container
// ->register('phprunner.task_handler', TaskHandler::class)
// ->setPublic(true)
// ->setArguments([
// new Reference('phprunner.task_locator'),
// new Reference(EventDispatcherInterface::class),
// ])
// ;
//
// $container
// ->register('phprunner.process_handler', ProcessHandler::class)
// ->setPublic(true)
// ->setArguments([
// new Reference('phprunner.process_locator'),
// new Reference(EventDispatcherInterface::class),
// ])
// ;
}

private function referenceMap(array $taggedServices): array
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* @psalm-suppress UndefinedMethod
*/
final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
Expand Down
44 changes: 23 additions & 21 deletions src/DependencyInjection/PhpRunnerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
use Luzrain\PhpRunnerBundle\Http\HttpRequestHandler;
use Luzrain\PhpRunnerBundle\ReloadStrategy\OnEachRequest;
use Luzrain\PhpRunnerBundle\ReloadStrategy\OnException;
use Luzrain\PhpRunnerBundle\ReloadStrategy\OnRequestsLimit;
use Luzrain\PhpRunnerBundle\ReloadStrategy\OnMemoryLimit;
use Luzrain\PhpRunnerBundle\ReloadStrategy\OnRequestsLimit;
use Luzrain\PhpRunnerBundle\ReloadStrategy\OnTTLLimit;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand All @@ -22,6 +23,7 @@ final class PhpRunnerExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
/** @var ConfigurationInterface $configuration */
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

Expand Down Expand Up @@ -114,28 +116,28 @@ public function load(array $configs, ContainerBuilder $container): void
;
}

// $container->registerAttributeForAutoconfiguration(AsProcess::class, $this->processConfig(...));
// $container->registerAttributeForAutoconfiguration(AsTask::class, $this->taskConfig(...));
// $container->registerAttributeForAutoconfiguration(AsProcess::class, $this->processConfig(...));
// $container->registerAttributeForAutoconfiguration(AsTask::class, $this->taskConfig(...));
}

// private function processConfig(ChildDefinition $definition, AsProcess $attribute): void
// {
// $definition->addTag('phprunner.process', [
// 'name' => $attribute->name,
// 'processes' => $attribute->processes,
// 'method' => $attribute->method,
// ]);
// }
//
// private function taskConfig(ChildDefinition $definition, AsTask $attribute): void
// {
// $definition->addTag('phprunner.task', [
// 'name' => $attribute->name,
// 'schedule' => $attribute->schedule,
// 'method' => $attribute->method,
// 'jitter' => $attribute->jitter,
// ]);
// }
// private function processConfig(ChildDefinition $definition, AsProcess $attribute): void
// {
// $definition->addTag('phprunner.process', [
// 'name' => $attribute->name,
// 'processes' => $attribute->processes,
// 'method' => $attribute->method,
// ]);
// }
//
// private function taskConfig(ChildDefinition $definition, AsTask $attribute): void
// {
// $definition->addTag('phprunner.task', [
// 'name' => $attribute->name,
// 'schedule' => $attribute->schedule,
// 'method' => $attribute->method,
// 'jitter' => $attribute->jitter,
// ]);
// }

public function getAlias(): string
{
Expand Down
1 change: 1 addition & 0 deletions src/Http/HttpRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private function handle(ServerRequestInterface $request): ResponseInterface

if ($this->kernel instanceof TerminableInterface) {
$worker->getEventLoop()->defer(function () use ($symfonyRequest, $symfonyResponse): void {
/** @psalm-suppress UndefinedInterfaceMethod */
$this->kernel->terminate($symfonyRequest, $symfonyResponse);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Psr17Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public function createStreamFromResource($resource): StreamInterface
*/
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
throw new \LogicException(sprintf('%s::createServerRequest() not implemented', self::class));
throw new \LogicException(\sprintf('%s::createServerRequest() not implemented', self::class));
}

public function createUploadedFile(StreamInterface $stream, int $size = null, int $error = \UPLOAD_ERR_OK, string $clientFilename = null, string $clientMediaType = null): UploadedFileInterface
{
throw new \LogicException(sprintf('%s::createUploadedFile() not implemented', self::class));
throw new \LogicException(\sprintf('%s::createUploadedFile() not implemented', self::class));
}
}
3 changes: 3 additions & 0 deletions src/KernelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
private string $environment;
private bool $isDebug;

/**
* @psalm-suppress InvalidPropertyAssignmentValue
*/
public function __construct(private \Closure $app, private array $args, array $options)
{
$this->projectDir = $options['project_dir'];
Expand Down
8 changes: 5 additions & 3 deletions src/ReloadStrategy/OnException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@

final class OnException implements ReloadStrategyInterface
{
/** @var array<class-string<\Throwable>> */
private array $allowedExceptions = [
HttpException::class,
HttpExceptionInterface::class,
\Symfony\Component\Serializer\Exception\ExceptionInterface::class,
'\Symfony\Component\Serializer\Exception\ExceptionInterface',
];

private bool $scheduleReload = false;

/**
* @param list<class-string<\Throwable>> $allowedExceptions
*/
public function __construct(array $allowedExceptions = [])
{
\array_push($this->allowedExceptions, ...$allowedExceptions);
Expand All @@ -41,7 +43,7 @@ public function onException(ExceptionEvent $event): void

public function shouldReload(int $eventCode, mixed $eventObject = null): bool
{
if ($eventCode === self::EVENT_CODE_EXCEPTION) {
if ($eventCode === self::EVENT_CODE_EXCEPTION && $eventObject instanceof \Throwable) {
return $this->shouldExceptionTriggerReload($eventObject);
}

Expand Down
3 changes: 0 additions & 3 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Luzrain\PhpRunnerBundle;

use Luzrain\PhpRunner\PhpRunner;
use Luzrain\PhpRunner\Server\Protocols\Http;
use Luzrain\PhpRunner\Server\Server;
use Luzrain\PhpRunner\WorkerProcess;
use Luzrain\PhpRunnerBundle\Worker\HttpServerWorker;
use Symfony\Component\Runtime\RunnerInterface;

Expand Down
5 changes: 2 additions & 3 deletions src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function getResolver(callable $callable, \ReflectionFunction|null $reflec
{
$resolver = parent::getResolver($callable, $reflector);

return new class ($resolver, $this->options) implements ResolverInterface
{
return new class ($resolver, $this->options) implements ResolverInterface {
public function __construct(private ResolverInterface $resolver, private array $options)
{
}
Expand All @@ -33,7 +32,7 @@ public function resolve(): array
{
[$app, $args] = $this->resolver->resolve();

return [static fn(...$args) => new KernelFactory(...$args), [$app, $args, $this->options]];
return [static fn(mixed ...$args) => new KernelFactory(...$args), [$app, $args, $this->options]];
}
};
}
Expand Down
7 changes: 6 additions & 1 deletion src/Worker/HttpServerWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ private function onStart(): void
/** @var EventDispatcherInterface $eventDispatcher */
$eventDispatcher = $kernel->getContainer()->get('event_dispatcher');

/**
* @psalm-suppress UndefinedClass
* @psalm-suppress UndefinedInterfaceMethod
*/
if ($logger instanceof \Monolog\Logger) {
$logger = $logger->withName('phprunner');
}

$errorHandler = ErrorHandler::register(null, false);
$errorHandlerClosure = static function (\Throwable $e) use ($errorHandler) {
$errorHandlerClosure = static function (\Throwable $e) use ($errorHandler): void {
$errorHandler->setExceptionHandler(static function (\Throwable $e): void {});
/** @psalm-suppress InternalMethod */
$errorHandler->handleException($e);
};

Expand Down
8 changes: 4 additions & 4 deletions tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
],
]);

// $container->autowire(ResponseTestController::class)->setAutoconfigured(true);
// $container->autowire(RequestTestController::class)->setAutoconfigured(true);
// $container->autowire(TestTask::class)->setAutoconfigured(true);
// $container->autowire(TestProcess::class)->setAutoconfigured(true);
// $container->autowire(ResponseTestController::class)->setAutoconfigured(true);
// $container->autowire(RequestTestController::class)->setAutoconfigured(true);
// $container->autowire(TestTask::class)->setAutoconfigured(true);
// $container->autowire(TestProcess::class)->setAutoconfigured(true);
});
}

Expand Down
4 changes: 2 additions & 2 deletions tests/App/TestProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function __construct(

public function __invoke(): void
{
file_put_contents($this->statusFile, time());
sleep(1);
\file_put_contents($this->statusFile, \time());
\sleep(1);
exit;
}
}
2 changes: 1 addition & 1 deletion tests/App/TestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function __construct(

public function __invoke(): void
{
file_put_contents($this->statusFile, time());
\file_put_contents($this->statusFile, \time());
}
}
2 changes: 1 addition & 1 deletion tests/App/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Luzrain\PhpRunnerBundle\Test\App\Kernel;

require_once dirname(__DIR__, 2) . '/vendor/autoload_runtime.php';
require_once \dirname(__DIR__, 2) . '/vendor/autoload_runtime.php';

$_SERVER['APP_RUNTIME'] = 'Luzrain\PhpRunnerBundle\Runtime';

Expand Down

0 comments on commit a5524ad

Please sign in to comment.