Skip to content

Commit

Permalink
some logic modify
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 4, 2019
1 parent 351856b commit 9834fa5
Show file tree
Hide file tree
Showing 27 changed files with 60 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ composer.lock
*.log
*.pid
*.patch
*.cache
.DS_Store
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
],
"require": {
"php": ">7.0.0",
"php": ">7.1.0",
"psr/http-message": "^1.0"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions src/Component/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public function __construct(array $items = [])
/**
* @param array $values
*/
public function sets(array $values)
public function sets(array $values): void
{
$this->replace($values);
}

/**
* @param array $items
*/
public function replace(array $items)
public function replace(array $items): void
{
foreach ($items as $key => $value) {
$this->set($key, $value);
Expand Down Expand Up @@ -122,7 +122,7 @@ public function remove($key)
/**
* clear all data
*/
public function clear()
public function clear(): void
{
foreach ($this as $key) {
unset($this[$key]);
Expand Down
2 changes: 1 addition & 1 deletion src/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Cookies extends Collection
* Set default cookie properties
* @param array $settings
*/
public function setDefaults(array $settings)
public function setDefaults(array $settings): void
{
$this->defaults = \array_replace($this->defaults, $settings);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Headers extends Collection
/**
* the connection header line data end char
*/
const EOL = "\r\n";
public const EOL = "\r\n";

const HEADER_END = "\r\n\r\n";
public const HEADER_END = "\r\n\r\n";

/**
* Special HTTP headers that do not have the "HTTP_" prefix
Expand Down Expand Up @@ -93,7 +93,7 @@ public function get(string $key, $default = null)
* @param null $default
* @return null|string
*/
public function getLine($name, $default = null)
public function getLine($name, $default = null): ?string
{
if ($val = $this->get($name)) {
return \implode(',', $val);
Expand Down Expand Up @@ -161,7 +161,7 @@ public function getAcceptLanguages(): array

if ($value = $this->getLine('Accept-Language')) {
if (\strpos($value, ';')) {
list($value,) = \explode(';', $value, 2);
[$value,] = \explode(';', $value, 2);
}

$value = \str_replace(' ', '', $value);
Expand All @@ -182,7 +182,7 @@ public function getAcceptEncodes(): array

if ($value = $this->getLine('Accept-Encoding')) {
if (\strpos($value, ';')) {
list($value,) = \explode(';', $value, 2);
[$value,] = \explode(';', $value, 2);
}

$value = \str_replace(' ', '', $value);
Expand Down
2 changes: 1 addition & 1 deletion src/HttpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static function createStream(string $content = ''): StreamInterface
* @return StreamInterface
* @throws \InvalidArgumentException
*/
public static function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
public static function createStreamFromFile(string $filename, string $mode = 'rb'): StreamInterface
{
// $stream = fopen('php://temp', $mode);
$stream = \fopen($filename, $mode);
Expand Down
4 changes: 2 additions & 2 deletions src/HttpUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
class HttpUtil
{
const FAV_ICON = '/favicon.ico';
public const FAV_ICON = '/favicon.ico';

/**
* Send the response the client
* @param ResponseInterface $response
* @param array $options
* @throws \RuntimeException
*/
public static function respond(ResponseInterface $response, array $options = [])
public static function respond(ResponseInterface $response, array $options = []): void
{
$options = \array_merge([
'chunkSize' => 4096,
Expand Down
2 changes: 1 addition & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Message implements MessageInterface
/**
* the connection header line data end char
*/
const EOL = "\r\n";
public const EOL = "\r\n";

/**
* BaseMessage constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Request implements RequestInterface
{
use RequestTrait, RequestHeadersTrait;

const FAV_ICON = '/favicon.ico';
public const FAV_ICON = '/favicon.ico';

/**
* Request constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Response implements ResponseInterface
/**
* the connection header line data end char
*/
const EOL = "\r\n";
public const EOL = "\r\n";

/**
* eg: 404
Expand Down
4 changes: 2 additions & 2 deletions src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class JsonResponse extends Response
*
* @const int
*/
const DEFAULT_JSON_FLAGS = 79;
public const DEFAULT_JSON_FLAGS = 79;

/**
* @var mixed
Expand Down Expand Up @@ -177,7 +177,7 @@ private function jsonEncode($data, int $encodingOptions): string
/**
* @param $data
*/
private function setPayload($data)
private function setPayload($data): void
{
if (\is_object($data)) {
$data = clone $data;
Expand Down
22 changes: 11 additions & 11 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ServerRequest implements ServerRequestInterface
/**
* the connection header line data end char
*/
const EOL = "\r\n";
public const EOL = "\r\n";

/**
* @var array
Expand Down Expand Up @@ -75,14 +75,14 @@ public static function makeByParseRawData(string $rawData)

// e.g: `GET / HTTP/1.1`
$first = array_shift($list);
list($method, $uri, $protoStr) = array_map('trim', explode(' ', trim($first)));
list($protocol, $protocolVersion) = explode('/', $protoStr);
[$method, $uri, $protoStr] = array_map('trim', explode(' ', trim($first)));
[$protocol, $protocolVersion] = explode('/', $protoStr);

// other header info
$headers = [];
foreach ($list as $item) {
if ($item) {
list($name, $value) = explode(': ', trim($item));
[$name, $value] = explode(': ', trim($item));
$headers[$name] = trim($value);
}
}
Expand All @@ -96,7 +96,7 @@ public static function makeByParseRawData(string $rawData)
$port = 80;
$host = '';
if ($val = $headers['Host'] ?? '') {
list($host, $port) = strpos($val, ':') ? explode(':', $val) : [$val, 80];
[$host, $port] = strpos($val, ':') ? explode(':', $val) : [$val, 80];
}

$path = $uri;
Expand Down Expand Up @@ -180,7 +180,7 @@ public function __toString()
/**
* registerDataParsers
*/
protected function registerDataParsers()
protected function registerDataParsers(): void
{
$this->registerMediaTypeParser('application/json', function ($input) {
$result = \json_decode($input, true);
Expand Down Expand Up @@ -245,7 +245,7 @@ public function toString(): string
* @param $mediaType
* @param callable $callable
*/
public function registerMediaTypeParser($mediaType, callable $callable)
public function registerMediaTypeParser($mediaType, callable $callable): void
{
if ($callable instanceof \Closure) {
$callable = $callable->bindTo($this);
Expand Down Expand Up @@ -281,7 +281,7 @@ public function getQueryParams(): array
* @see getQueryParam()
* @see getQueryParams()
*/
public function setQueryParams($values)
public function setQueryParams($values): void
{
$this->_queryParams = $values;
}
Expand Down Expand Up @@ -363,7 +363,7 @@ public function getRawBody(): string
* Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests.
* @param string $rawBody the request body
*/
public function setRawBody($rawBody)
public function setRawBody($rawBody): void
{
$this->_rawBody = $rawBody;
}
Expand Down Expand Up @@ -454,7 +454,7 @@ public function withParsedBody($data)
/**
* @param array $data
*/
public function setParsedBody($data)
public function setParsedBody($data): void
{
$this->bodyParsed = $data;
}
Expand Down Expand Up @@ -743,7 +743,7 @@ public function getServerParam(string $key, $default = null)
/**
* @param array $serverParams
*/
public function setServerParams(array $serverParams)
public function setServerParams(array $serverParams): void
{
$this->serverParams = $serverParams;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Stream implements StreamInterface
*
* This is octal as per header stat.h
*/
const FSTAT_MODE_S_IFIFO = 0010000;
public const FSTAT_MODE_S_IFIFO = 0010000;

/**
* Resource modes
Expand Down Expand Up @@ -144,7 +144,7 @@ protected function isAttached(): bool
*
* @throws InvalidArgumentException If argument is not a valid PHP resource.
*/
protected function attach($newStream)
protected function attach($newStream): void
{
if (\is_resource($newStream) === false) {
throw new InvalidArgumentException(__METHOD__ . ' argument must be a valid PHP resource');
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 4 additions & 5 deletions src/Traits/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public function initialize(
string $protocolVersion = '1.1',
$headers = null,
$body = 'php://memory'
)
{
): void {
$this->protocol = $protocol ?: 'http';
$this->protocolVersion = $protocolVersion ?: '1.1';

Expand Down Expand Up @@ -99,7 +98,7 @@ public function getProtocol(): string
/**
* @param string $protocol
*/
public function setProtocol(string $protocol)
public function setProtocol(string $protocol): void
{
$this->protocol = $protocol;
}
Expand All @@ -119,7 +118,7 @@ public function getProtocolVersion(): string
/**
* @param string $protocolVersion
*/
public function setProtocolVersion(string $protocolVersion)
public function setProtocolVersion(string $protocolVersion): void
{
$this->protocolVersion = $protocolVersion;
}
Expand Down Expand Up @@ -265,7 +264,7 @@ public function setHeaders(array $headers): self
* @return StreamInterface
* @throws \InvalidArgumentException
*/
protected function createBodyStream($body, string $mode = 'r'): StreamInterface
protected function createBodyStream($body, string $mode = 'rb'): StreamInterface
{
if ($body instanceof StreamInterface) {
return $body;
Expand Down
6 changes: 1 addition & 5 deletions src/Traits/RequestHeadersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ public function getContentCharset(): string
{
$mediaTypeParams = $this->getMediaTypeParams();

if (isset($mediaTypeParams['charset'])) {
return $mediaTypeParams['charset'];
}

return '';
return $mediaTypeParams['charset'] ?? '';
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Traits/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ trait RequestTrait
* @param string|null $method
* @throws \InvalidArgumentException
*/
protected function initializeRequest($uri = null, $method = null)
protected function initializeRequest($uri = null, $method = null): void
{
try {
$this->originalMethod = $this->filterMethod($method);
Expand Down Expand Up @@ -148,7 +148,7 @@ public function getMethod(): string
/**
* @param string $method
*/
public function setMethod(string $method)
public function setMethod(string $method): void
{
$this->method = $method;
}
Expand Down Expand Up @@ -244,7 +244,7 @@ public function withMethod($method): self
* @return null|string
* @throws \InvalidArgumentException on invalid HTTP method.
*/
protected function filterMethod($method)
protected function filterMethod($method): ?string
{
if ($method === null) {
return $method;
Expand Down Expand Up @@ -329,7 +329,7 @@ public function getUri(): Uri
/**
* @param Uri $uri
*/
public function setUri(Uri $uri)
public function setUri(Uri $uri): void
{
$this->uri = $uri;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class UploadedFile implements UploadedFileInterface
* Create a normalized tree of UploadedFile instances from the Environment.
* @return array|null A normalized tree of UploadedFile instances or null if none are provided.
*/
public static function createFromFILES()
public static function createFromFILES(): ?array
{
if (null !== $_FILES) {
return static::parseUploadedFiles($_FILES);
Expand Down
2 changes: 1 addition & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ protected function hasStandardPort(): bool
* @return null|int
* @throws \InvalidArgumentException If the port is invalid.
*/
protected function filterPort($port)
protected function filterPort($port): ?int
{
if (null === $port || (\is_int($port) && ($port >= 1 && $port <= 65535))) {
return $port;
Expand Down
6 changes: 3 additions & 3 deletions test/BodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
* Time: 上午12:04
*/

namespace PhpComp\Http\Message\Test;
namespace PhpComp\Http\MessageTest;

use PhpComp\Http\Message\Body;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;

/**
* Class BodyTest
* @package PhpComp\Http\Message\Test
* @package PhpComp\Http\MessageTest
*/
class BodyTest extends TestCase
{
public function testBody()
public function testBody(): void
{
$body = new Body();

Expand Down
Loading

0 comments on commit 9834fa5

Please sign in to comment.