Normalize imports for global namespace

This commit is contained in:
PJ Dietz 2020-08-10 07:31:06 -04:00
parent ff28f3c6eb
commit e6d1398bb1
12 changed files with 84 additions and 63 deletions

View File

@ -14,5 +14,10 @@ return PhpCsFixer\Config::create()
'blank_line_after_opening_tag' => true,
'cast_spaces' => true,
'class_attributes_separation' => ['elements' => ['method']],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
'single_quote' => true
]);

View File

@ -2,6 +2,8 @@
namespace WellRESTed\Dispatching;
class DispatchException extends \InvalidArgumentException
use InvalidArgumentException;
class DispatchException extends InvalidArgumentException
{
}

View File

@ -4,6 +4,7 @@ namespace WellRESTed\Message;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
use InvalidArgumentException;
/**
* Message defines core functionality for classes that represent HTTP messages.
@ -189,7 +190,7 @@ abstract class Message implements MessageInterface
* @param string $name Case-insensitive header field name.
* @param string|string[] $value Header value(s).
* @return static
* @throws \InvalidArgumentException for invalid header names or values.
* @throws InvalidArgumentException for invalid header names or values.
*/
public function withHeader($name, $value)
{
@ -213,7 +214,7 @@ abstract class Message implements MessageInterface
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s).
* @return static
* @throws \InvalidArgumentException for invalid header names or values.
* @throws InvalidArgumentException for invalid header names or values.
*/
public function withAddedHeader($name, $value)
{
@ -256,7 +257,7 @@ abstract class Message implements MessageInterface
*
* @param StreamInterface $body Body.
* @return static
* @throws \InvalidArgumentException When the body is not valid.
* @throws InvalidArgumentException When the body is not valid.
*/
public function withBody(StreamInterface $body)
{
@ -271,13 +272,13 @@ abstract class Message implements MessageInterface
* @param mixed $name
* @param mixed|mixed[] $values
* @return string[]
* @throws \InvalidArgumentException Name is not a string or value is not
* @throws InvalidArgumentException Name is not a string or value is not
* a string or array of strings
*/
private function getValidatedHeaders($name, $values)
{
if (!is_string($name)) {
throw new \InvalidArgumentException('Header name must be a string');
throw new InvalidArgumentException('Header name must be a string');
}
if (!is_array($values)) {
@ -290,7 +291,7 @@ abstract class Message implements MessageInterface
$invalid = array_filter($values, $isNotStringOrNumber);
if ($invalid) {
throw new \InvalidArgumentException('Header values must be a string or string[]');
throw new InvalidArgumentException('Header values must be a string or string[]');
}
return array_map('strval', $values);

View File

@ -3,6 +3,7 @@
namespace WellRESTed\Message;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
/**
* NullStream is a minimal, always-empty, non-writable stream.
@ -54,7 +55,7 @@ class NullStream implements StreamInterface
* Returns the current position of the file read/write pointer
*
* @return int Position of the file pointer
* @throws \RuntimeException on error.
* @throws RuntimeException on error.
*/
public function tell()
{
@ -92,11 +93,11 @@ class NullStream implements StreamInterface
* offset bytes SEEK_CUR: Set position to current location plus offset
* SEEK_END: Set position to end-of-stream plus offset.
* @return void
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function seek($offset, $whence = SEEK_SET)
{
throw new \RuntimeException('Unable to seek to position.');
throw new RuntimeException('Unable to seek to position.');
}
/**
@ -105,11 +106,11 @@ class NullStream implements StreamInterface
* @see seek()
* @link http://www.php.net/manual/en/function.fseek.php
* @return void
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function rewind()
{
throw new \RuntimeException('Unable to rewind stream.');
throw new RuntimeException('Unable to rewind stream.');
}
/**
@ -127,11 +128,11 @@ class NullStream implements StreamInterface
*
* @param string $string The string that is to be written.
* @return int Returns the number of bytes written to the stream.
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function write($string)
{
throw new \RuntimeException('Unable to write to stream.');
throw new RuntimeException('Unable to write to stream.');
}
/**
@ -152,7 +153,7 @@ class NullStream implements StreamInterface
* call returns fewer bytes.
* @return string Returns the data read from the stream, or an empty string
* if no bytes are available.
* @throws \RuntimeException if an error occurs.
* @throws RuntimeException if an error occurs.
*/
public function read($length)
{
@ -163,7 +164,7 @@ class NullStream implements StreamInterface
* Returns the remaining contents in a string
*
* @return string
* @throws \RuntimeException if unable to read or an error occurs while
* @throws RuntimeException if unable to read or an error occurs while
* reading.
*/
public function getContents()

View File

@ -5,6 +5,7 @@ namespace WellRESTed\Message;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use InvalidArgumentException;
/**
* Representation of an outgoing, client-side request.
@ -135,7 +136,7 @@ class Request extends Message implements RequestInterface
*
* @param string $method Case-insensitive method.
* @return static
* @throws \InvalidArgumentException for invalid HTTP methods.
* @throws InvalidArgumentException for invalid HTTP methods.
*/
public function withMethod($method)
{
@ -211,16 +212,16 @@ class Request extends Message implements RequestInterface
/**
* @param mixed $method
* @return string
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
private function getValidatedMethod($method)
{
if (!is_string($method)) {
throw new \InvalidArgumentException('Method must be a string.');
throw new InvalidArgumentException('Method must be a string.');
}
$method = trim($method);
if (strpos($method, ' ') !== false) {
throw new \InvalidArgumentException('Method cannot contain spaces.');
throw new InvalidArgumentException('Method cannot contain spaces.');
}
return $method;
}

View File

@ -4,6 +4,7 @@ namespace WellRESTed\Message;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use InvalidArgumentException;
/**
* Representation of an outgoing, server-side response.
@ -77,7 +78,7 @@ class Response extends Message implements ResponseInterface
* provided status code; if none is provided, implementations MAY
* use the defaults as suggested in the HTTP specification.
* @return static
* @throws \InvalidArgumentException For invalid status code arguments.
* @throws InvalidArgumentException For invalid status code arguments.
*/
public function withStatus($code, $reasonPhrase = '')
{

View File

@ -6,6 +6,7 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;
use InvalidArgumentException;
/**
* Representation of an incoming, server-side HTTP request.
@ -189,12 +190,12 @@ class ServerRequest extends Request implements ServerRequestInterface
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
* @return static
* @throws \InvalidArgumentException if an invalid structure is provided.
* @throws InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles)
{
if (!$this->isValidUploadedFilesTree($uploadedFiles)) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
'withUploadedFiles expects an array tree with UploadedFileInterface leaves.'
);
}
@ -249,7 +250,7 @@ class ServerRequest extends Request implements ServerRequestInterface
public function withParsedBody($data)
{
if (!(is_null($data) || is_array($data) || is_object($data))) {
throw new \InvalidArgumentException('Parsed body must be null, array, or object.');
throw new InvalidArgumentException('Parsed body must be null, array, or object.');
}
$request = clone $this;

View File

@ -3,6 +3,9 @@
namespace WellRESTed\Message;
use Psr\Http\Message\StreamInterface;
use InvalidArgumentException;
use Exception;
use RuntimeException;
class Stream implements StreamInterface
{
@ -32,7 +35,7 @@ class Stream implements StreamInterface
$this->write($resource);
}
} else {
throw new \InvalidArgumentException('Expected a resource handler.');
throw new InvalidArgumentException('Expected a resource handler.');
}
}
@ -54,7 +57,7 @@ class Stream implements StreamInterface
$this->rewind();
}
return $this->getContents();
} catch (\Exception $e) {
} catch (Exception $e) {
// Silence exceptions in order to conform with PHP's string casting
// operations.
return '';
@ -113,17 +116,17 @@ class Stream implements StreamInterface
* Returns the current position of the file read/write pointer
*
* @return int Position of the file pointer
* @throws \RuntimeException on error.
* @throws RuntimeException on error.
*/
public function tell()
{
if ($this->resource === null) {
throw new \RuntimeException('Unable to retrieve current position of detached stream.');
throw new RuntimeException('Unable to retrieve current position of detached stream.');
}
$position = ftell($this->resource);
if ($position === false) {
throw new \RuntimeException('Unable to retrieve current position of file pointer.');
throw new RuntimeException('Unable to retrieve current position of file pointer.');
}
return $position;
}
@ -167,12 +170,12 @@ class Stream implements StreamInterface
* offset bytes SEEK_CUR: Set position to current location plus offset
* SEEK_END: Set position to end-of-stream plus offset.
* @return void
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function seek($offset, $whence = SEEK_SET)
{
if ($this->resource === null) {
throw new \RuntimeException('Unable to seek detached stream.');
throw new RuntimeException('Unable to seek detached stream.');
}
$result = -1;
@ -180,7 +183,7 @@ class Stream implements StreamInterface
$result = fseek($this->resource, $offset, $whence);
}
if ($result === -1) {
throw new \RuntimeException('Unable to seek to position.');
throw new RuntimeException('Unable to seek to position.');
}
}
@ -193,12 +196,12 @@ class Stream implements StreamInterface
* @see seek()
* @link http://www.php.net/manual/en/function.fseek.php
* @return void
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function rewind()
{
if ($this->resource === null) {
throw new \RuntimeException('Unable to seek detached stream.');
throw new RuntimeException('Unable to seek detached stream.');
}
$result = false;
@ -206,7 +209,7 @@ class Stream implements StreamInterface
$result = rewind($this->resource);
}
if ($result === false) {
throw new \RuntimeException('Unable to rewind.');
throw new RuntimeException('Unable to rewind.');
}
}
@ -230,12 +233,12 @@ class Stream implements StreamInterface
*
* @param string $string The string that is to be written.
* @return int Returns the number of bytes written to the stream.
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function write($string)
{
if ($this->resource === null) {
throw new \RuntimeException('Unable to write to detached stream.');
throw new RuntimeException('Unable to write to detached stream.');
}
$result = false;
@ -243,7 +246,7 @@ class Stream implements StreamInterface
$result = fwrite($this->resource, $string);
}
if ($result === false) {
throw new \RuntimeException('Unable to write to stream.');
throw new RuntimeException('Unable to write to stream.');
}
return $result;
}
@ -271,12 +274,12 @@ class Stream implements StreamInterface
* call returns fewer bytes.
* @return string Returns the data read from the stream, or an empty string
* if no bytes are available.
* @throws \RuntimeException if an error occurs.
* @throws RuntimeException if an error occurs.
*/
public function read($length)
{
if ($this->resource === null) {
throw new \RuntimeException('Unable to read to detached stream.');
throw new RuntimeException('Unable to read to detached stream.');
}
$result = false;
@ -284,7 +287,7 @@ class Stream implements StreamInterface
$result = fread($this->resource, $length);
}
if ($result === false) {
throw new \RuntimeException('Unable to read from stream.');
throw new RuntimeException('Unable to read from stream.');
}
return $result;
}
@ -293,13 +296,13 @@ class Stream implements StreamInterface
* Returns the remaining contents in a string
*
* @return string
* @throws \RuntimeException if unable to read or an error occurs while
* @throws RuntimeException if unable to read or an error occurs while
* reading.
*/
public function getContents()
{
if ($this->resource === null) {
throw new \RuntimeException('Unable to read to detached stream.');
throw new RuntimeException('Unable to read to detached stream.');
}
$result = false;
@ -307,7 +310,7 @@ class Stream implements StreamInterface
$result = stream_get_contents($this->resource);
}
if ($result === false) {
throw new \RuntimeException('Unable to read from stream.');
throw new RuntimeException('Unable to read from stream.');
}
return $result;
}

View File

@ -4,6 +4,8 @@ namespace WellRESTed\Message;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use InvalidArgumentException;
use RuntimeException;
/**
* Value object representing a file uploaded through an HTTP request.
@ -85,19 +87,19 @@ class UploadedFile implements UploadedFileInterface
* raise an exception.
*
* @return StreamInterface Stream representation of the uploaded file.
* @throws \RuntimeException in cases when no stream is available or can
* @throws RuntimeException in cases when no stream is available or can
* be created.
*/
public function getStream()
{
if ($this->tmpName === null) {
throw new \RuntimeException('Unable to read uploaded file.');
throw new RuntimeException('Unable to read uploaded file.');
}
if ($this->moved) {
throw new \RuntimeException('File has already been moved.');
throw new RuntimeException('File has already been moved.');
}
if (php_sapi_name() !== 'cli' && !is_uploaded_file($this->tmpName)) {
throw new \RuntimeException('File is not an uploaded file.');
throw new RuntimeException('File is not an uploaded file.');
}
return $this->stream;
}
@ -117,14 +119,14 @@ class UploadedFile implements UploadedFileInterface
* @see http://php.net/move_uploaded_file
* @param string $path Path to which to move the uploaded file.
* @return void
* @throws \InvalidArgumentException if the $path specified is invalid.
* @throws \RuntimeException on any error during the move operation, or on
* @throws InvalidArgumentException if the $path specified is invalid.
* @throws RuntimeException on any error during the move operation, or on
* the second or subsequent call to the method.
*/
public function moveTo($path)
{
if ($this->tmpName === null || !file_exists($this->tmpName)) {
throw new \RuntimeException('File ' . $this->tmpName . ' does not exist.');
throw new RuntimeException('File ' . $this->tmpName . ' does not exist.');
}
if (php_sapi_name() === 'cli') {
rename($this->tmpName, $path);

View File

@ -3,6 +3,7 @@
namespace WellRESTed\Message;
use Psr\Http\Message\UriInterface;
use InvalidArgumentException;
/**
* Value object representing a URI.
@ -301,13 +302,13 @@ class Uri implements UriInterface
*
* @param string $scheme The scheme to use with the new instance.
* @return static A new instance with the specified scheme.
* @throws \InvalidArgumentException for invalid or unsupported schemes.
* @throws InvalidArgumentException for invalid or unsupported schemes.
*/
public function withScheme($scheme)
{
$scheme = $scheme ? strtolower($scheme) : '';
if (!in_array($scheme, ['', 'http', 'https'])) {
throw new \InvalidArgumentException('Scheme must be http, https, or empty.');
throw new InvalidArgumentException('Scheme must be http, https, or empty.');
}
$uri = clone $this;
$uri->scheme = $scheme;
@ -346,12 +347,12 @@ class Uri implements UriInterface
*
* @param string $host The hostname to use with the new instance.
* @return static A new instance with the specified host.
* @throws \InvalidArgumentException for invalid hostnames.
* @throws InvalidArgumentException for invalid hostnames.
*/
public function withHost($host)
{
if (!is_string($host)) {
throw new \InvalidArgumentException('Host must be a string.');
throw new InvalidArgumentException('Host must be a string.');
}
$uri = clone $this;
@ -374,18 +375,18 @@ class Uri implements UriInterface
* @param null|int $port The port to use with the new instance; a null value
* removes the port information.
* @return static A new instance with the specified port.
* @throws \InvalidArgumentException for invalid ports.
* @throws InvalidArgumentException for invalid ports.
*/
public function withPort($port)
{
if (is_numeric($port)) {
if ($port < self::MIN_PORT || $port > self::MAX_PORT) {
$message = sprintf('Port must be between %s and %s.', self::MIN_PORT, self::MAX_PORT);
throw new \InvalidArgumentException($message);
throw new InvalidArgumentException($message);
}
$port = (int) $port;
} elseif ($port !== null) {
throw new \InvalidArgumentException('Port must be an int or null.');
throw new InvalidArgumentException('Port must be an int or null.');
}
$uri = clone $this;
@ -408,12 +409,12 @@ class Uri implements UriInterface
*
* @param string $path The path to use with the new instance.
* @return static A new instance with the specified path.
* @throws \InvalidArgumentException for invalid paths.
* @throws InvalidArgumentException for invalid paths.
*/
public function withPath($path)
{
if (!is_string($path)) {
throw new \InvalidArgumentException('Path must be a string');
throw new InvalidArgumentException('Path must be a string');
}
$uri = clone $this;
$uri->path = $path;
@ -433,7 +434,7 @@ class Uri implements UriInterface
*
* @param string $query The query string to use with the new instance.
* @return static A new instance with the specified query string.
* @throws \InvalidArgumentException for invalid query strings.
* @throws InvalidArgumentException for invalid query strings.
*/
public function withQuery($query)
{

View File

@ -2,6 +2,8 @@
namespace WellRESTed\Routing\Route;
use RuntimeException;
class RegexRoute extends Route
{
/** @var array */
@ -26,7 +28,7 @@ class RegexRoute extends Route
$this->captures = $captures;
return true;
} elseif ($matched === false) {
throw new \RuntimeException('Invalid regular expression: ' . $this->getTarget());
throw new RuntimeException('Invalid regular expression: ' . $this->getTarget());
}
return false;
}

View File

@ -10,6 +10,7 @@ use WellRESTed\Message\ServerRequest;
use WellRESTed\Test\TestCase;
use WellRESTed\Transmission\HeaderStack;
use WellRESTed\Transmission\Transmitter;
use RuntimeException;
require_once __DIR__ . '/../../../src/HeaderStack.php';
@ -135,7 +136,7 @@ class TransmitterTest extends TestCase
$this->body->isSeekable()->willReturn(false);
$this->body->isReadable()->willReturn(true);
$this->body->rewind()->willThrow(new \RuntimeException());
$this->body->rewind()->willThrow(new RuntimeException());
$this->body->eof()->willReturn(false);
$this->body->read(Argument::any())->will(
function ($args) use ($content, &$position) {