Skip to content

Commit

Permalink
formatting and comment tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sndsgd committed Jul 14, 2016
1 parent e0d7974 commit aa6a448
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/bin export-ignore
/phpunit.xml export-ignore
/tests export-ignore
9 changes: 5 additions & 4 deletions src/http/HeaderCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class HeaderCollection
protected $keyMap = [];

/**
* Retrieve a key that can be used in a hashmap to prevent duplicate keys
*
* @param string $key
* @param bool $register Whether to register a new key
* @return string
Expand All @@ -26,8 +28,7 @@ protected function getKey(string $key, bool $register = false): string
$lowercaseKey = strtolower($key);
if (isset($this->keyMap[$lowercaseKey])) {
$key = $this->keyMap[$lowercaseKey];
}
elseif ($register) {
} elseif ($register) {
$this->keyMap[$lowercaseKey] = $key;
}
return $key;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function setMultiple(array $headers): HeaderCollection
* Add a request header
*
* @param string $key
* @param string|integer $value
* @param string $value
*/
public function add(string $key, string $value): HeaderCollection
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public function get(string $key): string
/**
* Get multiple header values
*
* @param string $key
* @param string ...$keys
* @return array<string>
*/
public function getMultiple(string ...$keys): array
Expand Down
13 changes: 5 additions & 8 deletions src/http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Request implements RequestParameterDecoderInterface
/**
* Headers are parsed and cached here
*
* @var array<string>
* @var array<string,string>
*/
protected $headers;

Expand Down Expand Up @@ -85,8 +85,7 @@ public function getPath(): string
if (isset($this->server["REQUEST_URI"])) {
$path = parse_url($this->server["REQUEST_URI"], PHP_URL_PATH);
$this->path = rawurldecode($path);
}
else {
} else {
$this->path = "/";
}
}
Expand Down Expand Up @@ -135,11 +134,9 @@ public function getIp(): string
*/
public function getHeader(string $name, string $default = ""): string
{
if (!$this->headers) {
$this->headers = $this->readHeaders();
}
$name = strtolower($name);
return $this->headers[$name] ?? $default;
$lowercaseName = strtolower($name);
$headers = $this->getHeaders();
return $headers[$lowercaseName] ?? $default;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/http/RequestParameterDecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
interface RequestParameterDecoderInterface
{
/**
* Retrieve request query parameters
*
* @return array<string,mixed>
*/
public function getQueryParameters(): array;

/**
* Get the request data using the content type
* Retrieve request body parameters
*
* @return array
*/
Expand Down
7 changes: 6 additions & 1 deletion src/http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Response
*/
protected $statusText = "OK";

/**
* @var \sndsgd\http\HeaderCollection
*/
protected $headers;

/**
* Once the body has been generated it is stored here
*
Expand Down Expand Up @@ -140,7 +145,7 @@ public function setBody(string $body): Response
*/
public function getBody()
{
return $this->body;
return $this->body;
}

public function send()
Expand Down
2 changes: 1 addition & 1 deletion src/http/UploadedFileError.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(int $code)
break;
default:
throw new \InvalidArgumentException(
"invalid value provided for 'code'; " .
"invalid value provided for 'code'; ".
"expecting one of the php `UPLOAD_ERR_*` constants"
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/http/data/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getValues(): array
/**
* Add a value to the collection
*
* @param string|number $name The index/key to add the value under
* @param string|number $key The index/key to add the value under
* @param string|\sndsgd\http\UploadedFile $value The value to add
*/
public function addValue(string $key, $value)
Expand All @@ -82,8 +82,7 @@ public function addValue(string $key, $value)
$openPos < $closePos
) {
$this->addNestedValue($key, $value, $openPos);
}
else {
} else {
\sndsgd\Arr::addValue($this->values, $key, $value);
}

Expand Down
8 changes: 4 additions & 4 deletions src/http/data/decoder/MultipartDataDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public function decode(): array
if ($this->fileCount < $this->maxFileCount) {
$value = $this->getFileFromField($name, $filename, $contentType);
$this->values->addValue($name, $value);
}
else {
} else {

}

Expand Down Expand Up @@ -224,7 +223,8 @@ protected function getFieldHeader()
);
}

list($match, $name, $filename) = array_pad($matches, 3, "");
# we have no need for the entire match, so we drop it here
list( , $name, $filename) = array_pad($matches, 3, "");

# if a filename was in the content disposition
# attempt to find its content type in the field header
Expand Down Expand Up @@ -264,7 +264,7 @@ private function getValueFromField()
/**
* Allow for stubbing the result of tempnam using reflection
*
* @return bool
* @return string
*/
protected function getTempFilePath()
{
Expand Down
18 changes: 9 additions & 9 deletions src/http/data/decoder/QueryStringDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QueryStringDecoder
* Provide a parameter collection to append to it; otherwise all decoded
* parameters will be added to a new collection
*
* @param string $contentType
* @param int $contentLength
* @param \sndsgd\http\data\Collection|null $values
*/
public function __construct(
Expand All @@ -46,21 +46,21 @@ public function __construct(
}

/**
* Decode a urlencoded parameter pair
* Decode a urlencoded parameter key value pair
*
* @param string $pair
* @param string $keyValuePair
* @return array<string> The decoded key and value
*/
public function decodePair(string $pair): array
public function decodePair(string $keyValuePair): array
{
# more than a handful of clients like to use '+' characters
# when encoding data that is (almost) compliant with rfc 3986
# this is a hack that allows for it
if (strpos($pair, "+") !== false) {
$pair = str_replace("+", " ", $pair);
if (strpos($keyValuePair, "+") !== false) {
$keyValuePair = str_replace("+", " ", $keyValuePair);
}

$parts = explode("=", $pair, 2);
$parts = explode("=", $keyValuePair, 2);
$key = rawurldecode($parts[0]);

# interpret a key with an empty value as `null`
Expand All @@ -76,8 +76,8 @@ public function decodePair(string $pair): array
*/
public function decode($query): array
{
foreach (explode("&", $query) as $pair) {
list($key, $value) = $this->decodePair($pair);
foreach (explode("&", $query) as $keyValuePair) {
list($key, $value) = $this->decodePair($keyValuePair);
$this->values->addValue($key, $value);
}
return $this->values->getValues();
Expand Down
3 changes: 1 addition & 2 deletions src/http/request/BodyDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ protected function parsePost()
]);
\sndsgd\Arr::addValue($ret, $name, $file);
}
}
else {
} else {
$file = $this->createUploadedFile($info);
\sndsgd\Arr::addValue($ret, $name, $file);
}
Expand Down

0 comments on commit aa6a448

Please sign in to comment.