-
Notifications
You must be signed in to change notification settings - Fork 85
Small optimalizations with count and strlen #129
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaceship operator: only case where I ever suggested its usage.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = []; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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])) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should check for key 2. Test needed
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,7 @@ public function getContent() | |
| { | ||
| if (empty($this->content)) { | ||
| $requestBody = file_get_contents('php://input'); | ||
| if (strlen($requestBody) > 0) { | ||
| if (! empty($requestBody)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for |
||
| $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; | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.