diff --git a/src/Client/Adapter/Socket.php b/src/Client/Adapter/Socket.php index cbcee98d06..43e8e882d0 100644 --- a/src/Client/Adapter/Socket.php +++ b/src/Client/Adapter/Socket.php @@ -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; } @@ -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; } @@ -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 { diff --git a/src/Cookies.php b/src/Cookies.php index d146e89254..74b90eff2e 100644 --- a/src/Cookies.php +++ b/src/Cookies.php @@ -357,7 +357,7 @@ public static function fromResponse(Response $response, $refUri) */ public function isEmpty() { - return count($this) == 0; + return empty($this->headers); } /** diff --git a/src/Header/AbstractAccept.php b/src/Header/AbstractAccept.php index 5aa697bf8a..ed4924778e 100644 --- a/src/Header/AbstractAccept.php +++ b/src/Header/AbstractAccept.php @@ -170,7 +170,7 @@ protected function getParametersFromFieldValuePart($fieldValuePart) foreach ($paramsStrings as $param) { $explode = explode('=', $param, 2); - if (count($explode) === 2) { + if (isset($explode[1])) { $value = trim($explode[1]); } else { $value = null; @@ -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], '>=') ) @@ -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) { return 0; } - return (strlen($a->raw) > strlen($b->raw)) ? -1 : 1; + return ($aLength > $bLength) ? -1 : 1; }; usort($this->fieldValueParts, $sort); diff --git a/src/Header/ContentType.php b/src/Header/ContentType.php index eb899539f7..ca81b425f1 100644 --- a/src/Header/ContentType.php +++ b/src/Header/ContentType.php @@ -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 = []; foreach ($parts as $parameter) { $parameter = trim($parameter); @@ -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 diff --git a/src/Header/Cookie.php b/src/Header/Cookie.php index 1464cdb319..e391da28b5 100644 --- a/src/Header/Cookie.php +++ b/src/Header/Cookie.php @@ -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])) { throw new Exception\RuntimeException('Malformed Cookie header found'); } list($name, $value) = $parts; diff --git a/src/Header/GenericHeader.php b/src/Header/GenericHeader.php index 4222646af8..46840df74d 100644 --- a/src/Header/GenericHeader.php +++ b/src/Header/GenericHeader.php @@ -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"'); } diff --git a/src/Header/SetCookie.php b/src/Header/SetCookie.php index 707c1f3507..4dd824203b 100644 --- a/src/Header/SetCookie.php +++ b/src/Header/SetCookie.php @@ -165,7 +165,7 @@ public static function fromString($headerLine, $bypassHeaderFieldName = false) $multipleHeaders = preg_split('#(?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)) { + $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); diff --git a/src/PhpEnvironment/Request.php b/src/PhpEnvironment/Request.php index 2cbef7e93d..944faeadb0 100644 --- a/src/PhpEnvironment/Request.php +++ b/src/PhpEnvironment/Request.php @@ -95,7 +95,7 @@ public function getContent() { if (empty($this->content)) { $requestBody = file_get_contents('php://input'); - if (strlen($requestBody) > 0) { + if (! empty($requestBody)) { $this->content = $requestBody; } } @@ -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 @@ -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; diff --git a/src/Response.php b/src/Response.php index 3aee4cc616..0d89ceaea2 100644 --- a/src/Response.php +++ b/src/Response.php @@ -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); } diff --git a/src/Response/Stream.php b/src/Response/Stream.php index 261d687148..4fef2866b6 100644 --- a/src/Response/Stream.php +++ b/src/Response/Stream.php @@ -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),