genkgo /
mail
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Genkgo\Mail\Stream; |
||
| 5 | |||
| 6 | use Genkgo\Mail\StreamInterface; |
||
| 7 | |||
| 8 | final class ResourceStream implements StreamInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var resource |
||
| 12 | */ |
||
| 13 | private $resource; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param resource $resource |
||
| 17 | */ |
||
| 18 | 113 | public function __construct($resource) |
|
| 19 | { |
||
| 20 | 113 | if (!\is_resource($resource)) { |
|
| 21 | throw new \InvalidArgumentException('Argument 0 must be a resource'); |
||
| 22 | } |
||
| 23 | |||
| 24 | 113 | \rewind($resource); |
|
| 25 | 113 | $this->resource = $resource; |
|
| 26 | 113 | } |
|
| 27 | |||
| 28 | /** |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | 52 | public function __toString(): string |
|
| 32 | { |
||
| 33 | 52 | \rewind($this->resource); |
|
| 34 | 52 | return (string)\stream_get_contents($this->resource); |
|
| 35 | } |
||
| 36 | |||
| 37 | public function close(): void |
||
| 38 | { |
||
| 39 | \fclose($this->resource); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | 107 | public function detach() |
|
| 46 | { |
||
| 47 | 107 | return $this->resource; |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return int|null |
||
| 52 | */ |
||
| 53 | 8 | public function getSize(): ?int |
|
| 54 | { |
||
| 55 | 8 | $stat = \fstat($this->resource); |
|
| 56 | 8 | if ($stat === false) { |
|
| 57 | throw new \UnexpectedValueException('Cannot get stat from resource'); |
||
| 58 | } |
||
| 59 | |||
| 60 | 8 | return $stat['size']; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return int |
||
| 65 | * @throws \RuntimeException |
||
| 66 | */ |
||
| 67 | 6 | public function tell(): int |
|
| 68 | { |
||
| 69 | 6 | $tell = \ftell($this->resource); |
|
| 70 | 6 | if ($tell === false) { |
|
| 71 | throw new \UnexpectedValueException('Cannot get tell from resource'); |
||
| 72 | } |
||
| 73 | |||
| 74 | 6 | return $tell; |
|
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 7 | public function eof(): bool |
|
| 81 | { |
||
| 82 | 7 | return \feof($this->resource); |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return bool |
||
| 87 | */ |
||
| 88 | public function isSeekable(): bool |
||
| 89 | { |
||
| 90 | $metaData = \stream_get_meta_data($this->resource); |
||
| 91 | if (!isset($metaData['seekable'])) { |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $metaData['seekable']; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param int $offset |
||
| 100 | * @param int $whence |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | 1 | public function seek(int $offset, int $whence = SEEK_SET): int |
|
| 104 | { |
||
| 105 | 1 | return \fseek($this->resource, $offset, $whence); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | 55 | public function rewind(): bool |
|
| 112 | { |
||
| 113 | 55 | return \rewind($this->resource); |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | 1 | public function isWritable(): bool |
|
| 120 | { |
||
| 121 | 1 | $metaData = \stream_get_meta_data($this->resource); |
|
| 122 | 1 | if (!isset($metaData['uri'])) { |
|
| 123 | return false; |
||
| 124 | } |
||
| 125 | |||
| 126 | 1 | return \is_writable($metaData['uri']) || $metaData['uri'] === 'php://memory'; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $string |
||
| 131 | * @return int |
||
| 132 | */ |
||
| 133 | 1 | public function write($string): int |
|
| 134 | { |
||
| 135 | 1 | $written = \fwrite($this->resource, $string); |
|
| 136 | 1 | if ($written === false) { |
|
| 137 | throw new \UnexpectedValueException('Cannot write data to resource'); |
||
| 138 | } |
||
| 139 | |||
| 140 | 1 | return $written; |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | public function isReadable(): bool |
||
| 147 | { |
||
| 148 | return true; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param int $length |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 15 | public function read(int $length): string |
|
| 156 | { |
||
| 157 | 15 | $bytes = \fread($this->resource, $length); |
|
| 158 | 15 | if ($bytes === false) { |
|
| 159 | throw new \UnexpectedValueException('Cannot read from resource'); |
||
| 160 | } |
||
| 161 | |||
| 162 | 15 | return $bytes; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 17 | public function getContents(): string |
|
| 169 | { |
||
| 170 | 17 | $contents = \stream_get_contents($this->resource); |
|
| 171 | 17 | if ($contents === false) { |
|
| 172 | throw new \UnexpectedValueException('Cannot read contents from resource'); |
||
| 173 | } |
||
| 174 | |||
| 175 | 17 | return $contents; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param array<int, string> $keys |
||
| 180 | * @return array<string, mixed> |
||
|
0 ignored issues
–
show
|
|||
| 181 | */ |
||
| 182 | 43 | public function getMetadata(array $keys = []): array |
|
| 183 | { |
||
| 184 | 43 | $metaData = \stream_get_meta_data($this->resource); |
|
| 185 | |||
| 186 | 43 | $keys = \array_map('strtolower', $keys); |
|
| 187 | |||
| 188 | 43 | return \array_filter( |
|
| 189 | 43 | $metaData, |
|
| 190 | function ($key) use ($keys) { |
||
| 191 | 43 | return \in_array(\strtolower($key), $keys); |
|
| 192 | 43 | }, |
|
| 193 | 43 | ARRAY_FILTER_USE_KEY |
|
| 194 | ); |
||
| 195 | } |
||
| 196 | } |
||
| 197 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.