Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert values according to PSR standard #250

Merged
merged 6 commits into from Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/MessageTrait.php
Expand Up @@ -66,6 +66,7 @@ public function getHeaderLine($header)

public function withHeader($header, $value)
{
$this->assertHeader($header);
if (!is_array($value)) {
$value = [$value];
}
Expand All @@ -85,7 +86,12 @@ public function withHeader($header, $value)

public function withAddedHeader($header, $value)
{
$this->assertHeader($header);
if (!is_array($value)) {
if (!is_string($value)) {
throw new \InvalidArgumentException('Header value must be a string or an array of strings.');
}

$value = [$value];
}

Expand Down Expand Up @@ -145,6 +151,10 @@ private function setHeaders(array $headers)
$this->headerNames = $this->headers = [];
foreach ($headers as $header => $value) {
if (!is_array($value)) {
if (!is_string($value)) {
throw new \InvalidArgumentException('Header value must be a string or an array of strings.');
}

$value = [$value];
}

Expand Down Expand Up @@ -180,4 +190,14 @@ private function trimHeaderValues(array $values)
return trim($value, " \t");
}, $values);
}

/**
* @param string $header
sagikazarmark marked this conversation as resolved.
Show resolved Hide resolved
*/
private function assertHeader($header)
{
if (!is_string($header) || $header === '') {
throw new \InvalidArgumentException('Header must be a non empty string');
}
}
}
12 changes: 12 additions & 0 deletions src/Request.php
Expand Up @@ -36,6 +36,7 @@ public function __construct(
$body = null,
$version = '1.1'
) {
$this->assertMethod($method);
if (!($uri instanceof UriInterface)) {
$uri = new Uri($uri);
}
Expand Down Expand Up @@ -91,6 +92,7 @@ public function getMethod()

public function withMethod($method)
{
$this->assertMethod($method);
$new = clone $this;
$new->method = strtoupper($method);
Copy link
Member Author

@gmponos gmponos Dec 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the tests the strtoupper should not be here.. the current PR targets 1,x.. Can this be fixed on v1. and add the change on the current PR or it should be fixed on version 2.x?

return $new;
Expand Down Expand Up @@ -139,4 +141,14 @@ private function updateHostFromUri()
// See: http://tools.ietf.org/html/rfc7230#section-5.4
$this->headers = [$header => [$host]] + $this->headers;
}

/**
* @param string $method
*/
private function assertMethod($method)
{
if (!is_string($method) || $method === '') {
throw new \InvalidArgumentException('Method should be a non empty string.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 method is defined as token = 1*tchar

}
}
}
32 changes: 28 additions & 4 deletions src/Response.php
Expand Up @@ -93,9 +93,9 @@ public function __construct(
$version = '1.1',
$reason = null
) {
if (filter_var($status, FILTER_VALIDATE_INT) === false) {
throw new \InvalidArgumentException('Status code must be an integer value.');
}
$this->assertStatusCodeIsInteger($status);
$status = (int) $status;
$this->assertStatusCodeRange($status);

$this->statusCode = (int) $status;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove this cast since you casted already a few lines above


Expand Down Expand Up @@ -125,12 +125,36 @@ public function getReasonPhrase()

public function withStatus($code, $reasonPhrase = '')
{
$this->assertStatusCodeIsInteger($code);
$code = (int) $code;
$this->assertStatusCodeRange($code);

$new = clone $this;
$new->statusCode = (int) $code;
$new->statusCode = $code;
if ($reasonPhrase == '' && isset(self::$phrases[$new->statusCode])) {
$reasonPhrase = self::$phrases[$new->statusCode];
}
$new->reasonPhrase = $reasonPhrase;
return $new;
}

/**
* @param int $statusCode
*/
private function assertStatusCodeIsInteger($statusCode)
{
if (filter_var($statusCode, FILTER_VALIDATE_INT) === false) {
throw new \InvalidArgumentException('Status code must be an integer value.');
}
}

/**
* @param int $statusCode
*/
private function assertStatusCodeRange($statusCode)
{
if ($statusCode < 100 || $statusCode > 600) {
throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.');
}
}
}