Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Add PHPStan to test environment with max level #76

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/.gitattributes export-ignore
/.github/ export-ignore
/.gitignore export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/tests/ export-ignore
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ jobs:
ini-file: development
- run: composer install
- run: vendor/bin/phpunit --coverage-text

PHPStan:
name: PHPStan (PHP ${{ matrix.php }})
runs-on: ubuntu-22.04
strategy:
matrix:
php:
- 8.2
- 8.1
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
- run: composer install
- run: vendor/bin/phpstan
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,12 @@ To run the test suite, go to the project root and run:
vendor/bin/phpunit
```

On top of this, we use PHPStan on max level to ensure type safety across the project:

```bash
vendor/bin/phpstan
```

## License

MIT, see [LICENSE file](LICENSE).
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"react/promise": "^3.0 || ^2.8 || ^1.2.1"
},
"require-dev": {
"phpstan/phpstan": "1.10.18",
"phpunit/phpunit": "^9.5"
},
"autoload": {
Expand Down
11 changes: 11 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
parameters:
level: max

paths:
- src/
- tests/

reportUnmatchedIgnoredErrors: false
ignoreErrors:
# ignore generic usage like `PromiseInterface<T>` until fixed upstream
- '/^PHPDoc .* contains generic type React\\Promise\\PromiseInterface<.+> but interface React\\Promise\\PromiseInterface is not generic\.$/'
WyriHaximus marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 10 additions & 2 deletions src/FiberMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,43 @@
*/
final class FiberMap
{
/** @var array<int,bool> */
private static array $status = [];
private static array $map = [];

/** @var array<int,PromiseInterface> */
private static array $map = [];

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function register(\Fiber $fiber): void
{
self::$status[\spl_object_id($fiber)] = false;
self::$map[\spl_object_id($fiber)] = [];
}

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function cancel(\Fiber $fiber): void
{
self::$status[\spl_object_id($fiber)] = true;
}

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function setPromise(\Fiber $fiber, PromiseInterface $promise): void
{
self::$map[\spl_object_id($fiber)] = $promise;
}

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function unsetPromise(\Fiber $fiber, PromiseInterface $promise): void
{
unset(self::$map[\spl_object_id($fiber)]);
}

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function getPromise(\Fiber $fiber): ?PromiseInterface
{
return self::$map[\spl_object_id($fiber)] ?? null;
}

/** @param \Fiber<mixed,mixed,mixed,mixed> $fiber */
public static function unregister(\Fiber $fiber): void
{
unset(self::$status[\spl_object_id($fiber)], self::$map[\spl_object_id($fiber)]);
Expand Down
10 changes: 9 additions & 1 deletion src/SimpleFiber.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
*/
final class SimpleFiber implements FiberInterface
{
/** @var ?\Fiber<void,void,void,callable(): mixed> */
private static ?\Fiber $scheduler = null;

private static ?\Closure $suspend = null;

/** @var ?\Fiber<mixed,mixed,mixed,mixed> */
private ?\Fiber $fiber = null;

public function __construct()
Expand Down Expand Up @@ -57,13 +61,17 @@ public function suspend(): mixed
self::$scheduler = new \Fiber(static fn() => Loop::run());
// Run event loop to completion on shutdown.
\register_shutdown_function(static function (): void {
assert(self::$scheduler instanceof \Fiber);
if (self::$scheduler->isSuspended()) {
self::$scheduler->resume();
}
});
}

return (self::$scheduler->isStarted() ? self::$scheduler->resume() : self::$scheduler->start())();
$ret = (self::$scheduler->isStarted() ? self::$scheduler->resume() : self::$scheduler->start());
assert(\is_callable($ret));

return $ret();
}

return \Fiber::suspend();
Expand Down
25 changes: 20 additions & 5 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@
* await($promise);
* ```
*
* @param callable(mixed ...$args):mixed $function
* @return callable(): PromiseInterface<mixed>
* @param callable $function
* @return callable(mixed ...): PromiseInterface<mixed>
* @since 4.0.0
* @see coroutine()
*/
Expand All @@ -192,6 +192,7 @@ function async(callable $function): callable
} catch (\Throwable $exception) {
$reject($exception);
} finally {
assert($fiber instanceof \Fiber);
FiberMap::unregister($fiber);
}
});
Expand All @@ -200,6 +201,7 @@ function async(callable $function): callable

