From 3af4c310ef93512e8d20a90cc6706900afaead82 Mon Sep 17 00:00:00 2001 From: Charlotte Dunois Date: Sat, 20 Oct 2018 19:08:50 +0200 Subject: [PATCH] Change promise usage to recommendation --- src/AbstractSyncAdapter.php | 46 ++++------------------------- src/Eio/Adapter.php | 3 -- src/Eio/ConstTypeDetector.php | 4 +-- src/MappedTypeDetector.php | 4 +-- src/ModeTypeDetector.php | 6 ++-- src/Node/Directory.php | 5 +--- src/Node/File.php | 14 ++++----- src/OpenFileLimiter.php | 3 +- src/Stream/DuplexStream.php | 4 +-- src/Stream/ReadableStreamTrait.php | 7 ++--- src/functions.php | 4 +-- tests/Eio/ConstTypeDetectorTest.php | 3 +- tests/ModeTypeDetectorTest.php | 3 +- tests/Node/FileTest.php | 24 ++------------- 14 files changed, 33 insertions(+), 97 deletions(-) diff --git a/src/AbstractSyncAdapter.php b/src/AbstractSyncAdapter.php index 62c5db5d..e5e9df02 100644 --- a/src/AbstractSyncAdapter.php +++ b/src/AbstractSyncAdapter.php @@ -3,13 +3,9 @@ namespace React\Filesystem; use DateTime; +use LogicException; use React\Filesystem\Node\NodeInterface; -use React\Filesystem\Stream\StreamFactory; -use React\Promise\FulfilledPromise; use React\Promise\PromiseInterface; -use React\Promise\RejectedPromise; -use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory; -use WyriHaximus\React\ChildProcess\Messenger\Messenger; abstract class AbstractSyncAdapter implements AdapterInterface { @@ -123,8 +119,6 @@ protected function processLsContents($basePath, $result, ObjectStream $stream) ]; $promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) { $stream->write($node); - - return new FulfilledPromise(); }); } @@ -154,19 +148,7 @@ public function touch($path, $mode = self::CREATION_MODE) */ public function open($path, $flags, $mode = self::CREATION_MODE) { - return new RejectedPromise(); - $id = null; - return \WyriHaximus\React\ChildProcess\Messenger\Factory::parentFromClass(self::CHILD_CLASS_NAME, $this->loop)->then(function (Messenger $messenger) use (&$id, $path, $flags, $mode) { - $id = count($this->fileDescriptors); - $this->fileDescriptors[$id] = $messenger; - return $this->fileDescriptors[$id]->rpc(Factory::rpc('open', [ - 'path' => $path, - 'flags' => $flags, - 'mode' => $mode, - ])); - })->then(function () use ($path, $flags, &$id) { - return \React\Promise\resolve(StreamFactory::create($path, $id, $flags, $this)); - }); + return \React\Promise\reject(new LogicException('Not implemented')); } /** @@ -177,13 +159,7 @@ public function open($path, $flags, $mode = self::CREATION_MODE) */ public function read($fileDescriptor, $length, $offset) { - return new RejectedPromise(); - return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('read', [ - 'length' => $length, - 'offset' => $offset, - ]))->then(function ($payload) { - return \React\Promise\resolve($payload['chunk']); - }); + return \React\Promise\reject(new LogicException('Not implemented')); } /** @@ -195,12 +171,7 @@ public function read($fileDescriptor, $length, $offset) */ public function write($fileDescriptor, $data, $length, $offset) { - return new RejectedPromise(); - return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('write', [ - 'chunk' => $data, - 'length' => $length, - 'offset' => $offset, - ])); + return \React\Promise\reject(new LogicException('Not implemented')); } /** @@ -209,14 +180,7 @@ public function write($fileDescriptor, $data, $length, $offset) */ public function close($fd) { - return new RejectedPromise(); - $fileDescriptor = $this->fileDescriptors[$fd]; - unset($this->fileDescriptors[$fd]); - return $fileDescriptor->rpc(Factory::rpc('close'))->then(function () use ($fileDescriptor) { - return $fileDescriptor->softTerminate(); - }, function () use ($fileDescriptor) { - return $fileDescriptor->softTerminate(); - }); + return \React\Promise\reject(new LogicException('Not implemented')); } /** diff --git a/src/Eio/Adapter.php b/src/Eio/Adapter.php index aee022a7..a06b8f06 100644 --- a/src/Eio/Adapter.php +++ b/src/Eio/Adapter.php @@ -15,7 +15,6 @@ use React\Filesystem\Stream\StreamFactory; use React\Filesystem\TypeDetectorInterface; use React\Promise\Deferred; -use React\Promise\FulfilledPromise; class Adapter implements AdapterInterface { @@ -228,8 +227,6 @@ protected function processLsContents($basePath, $result, ObjectStream $stream) ]; $promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) { $stream->write($node); - - return new FulfilledPromise(); }); } diff --git a/src/Eio/ConstTypeDetector.php b/src/Eio/ConstTypeDetector.php index 95d0a117..74a5bb80 100644 --- a/src/Eio/ConstTypeDetector.php +++ b/src/Eio/ConstTypeDetector.php @@ -2,9 +2,9 @@ namespace React\Filesystem\Eio; +use Exception; use React\Filesystem\FilesystemInterface; use React\Filesystem\TypeDetectorInterface; -use React\Promise\RejectedPromise; class ConstTypeDetector implements TypeDetectorInterface { @@ -37,7 +37,7 @@ public function __construct(FilesystemInterface $filesystem) public function detect(array $node) { if (!isset($node['type']) || !isset($this->mapping[$node['type']])) { - return new RejectedPromise(); + return \React\Promise\reject(new Exception('Unknown node type')); } return \React\Promise\resolve([ diff --git a/src/MappedTypeDetector.php b/src/MappedTypeDetector.php index 7010904a..558aa9d8 100644 --- a/src/MappedTypeDetector.php +++ b/src/MappedTypeDetector.php @@ -2,7 +2,7 @@ namespace React\Filesystem; -use React\Promise\RejectedPromise; +use Exception; class MappedTypeDetector implements TypeDetectorInterface { @@ -52,7 +52,7 @@ public function __construct(FilesystemInterface $filesystem, $options = []) public function detect(array $node) { if (!isset($node['type']) || !isset($this->mapping[$node['type']])) { - return new RejectedPromise(); + return \React\Promise\reject(new Exception('Unknown type')); } return \React\Promise\resolve([ diff --git a/src/ModeTypeDetector.php b/src/ModeTypeDetector.php index 54f14181..de2f5594 100644 --- a/src/ModeTypeDetector.php +++ b/src/ModeTypeDetector.php @@ -2,9 +2,9 @@ namespace React\Filesystem; +use Exception; use React\Filesystem\FilesystemInterface; use React\Filesystem\TypeDetectorInterface; -use React\Promise\RejectedPromise; class ModeTypeDetector implements TypeDetectorInterface { @@ -43,7 +43,7 @@ public function detect(array $node) protected function walkMapping($stat) { - $promiseChain = new RejectedPromise(); + $promiseChain = \React\Promise\reject(new Exception('Unknown type')); foreach ($this->mapping as $mappingMode => $method) { $promiseChain = $promiseChain->otherwise(function () use ($stat, $mappingMode, $method) { return $this->matchMapping($stat['mode'], $mappingMode, $method); @@ -61,6 +61,6 @@ protected function matchMapping($mode, $mappingMode, $method) ]); } - return new RejectedPromise(); + return \React\Promise\reject(new Exception('Unknown filesystem method for type')); } } diff --git a/src/Node/Directory.php b/src/Node/Directory.php index 59db5ab7..7d7b1abd 100644 --- a/src/Node/Directory.php +++ b/src/Node/Directory.php @@ -8,7 +8,6 @@ use React\Filesystem\ObjectStream; use React\Filesystem\ObjectStreamSink; use React\Promise\Deferred; -use React\Promise\FulfilledPromise; class Directory implements DirectoryInterface { @@ -114,7 +113,6 @@ protected function processSizeContents($nodes, $recursive) $numbers['directories'] += $size['directories']; $numbers['files'] += $size['files']; $numbers['size'] += $size['size']; - return new FulfilledPromise(); }); } break; @@ -122,7 +120,6 @@ protected function processSizeContents($nodes, $recursive) $numbers['files']++; $promises[] = $node->size()->then(function ($size) use (&$numbers) { $numbers['size'] += $size; - return new FulfilledPromise(); }); break; } @@ -189,7 +186,7 @@ public function createRecursive($mode = AdapterInterface::CREATION_MODE) })->then(function () use ($mode) { return $this->create($mode); })->then(function () { - return new FulfilledPromise(); + return null; }); } diff --git a/src/Node/File.php b/src/Node/File.php index 31e47127..c62b5ea0 100644 --- a/src/Node/File.php +++ b/src/Node/File.php @@ -2,13 +2,12 @@ namespace React\Filesystem\Node; +use Exception; use React\Filesystem\AdapterInterface; use React\Filesystem\FilesystemInterface; use React\Filesystem\ObjectStream; use React\Filesystem\ObjectStreamSink; use React\Filesystem\Stream\GenericStreamInterface; -use React\Promise\FulfilledPromise; -use React\Promise\RejectedPromise; use React\Promise\Stream; use React\Stream\ReadableStreamInterface; use React\Stream\WritableStreamInterface; @@ -50,9 +49,9 @@ public function __construct($filename, FilesystemInterface $filesystem) public function exists() { return $this->stat()->then(function () { - return new FulfilledPromise(); + return null; }, function () { - return new RejectedPromise(new \Exception('Not found')); + throw new Exception('Not found'); }); } @@ -96,7 +95,7 @@ public function rename($toFilename) public function create($mode = AdapterInterface::CREATION_MODE, $time = null) { return $this->stat()->then(function () { - return new RejectedPromise(new \Exception('File exists')); + throw new \Exception('File exists already'); }, function () use ($mode, $time) { return $this->adapter->touch($this->path, $mode, $time); }); @@ -116,7 +115,7 @@ public function touch($mode = AdapterInterface::CREATION_MODE, $time = null) public function open($flags, $mode = AdapterInterface::CREATION_MODE) { if ($this->open === true) { - return new RejectedPromise(); + return \React\Promise\reject(new Exception('File is already open')); } return $this->adapter->open($this->path, $flags, $mode)->then(function (GenericStreamInterface $stream) { @@ -132,13 +131,12 @@ public function open($flags, $mode = AdapterInterface::CREATION_MODE) public function close() { if ($this->open === false) { - return new RejectedPromise(); + return \React\Promise\reject(new Exception('File is already closed')); } return $this->adapter->close($this->fileDescriptor)->then(function () { $this->open = false; $this->fileDescriptor = null; - return new FulfilledPromise(); }); } diff --git a/src/OpenFileLimiter.php b/src/OpenFileLimiter.php index 4778acca..0f52063a 100644 --- a/src/OpenFileLimiter.php +++ b/src/OpenFileLimiter.php @@ -3,7 +3,6 @@ namespace React\Filesystem; use React\Promise\Deferred; -use React\Promise\FulfilledPromise; class OpenFileLimiter { @@ -43,7 +42,7 @@ public function open() { if ($this->current < $this->limit) { $this->current++; - return new FulfilledPromise(); + return \React\Promise\resolve(); } $deferred = new Deferred(); diff --git a/src/Stream/DuplexStream.php b/src/Stream/DuplexStream.php index 913843db..f00e5f9e 100644 --- a/src/Stream/DuplexStream.php +++ b/src/Stream/DuplexStream.php @@ -5,7 +5,6 @@ use Evenement\EventEmitter; use React\Filesystem\AdapterInterface; use React\Stream\DuplexStreamInterface; -use React\Promise\FulfilledPromise; class DuplexStream extends EventEmitter implements DuplexStreamInterface, GenericStreamInterface { @@ -39,12 +38,11 @@ protected function readChunk() protected function resolveSize() { if ($this->readCursor < $this->size) { - return new FulfilledPromise(); + return \React\Promise\resolve(); } return $this->getFilesystem()->stat($this->path)->then(function ($stat) { $this->size = $stat['size']; - return new FulfilledPromise(); }); } } diff --git a/src/Stream/ReadableStreamTrait.php b/src/Stream/ReadableStreamTrait.php index 40e5e571..378a0644 100644 --- a/src/Stream/ReadableStreamTrait.php +++ b/src/Stream/ReadableStreamTrait.php @@ -2,8 +2,7 @@ namespace React\Filesystem\Stream; -use React\Promise\FulfilledPromise; -use React\Promise\RejectedPromise; +use Exception; use React\Stream\Util; use React\Stream\WritableStreamInterface; @@ -27,11 +26,11 @@ public function resume() if ($this->size === null && $this->sizeLookupPromise === null) { $this->sizeLookupPromise = $this->getFilesystem()->stat($this->getPath())->then(function ($info) { if ($this->size !== null) { - return new RejectedPromise(); + throw new Exception('File was already stat-ed'); } + $this->size = $info['size']; $this->readCursor = 0; - return new FulfilledPromise(); }); } diff --git a/src/functions.php b/src/functions.php index 715b36eb..184f3796 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,7 +2,7 @@ namespace React\Filesystem; -use React\Promise\RejectedPromise; +use Exception; /** * @param AdapterInterface $adapter @@ -40,7 +40,7 @@ function getOpenFileLimit(array $options) */ function detectType(array $typeDetectors, array $node) { - $promiseChain = new RejectedPromise(); + $promiseChain = \React\Promise\reject(new Exception('Unknown type')); foreach ($typeDetectors as $detector) { $promiseChain = $promiseChain->otherwise(function () use ($node, $detector) { return $detector->detect($node); diff --git a/tests/Eio/ConstTypeDetectorTest.php b/tests/Eio/ConstTypeDetectorTest.php index 18f4e95d..f5cab9c2 100644 --- a/tests/Eio/ConstTypeDetectorTest.php +++ b/tests/Eio/ConstTypeDetectorTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Filesystem\Eio; +use Exception; use React\Filesystem\Eio\ConstTypeDetector; use React\Filesystem\Filesystem; use React\Tests\Filesystem\TestCase; @@ -62,7 +63,7 @@ public function testDetectUnknown() (new ConstTypeDetector($filesystem))->detect([ 'type' => 123, ])->otherwise(function ($result) use (&$callbackFired) { - $this->assertNull($result); + $this->assertInstanceOf('Exception', $result); $callbackFired = true; }); diff --git a/tests/ModeTypeDetectorTest.php b/tests/ModeTypeDetectorTest.php index 4777b6bf..b8f1e497 100644 --- a/tests/ModeTypeDetectorTest.php +++ b/tests/ModeTypeDetectorTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Filesystem; +use Exception; use React\Filesystem\Filesystem; use React\Filesystem\ModeTypeDetector; use React\Promise\FulfilledPromise; @@ -69,7 +70,7 @@ public function testDetectUnknown() (new ModeTypeDetector($filesystem))->detect([ 'path' => 'foo.bar', ])->otherwise(function ($result) use (&$callbackFired) { - $this->assertNull($result); + $this->assertInstanceOf('Exception', $result); $callbackFired = true; }); diff --git a/tests/Node/FileTest.php b/tests/Node/FileTest.php index cd012150..98119732 100644 --- a/tests/Node/FileTest.php +++ b/tests/Node/FileTest.php @@ -85,16 +85,7 @@ public function testExists() Filesystem::createFromAdapter($filesystem), ]); - $promise = $this->getMock('React\Promise\PromiseInterface'); - - $promise - ->expects($this->once()) - ->method('then') - ->with($this->isType('callable')) - ->will($this->returnCallback(function ($resolveCb) { - return $resolveCb(); - })) - ; + $promise = \React\Promise\resolve(); $file ->expects($this->once()) @@ -118,16 +109,7 @@ public function testDoesntExists() Filesystem::createFromAdapter($filesystem), ]); - $promise = $this->getMock('React\Promise\PromiseInterface'); - - $promise - ->expects($this->once()) - ->method('then') - ->with($this->isType('callable')) - ->will($this->returnCallback(function ($null, $resolveCb) { - return $resolveCb(); - })) - ; + $promise = \React\Promise\resolve(); $file ->expects($this->once()) @@ -241,7 +223,7 @@ public function testCreateFail() $callbackFired = false; (new File($path, Filesystem::createFromAdapter($filesystem)))->create()->then(null, function ($e) use (&$callbackFired) { $this->assertInstanceOf('Exception', $e); - $this->assertSame('File exists', $e->getMessage()); + $this->assertSame('File exists already', $e->getMessage()); $callbackFired = true; });