Skip to content

Commit

Permalink
🚧 Validate some PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Ledru committed Aug 13, 2024
1 parent 7285f6c commit 5f1eb06
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Flow\Ip;
use Flow\IpStrategy\MaxIpStrategy;

$driver = match (random_int(4, 4)) {
$driver = match (random_int(1, 4)) {
1 => new AmpDriver(),
2 => new FiberDriver(),
3 => new ReactDriver(),
Expand Down
5 changes: 5 additions & 0 deletions src/AsyncHandler/AsyncHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
use Flow\Event;
use Flow\Event\AsyncEvent;

/**
* @template T
*
* @implements AsyncHandlerInterface<T>
*/
final class AsyncHandler implements AsyncHandlerInterface
{
public static function getSubscribedEvents()
Expand Down
14 changes: 13 additions & 1 deletion src/AsyncHandler/BatchAsyncHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@
use Symfony\Component\Messenger\Handler\BatchHandlerTrait;
use Throwable;

/**
* @template T1
* @template T2
*
* @implements AsyncHandlerInterface<T1>
*/
final class BatchAsyncHandler implements BatchHandlerInterface, AsyncHandlerInterface
{
use BatchHandlerTrait;

/**
* @var AsyncHandlerInterface<T2>
*/
private AsyncHandlerInterface $asyncHandler;

/**
* @param null|AsyncHandlerInterface<T2> $asyncHandler
*/
public function __construct(
private int $batchSize = 10,
?AsyncHandlerInterface $asyncHandler = null,
Expand Down Expand Up @@ -46,7 +58,7 @@ public function async(AsyncEvent $event): void
* https://github.com/phpstan/phpstan/issues/6039
* https://phpstan.org/r/8f7de023-9888-4dcb-b12c-e2fcf9547b6c.
*
* @param array{0: AsyncEvent, 1: Acknowledger}[] $jobs
* @param array{0: AsyncEvent<T1>, 1: Acknowledger}[] $jobs
*
* @phpstan-ignore method.unused
*/
Expand Down
5 changes: 5 additions & 0 deletions src/AsyncHandler/DeferAsyncHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
use Flow\Event;
use Flow\Event\AsyncEvent;

/**
* @template T
*
* @implements AsyncHandlerInterface<T>
*/
final class DeferAsyncHandler implements AsyncHandlerInterface
{
public static function getSubscribedEvents()
Expand Down
6 changes: 6 additions & 0 deletions src/AsyncHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
use Flow\Event\AsyncEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @template T
*/
interface AsyncHandlerInterface extends EventSubscriberInterface
{
/**
* @param AsyncEvent<T> $event
*/
public function async(AsyncEvent $event): void;
}
20 changes: 12 additions & 8 deletions src/Driver/AmpDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public function __construct(?Driver $driver = null)
}
}

/**
* @return Closure(TArgs): Future<TReturn>
*/
public function async(Closure $callback): Closure
{
return static function (...$args) use ($callback) {
Expand All @@ -59,6 +62,9 @@ public function async(Closure $callback): Closure
};
}

/**
* @return Future<TReturn>
*/
public function defer(Closure $callback): Future
{
$deferred = new DeferredFuture();
Expand All @@ -84,20 +90,18 @@ public function await(array &$stream): void
return function (mixed $data) use ($job) {
$async = $this->async($job);

if ($data === null) {
$future = $async();
} else {
$future = $async($data);
}
$future = $async($data);

return static function ($map) use ($future) {
return static function (Closure $map) use ($future) {
/** @var Closure(TReturn): mixed $map */
$future->map($map);
};
};
};

$defer = function (Closure $job) {
return function ($map) use ($job) {
return function (Closure $map) use ($job) {
/** @var Closure(TReturn): mixed $map */
$future = $this->defer($job);
$future->map($map);
};
Expand Down Expand Up @@ -143,7 +147,7 @@ public function delay(float $seconds): void
delay($seconds);
}

public function tick($interval, Closure $callback): Closure
public function tick(float $interval, Closure $callback): Closure
{
$this->ticks++;
$tickId = EventLoop::repeat($interval, $callback);
Expand Down
6 changes: 1 addition & 5 deletions src/Driver/FiberDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ public function await(array &$stream): void
$exception = null;

try {
if ($data === null) {
$fiber->start();
} else {
$fiber->start($data);
}
$fiber->start($data);
} catch (Throwable $fiberException) {
$exception = $fiberException;
}
Expand Down
9 changes: 4 additions & 5 deletions src/Driver/ReactDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function async(Closure $callback): Closure
};
}

/**
* @return Promise<TReturn>
*/
public function defer(Closure $callback): Promise
{
$deferred = new Deferred();
Expand All @@ -82,11 +85,7 @@ public function await(array &$stream): void
return function (mixed $data) use ($job) {
$async = $this->async($job);

if ($data === null) {
$promise = $async();
} else {
$promise = $async($data);
}
$promise = $async($data);

return static function ($then) use ($promise) {
$promise->then($then);
Expand Down
8 changes: 1 addition & 7 deletions src/Driver/SpatieDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@ public function await(array &$stream): void
return function (mixed $data) use ($job) {
$async = $this->async($job);

if ($data === null) {
$fn = $async();
} else {
$fn = $async($data);
}

return $fn;
return $async($data);
};
};

Expand Down
8 changes: 1 addition & 7 deletions src/Driver/SwooleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ public function await(array &$stream): void
return function (mixed $data) use ($job) {
$async = $this->async($job);

if ($data === null) {
$fn = $async();
} else {
$fn = $async($data);
}

return $fn;
return $async($data);
};
};

Expand Down
2 changes: 2 additions & 0 deletions src/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function async(Closure $callback): Closure;
* $callback will be given two callbacks
* - an complete callback to store result
* - an async callback to go to the next async call.
*
* @param Closure(callable(TReturn): void, callable(mixed, callable): void): void $callback
*/
public function defer(Closure $callback): mixed;

Expand Down
9 changes: 9 additions & 0 deletions src/Event/AsyncEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
use Flow\Ip;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @template T
*/
final class AsyncEvent extends Event
{
/**
* @param Ip<T> $ip
*/
public function __construct(
private Closure $async,
private Closure $defer,
Expand All @@ -33,6 +39,9 @@ public function getJob(): Closure
return $this->job;
}

/**
* @return Ip<T>
*/
public function getIp(): Ip
{
return $this->ip;
Expand Down
1 change: 1 addition & 0 deletions src/Flow/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Flow implements FlowInterface
* @param Closure(T1): T2 $job
* @param Closure(ExceptionInterface): void $errorJob
* @param null|IpStrategyInterface<T1> $ipStrategy
* @param null|AsyncHandlerInterface<T1> $asyncHandler
* @param null|DriverInterface<T1,T2> $driver
*/
public function __construct(
Expand Down
1 change: 1 addition & 0 deletions src/Flow/YFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class YFlow extends Flow
/**
* @param null|Closure(ExceptionInterface): void $errorJob
* @param null|IpStrategyInterface<T1> $ipStrategy
* @param null|AsyncHandlerInterface<T1> $asyncHandler
* @param null|DriverInterface<T1,T2> $driver
*/
public function __construct(
Expand Down
1 change: 1 addition & 0 deletions tools/php-cs-fixer/.php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'php_unit_internal_class' => false, // From @PhpCsFixer but we don't want it
'php_unit_test_class_requires_covers' => false, // From @PhpCsFixer but we don't want it
'phpdoc_add_missing_param_annotation' => false, // From @PhpCsFixer but we don't want it
'phpdoc_to_comment' => false, // We want PHPStan keep pass with anotation line comments
'concat_space' => ['spacing' => 'one'],
'ordered_class_elements' => true, // Symfony(PSR12) override the default value, but we don't want
'blank_line_before_statement' => true, // Symfony(PSR12) override the default value, but we don't want
Expand Down

0 comments on commit 5f1eb06

Please sign in to comment.