diff --git a/src/Observable.php b/src/Observable.php index 79db73ba..2d97b7b5 100644 --- a/src/Observable.php +++ b/src/Observable.php @@ -1,6 +1,6 @@ dispose(); - $observer->onError($throwable); - } - }, + try { + $onNextOrObserver($value); + } catch (\Throwable $throwable) { + $disposable->dispose(); + $observer->onError($throwable); + } + }, $onError, $onCompleted ); @@ -1684,7 +1684,7 @@ public function bufferWithCount(int $count, int $skip = null): Observable * @operator * @reactivex catch */ - public function catch (callable $selector): Observable + public function catch(callable $selector): Observable { return $this->lift(function () use ($selector) { return new CatchErrorOperator(new ObservableFactoryWrapper($selector)); @@ -1831,7 +1831,7 @@ public function timestamp(SchedulerInterface $scheduler = null): Observable * @operator * @reactivex switch */ - public function switch (): Observable + public function switch(): Observable { return $this->lift(function () { return new SwitchLatestOperator(); @@ -1978,10 +1978,10 @@ public function pluck($property): Observable } return $this->map(function ($x) use ($property) { - if (is_array($x) && isset($x[$property])) { + if (is_array($x) && array_key_exists($property, $x)) { return $x[$property]; } - if (is_object($x) && isset($x->$property)) { + if (is_object($x) && property_exists($x, $property)) { return $x->$property; } diff --git a/test/Rx/Functional/Operator/PluckTest.php b/test/Rx/Functional/Operator/PluckTest.php index bd17ef1f..61c4961d 100644 --- a/test/Rx/Functional/Operator/PluckTest.php +++ b/test/Rx/Functional/Operator/PluckTest.php @@ -254,4 +254,66 @@ public function pluck_nested_numeric_key_with_object_properties() subscribe(200, 400) ], $xs->getSubscriptions()); } + + /** + * @test + */ + public function pluck_object_property_null() + { + $xs = $this->createHotObservable([ + onNext(180, (object)['prop' => 1]), + onNext(210, (object)['prop' => 2]), + onNext(240, (object)['prop' => 3]), + onNext(290, (object)['prop' => null]), + onNext(350, (object)['prop' => 5]), + onError(400, new \Exception()) + ]); + + $results = $this->scheduler->startWithCreate(function () use ($xs) { + return $xs->pluck('prop'); + }); + + $this->assertMessages([ + onNext(210, 2), + onNext(240, 3), + onNext(290, null), + onNext(350, 5), + onError(400, new \Exception()) + ], $results->getMessages()); + + $this->assertSubscriptions([ + subscribe(200, 400) + ], $xs->getSubscriptions()); + } + + /** + * @test + */ + public function pluck_array_assoc_null() + { + $xs = $this->createHotObservable([ + onNext(180, ['prop' => 1]), + onNext(210, ['prop' => 2]), + onNext(240, ['prop' => 3]), + onNext(290, ['prop' => null]), + onNext(350, ['prop' => 5]), + onError(400, new \Exception()) + ]); + + $results = $this->scheduler->startWithCreate(function () use ($xs) { + return $xs->pluck('prop'); + }); + + $this->assertMessages([ + onNext(210, 2), + onNext(240, 3), + onNext(290, null), + onNext(350, 5), + onError(400, new \Exception()) + ], $results->getMessages()); + + $this->assertSubscriptions([ + subscribe(200, 400) + ], $xs->getSubscriptions()); + } }