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 Dec 1, 2023
1 parent eee8eb2 commit 6c56873
Show file tree
Hide file tree
Showing 114 changed files with 1,640 additions and 363 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": "^3 || ~2.2"
"react/promise": "^3"
},
"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
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ parameters:
level: max

paths:
- src/
- test/types/

fileExtensions:
- php
- php
10 changes: 6 additions & 4 deletions src/Disposable/BinaryDisposable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
*/
class BinaryDisposable implements DisposableInterface
{
/** @var \Rx\DisposableInterface */
/** @var ?\Rx\DisposableInterface */
private $first;

/** @var \Rx\DisposableInterface */
/** @var ?\Rx\DisposableInterface */
private $second;

/** @var bool */
protected $isDisposed = false;

/**
* BinaryDisposable constructor.
* @param $first
* @param $second
* @param DisposableInterface $first
* @param DisposableInterface $second
*/
public function __construct(DisposableInterface $first, DisposableInterface $second)
{
Expand All @@ -40,7 +40,9 @@ public function dispose()

$this->isDisposed = true;

/** @phpstan-ignore-next-line */
$this->first->dispose();
/** @phpstan-ignore-next-line */
$this->second->dispose();

$this->first = null;
Expand Down
7 changes: 7 additions & 0 deletions src/Disposable/CallbackDisposable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

class CallbackDisposable implements DisposableInterface
{
/**
* @var callable
*/
private $action;

/**
* @var bool
*/
private $disposed = false;

public function __construct(callable $action)
Expand Down
19 changes: 19 additions & 0 deletions src/Disposable/CompositeDisposable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@

class CompositeDisposable implements DisposableInterface
{
/**
* @var array<DisposableInterface>
*/
private $disposables;

/**
* @var bool
*/
private $isDisposed = false;

/**
* @param array<DisposableInterface> $disposables
*/
public function __construct(array $disposables = [])
{
$this->disposables = $disposables;
Expand All @@ -32,6 +42,9 @@ public function dispose()
}
}

/**
* @return void
*/
public function add(DisposableInterface $disposable)
{
if ($this->isDisposed) {
Expand Down Expand Up @@ -65,11 +78,17 @@ public function contains(DisposableInterface $disposable): bool
return in_array($disposable, $this->disposables, true);
}

/**
* @return int
*/
public function count()
{
return count($this->disposables);
}

/**
* @return void
*/
public function clear()
{
$disposables = $this->disposables;
Expand Down
18 changes: 18 additions & 0 deletions src/Disposable/RefCountDisposable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@

class RefCountDisposable implements DisposableInterface
{
/**
* @var int
*/
private $count = 0;

/**
* @var DisposableInterface
*/
private $disposable;

/**
* @var bool
*/
private $isDisposed = false;

/**
* @var bool
*/
private $isPrimaryDisposed = false;

public function __construct(DisposableInterface $disposable)
Expand All @@ -32,6 +47,9 @@ public function dispose()
}
}

/**
* @return DisposableInterface
*/
public function getDisposable()
{
if (!$this->isDisposed) {
Expand Down
5 changes: 3 additions & 2 deletions src/Disposable/SerialDisposable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SerialDisposable implements DisposableInterface
/** @var bool */
private $isDisposed = false;

/** @var DisposableInterface */
/** @var ?DisposableInterface */
private $disposable = null;

public function dispose()
Expand All @@ -34,7 +34,7 @@ public function dispose()
}

/**
* @return DisposableInterface
* @return ?DisposableInterface
*/
public function getDisposable()
{
Expand All @@ -43,6 +43,7 @@ public function getDisposable()

/**
* @param DisposableInterface $disposable
* @return void
*/
public function setDisposable(DisposableInterface $disposable)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Disposable/SingleAssignmentDisposable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

class SingleAssignmentDisposable implements DisposableInterface
{
/**
* @var ?DisposableInterface
*/
private $current;

/**
* @var bool
*/
private $isDisposed = false;

public function dispose()
Expand All @@ -27,6 +34,9 @@ public function dispose()
}
}

/**
* @return void
*/
public function setDisposable(DisposableInterface $disposable = null)
{
if ($this->current) {
Expand All @@ -42,11 +52,17 @@ public function setDisposable(DisposableInterface $disposable = null)
}
}

/**
* @return DisposableInterface|null
*/
public function getDisposable()
{
return $this->current;
}

/**
* @return bool
*/
public function isDisposed()
{
return $this->isDisposed;
Expand Down
3 changes: 3 additions & 0 deletions src/DisposableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@

interface DisposableInterface
{
/**
* @return void
*/
public function dispose();
}
34 changes: 23 additions & 11 deletions src/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,45 @@
*/
abstract class Notification
{
private $kind;

/**
* @param mixed $kind Kind of notification
* @template T
* @param (callable(T): void)|ObserverInterface $observerOrOnNext
* @param (callable(\Throwable): void) $onError
* @param (callable(): void) $onCompleted
* @return void
*/
public function __construct($kind)
{
$this->kind = $kind;
}

public function accept($observerOrOnNext, $onError = null, $onCompleted = null)
public function accept($observerOrOnNext, callable $onError = null, callable $onCompleted = null)
{
if (null === $onError && null === $onCompleted && $observerOrOnNext instanceof ObserverInterface) {
$this->doAcceptObservable($observerOrOnNext);

return;
}

return $this->doAccept($observerOrOnNext, $onError, $onCompleted);
assert(is_callable($observerOrOnNext));
$this->doAccept($observerOrOnNext, $onError, $onCompleted);
}

/**
* @param mixed $other
*/
public function equals($other): bool
{
/** @phpstan-ignore-next-line */
return (string)$this === (string)$other;
}

/**
* @return void
*/
abstract protected function doAcceptObservable(ObserverInterface $observer);

abstract protected function doAccept($onNext, $onError, $onCompleted);
/**
* @template T
* @param (callable(T): void) $onNext
* @param (callable(\Throwable): void) $onError
* @param (callable(): void) $onCompleted
* @return void
*/
abstract protected function doAccept(callable $onNext, callable $onError = null, callable $onCompleted = null);
}
11 changes: 5 additions & 6 deletions src/Notification/OnCompletedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@

class OnCompletedNotification extends Notification
{
public function __construct()
{
parent::__construct('C');
}

/**
* @return void
*/
protected function doAcceptObservable(ObserverInterface $observer)
{
$observer->onCompleted();
}

protected function doAccept($onNext, $onError, $onCompleted)
protected function doAccept(callable $onNext, callable $onError = null, callable $onCompleted = null)
{
assert(is_callable($onCompleted));
$onCompleted();
}

Expand Down
11 changes: 8 additions & 3 deletions src/Notification/OnErrorNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@

class OnErrorNotification extends Notification
{
/**
* @var \Throwable
*/
private $exception;

public function __construct(\Throwable $exception)
{
parent::__construct('E');

$this->exception = $exception;
}

/**
* @return void
*/
protected function doAcceptObservable(ObserverInterface $observer)
{
$observer->onError($this->exception);
}

protected function doAccept($onNext, $onError, $onCompleted)
protected function doAccept(callable $onNext, callable $onError = null, callable $onCompleted = null)
{
assert(is_callable($onError));
$onError($this->exception);
}

Expand Down
Loading

0 comments on commit 6c56873

Please sign in to comment.