Skip to content

Commit

Permalink
Merge pull request #97 from samsonasik/apply-php74
Browse files Browse the repository at this point in the history
Apply PHP 7.4 syntax and typed property
  • Loading branch information
Ocramius authored Sep 13, 2022
2 parents 095b57e + 2d858c6 commit 12d71c1
Show file tree
Hide file tree
Showing 52 changed files with 161 additions and 239 deletions.
2 changes: 1 addition & 1 deletion src/Event/HotCodeReloaderWorkerStartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __invoke(WorkerStartEvent $event): void
$fileWatcher = $this->fileWatcher;
$logger = $this->logger;

$server->tick($this->interval, function () use ($server, $fileWatcher, $logger) {
$server->tick($this->interval, static function () use ($server, $fileWatcher, $logger): void {
$changedFilePaths = $fileWatcher->readChangedFilePaths();
if (! $changedFilePaths) {
return;
Expand Down
15 changes: 6 additions & 9 deletions src/Event/RequestHandlerRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,22 @@ public function __construct(
// Factories are cast as Closures to ensure return type safety.
/** @psalm-suppress MixedInferredReturnType */
$this->serverRequestFactory
= static function (SwooleHttpRequest $request) use ($serverRequestFactory): ServerRequestInterface {
= static fn(SwooleHttpRequest $request): ServerRequestInterface =>
/** @psalm-suppress MixedReturnStatement */
return $serverRequestFactory($request);
};
$serverRequestFactory($request);

/** @psalm-suppress MixedInferredReturnType */
$this->serverRequestErrorResponseGenerator
= static function (Throwable $exception) use ($serverRequestErrorResponseGenerator): ResponseInterface {
= static fn(Throwable $exception): ResponseInterface =>
/** @psalm-suppress MixedReturnStatement */
return $serverRequestErrorResponseGenerator($exception);
};
$serverRequestErrorResponseGenerator($exception);

if ($emitterFactory) {
/** @psalm-suppress MixedInferredReturnType */
$this->emitterFactory
= static function (SwooleHttpResponse $response) use ($emitterFactory): SwooleEmitter {
= static fn(SwooleHttpResponse $response): SwooleEmitter =>
/** @psalm-suppress MixedReturnStatement */
return $emitterFactory($response);
};
$emitterFactory($response);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/HotCodeReload/FileWatcher/InotifyFileWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Mezzio\Swoole\HotCodeReload\FileWatcherInterface;
use Webmozart\Assert\Assert;

use function array_merge;
use function array_values;
use function extension_loaded;
use function in_array;
Expand Down Expand Up @@ -112,7 +111,7 @@ private function listSubdirectoriesRecursively(string $path): array
continue;
}

$paths = array_merge($paths, $this->listSubdirectoriesRecursively($filename));
$paths = [...$paths, ...$this->listSubdirectoriesRecursively($filename)];
}

Assert::allStringNotEmpty($paths);
Expand Down
2 changes: 1 addition & 1 deletion src/Log/StrftimeToICUFormatMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function mapStrftimeToICU(string $format, DateTimeInterface $reque
private static function generateMapCallback(DateTimeInterface $requestTime): callable
{
/** @psalm-param array<array-key, string> */
return function (array $matches) use ($requestTime): string {
return static function (array $matches) use ($requestTime): string {
Assert::keyExists($matches, 'token');
switch (true) {
case $matches['token'] === '%a':
Expand Down
2 changes: 1 addition & 1 deletion src/ServerRequestSwooleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __invoke(ContainerInterface $container): callable
? $container->get(FilterServerRequestInterface::class)
: FilterUsingXForwardedHeaders::trustReservedSubnets();

$stripXForwardedHeaders = function (array $headers): array {
$stripXForwardedHeaders = static function (array $headers): array {
/** @psalm-var list<string> */
static $disallowedHeaders = [
'X-FORWARDED-FOR',
Expand Down
2 changes: 1 addition & 1 deletion src/StaticResourceHandler/CacheControlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function validateCacheControlDirectives(array $cacheControlDirectives):
));
}

array_walk($directives, function ($directive) use ($regex) {
array_walk($directives, function ($directive) use ($regex): void {
if (! is_string($directive)) {
throw new Exception\InvalidArgumentException(sprintf(
'One or more Cache-Control directives associated with the regex "%s" are invalid;'
Expand Down
2 changes: 1 addition & 1 deletion src/StaticResourceHandler/FileLocationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function findFile(string $filename): ?string
{
foreach ($this->mappedDocRoots as $prefix => $directories) {
foreach ($directories as $directory) {
if (stripos($filename, $prefix) === 0) {
if (stripos($filename, (string) $prefix) === 0) {
$mappedFileName = $directory . substr($filename, strlen($prefix));
if (file_exists($mappedFileName)) {
return $mappedFileName;
Expand Down
3 changes: 1 addition & 2 deletions src/StaticResourceHandler/StaticResourceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

class StaticResourceResponse
{
/** @var int */
private $contentLength = 0;
private int $contentLength = 0;

/** @psalm-var array<string, string> */
private array $headers = [];
Expand Down
4 changes: 1 addition & 3 deletions src/StaticResourceHandler/ValidateRegexTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ trait ValidateRegexTrait
{
private function isValidRegex(string $regex): bool
{
set_error_handler(static function ($errno) {
return $errno === E_WARNING;
});
set_error_handler(static fn($errno) => $errno === E_WARNING);
$isValid = preg_match($regex, '') !== false;
restore_error_handler();
return $isValid;
Expand Down
2 changes: 1 addition & 1 deletion src/SwooleEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SwooleEmitter implements EmitterInterface
/**
* @see https://www.swoole.co.uk/docs/modules/swoole-http-server/methods-properties#swoole-http-response-write
*/
public const CHUNK_SIZE = 2097152; // 2 MB
public const CHUNK_SIZE = 2_097_152; // 2 MB

private SwooleHttpResponse $swooleResponse;

Expand Down
23 changes: 8 additions & 15 deletions test/Command/ReloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ public function testExecuteEndsWithErrorWhenStopCommandFails(): void
$stopCommand
->method('run')
->with(
$this->callback(static function (ArrayInput $arg) {
return 'stop' === (string) $arg;
}),
$this->callback(static fn(ArrayInput $arg) => 'stop' === (string) $arg),
$this->output
)
->willReturn(1);
Expand Down Expand Up @@ -190,9 +188,7 @@ public function testExecuteEndsWithErrorWhenStartCommandFails(): void
$stopCommand
->method('run')
->with(
$this->callback(static function (ArrayInput $arg) {
return 'stop' === (string) $arg;
}),
$this->callback(static fn(ArrayInput $arg) => 'stop' === (string) $arg),
$this->output
)
->willReturn(0);
Expand All @@ -201,9 +197,7 @@ public function testExecuteEndsWithErrorWhenStartCommandFails(): void
$startCommand
->method('run')
->with(
$this->callback(static function (ArrayInput $arg) {
return 'start --daemonize=1 --num-workers=5' === (string) $arg;
}),
$this->callback(static fn(ArrayInput $arg) => 'start --daemonize=1 --num-workers=5' === (string) $arg),
$this->output
)
->willReturn(1);
Expand Down Expand Up @@ -269,9 +263,7 @@ public function testExecuteEndsWithSuccessWhenBothStopAndStartCommandsSucceed():
$stopCommand
->method('run')
->with(
$this->callback(static function (ArrayInput $arg) {
return 'stop' === (string) $arg;
}),
$this->callback(static fn(ArrayInput $arg) => 'stop' === (string) $arg),
$this->output
)
->willReturn(0);
Expand All @@ -280,9 +272,10 @@ public function testExecuteEndsWithSuccessWhenBothStopAndStartCommandsSucceed():
$startCommand
->method('run')
->with(
$this->callback(static function (ArrayInput $arg) {
return 'start --daemonize=1 --num-workers=5 --num-task-workers=2' === (string) $arg;
}),
$this->callback(
static fn(ArrayInput $arg)
=> 'start --daemonize=1 --num-workers=5 --num-task-workers=2' === (string) $arg
),
$this->output
)
->willReturn(0);
Expand Down
23 changes: 10 additions & 13 deletions test/Command/StartCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class StartCommandTest extends TestCase
*/
private $input;

/** @var string */
private $originalIncludePath;
private string $originalIncludePath;

/**
* @var OutputInterface|MockObject
Expand Down Expand Up @@ -192,7 +191,7 @@ public function testExecuteReturnsErrorIfServerIsRunningInBaseMode(): void

public function testExecuteReturnsErrorIfServerIsRunningInProcessMode(): void
{
$this->pidManager->method('read')->willReturn([1000000, getmypid()]);
$this->pidManager->method('read')->willReturn([1_000_000, getmypid()]);
$this->container->method('get')->with(PidManager::class)->willReturn($this->pidManager);

$command = new StartCommand($this->container);
Expand All @@ -210,8 +209,8 @@ public function noRunningProcesses(): iterable
{
yield 'empty' => [[]];
yield 'null-all' => [[null, null]];
yield 'base-mode' => [[1000000, null]];
yield 'process-mode' => [[1000000, 1000001]];
yield 'base-mode' => [[1_000_000, null]];
yield 'process-mode' => [[1_000_000, 1_000_001]];
}

/**
Expand All @@ -236,14 +235,12 @@ public function testExecuteRunsApplicationIfServerIsNotCurrentlyRunning(array $p
$httpServer
->expects($this->once())
->method('set')
->with($this->callback(static function (array $options) {
return array_key_exists('daemonize', $options)
&& array_key_exists('worker_num', $options)
&& array_key_exists('task_worker_num', $options)
&& true === $options['daemonize']
&& 6 === $options['worker_num']
&& 4 === $options['task_worker_num'];
}));
->with($this->callback(static fn(array $options) => array_key_exists('daemonize', $options)
&& array_key_exists('worker_num', $options)
&& array_key_exists('task_worker_num', $options)
&& true === $options['daemonize']
&& 6 === $options['worker_num']
&& 4 === $options['task_worker_num']));

$application->expects($this->once())->method('run');

Expand Down
6 changes: 3 additions & 3 deletions test/Command/StatusCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testStatusCommandIsASymfonyConsoleCommand(StatusCommand $command
public function runningProcesses(): iterable
{
yield 'base-mode' => [[getmypid(), null]];
yield 'process-mode' => [[1000000, getmypid()]];
yield 'process-mode' => [[1_000_000, getmypid()]];
}

/**
Expand Down Expand Up @@ -104,8 +104,8 @@ public function noRunningProcesses(): iterable
{
yield 'empty' => [[]];
yield 'null-all' => [[null, null]];
yield 'base-mode' => [[1000000, null]];
yield 'process-mode' => [[1000000, 1000001]];
yield 'base-mode' => [[1_000_000, null]];
yield 'process-mode' => [[1_000_000, 1_000_001]];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/Command/StopCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function noRunningProcesses(): iterable
{
yield 'empty' => [[]];
yield 'null-all' => [[null, null]];
yield 'base-mode' => [[1000000, null]];
yield 'process-mode' => [[1000000, 1000001]];
yield 'base-mode' => [[1_000_000, null]];
yield 'process-mode' => [[1_000_000, 1_000_001]];
}

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ public function testExecuteReturnsSuccessWhenServerIsNotCurrentlyRunning(array $
public function runningProcesses(): iterable
{
yield 'base-mode' => [[getmypid(), null]];
yield 'process-mode' => [[1000000, getmypid()]];
yield 'process-mode' => [[1_000_000, getmypid()]];
}

/**
Expand Down
3 changes: 1 addition & 2 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

class ConfigProviderTest extends TestCase
{
/** @var ConfigProvider */
private $provider;
private ConfigProvider $provider;

protected function setUp(): void
{
Expand Down
4 changes: 2 additions & 2 deletions test/Event/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testDispatchNotifiesAllRelevantListenersAndReturnsEventWhenNoErr

$listeners = [];
for ($i = 0; $i < 5; $i += 1) {
$listeners[] = function (object $event) use ($spy): void {
$listeners[] = static function (object $event) use ($spy): void {
$spy->caught += 1;
};
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public function isPropagationStopped(): bool

$listeners = [];
for ($i = 0; $i < 5; $i += 1) {
$listeners[] = function (object $event) use ($spy): void {
$listeners[] = static function (object $event) use ($spy): void {
$spy->caught += 1;
};
}
Expand Down
9 changes: 3 additions & 6 deletions test/Event/RequestHandlerRequestListenerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ class RequestHandlerRequestListenerFactoryTest extends TestCase
public function testFactoryProducesListenerUsingServicesFromContainer(): void
{
$pipeline = $this->createMock(RequestHandlerInterface::class);
$requestFactory = function (SwooleHttpRequest $request): ServerRequestInterface {
return $this->createMock(ServerRequestInterface::class);
};
$errorResponseFactory = function (Throwable $e): ResponseInterface {
return $this->createMock(ResponseInterface::class);
};
$requestFactory = fn(SwooleHttpRequest $request): ServerRequestInterface
=> $this->createMock(ServerRequestInterface::class);
$errorResponseFactory = fn(Throwable $e): ResponseInterface => $this->createMock(ResponseInterface::class);
$logger = $this->createMock(AccessLogInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container
Expand Down
4 changes: 1 addition & 3 deletions test/Event/RequestHandlerRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public function setUp(): void
};

$this->emitter = $emitter = $this->createMock(SwooleEmitter::class);
$emitterFactory = function (SwooleHttpResponse $response) use ($emitter): SwooleEmitter {
return $emitter;
};
$emitterFactory = static fn(SwooleHttpResponse $response): SwooleEmitter => $emitter;

$this->requestHandler = $this->createMock(RequestHandlerInterface::class);
$this->logger = $this->createMock(AccessLogInterface::class);
Expand Down
2 changes: 1 addition & 1 deletion test/Event/ServerStartListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testListenerUpdatesPidManagerChangesPWDSetsProcessNameAndLogsSer
$processName = 'alternate-process-name';
$masterPid = random_int(1, 10000);
$managerPid = random_int(1, 10000);
$setProcessName = function (string $name) use ($processName): void {
$setProcessName = static function (string $name) use ($processName): void {
TestCase::assertSame($processName . '-master', $name);
};
$pidManager = $this->createMock(PidManager::class);
Expand Down
2 changes: 1 addition & 1 deletion test/Event/SwooleListenerProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testFactoryRaisesErrorForNonCallableListenerService(): void

public function testFactoryProducesProviderWithConfiguredListeners(): void
{
$listener = function (stdClass $event): void {
$listener = static function (stdClass $event): void {
};

$config = [
Expand Down
6 changes: 3 additions & 3 deletions test/Event/SwooleListenerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class SwooleListenerProviderTest extends TestCase
{
public function testProviderAllowsListenerRegistrationAndReturnsListenersBasedOnEventType(): void
{
$listenerForTestEvent = function (TestAsset\TestEvent $e): void {
$listenerForTestEvent = static function (TestAsset\TestEvent $e): void {
};
$listenerForStdclass = function (stdClass $e): void {
$listenerForStdclass = static function (stdClass $e): void {
};

$provider = new SwooleListenerProvider();
Expand All @@ -40,7 +40,7 @@ public function testProviderAllowsListenerRegistrationAndReturnsListenersBasedOn

public function testProviderDoesNotAllowDuplicateRegistration(): void
{
$listenerForTestEvent = function (TestAsset\TestEvent $e): void {
$listenerForTestEvent = static function (TestAsset\TestEvent $e): void {
};

$provider = new SwooleListenerProvider();
Expand Down
4 changes: 2 additions & 2 deletions test/Event/WorkerStartListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testListenerSwitchesToConfiguredDirectorySetsWorkerNameAndLogs()
$workerId = random_int(0, 3);
$listener = new WorkerStartListener($logger, $cwd, $processName);
$event = new WorkerStartEvent($server, $workerId);
$listener::$setProcessName = function (string $name) use ($processName, $workerId): void {
$listener::$setProcessName = static function (string $name) use ($processName, $workerId): void {
TestCase::assertSame($processName . '-worker-' . $workerId, $name);
};

Expand Down Expand Up @@ -82,7 +82,7 @@ public function testListenerSwitchesToConfiguredDirectorySetsTaskWorkerNameAndLo
$workerId = random_int(4, 7);
$listener = new WorkerStartListener($logger, $cwd, $processName);
$event = new WorkerStartEvent($server, $workerId);
$listener::$setProcessName = function (string $name) use ($processName, $workerId): void {
$listener::$setProcessName = static function (string $name) use ($processName, $workerId): void {
TestCase::assertSame($processName . '-task-worker-' . $workerId, $name);
};

Expand Down
Loading

0 comments on commit 12d71c1

Please sign in to comment.