$fiber->start();
}, function () use (&$fiber): void {
assert($fiber instanceof \Fiber);
FiberMap::cancel($fiber);
$promise = FiberMap::getPromise($fiber);
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
Expand Down Expand Up @@ -287,6 +289,7 @@ function (mixed $value) use (&$resolved, &$resolvedValue, &$fiber, $lowLevelFibe
FiberMap::unsetPromise($lowLevelFiber, $promise);
}

/** @var ?\Fiber<mixed,mixed,mixed,mixed> $fiber */
if ($fiber === null) {
$resolved = true;
$resolvedValue = $value;
Expand All @@ -309,6 +312,7 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
// what a lovely piece of code!
$r = new \ReflectionProperty('Exception', 'trace');
$trace = $r->getValue($throwable);
assert(\is_array($trace));

// Exception trace arguments only available when zend.exception_ignore_args is not set
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -340,6 +344,7 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
}

if ($rejected) {
assert($rejectedThrowable instanceof \Throwable);
throw $rejectedThrowable;
}

Expand Down Expand Up @@ -587,7 +592,7 @@ function delay(float $seconds): void
* });
* ```
*
* @param callable(...$args):\Generator<mixed,PromiseInterface,mixed,mixed> $function
* @param callable(mixed ...$args):(\Generator<mixed,PromiseInterface,mixed,mixed>|mixed) $function
* @param mixed ...$args Optional list of additional arguments that will be passed to the given `$function` as is
* @return PromiseInterface<mixed>
* @since 3.0.0
Expand All @@ -606,6 +611,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface

$promise = null;
$deferred = new Deferred(function () use (&$promise) {
/** @var ?PromiseInterface $promise */
if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) {
$promise->cancel();
}
Expand All @@ -626,6 +632,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
return;
}

/** @var mixed $promise */
$promise = $generator->current();
if (!$promise instanceof PromiseInterface) {
$next = null;
Expand All @@ -635,6 +642,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
return;
}

assert($next instanceof \Closure);
$promise->then(function ($value) use ($generator, $next) {
$generator->send($value);
$next();
Expand All @@ -657,6 +665,7 @@ function coroutine(callable $function, mixed ...$args): PromiseInterface
*/
function parallel(iterable $tasks): PromiseInterface
{
/** @var array<int,PromiseInterface> $pending */
$pending = [];
$deferred = new Deferred(function () use (&$pending) {
foreach ($pending as $promise) {
Expand Down Expand Up @@ -718,6 +727,7 @@ function series(iterable $tasks): PromiseInterface
{
$pending = null;
$deferred = new Deferred(function () use (&$pending) {
/** @var ?PromiseInterface $pending */
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {
$pending->cancel();
}
Expand All @@ -730,9 +740,9 @@ function series(iterable $tasks): PromiseInterface
assert($tasks instanceof \Iterator);
}

/** @var callable():void $next */
$taskCallback = function ($result) use (&$results, &$next) {
$results[] = $result;
/** @var \Closure $next */
$next();
};

Expand All @@ -746,9 +756,11 @@ function series(iterable $tasks): PromiseInterface
$task = $tasks->current();
$tasks->next();
} else {
assert(\is_array($tasks));
$task = \array_shift($tasks);
}

assert(\is_callable($task));
$promise = \call_user_func($task);
assert($promise instanceof PromiseInterface);
$pending = $promise;
Expand All @@ -762,13 +774,14 @@ function series(iterable $tasks): PromiseInterface
}

/**
* @param iterable<callable(mixed=):PromiseInterface<mixed>> $tasks
* @param iterable<(callable():PromiseInterface<mixed>)|(callable(mixed):PromiseInterface<mixed>)> $tasks
* @return PromiseInterface<mixed>
*/
function waterfall(iterable $tasks): PromiseInterface
{
$pending = null;
$deferred = new Deferred(function () use (&$pending) {
/** @var ?PromiseInterface $pending */
if ($pending instanceof PromiseInterface && \method_exists($pending, 'cancel')) {
$pending->cancel();
}
Expand All @@ -791,9 +804,11 @@ function waterfall(iterable $tasks): PromiseInterface
$task = $tasks->current();
$tasks->next();
} else {
assert(\is_array($tasks));
$task = \array_shift($tasks);
}

assert(\is_callable($task));
$promise = \call_user_func_array($task, func_get_args());
assert($promise instanceof PromiseInterface);
$pending = $promise;
Expand Down
Loading