Skip to content

Commit

Permalink
Add support template types
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Nov 21, 2023
1 parent 2a2c228 commit 5db6e71
Show file tree
Hide file tree
Showing 33 changed files with 405 additions and 49 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,27 @@ jobs:
coverage: xdebug
- run: composer install
- run: vendor/bin/phpunit --coverage-text
PHPStan:
name: PHPStan (PHP ${{ matrix.php }} on ${{ matrix.os }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-20.04
- windows-2019
php:
- 8.2
- 8.1
- 8.0
- 7.4
- 7.3
- 7.2
steps:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
- run: composer install
- run: vendor/bin/phpstan
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
],
"require": {
"php": ">=7.0.0",
"react/promise": "~2.2"
"react/promise": "^3 || ~2.2"
},
"require-dev": {
"satooshi/php-coveralls": "~1.0",
"phpunit/phpunit": "^8.5 || ^9",
"react/event-loop": "^1.0 || ^0.5 || ^0.4.2"
"react/event-loop": "^1.0 || ^0.5 || ^0.4.2",
"phpstan/phpstan": "^1.10"
},
"suggest": {
"react/event-loop": "Used for scheduling async operations"
Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: max

paths:
- src/Observable.php
- src/ObservableInterface.php
- src/Observable/
- src/Subject/
- test/types/

fileExtensions:
- php
33 changes: 18 additions & 15 deletions src/Observable.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@
use Rx\Subject\ReplaySubject;
use Rx\Subject\Subject;

/**
* @template-covariant T
*/
abstract class Observable implements ObservableInterface

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2 on ubuntu-20.04)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.1 on ubuntu-20.04)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4 on ubuntu-20.04)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8 on ubuntu-20.04)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.3 on ubuntu-20.04)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2 on ubuntu-20.04)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2 on windows-2019)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.3 on windows-2019)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8 on windows-2019)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4 on windows-2019)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2 on windows-2019)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T

Check failure on line 84 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.1 on windows-2019)

Class Rx\Observable implements generic interface Rx\ObservableInterface but does not specify its types: T
{
/**
* @param callable|ObserverInterface|null $onNextOrObserver
* @param callable(T)|ObserverInterface|null $onNextOrObserver
* @param callable|null $onError
* @param callable|null $onCompleted
* @return DisposableInterface
Expand Down Expand Up @@ -175,15 +178,15 @@ public static function interval(int $interval, AsyncSchedulerInterface $schedule
/**
* Returns an observable sequence that contains a single element.
*
* @param mixed $value Single element in the resulting observable sequence.
* @param SchedulerInterface $scheduler
* @return ReturnObservable An observable sequence with the single element.
* @param T $value Single element in the resulting observable sequence.
* @param ?SchedulerInterface $scheduler
* @return Observable<T>
*
* @demo of/of.php
* @operator
* @reactivex just
*/
public static function of($value, SchedulerInterface $scheduler = null): ReturnObservable
public static function of($value, SchedulerInterface $scheduler = null): Observable
{
return new ReturnObservable($value, $scheduler ?: Scheduler::getDefault());
}
Expand All @@ -194,9 +197,9 @@ public static function of($value, SchedulerInterface $scheduler = null): ReturnO
*
* @param $value
* @param SchedulerInterface|null $scheduler
* @return ReturnObservable
* @return Observable
*/
public static function just($value, SchedulerInterface $scheduler = null): ReturnObservable
public static function just($value, SchedulerInterface $scheduler = null): Observable

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2 on ubuntu-20.04)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2 on ubuntu-20.04)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.1 on ubuntu-20.04)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.1 on ubuntu-20.04)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4 on ubuntu-20.04)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4 on ubuntu-20.04)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8 on ubuntu-20.04)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8 on ubuntu-20.04)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.3 on ubuntu-20.04)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.3 on ubuntu-20.04)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2 on ubuntu-20.04)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2 on ubuntu-20.04)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2 on windows-2019)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.2 on windows-2019)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.3 on windows-2019)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.3 on windows-2019)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8 on windows-2019)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8 on windows-2019)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4 on windows-2019)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 7.4 on windows-2019)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2 on windows-2019)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.2 on windows-2019)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.1 on windows-2019)

Method Rx\Observable::just() has parameter $value with no type specified.

Check failure on line 202 in src/Observable.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.1 on windows-2019)

Method Rx\Observable::just() return type with generic class Rx\Observable does not specify its types: T
{
return static::of($value, $scheduler);
}
Expand Down Expand Up @@ -296,25 +299,25 @@ public function mergeAll(): Observable
/**
* Converts an array to an observable sequence
*
* @param array $array
* @param SchedulerInterface $scheduler
* @return ArrayObservable
* @param array<T> $array
* @param ?SchedulerInterface $scheduler
* @return ObservableInterface<T>
*
* @demo fromArray/fromArray.php
* @operator
* @reactivex from
*/
public static function fromArray(array $array, SchedulerInterface $scheduler = null): ArrayObservable
public static function fromArray(array $array, SchedulerInterface $scheduler = null): ObservableInterface
{
return new ArrayObservable($array, $scheduler ?: Scheduler::getDefault());
}

