Skip to content

Commit

Permalink
Refactor StreamTrait functionality into Stream.php
Browse files Browse the repository at this point in the history
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 <matthew@matthewsetter.com>
  • Loading branch information
settermjd authored and weierophinney committed Sep 2, 2020
1 parent 0fb74eb commit 5e01ce5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
18 changes: 17 additions & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
);
}
}
6 changes: 0 additions & 6 deletions src/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

class StreamFactory implements StreamFactoryInterface
{
use StreamTrait;

/**
* {@inheritDoc}
Expand All @@ -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);
}
}
30 changes: 0 additions & 30 deletions src/StreamTrait.php

This file was deleted.

0 comments on commit 5e01ce5

Please sign in to comment.