Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Client/Adapter/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function read()
}
} else {
$line = fread($this->socket, $readTo - $currentPos);
if ($line === false || strlen($line) === 0) {
if (empty($line)) {
$this->_checkSocketReadTimeout();
break;
}
Expand Down Expand Up @@ -548,7 +548,7 @@ public function read()
}
} else {
$chunk = fread($this->socket, $readTo - $currentPos);
if ($chunk === false || strlen($chunk) === 0) {
if (empty($chunk)) {
$this->_checkSocketReadTimeout();
break;
}
Expand All @@ -572,7 +572,7 @@ public function read()
}
} else {
$buff = fread($this->socket, 8192);
if ($buff === false || strlen($buff) === 0) {
if (empty($buff)) {
$this->_checkSocketReadTimeout();
break;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public static function fromResponse(Response $response, $refUri)
*/
public function isEmpty()
{
return count($this) == 0;
return empty($this->headers);
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Header/AbstractAccept.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ protected function getParametersFromFieldValuePart($fieldValuePart)
foreach ($paramsStrings as $param) {
$explode = explode('=', $param, 2);

if (count($explode) === 2) {
if (isset($explode[1])) {
Copy link
Member

Choose a reason for hiding this comment

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

Should check that key 0 exists, and key 2 doesn't.

Copy link
Member

Choose a reason for hiding this comment

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

The explode operation was given a limit of 2, so there's no chance a key 2 will ever exist.

$value = trim($explode[1]);
} else {
$value = null;
Expand Down Expand Up @@ -349,7 +349,7 @@ protected function matchAcceptParams($match1, $match2)
$pieces
);

if (count($pieces) == 3 &&
if (count($pieces) === 3 &&
(version_compare($pieces[1], $match1->params[$key], '<=') xor
version_compare($pieces[2], $match1->params[$key], '>=')
)
Expand Down Expand Up @@ -438,10 +438,12 @@ protected function sortFieldValueParts()
//@todo count number of dots in case of type==application in subtype

// So far they're still the same. Longest string length may be more specific
if (strlen($a->raw) == strlen($b->raw)) {
$aLength = strlen($a->raw);
$bLength = strlen($b->raw);
if ($aLength === $bLength) {
Copy link
Member

Choose a reason for hiding this comment

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

Spaceship operator: only case where I ever suggested its usage.

Copy link
Member

Choose a reason for hiding this comment

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

Can't use it here; this component still supports PHP 5.6.

return 0;
}
return (strlen($a->raw) > strlen($b->raw)) ? -1 : 1;
return ($aLength > $bLength) ? -1 : 1;
};

usort($this->fieldValueParts, $sort);
Expand Down
4 changes: 2 additions & 2 deletions src/Header/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function fromString($headerLine)
$mediaType = array_shift($parts);
$header = new static($value, trim($mediaType));

if (count($parts) > 0) {
if (! empty($parts)) {
$parameters = [];
Copy link
Member

Choose a reason for hiding this comment

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

empty() not needed: the ! is sufficient

foreach ($parts as $parameter) {
$parameter = trim($parameter);
Expand Down Expand Up @@ -290,7 +290,7 @@ protected function getMediaTypeObjectFromString($string)
}

$parts = explode('/', $string, 2);
if (1 == count($parts)) {
if (1 === count($parts)) {
throw new Exception\DomainException(sprintf(
'Invalid mediatype "%s" provided',
$string
Expand Down
2 changes: 1 addition & 1 deletion src/Header/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function fromString($headerLine)
$arrayInfo = [];
foreach ($nvPairs as $nvPair) {
$parts = explode('=', $nvPair, 2);
if (count($parts) != 2) {
if (! isset($parts[1])) {
Copy link
Member

Choose a reason for hiding this comment

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

Should check for key 2. Test needed

Copy link
Member

Choose a reason for hiding this comment

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

Never mind, explode is limited to 2 elements

throw new Exception\RuntimeException('Malformed Cookie header found');
}
list($name, $value) = $parts;
Expand Down
2 changes: 1 addition & 1 deletion src/Header/GenericHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function fromString($headerLine)
public static function splitHeaderLine($headerLine)
{
$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
if (! isset($parts[1])) {
throw new Exception\InvalidArgumentException('Header must match with the format "name:value"');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Header/SetCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static function fromString($headerLine, $bypassHeaderFieldName = false)

$multipleHeaders = preg_split('#(?<!Sun|Mon|Tue|Wed|Thu|Fri|Sat),\s*#', $value);

if (count($multipleHeaders) <= 1) {
if (! isset($multipleHeaders[1])) {
return $setCookieProcessor(array_pop($multipleHeaders));
} else {
$headers = [];
Expand Down
20 changes: 15 additions & 5 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,22 @@ public function addHeaders($headers)
if (is_int($name)) {
if (is_string($value)) {
$this->addHeaderLine($value);
} elseif (is_array($value) && count($value) == 1) {
$this->addHeaderLine(key($value), current($value));
} elseif (is_array($value) && count($value) == 2) {
$this->addHeaderLine($value[0], $value[1]);
} elseif ($value instanceof Header\HeaderInterface) {
continue;
}
if (is_array($value)) {
Copy link
Member

Choose a reason for hiding this comment

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

Move contents of this block to a private method

$valueCount = count($value);
if ($valueCount === 1) {
$this->addHeaderLine(key($value), current($value));
continue;
} elseif ($valueCount === 2) {
$this->addHeaderLine($value[0], $value[1]);
continue;
}
}

if ($value instanceof Header\HeaderInterface) {
$this->addHeader($value);
continue;
}
} elseif (is_string($name)) {
$this->addHeaderLine($name, $value);
Expand Down
10 changes: 6 additions & 4 deletions src/PhpEnvironment/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getContent()
{
if (empty($this->content)) {
$requestBody = file_get_contents('php://input');
if (strlen($requestBody) > 0) {
if (! empty($requestBody)) {
Copy link
Member

Choose a reason for hiding this comment

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

empty() not needed: ! is sufficient

Copy link
Contributor

Choose a reason for hiding this comment

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

for bool comparison usage, with if ($requestBody) { should be enough

$this->content = $requestBody;
}
}
Expand Down Expand Up @@ -220,7 +220,7 @@ public function setServer(ParametersInterface $server)
$headers = [];

foreach ($server as $key => $value) {
if ($value || (! is_array($value) && strlen($value))) {
if ($value || ! is_array($value)) {
if (strpos($key, 'HTTP_') === 0) {
if (strpos($key, 'HTTP_COOKIE') === 0) {
// Cookies are handled using the $_COOKIE superglobal
Expand Down Expand Up @@ -550,10 +550,12 @@ protected function detectBaseUrl()
// If using mod_rewrite or ISAPI_Rewrite strip the script filename
// out of the base path. $pos !== 0 makes sure it is not matching a
// value from PATH_INFO or QUERY_STRING.
if (strlen($requestUri) >= strlen($baseUrl)
$baseUrlLength = strlen($baseUrl);

if (isset($requestUri[$baseUrlLength - 1])
&& (false !== ($pos = strpos($requestUri, $baseUrl)) && $pos !== 0)
) {
$baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
$baseUrl = substr($requestUri, 0, $pos + $baseUrlLength);
}

return $baseUrl;
Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Response extends AbstractMessage implements ResponseInterface
public static function fromString($string)
{
$lines = explode("\r\n", $string);
if (! is_array($lines) || count($lines) === 1) {
if (! isset($lines[1])) {
$lines = explode("\n", $string);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Response/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static function fromStream($responseString, $stream)
if ($header instanceof \Zend\Http\Header\ContentLength) {
$response->setContentLength((int) $header->getFieldValue());
$contentLength = $response->getContentLength();
if (strlen($response->content) > $contentLength) {
if (isset($response->content[$contentLength])) {
throw new Exception\OutOfRangeException(sprintf(
'Too much content was extracted from the stream (%d instead of %d bytes)',
strlen($response->content),
Expand Down