| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| @@ -7,19 +8,17 @@ | ||
| 7 | 8 | * Compose stream implementations based on a hash of functions. |
| 8 | 9 | * |
| 9 | 10 | * Allows for easy testing and extension of a provided stream without needing |
| 10 | 11 | * to create a concrete class for a simple extension point. |
| 11 | - * | |
| 12 | - * @final | |
| 13 | 12 | */ |
| 14 | -class FnStream implements StreamInterface | |
| 13 | +#[\AllowDynamicProperties] | |
| 14 | +final class FnStream implements StreamInterface | |
| 15 | 15 | { |
| 16 | - /** @var array */ | |
| 16 | + private const SLOTS = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; | |
| 17 | + /** @var array<string, callable> */ | |
| 17 | 18 | private $methods; |
| 18 | - /** @var array Methods that must be implemented in the given array */ | |
| 19 | - private static $slots = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; | |
| 20 | 19 | /** |
| 21 | - * @param array $methods Hash of method name to a callable. | |
| 20 | + * @param array<string, callable> $methods Hash of method name to a callable. | |
| 22 | 21 | */ |
| 23 | 22 | public function __construct(array $methods) |
| 24 | 23 | { |
| 25 | 24 | $this->methods = $methods; |
| @@ -32,9 +31,9 @@ | ||
| 32 | 31 | * Lazily determine which methods are not implemented. |
| 33 | 32 | * |
| 34 | 33 | * @throws \BadMethodCallException |
| 35 | 34 | */ |
| 36 | - public function __get($name) | |
| 35 | + public function __get(string $name) : void | |
| 37 | 36 | { |
| 38 | 37 | throw new \BadMethodCallException(\str_replace('_fn_', '', $name) . '() is not implemented in the FnStream'); |
| 39 | 38 | } |
| 40 | 39 | /** |
| @@ -42,9 +41,9 @@ | ||
| 42 | 41 | */ |
| 43 | 42 | public function __destruct() |
| 44 | 43 | { |
| 45 | 44 | if (isset($this->_fn_close)) { |
| 46 | - \call_user_func($this->_fn_close); | |
| 45 | + ($this->_fn_close)(); | |
| 47 | 46 | } |
| 48 | 47 | } |
| 49 | 48 | /** |
| 50 | 49 | * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
| @@ -50,9 +49,9 @@ | ||
| 50 | 49 | * An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
| 51 | 50 | * |
| 52 | 51 | * @throws \LogicException |
| 53 | 52 | */ |
| 54 | - public function __wakeup() | |
| 53 | + public function __wakeup() : void | |
| 55 | 54 | { |
| 56 | 55 | throw new \LogicException('FnStream should never be unserialized'); |
| 57 | 56 | } |
| 58 | 57 | /** |
| @@ -58,10 +57,10 @@ | ||
| 58 | 57 | /** |
| 59 | 58 | * Adds custom functionality to an underlying stream by intercepting |
| 60 | 59 | * specific method calls. |
| 61 | 60 | * |
| 62 | - * @param StreamInterface $stream Stream to decorate | |
| 63 | - * @param array $methods Hash of method name to a closure | |
| 61 | + * @param StreamInterface $stream Stream to decorate | |
| 62 | + * @param array<string, callable> $methods Hash of method name to a closure | |
| 64 | 63 | * |
| 65 | 64 | * @return FnStream |
| 66 | 65 | */ |
| 67 | 66 | public static function decorate(StreamInterface $stream, array $methods) |
| @@ -67,70 +66,84 @@ | ||
| 67 | 66 | public static function decorate(StreamInterface $stream, array $methods) |
| 68 | 67 | { |
| 69 | 68 | // If any of the required methods were not provided, then simply |
| 70 | 69 | // proxy to the decorated stream. |
| 71 | - foreach (\array_diff(self::$slots, \array_keys($methods)) as $diff) { | |
| 72 | - $methods[$diff] = [$stream, $diff]; | |
| 70 | + foreach (\array_diff(self::SLOTS, \array_keys($methods)) as $diff) { | |
| 71 | + /** @var callable $callable */ | |
| 72 | + $callable = [$stream, $diff]; | |
| 73 | + $methods[$diff] = $callable; | |
| 73 | 74 | } |
| 74 | 75 | return new self($methods); |
| 75 | 76 | } |
| 76 | - public function __toString() | |
| 77 | + public function __toString() : string | |
| 77 | 78 | { |
| 78 | - return \call_user_func($this->_fn___toString); | |
| 79 | + try { | |
| 80 | + /** @var string */ | |
| 81 | + return ($this->_fn___toString)(); | |
| 82 | + } catch (\Throwable $e) { | |
| 83 | + if (\PHP_VERSION_ID >= 70400) { | |
| 84 | + throw $e; | |
| 85 | + } | |
| 86 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); | |
| 87 | + return ''; | |
| 88 | + } | |
| 79 | 89 | } |
| 80 | - public function close() | |
| 90 | + public function close() : void | |
| 81 | 91 | { |
| 82 | - return \call_user_func($this->_fn_close); | |
| 92 | + ($this->_fn_close)(); | |
| 83 | 93 | } |
| 84 | 94 | public function detach() |
| 85 | 95 | { |
| 86 | - return \call_user_func($this->_fn_detach); | |
| 96 | + return ($this->_fn_detach)(); | |
| 87 | 97 | } |
| 88 | - public function getSize() | |
| 98 | + public function getSize() : ?int | |
| 89 | 99 | { |
| 90 | - return \call_user_func($this->_fn_getSize); | |
| 100 | + return ($this->_fn_getSize)(); | |
| 91 | 101 | } |
| 92 | - public function tell() | |
| 102 | + public function tell() : int | |
| 93 | 103 | { |
| 94 | - return \call_user_func($this->_fn_tell); | |
| 104 | + return ($this->_fn_tell)(); | |
| 95 | 105 | } |
| 96 | - public function eof() | |
| 106 | + public function eof() : bool | |
| 97 | 107 | { |
| 98 | - return \call_user_func($this->_fn_eof); | |
| 108 | + return ($this->_fn_eof)(); | |
| 99 | 109 | } |
| 100 | - public function isSeekable() | |
| 110 | + public function isSeekable() : bool | |
| 101 | 111 | { |
| 102 | - return \call_user_func($this->_fn_isSeekable); | |
| 112 | + return ($this->_fn_isSeekable)(); | |
| 103 | 113 | } |
| 104 | - public function rewind() | |
| 114 | + public function rewind() : void | |
| 105 | 115 | { |
| 106 | - \call_user_func($this->_fn_rewind); | |
| 116 | + ($this->_fn_rewind)(); | |
| 107 | 117 | } |
| 108 | - public function seek($offset, $whence = \SEEK_SET) | |
| 118 | + public function seek($offset, $whence = \SEEK_SET) : void | |
| 109 | 119 | { |
| 110 | - \call_user_func($this->_fn_seek, $offset, $whence); | |
| 120 | + ($this->_fn_seek)($offset, $whence); | |
| 111 | 121 | } |
| 112 | - public function isWritable() | |
| 122 | + public function isWritable() : bool | |
| 113 | 123 | { |
| 114 | - return \call_user_func($this->_fn_isWritable); | |
| 124 | + return ($this->_fn_isWritable)(); | |
| 115 | 125 | } |
| 116 | - public function write($string) | |
| 126 | + public function write($string) : int | |
| 117 | 127 | { |
| 118 | - return \call_user_func($this->_fn_write, $string); | |
| 128 | + return ($this->_fn_write)($string); | |
| 119 | 129 | } |
| 120 | - public function isReadable() | |
| 130 | + public function isReadable() : bool | |
| 121 | 131 | { |
| 122 | - return \call_user_func($this->_fn_isReadable); | |
| 132 | + return ($this->_fn_isReadable)(); | |
| 123 | 133 | } |
| 124 | - public function read($length) | |
| 134 | + public function read($length) : string | |
| 125 | 135 | { |
| 126 | - return \call_user_func($this->_fn_read, $length); | |
| 136 | + return ($this->_fn_read)($length); | |
| 127 | 137 | } |
| 128 | - public function getContents() | |
| 138 | + public function getContents() : string | |
| 129 | 139 | { |
| 130 | - return \call_user_func($this->_fn_getContents); | |
| 140 | + return ($this->_fn_getContents)(); | |
| 131 | 141 | } |
| 142 | + /** | |
| 143 | + * @return mixed | |
| 144 | + */ | |
| 132 | 145 | public function getMetadata($key = null) |
| 133 | 146 | { |
| 134 | - return \call_user_func($this->_fn_getMetadata, $key); | |
| 147 | + return ($this->_fn_getMetadata)($key); | |
| 135 | 148 | } |
| 136 | 149 | } |