diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index f35538b..04765de 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -11,8 +11,7 @@ return $config->setRules([ '@Symfony' => true, '@Symfony:risky' => true, - 'array_syntax' => array('syntax' => 'short'), - 'native_function_invocation' => true, + 'native_function_invocation' => ['include'=> ['@all']], 'native_constant_invocation' => true, 'ordered_imports' => true, 'declare_strict_types' => false, diff --git a/psalm.baseline.xml b/psalm.baseline.xml index 8cdb7fa..fe5b92e 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -2,7 +2,7 @@ - return trigger_error((string) $e, \E_USER_ERROR); + return \trigger_error((string) $e, \E_USER_ERROR); diff --git a/src/Factory/Psr17Factory.php b/src/Factory/Psr17Factory.php index 326e517..440bec3 100644 --- a/src/Factory/Psr17Factory.php +++ b/src/Factory/Psr17Factory.php @@ -41,12 +41,12 @@ public function createStreamFromFile(string $filename, string $mode = 'r'): Stre throw new \RuntimeException('Path cannot be empty'); } - if (false === $resource = @fopen($filename, $mode)) { + if (false === $resource = @\fopen($filename, $mode)) { if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) { - throw new \InvalidArgumentException(sprintf('The mode "%s" is invalid.', $mode)); + throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode)); } - throw new \RuntimeException(sprintf('The file "%s" cannot be opened: %s', $filename, error_get_last()['message'] ?? '')); + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? '')); } return Stream::create($resource); diff --git a/src/MessageTrait.php b/src/MessageTrait.php index e8ec75f..2544fa4 100644 --- a/src/MessageTrait.php +++ b/src/MessageTrait.php @@ -57,7 +57,7 @@ public function getHeaders(): array public function hasHeader($header): bool { - return isset($this->headerNames[strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]); + return isset($this->headerNames[\strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')]); } public function getHeader($header): array @@ -66,7 +66,7 @@ public function getHeader($header): array throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string'); } - $header = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); + $header = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); if (!isset($this->headerNames[$header])) { return []; } @@ -78,13 +78,13 @@ public function getHeader($header): array public function getHeaderLine($header): string { - return implode(', ', $this->getHeader($header)); + return \implode(', ', $this->getHeader($header)); } public function withHeader($header, $value): self { $value = $this->validateAndTrimHeader($header, $value); - $normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); + $normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); $new = clone $this; if (isset($new->headerNames[$normalized])) { @@ -114,7 +114,7 @@ public function withoutHeader($header): self throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string'); } - $normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); + $normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); if (!isset($this->headerNames[$normalized])) { return $this; } @@ -156,10 +156,10 @@ private function setHeaders(array $headers): void $header = (string) $header; } $value = $this->validateAndTrimHeader($header, $value); - $normalized = strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); + $normalized = \strtr($header, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); if (isset($this->headerNames[$normalized])) { $header = $this->headerNames[$normalized]; - $this->headers[$header] = array_merge($this->headers[$header], $value); + $this->headers[$header] = \array_merge($this->headers[$header], $value); } else { $this->headerNames[$normalized] = $header; $this->headers[$header] = $value; @@ -187,17 +187,17 @@ private function setHeaders(array $headers): void */ private function validateAndTrimHeader($header, $values): array { - if (!\is_string($header) || 1 !== preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header)) { + if (!\is_string($header) || 1 !== \preg_match("@^[!#$%&'*+.^_`|~0-9A-Za-z-]+$@", $header)) { throw new \InvalidArgumentException('Header name must be an RFC 7230 compatible string'); } if (!\is_array($values)) { // This is simple, just one value. - if ((!is_numeric($values) && !\is_string($values)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $values)) { + if ((!\is_numeric($values) && !\is_string($values)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $values)) { throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings'); } - return [trim((string) $values, " \t")]; + return [\trim((string) $values, " \t")]; } if (empty($values)) { @@ -207,11 +207,11 @@ private function validateAndTrimHeader($header, $values): array // Assert Non empty array $returnValues = []; foreach ($values as $v) { - if ((!is_numeric($v) && !\is_string($v)) || 1 !== preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $v)) { + if ((!\is_numeric($v) && !\is_string($v)) || 1 !== \preg_match("@^[ \t\x21-\x7E\x80-\xFF]*$@", (string) $v)) { throw new \InvalidArgumentException('Header values must be RFC 7230 compatible strings'); } - $returnValues[] = trim((string) $v, " \t"); + $returnValues[] = \trim((string) $v, " \t"); } return $returnValues; diff --git a/src/RequestTrait.php b/src/RequestTrait.php index 597874d..7c39bbb 100644 --- a/src/RequestTrait.php +++ b/src/RequestTrait.php @@ -46,7 +46,7 @@ public function withRequestTarget($requestTarget): self throw new \InvalidArgumentException('Request target must be a string'); } - if (preg_match('#\s#', $requestTarget)) { + if (\preg_match('#\s#', $requestTarget)) { throw new \InvalidArgumentException('Invalid request target provided; cannot contain whitespace'); } diff --git a/src/Response.php b/src/Response.php index 3062b3b..9a26d2c 100644 --- a/src/Response.php +++ b/src/Response.php @@ -75,7 +75,7 @@ public function withStatus($code, $reasonPhrase = ''): self $code = (int) $code; if ($code < 100 || $code > 599) { - throw new \InvalidArgumentException(sprintf('Status code has to be an integer between 100 and 599. A status code of %d was given', $code)); + throw new \InvalidArgumentException(\sprintf('Status code has to be an integer between 100 and 599. A status code of %d was given', $code)); } $new = clone $this; diff --git a/src/ServerRequest.php b/src/ServerRequest.php index 1e47a06..7f5022e 100644 --- a/src/ServerRequest.php +++ b/src/ServerRequest.php @@ -56,7 +56,7 @@ public function __construct(string $method, $uri, array $headers = [], $body = n $this->uri = $uri; $this->setHeaders($headers); $this->protocol = $version; - parse_str($uri->getQuery(), $this->queryParams); + \parse_str($uri->getQuery(), $this->queryParams); if (!$this->hasHeader('Host')) { $this->updateHostFromUri(); diff --git a/src/Stream.php b/src/Stream.php index 2c08b9f..d173f35 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -61,8 +61,8 @@ public function __construct($body) } $this->stream = $body; - $meta = stream_get_meta_data($this->stream); - $this->seekable = $meta['seekable'] && 0 === fseek($this->stream, 0, \SEEK_CUR); + $meta = \stream_get_meta_data($this->stream); + $this->seekable = $meta['seekable'] && 0 === \fseek($this->stream, 0, \SEEK_CUR); $this->readable = isset(self::READ_WRITE_HASH['read'][$meta['mode']]); $this->writable = isset(self::READ_WRITE_HASH['write'][$meta['mode']]); } @@ -81,9 +81,9 @@ public static function create($body = ''): StreamInterface } if (\is_string($body)) { - $resource = fopen('php://temp', 'rw+'); - fwrite($resource, $body); - fseek($resource, 0); + $resource = \fopen('php://temp', 'rw+'); + \fwrite($resource, $body); + \fseek($resource, 0); $body = $resource; } @@ -118,13 +118,13 @@ public function __toString() throw $e; } - if (\is_array($errorHandler = set_error_handler('var_dump'))) { + if (\is_array($errorHandler = \set_error_handler('var_dump'))) { $errorHandler = $errorHandler[0] ?? null; } - restore_error_handler(); + \restore_error_handler(); if ($e instanceof \Error || $errorHandler instanceof SymfonyErrorHandler || $errorHandler instanceof SymfonyLegacyErrorHandler) { - return trigger_error((string) $e, \E_USER_ERROR); + return \trigger_error((string) $e, \E_USER_ERROR); } return ''; @@ -135,7 +135,7 @@ public function close(): void { if (isset($this->stream)) { if (\is_resource($this->stream)) { - fclose($this->stream); + \fclose($this->stream); } $this->detach(); } @@ -176,10 +176,10 @@ public function getSize(): ?int // Clear the stat cache if the stream has a URI if ($uri = $this->getUri()) { - clearstatcache(true, $uri); + \clearstatcache(true, $uri); } - $stats = fstat($this->stream); + $stats = \fstat($this->stream); if (isset($stats['size'])) { $this->size = $stats['size']; @@ -195,8 +195,8 @@ public function tell(): int throw new \RuntimeException('Stream is detached'); } - if (false === $result = @ftell($this->stream)) { - throw new \RuntimeException('Unable to determine stream position: ' . (error_get_last()['message'] ?? '')); + if (false === $result = @\ftell($this->stream)) { + throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -204,7 +204,7 @@ public function tell(): int public function eof(): bool { - return !isset($this->stream) || feof($this->stream); + return !isset($this->stream) || \feof($this->stream); } public function isSeekable(): bool @@ -222,8 +222,8 @@ public function seek($offset, $whence = \SEEK_SET): void throw new \RuntimeException('Stream is not seekable'); } - if (-1 === fseek($this->stream, $offset, $whence)) { - throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . var_export($whence, true)); + if (-1 === \fseek($this->stream, $offset, $whence)) { + throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . \var_export($whence, true)); } } @@ -250,8 +250,8 @@ public function write($string): int // We can't know the size after writing anything $this->size = null; - if (false === $result = @fwrite($this->stream, $string)) { - throw new \RuntimeException('Unable to write to stream: ' . (error_get_last()['message'] ?? '')); + if (false === $result = @\fwrite($this->stream, $string)) { + throw new \RuntimeException('Unable to write to stream: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -272,8 +272,8 @@ public function read($length): string throw new \RuntimeException('Cannot read from non-readable stream'); } - if (false === $result = @fread($this->stream, $length)) { - throw new \RuntimeException('Unable to read from stream: ' . (error_get_last()['message'] ?? '')); + if (false === $result = @\fread($this->stream, $length)) { + throw new \RuntimeException('Unable to read from stream: ' . (\error_get_last()['message'] ?? '')); } return $result; @@ -285,8 +285,8 @@ public function getContents(): string throw new \RuntimeException('Stream is detached'); } - if (false === $contents = @stream_get_contents($this->stream)) { - throw new \RuntimeException('Unable to read stream contents: ' . (error_get_last()['message'] ?? '')); + if (false === $contents = @\stream_get_contents($this->stream)) { + throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? '')); } return $contents; @@ -305,7 +305,7 @@ public function getMetadata($key = null) return $key ? null : []; } - $meta = stream_get_meta_data($this->stream); + $meta = \stream_get_meta_data($this->stream); if (null === $key) { return $meta; diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 0789943..c77dca4 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -114,8 +114,8 @@ public function getStream(): StreamInterface return $this->stream; } - if (false === $resource = @fopen($this->file, 'r')) { - throw new \RuntimeException(sprintf('The file "%s" cannot be opened: %s', $this->file, error_get_last()['message'] ?? '')); + if (false === $resource = @\fopen($this->file, 'r')) { + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? '')); } return Stream::create($resource); @@ -130,10 +130,10 @@ public function moveTo($targetPath): void } if (null !== $this->file) { - $this->moved = 'cli' === \PHP_SAPI ? @rename($this->file, $targetPath) : @move_uploaded_file($this->file, $targetPath); + $this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath); if (false === $this->moved) { - throw new \RuntimeException(sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, error_get_last()['message'] ?? '')); + throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? '')); } } else { $stream = $this->getStream(); @@ -141,8 +141,8 @@ public function moveTo($targetPath): void $stream->rewind(); } - if (false === $resource = @fopen($targetPath, 'w')) { - throw new \RuntimeException(sprintf('The file "%s" cannot be opened: %s', $targetPath, error_get_last()['message'] ?? '')); + if (false === $resource = @\fopen($targetPath, 'w')) { + throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? '')); } $dest = Stream::create($resource); diff --git a/src/Uri.php b/src/Uri.php index 3946765..0d2c975 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -51,14 +51,14 @@ class Uri implements UriInterface public function __construct(string $uri = '') { if ('' !== $uri) { - if (false === $parts = parse_url($uri)) { - throw new \InvalidArgumentException(sprintf('Unable to parse URI: "%s"', $uri)); + if (false === $parts = \parse_url($uri)) { + throw new \InvalidArgumentException(\sprintf('Unable to parse URI: "%s"', $uri)); } // Apply parse_url parts to a URI. - $this->scheme = isset($parts['scheme']) ? strtr($parts['scheme'], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') : ''; + $this->scheme = isset($parts['scheme']) ? \strtr($parts['scheme'], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') : ''; $this->userInfo = $parts['user'] ?? ''; - $this->host = isset($parts['host']) ? strtr($parts['host'], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') : ''; + $this->host = isset($parts['host']) ? \strtr($parts['host'], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') : ''; $this->port = isset($parts['port']) ? $this->filterPort($parts['port']) : null; $this->path = isset($parts['path']) ? $this->filterPath($parts['path']) : ''; $this->query = isset($parts['query']) ? $this->filterQueryAndFragment($parts['query']) : ''; @@ -124,7 +124,7 @@ public function getPath(): string } elseif (isset($path[1]) && '/' === $path[1]) { // If the path is starting with more than one "/", the // starting slashes MUST be reduced to one. - $path = '/' . ltrim($path, '/'); + $path = '/' . \ltrim($path, '/'); } return $path; @@ -146,7 +146,7 @@ public function withScheme($scheme): self throw new \InvalidArgumentException('Scheme must be a string'); } - if ($this->scheme === $scheme = strtr($scheme, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) { + if ($this->scheme === $scheme = \strtr($scheme, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) { return $this; } @@ -163,13 +163,13 @@ public function withUserInfo($user, $password = null): self throw new \InvalidArgumentException('User must be a string'); } - $info = preg_replace_callback('/[' . self::CHAR_GEN_DELIMS . self::CHAR_SUB_DELIMS . ']++/', [__CLASS__, 'rawurlencodeMatchZero'], $user); + $info = \preg_replace_callback('/[' . self::CHAR_GEN_DELIMS . self::CHAR_SUB_DELIMS . ']++/', [__CLASS__, 'rawurlencodeMatchZero'], $user); if (null !== $password && '' !== $password) { if (!\is_string($password)) { throw new \InvalidArgumentException('Password must be a string'); } - $info .= ':' . preg_replace_callback('/[' . self::CHAR_GEN_DELIMS . self::CHAR_SUB_DELIMS . ']++/', [__CLASS__, 'rawurlencodeMatchZero'], $password); + $info .= ':' . \preg_replace_callback('/[' . self::CHAR_GEN_DELIMS . self::CHAR_SUB_DELIMS . ']++/', [__CLASS__, 'rawurlencodeMatchZero'], $password); } if ($this->userInfo === $info) { @@ -188,7 +188,7 @@ public function withHost($host): self throw new \InvalidArgumentException('Host must be a string'); } - if ($this->host === $host = strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) { + if ($this->host === $host = \strtr($host, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')) { return $this; } @@ -270,7 +270,7 @@ private static function createUriString(string $scheme, string $authority, strin if ('' === $authority) { // If the path is starting with more than one "/" and no authority is present, the // starting slashes MUST be reduced to one. - $path = '/' . ltrim($path, '/'); + $path = '/' . \ltrim($path, '/'); } } @@ -304,7 +304,7 @@ private function filterPort($port): ?int $port = (int) $port; if (0 > $port || 0xFFFF < $port) { - throw new \InvalidArgumentException(sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); + throw new \InvalidArgumentException(\sprintf('Invalid port: %d. Must be between 0 and 65535', $port)); } return self::isNonStandardPort($this->scheme, $port) ? $port : null; @@ -316,7 +316,7 @@ private function filterPath($path): string throw new \InvalidArgumentException('Path must be a string'); } - return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path); + return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $path); } private function filterQueryAndFragment($str): string @@ -325,11 +325,11 @@ private function filterQueryAndFragment($str): string throw new \InvalidArgumentException('Query and fragment must be a string'); } - return preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str); + return \preg_replace_callback('/(?:[^' . self::CHAR_UNRESERVED . self::CHAR_SUB_DELIMS . '%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/', [__CLASS__, 'rawurlencodeMatchZero'], $str); } private static function rawurlencodeMatchZero(array $match): string { - return rawurlencode($match[0]); + return \rawurlencode($match[0]); } } diff --git a/tests/StreamTest.php b/tests/StreamTest.php index abeeb17..a77d5c4 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -13,8 +13,8 @@ class StreamTest extends TestCase { public function testConstructorInitializesProperties() { - $handle = fopen('php://temp', 'r+'); - fwrite($handle, 'data'); + $handle = \fopen('php://temp', 'r+'); + \fwrite($handle, 'data'); $stream = Stream::create($handle); $this->assertTrue($stream->isReadable()); $this->assertTrue($stream->isWritable()); @@ -42,7 +42,7 @@ public function testConstructorSeekWithStringContent() public function testStreamClosesHandleOnDestruct() { - $handle = fopen('php://temp', 'r'); + $handle = \fopen('php://temp', 'r'); $stream = Stream::create($handle); unset($stream); $this->assertFalse(\is_resource($handle)); @@ -50,8 +50,8 @@ public function testStreamClosesHandleOnDestruct() public function testConvertsToString() { - $handle = fopen('php://temp', 'w+'); - fwrite($handle, 'data'); + $handle = \fopen('php://temp', 'w+'); + \fwrite($handle, 'data'); $stream = Stream::create($handle); $this->assertEquals('data', (string) $stream); $this->assertEquals('data', (string) $stream); @@ -68,8 +68,8 @@ public function testBuildFromString() public function testGetsContents() { - $handle = fopen('php://temp', 'w+'); - fwrite($handle, 'data'); + $handle = \fopen('php://temp', 'w+'); + \fwrite($handle, 'data'); $stream = Stream::create($handle); $this->assertEquals('', $stream->getContents()); $stream->seek(0); @@ -79,8 +79,8 @@ public function testGetsContents() public function testChecksEof() { - $handle = fopen('php://temp', 'w+'); - fwrite($handle, 'data'); + $handle = \fopen('php://temp', 'w+'); + \fwrite($handle, 'data'); $stream = Stream::create($handle); $this->assertFalse($stream->eof()); $stream->read(4); @@ -90,8 +90,8 @@ public function testChecksEof() public function testGetSize() { - $size = filesize(__FILE__); - $handle = fopen(__FILE__, 'r'); + $size = \filesize(__FILE__); + $handle = \fopen(__FILE__, 'r'); $stream = Stream::create($handle); $this->assertEquals($size, $stream->getSize()); // Load from cache @@ -101,8 +101,8 @@ public function testGetSize() public function testEnsuresSizeIsConsistent() { - $h = fopen('php://temp', 'w+'); - $this->assertEquals(3, fwrite($h, 'foo')); + $h = \fopen('php://temp', 'w+'); + $this->assertEquals(3, \fwrite($h, 'foo')); $stream = Stream::create($h); $this->assertEquals(3, $stream->getSize()); $this->assertEquals(4, $stream->write('test')); @@ -113,20 +113,20 @@ public function testEnsuresSizeIsConsistent() public function testProvidesStreamPosition() { - $handle = fopen('php://temp', 'w+'); + $handle = \fopen('php://temp', 'w+'); $stream = Stream::create($handle); $this->assertEquals(0, $stream->tell()); $stream->write('foo'); $this->assertEquals(3, $stream->tell()); $stream->seek(1); $this->assertEquals(1, $stream->tell()); - $this->assertSame(ftell($handle), $stream->tell()); + $this->assertSame(\ftell($handle), $stream->tell()); $stream->close(); } public function testCanDetachStream() { - $r = fopen('php://temp', 'w+'); + $r = \fopen('php://temp', 'w+'); $stream = Stream::create($r); $stream->write('foo'); $this->assertTrue($stream->isReadable()); @@ -178,8 +178,8 @@ public function testCanDetachStream() $throws(function ($stream) { (string) $stream; }); - restore_error_handler(); - restore_exception_handler(); + \restore_error_handler(); + \restore_exception_handler(); } $stream->close(); @@ -187,7 +187,7 @@ public function testCanDetachStream() public function testCloseClearProperties() { - $handle = fopen('php://temp', 'r+'); + $handle = \fopen('php://temp', 'r+'); $stream = Stream::create($handle); $stream->close(); @@ -200,9 +200,9 @@ public function testCloseClearProperties() public function testUnseekableStreamWrapper() { - stream_wrapper_register('nyholm-psr7-test', TestStreamWrapper::class); - $handle = fopen('nyholm-psr7-test://', 'r'); - stream_wrapper_unregister('nyholm-psr7-test'); + \stream_wrapper_register('nyholm-psr7-test', TestStreamWrapper::class); + $handle = \fopen('nyholm-psr7-test://', 'r'); + \stream_wrapper_unregister('nyholm-psr7-test'); $stream = Stream::create($handle); $this->assertFalse($stream->isSeekable()); diff --git a/tests/UploadedFileTest.php b/tests/UploadedFileTest.php index 9c1f35e..8a8f9f8 100644 --- a/tests/UploadedFileTest.php +++ b/tests/UploadedFileTest.php @@ -22,8 +22,8 @@ protected function setUp(): void protected function tearDown(): void { foreach ($this->cleanup as $file) { - if (\is_scalar($file) && file_exists($file)) { - unlink($file); + if (\is_scalar($file) && \file_exists($file)) { + \unlink($file); } } } @@ -75,7 +75,7 @@ public function testRaisesExceptionOnInvalidErrorStatus($status) $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('status'); - new UploadedFile(fopen('php://temp', 'wb+'), 0, $status); + new UploadedFile(\fopen('php://temp', 'wb+'), 0, $status); } public static function invalidFilenamesAndMediaTypes() @@ -98,7 +98,7 @@ public function testRaisesExceptionOnInvalidClientFilename($filename) $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('filename'); - new UploadedFile(fopen('php://temp', 'wb+'), 0, \UPLOAD_ERR_OK, $filename); + new UploadedFile(\fopen('php://temp', 'wb+'), 0, \UPLOAD_ERR_OK, $filename); } /** @@ -109,7 +109,7 @@ public function testRaisesExceptionOnInvalidClientMediaType($mediaType) $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('media type'); - new UploadedFile(fopen('php://temp', 'wb+'), 0, \UPLOAD_ERR_OK, 'foobar.baz', $mediaType); + new UploadedFile(\fopen('php://temp', 'wb+'), 0, \UPLOAD_ERR_OK, 'foobar.baz', $mediaType); } public function testGetStreamReturnsOriginalStreamObject() @@ -122,7 +122,7 @@ public function testGetStreamReturnsOriginalStreamObject() public function testGetStreamReturnsWrappedPhpStream() { - $stream = fopen('php://temp', 'wb+'); + $stream = \fopen('php://temp', 'wb+'); $upload = new UploadedFile($stream, 0, \UPLOAD_ERR_OK); $uploadStream = $upload->getStream()->detach(); @@ -146,10 +146,10 @@ public function testSuccessful() $this->assertEquals('filename.txt', $upload->getClientFilename()); $this->assertEquals('text/plain', $upload->getClientMediaType()); - $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'successful'); + $this->cleanup[] = $to = \tempnam(\sys_get_temp_dir(), 'successful'); $upload->moveTo($to); $this->assertFileExists($to); - $this->assertEquals($stream->__toString(), file_get_contents($to)); + $this->assertEquals($stream->__toString(), \file_get_contents($to)); } public static function invalidMovePaths() @@ -186,9 +186,9 @@ public function testMoveCannotBeCalledMoreThanOnce() $stream = (new \Nyholm\Psr7\Factory\Psr17Factory())->createStream('Foo bar!'); $upload = new UploadedFile($stream, 0, \UPLOAD_ERR_OK); - $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac'); + $this->cleanup[] = $to = \tempnam(\sys_get_temp_dir(), 'diac'); $upload->moveTo($to); - $this->assertTrue(file_exists($to)); + $this->assertTrue(\file_exists($to)); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('moved'); @@ -200,7 +200,7 @@ public function testCannotRetrieveStreamAfterMove() $stream = (new \Nyholm\Psr7\Factory\Psr17Factory())->createStream('Foo bar!'); $upload = new UploadedFile($stream, 0, \UPLOAD_ERR_OK); - $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'diac'); + $this->cleanup[] = $to = \tempnam(\sys_get_temp_dir(), 'diac'); $upload->moveTo($to); $this->assertFileExists($to); @@ -239,7 +239,7 @@ public function testMoveToRaisesExceptionWhenErrorStatusPresent($status) $uploadedFile = new UploadedFile('not ok', 0, $status); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('upload error'); - $uploadedFile->moveTo(__DIR__ . '/' . uniqid()); + $uploadedFile->moveTo(__DIR__ . '/' . \uniqid()); } /** @@ -255,12 +255,12 @@ public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status) public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided() { - $this->cleanup[] = $from = tempnam(sys_get_temp_dir(), 'copy_from'); - $this->cleanup[] = $to = tempnam(sys_get_temp_dir(), 'copy_to'); + $this->cleanup[] = $from = \tempnam(\sys_get_temp_dir(), 'copy_from'); + $this->cleanup[] = $to = \tempnam(\sys_get_temp_dir(), 'copy_to'); - copy(__FILE__, $from); + \copy(__FILE__, $from); - $uploadedFile = new UploadedFile($from, 100, \UPLOAD_ERR_OK, basename($from), 'text/plain'); + $uploadedFile = new UploadedFile($from, 100, \UPLOAD_ERR_OK, \basename($from), 'text/plain'); $uploadedFile->moveTo($to); $this->assertFileEquals(__FILE__, $to); diff --git a/tests/UriTest.php b/tests/UriTest.php index 0c80f5f..4aed446 100644 --- a/tests/UriTest.php +++ b/tests/UriTest.php @@ -158,7 +158,7 @@ public function testParseUriPortCannotBeNegative() public function testParseUriPortCanBeZero() { - if (version_compare(\PHP_VERSION, '7.4.12') < 0) { + if (\version_compare(\PHP_VERSION, '7.4.12') < 0) { self::markTestSkipped('Skipping this on low PHP versions.'); }