From 5e01ce59d2b1d33ae031643c5bfa687d0f104176 Mon Sep 17 00:00:00 2001 From: Matthew Setter Date: Thu, 13 Aug 2020 21:18:48 +0200 Subject: [PATCH] Refactor StreamTrait functionality into Stream.php Following on from the advice of @lcobucci, this change refactors the StreamTrait functionality into Stream, and removes the trait from both Stream and StreamFactory. On thinking about it, it makes most sense to have this functionality directly in Stream, and not, in effect, duplicated in multiple locations by another form. Signed-off-by: Matthew Setter --- src/Stream.php | 18 +++++++++++++++++- src/StreamFactory.php | 6 ------ src/StreamTrait.php | 30 ------------------------------ 3 files changed, 17 insertions(+), 37 deletions(-) delete mode 100644 src/StreamTrait.php diff --git a/src/Stream.php b/src/Stream.php index b841fc2a..d6e165e1 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -40,7 +40,10 @@ */ class Stream implements StreamInterface { - use StreamTrait; + /** + * A list of allowed stream resource types that are allowed to instantiate a Stream + */ + private const ALLOWED_STREAM_RESOURCE_TYPES = ['gd', 'stream']; /** * @var resource|null @@ -359,4 +362,17 @@ private function setStream($stream, string $mode = 'r') : void $this->resource = $resource; } + + /** + * Determine if a resource is one of the resource types allowed to instantiate a Stream + * + * @param resource $resource Stream resource. + */ + private function isValidStreamResourceType($resource): bool + { + return ( + is_resource($resource) && + in_array(get_resource_type($resource), self::ALLOWED_STREAM_RESOURCE_TYPES, true) + ); + } } diff --git a/src/StreamFactory.php b/src/StreamFactory.php index ce95a0c8..9f4ab328 100644 --- a/src/StreamFactory.php +++ b/src/StreamFactory.php @@ -21,7 +21,6 @@ class StreamFactory implements StreamFactoryInterface { - use StreamTrait; /** * {@inheritDoc} @@ -48,11 +47,6 @@ public function createStreamFromFile(string $file, string $mode = 'r') : StreamI */ public function createStreamFromResource($resource) : StreamInterface { - if (! $this->isValidStreamResourceType($resource)) { - throw new Exception\InvalidArgumentException( - 'Invalid stream provided; must be a stream resource' - ); - } return new Stream($resource); } } diff --git a/src/StreamTrait.php b/src/StreamTrait.php deleted file mode 100644 index 44f33850..00000000 --- a/src/StreamTrait.php +++ /dev/null @@ -1,30 +0,0 @@ -allowedTypes, true)); - } -}