Skip to content

Commit

Permalink
Cleanupt (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
devanych committed Aug 26, 2020
2 parents 504ba4c + b0551e5 commit 13b495a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,16 @@ private function validateHeaderValue($value): void
private function validateProtocolVersion($protocol): void
{
if (!is_string($protocol) || empty($protocol)) {
throw new InvalidArgumentException('HTTP protocol version must be an string and must not be empty.');
throw new InvalidArgumentException('HTTP protocol version must be a string and must not be empty.');
}

$supportedProtocolVersions = ['1.0', '1.1', '2.0', '2'];

if (!in_array($protocol, $supportedProtocolVersions, true)) {
throw new InvalidArgumentException(sprintf(
'Unsupported HTTP protocol version `%s` provided. Supported (%s) in string types.',
'Unsupported HTTP protocol version "%s" provided. The following strings are supported: "%s".',
$protocol,
implode(', ', $supportedProtocolVersions)
implode('", "', $supportedProtocolVersions)
));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function withRequestTarget($requestTarget): RequestInterface

if (!is_string($requestTarget) || preg_match('/\s/', $requestTarget)) {
throw new InvalidArgumentException(sprintf(
'`%s` is not valid request target. Request target must be a string and cannot contain whitespace',
'"%s" is not valid request target. Request target must be a string and cannot contain whitespace.',
(is_object($requestTarget) ? get_class($requestTarget) : gettype($requestTarget))
));
}
Expand Down Expand Up @@ -140,7 +140,7 @@ public function withMethod($method): RequestInterface

if (!is_string($method)) {
throw new InvalidArgumentException(sprintf(
'Invalid HTTP method. Must be a string type, received `%s`.',
'Invalid HTTP method. It must be a string, %s received.',
(is_object($method) ? get_class($method) : gettype($method))
));
}
Expand Down Expand Up @@ -256,7 +256,7 @@ private function setUri($uri): void
}

throw new InvalidArgumentException(sprintf(
'`%s` is not valid URI. Must be `null` or a `string` or a `Psr\Http\Message\UriInterface` instance.',
'"%s" is not valid URI. It must be a null, a string, or a "\Psr\Http\Message\UriInterface" instance.',
(is_object($uri) ? get_class($uri) : gettype($uri))
));
}
Expand Down
6 changes: 3 additions & 3 deletions src/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function withStatus($code, $reasonPhrase = ''): ResponseInterface
if (!is_int($code)) {
if (!is_numeric($code) || is_float($code)) {
throw new InvalidArgumentException(sprintf(
'Response status code is not valid. Must be a integer, received `%s`.',
'Response status code is not valid. It must be an integer, %s received.',
(is_object($code) ? get_class($code) : gettype($code))
));
}
Expand All @@ -162,7 +162,7 @@ public function withStatus($code, $reasonPhrase = ''): ResponseInterface

if (!is_string($reasonPhrase)) {
throw new InvalidArgumentException(sprintf(
'Response reason phrase is not valid. Must be a string, received `%s`.',
'Response reason phrase is not valid. It must be a string, %s received.',
(is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase))
));
}
Expand Down Expand Up @@ -222,7 +222,7 @@ private function setStatus(int $statusCode, string $reasonPhrase = ''): void
{
if ($statusCode < 100 || $statusCode > 599) {
throw new InvalidArgumentException(sprintf(
'Response status code `%d` is not valid. Must be in the range from 100 to 599 enabled.',
'Response status code "%d" is not valid. It must be in 100..599 range.',
$statusCode
));
}
Expand Down
4 changes: 2 additions & 2 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function withParsedBody($data): self
{
if (!is_array($data) && !is_object($data) && $data !== null) {
throw new InvalidArgumentException(sprintf(
'`%s` is not valid Parsed Body. Must be `null` or a `array` or a `object`.',
'"%s" is not valid Parsed Body. It must be a null, an array, or an object.',
gettype($data)
));
}
Expand Down Expand Up @@ -238,7 +238,7 @@ private function validateUploadedFiles(array $uploadedFiles): void
if (!$file instanceof UploadedFileInterface) {
throw new InvalidArgumentException(sprintf(
'Invalid item in uploaded files structure.'
. '`%s` is not an instance of `Psr\Http\Message\UploadedFileInterface`.',
. '"%s" is not an instance of "\Psr\Http\Message\UploadedFileInterface".',
(is_object($file) ? get_class($file) : gettype($file))
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function createStreamFromFile(string $filename, string $mode = 'r'): Stre
public function createStreamFromResource($resource): StreamInterface
{
if (is_string($resource)) {
throw new InvalidArgumentException('Invalid stream provided. Must be a stream resource.');
throw new InvalidArgumentException('Invalid stream provided. It must be a stream resource.');
}

return new Stream($resource);
Expand Down
30 changes: 15 additions & 15 deletions src/StreamTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ public function isSeekable(): bool
public function seek($offset, $whence = SEEK_SET): void
{
if (!$this->resource) {
throw new RuntimeException('No resource available. Cannot seek position');
throw new RuntimeException('No resource available. Cannot seek position.');
}

if (!$this->isSeekable()) {
throw new RuntimeException('Stream is not seekable');
throw new RuntimeException('Stream is not seekable.');
}

if (($result = fseek($this->resource, $offset, $whence)) !== 0) {
throw new RuntimeException('Error seeking within stream');
if (fseek($this->resource, $offset, $whence) !== 0) {
throw new RuntimeException('Error seeking within stream.');
}
}

Expand Down Expand Up @@ -224,15 +224,15 @@ public function isWritable(): bool
public function write($string): int
{
if (!$this->resource) {
throw new RuntimeException('No resource available. Cannot write');
throw new RuntimeException('No resource available. Cannot write.');
}

if (!$this->isWritable()) {
throw new RuntimeException('Stream is not writable');
throw new RuntimeException('Stream is not writable.');
}

if (!is_int($result = fwrite($this->resource, $string))) {
throw new RuntimeException('Error writing to stream');
throw new RuntimeException('Error writing to stream.');
}

return $result;
Expand Down Expand Up @@ -266,15 +266,15 @@ public function isReadable(): bool
public function read($length): string
{
if (!$this->resource) {
throw new RuntimeException('No resource available. Cannot read');
throw new RuntimeException('No resource available. Cannot read.');
}

if (!$this->isReadable()) {
throw new RuntimeException('Stream is not readable');
throw new RuntimeException('Stream is not readable.');
}

if (!is_string($result = fread($this->resource, $length))) {
throw new RuntimeException('Error reading stream');
throw new RuntimeException('Error reading stream.');
}

return $result;
Expand All @@ -290,11 +290,11 @@ public function read($length): string
public function getContents(): string
{
if (!$this->isReadable()) {
throw new RuntimeException('Stream is not readable');
throw new RuntimeException('Stream is not readable.');
}

if (!is_string($result = stream_get_contents($this->resource))) {
throw new RuntimeException('Error reading stream');
throw new RuntimeException('Error reading stream.');
}

return $result;
Expand All @@ -307,7 +307,7 @@ public function getContents(): string
* stream_get_meta_data() function.
*
* @link http://php.net/manual/en/function.stream-get-meta-data.php
* @param string $key Specific metadata to retrieve.
* @param string|null $key Specific metadata to retrieve.
* @return array|mixed|null Returns an associative array if no key is
* provided. Returns a specific key value if a key is provided and the
* value is found, or null if the key is not found.
Expand Down Expand Up @@ -345,7 +345,7 @@ private function init($stream, string $mode): void
if (is_string($stream)) {
set_error_handler(static function (int $error): bool {
if ($error === E_WARNING) {
throw new InvalidArgumentException('Invalid stream reference provided');
throw new InvalidArgumentException('Invalid stream reference provided.');
}
return true;
});
Expand All @@ -359,7 +359,7 @@ private function init($stream, string $mode): void

if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
throw new InvalidArgumentException(
'Invalid stream provided. Must be a string stream identifier or stream resource'
'Invalid stream provided. It must be a string stream identifier or stream resource.'
);
}

Expand Down
16 changes: 8 additions & 8 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public function __construct(
) {
if (!array_key_exists($error, self::ERRORS)) {
throw new InvalidArgumentException(sprintf(
'`%s` is not valid error status for `UploadedFile`. Must be number from `UPLOAD_ERR_*` constant (%s)',
'"%s" is not valid error status for "UploadedFile". It must be one of "UPLOAD_ERR_*" constants: "%s".',
$error,
implode(', ', array_keys(self::ERRORS))
implode('", "', array_keys(self::ERRORS))
));
}

Expand Down Expand Up @@ -139,7 +139,7 @@ public function __construct(
}

throw new InvalidArgumentException(sprintf(
'`%s` is not valid stream or file provided for `UploadedFile`.',
'"%s" is not valid stream or file provided for "UploadedFile".',
(is_object($streamOrFile) ? get_class($streamOrFile) : gettype($streamOrFile))
));
}
Expand Down Expand Up @@ -183,20 +183,20 @@ public function moveTo($targetPath): void

if (!is_string($targetPath)) {
throw new InvalidArgumentException(sprintf(
'`%s` is not valid target path for move. Must be a string type.',
'"%s" is not valid target path for move. It must be a string type.',
(is_object($targetPath) ? get_class($targetPath) : gettype($targetPath))
));
}

if (empty($targetPath)) {
throw new InvalidArgumentException('Target path is not valid for move. Must be a non-empty string.');
throw new InvalidArgumentException('Target path is not valid for move. It must be a non-empty string.');
}

$targetDirectory = dirname($targetPath);

if (!is_dir($targetDirectory) || !is_writable($targetDirectory)) {
throw new RuntimeException(sprintf(
'The target directory `%s` does not exists or is not writable.',
'The target directory "%s" does not exist or is not writable.',
$targetDirectory
));
}
Expand Down Expand Up @@ -250,14 +250,14 @@ private function moveOrWriteFile(string $targetPath): void
$isCliEnv = (!PHP_SAPI || strpos(PHP_SAPI, 'cli') === 0 || strpos(PHP_SAPI, 'phpdbg') === 0);

if (!($isCliEnv ? rename($this->file, $targetPath) : move_uploaded_file($this->file, $targetPath))) {
throw new RuntimeException(sprintf('Uploaded file could not be moved to `%s`', $targetPath));
throw new RuntimeException(sprintf('Uploaded file could not be moved to "%s".', $targetPath));
}

return;
}

if (!$file = fopen($targetPath, 'wb+')) {
throw new RuntimeException(sprintf('Unable to write to designated path (%s).', $targetPath));
throw new RuntimeException(sprintf('Unable to write to "%s".', $targetPath));
}

$this->stream->rewind();
Expand Down
12 changes: 6 additions & 6 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ private function normalizeScheme(string $scheme): string
return '';
}

if (!in_array($scheme, self::SCHEMES)) {
if (!in_array($scheme, self::SCHEMES, true)) {
throw new InvalidArgumentException(sprintf(
'Unsupported scheme `%s`; must be an empty string or any of the set (%s).',
'Unsupported scheme "%s". It must be an empty string or any of "%s".',
$scheme,
implode(', ', self::SCHEMES)
implode('", "', self::SCHEMES)
));
}

Expand Down Expand Up @@ -408,7 +408,7 @@ private function normalizePort($port): ?int

if (!is_numeric($port) || is_float($port)) {
throw new InvalidArgumentException(sprintf(
'Invalid port `%s` specified; must be an integer, an integer string, or null.',
'Invalid port "%s" specified. It must be an integer, an integer string, or null.',
(is_object($port) ? get_class($port) : gettype($port))
));
}
Expand All @@ -417,7 +417,7 @@ private function normalizePort($port): ?int

if ($port < 1 || $port > 65535) {
throw new InvalidArgumentException(sprintf(
'Invalid port `%d` specified. Must be a valid TCP/UDP port (more 1 and less 65535).',
'Invalid port "%d" specified. It must be a valid TCP/UDP port in range 2..65534.',
$port
));
}
Expand Down Expand Up @@ -515,7 +515,7 @@ private function checkStringType($value, string $phrase, string $method): void
{
if (!is_string($value)) {
throw new InvalidArgumentException(sprintf(
'`%s` method expects a string type %s; received `%s`.',
'"%s" method expects a string type %s. "%s" received.',
$method,
$phrase,
(is_object($value) ? get_class($value) : gettype($value))
Expand Down

0 comments on commit 13b495a

Please sign in to comment.