/**
* Converts an Iterator into an observable sequence
*
* @param \Iterator $iterator
* @param \Iterator<T> $iterator
* @param SchedulerInterface $scheduler
* @return IteratorObservable
* @return ObservableInterface<T>
*
* @demo iterator/iterator.php
* @operator
Expand Down Expand Up @@ -2049,8 +2052,8 @@ public function finally(callable $callback): Observable
/**
* Converts a promise into an observable
*
* @param PromiseInterface $promise
* @return Observable
* @param PromiseInterface<T> $promise
* @return Observable<T>
* @throws \InvalidArgumentException
*
* @demo promise/fromPromise.php
Expand Down
8 changes: 8 additions & 0 deletions src/Observable/AnonymousObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
use Rx\Disposable\CallbackDisposable;
use Rx\DisposableInterface;
use Rx\Observable;
use Rx\ObservableInterface;
use Rx\ObserverInterface;
use Rx\Observer\AutoDetachObserver;

/**
* @template T
* @template-extends Observable<T>
*/
class AnonymousObservable extends Observable
{
/**
* @var callable
*/
private $subscribeAction;

public function __construct(callable $subscribeAction)
Expand Down
14 changes: 14 additions & 0 deletions src/Observable/ArrayObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@

use Rx\DisposableInterface;
use Rx\Observable;
use Rx\ObservableInterface;
use Rx\ObserverInterface;
use Rx\SchedulerInterface;

/**
* @template T
* @template-extends Observable<T>
*/
class ArrayObservable extends Observable
{
/**
* @var array<T>
*/
private $data;

/**
* @var SchedulerInterface
*/
private $scheduler;

/**
* @param array<T> $data
*/
public function __construct(array $data, SchedulerInterface $scheduler)
{
$this->data = $data;
Expand Down
13 changes: 9 additions & 4 deletions src/Observable/ConnectableObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@
use Rx\Subject\Subject;

/**
* @template T
* @template-extends Observable<T>
* Class ConnectableObservable
* @package Rx\Observable
*/
class ConnectableObservable extends Observable
{
/** @var \Rx\Subject\Subject */
/** @var \Rx\Subject\Subject<T> */
protected $subject;

/** @var BinaryDisposable */
protected $subscription;

/** @var Observable */
/** @var Observable<T> */
protected $sourceObservable;

/** @var bool */
protected $hasSubscription;

/**
* ConnectableObservable constructor.
* @param Observable $source
* @param \Rx\Subject\Subject $subject
* @param Observable<T> $source
* @param \Rx\Subject\Subject<T> $subject
*/
public function __construct(Observable $source, Subject $subject = null)
{
Expand Down Expand Up @@ -63,6 +65,9 @@ public function connect(): DisposableInterface
return $this->subscription;
}

/**
* @return RefCountObservable<T>
*/
public function refCount(): RefCountObservable
{
return new RefCountObservable($this);
Expand Down
7 changes: 7 additions & 0 deletions src/Observable/EmptyObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
use Rx\ObserverInterface;
use Rx\SchedulerInterface;

/**
* @template T
* @template-extends Observable<T>
*/
class EmptyObservable extends Observable
{
/**
* @var SchedulerInterface
*/
private $scheduler;

public function __construct(SchedulerInterface $scheduler)
Expand Down
11 changes: 11 additions & 0 deletions src/Observable/ErrorObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@
use Rx\ObserverInterface;
use Rx\SchedulerInterface;

/**
* @template T
* @template-extends Observable<T>
*/
class ErrorObservable extends Observable
{
/**
* @var \Throwable
*/
private $error;

/**
* @var SchedulerInterface
*/
private $scheduler;

public function __construct(\Throwable $error, SchedulerInterface $scheduler)
Expand Down
18 changes: 17 additions & 1 deletion src/Observable/ForkJoinObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,35 @@
use Rx\ObserverInterface;
use Rx\Disposable\CompositeDisposable;

/**
* @template T
* @template-extends Observable<T>
*/
class ForkJoinObservable extends Observable
{
/**
* @var Observable[]
* @var array<Observable<T>>
*/
private $observables;

/**
* @var array<T>
*/
private $values = [];

/**
* @var int
*/
private $completed = 0;

/**
* @var callable|null
*/
private $resultSelector;

/**
* @param array<Observable<T>> $observables
*/
public function __construct(array $observables = [], callable $resultSelector = null)
{
$this->observables = $observables;
Expand Down
22 changes: 22 additions & 0 deletions src/Observable/GroupedObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@
use Rx\Disposable\RefCountDisposable;
use Rx\DisposableInterface;

/**
* @template T
* @template-extends Observable<T>
*/
class GroupedObservable extends Observable
{
/**
* @var mixed
*/
private $key;

/**
* @var ObservableInterface<T>
*/
private $underlyingObservable;

/**
* @param mixed $key
* @param ObservableInterface<T> $underlyingObservable
*/
public function __construct($key, ObservableInterface $underlyingObservable, RefCountDisposable $mergedDisposable = null)
{
$this->key = $key;
Expand All @@ -25,6 +40,9 @@ public function __construct($key, ObservableInterface $underlyingObservable, Ref
$this->newUnderlyingObservable($mergedDisposable, $underlyingObservable);
}

/**
* @return mixed
*/
public function getKey()
{
return $this->key;
Expand All @@ -35,6 +53,10 @@ protected function _subscribe(ObserverInterface $observer): DisposableInterface
return $this->underlyingObservable->subscribe($observer);
}

/**
* @param ObservableInterface<T> $underlyingObservable
* @return Observable<T>
*/
private function newUnderlyingObservable(RefCountDisposable $mergedDisposable, ObservableInterface $underlyingObservable): Observable
{
return new AnonymousObservable(
Expand Down
7 changes: 7 additions & 0 deletions src/Observable/IntervalObservable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
use Rx\ObserverInterface;
use Rx\AsyncSchedulerInterface;

/**
* @template T
* @template-extends Observable<T>
*/
class IntervalObservable extends Observable
{
/**
* @var int
*/
private $interval;

/** @var AsyncSchedulerInterface */
Expand Down
Loading

0 comments on commit 5db6e71

Please sign in to comment.