Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typehints and return types to private functions and constructors. #242

Merged
merged 3 commits into from Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BufferStream.php
Expand Up @@ -26,7 +26,7 @@ class BufferStream implements StreamInterface
* but will return false to inform writers to slow down
* until the buffer has been drained by reading from it.
*/
public function __construct($hwm = 16384)
public function __construct(int $hwm = 16384)
{
$this->hwm = $hwm;
}
Expand Down
2 changes: 1 addition & 1 deletion src/CachingStream.php
Expand Up @@ -131,7 +131,7 @@ public function close()
$this->remoteStream->close() && $this->stream->close();
}

private function cacheEntireStream()
private function cacheEntireStream(): int
{
$target = new FnStream(['write' => 'strlen']);
copy_to_stream($this, $target);
Expand Down
2 changes: 1 addition & 1 deletion src/DroppingStream.php
Expand Up @@ -20,7 +20,7 @@ class DroppingStream implements StreamInterface
* @param StreamInterface $stream Underlying stream to decorate.
* @param int $maxLength Maximum size before dropping data.
*/
public function __construct(StreamInterface $stream, $maxLength)
public function __construct(StreamInterface $stream, int $maxLength)
{
$this->stream = $stream;
$this->maxLength = $maxLength;
Expand Down
7 changes: 1 addition & 6 deletions src/InflateStream.php
Expand Up @@ -33,12 +33,7 @@ public function __construct(StreamInterface $stream)
$this->stream = $stream->isSeekable() ? new Stream($resource) : new NoSeekStream(new Stream($resource));
}

/**
* @param StreamInterface $stream
* @param $header
* @return int
*/
private function getLengthOfPossibleFilenameHeader(StreamInterface $stream, $header)
private function getLengthOfPossibleFilenameHeader(StreamInterface $stream, string $header): int
{
$filename_header_length = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/LazyOpenStream.php
Expand Up @@ -24,7 +24,7 @@ class LazyOpenStream implements StreamInterface
* @param string $filename File to lazily open
* @param string $mode fopen mode to use when opening the stream
*/
public function __construct($filename, $mode)
public function __construct(string $filename, string $mode)
{
$this->filename = $filename;
$this->mode = $mode;
Expand Down
6 changes: 3 additions & 3 deletions src/LimitStream.php
Expand Up @@ -29,8 +29,8 @@ class LimitStream implements StreamInterface
*/
public function __construct(
StreamInterface $stream,
$limit = -1,
$offset = 0
int $limit = -1,
int $offset = 0
) {
$this->stream = $stream;
$this->setLimit($limit);
Expand Down Expand Up @@ -75,7 +75,7 @@ public function seek($offset, $whence = SEEK_SET)
{
if ($whence !== SEEK_SET || $offset < 0) {
throw new \RuntimeException(sprintf(
'Cannot seek to offset % with whence %s',
'Cannot seek to offset %s with whence %s',
Tobion marked this conversation as resolved.
Show resolved Hide resolved
$offset,
$whence
));
Expand Down
6 changes: 3 additions & 3 deletions src/MessageTrait.php
Expand Up @@ -137,7 +137,7 @@ public function withBody(StreamInterface $body)
return $new;
}

private function setHeaders(array $headers)
private function setHeaders(array $headers): void
{
$this->headerNames = $this->headers = [];
foreach ($headers as $header => $value) {
Expand All @@ -159,7 +159,7 @@ private function setHeaders(array $headers)
}
}

private function normalizeHeaderValue($value)
private function normalizeHeaderValue($value): array
{
if (!is_array($value)) {
return $this->trimHeaderValues([$value]);
Expand All @@ -186,7 +186,7 @@ private function normalizeHeaderValue($value)
*
* @see https://tools.ietf.org/html/rfc7230#section-3.2.4
*/
private function trimHeaderValues(array $values)
private function trimHeaderValues(array $values): array
{
return array_map(function ($value) {
if (!is_string($value) && !is_numeric($value)) {
Expand Down
13 changes: 5 additions & 8 deletions src/MultipartStream.php
Expand Up @@ -28,7 +28,7 @@ class MultipartStream implements StreamInterface
*
* @throws \InvalidArgumentException
*/
public function __construct(array $elements = [], $boundary = null)
public function __construct(array $elements = [], string $boundary = null)
{
$this->boundary = $boundary ?: sha1(uniqid('', true));
$this->stream = $this->createStream($elements);
Expand All @@ -52,7 +52,7 @@ public function isWritable()
/**
* Get the headers needed before transferring the content of a POST file
*/
private function getHeaders(array $headers)
private function getHeaders(array $headers): string
{
$str = '';
foreach ($headers as $key => $value) {
Expand All @@ -79,7 +79,7 @@ protected function createStream(array $elements)
return $stream;
}

private function addElement(AppendStream $stream, array $element)
private function addElement(AppendStream $stream, array $element): void
{
foreach (['contents', 'name'] as $key) {
if (!array_key_exists($key, $element)) {
Expand Down Expand Up @@ -108,10 +108,7 @@ private function addElement(AppendStream $stream, array $element)
$stream->addStream(stream_for("\r\n"));
}

/**
* @return array
*/
private function createElement($name, StreamInterface $stream, $filename, array $headers)
private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers): array
gmponos marked this conversation as resolved.
Show resolved Hide resolved
{
// Set a default content-disposition header if one was no provided
$disposition = $this->getHeader($headers, 'content-disposition');
Expand Down Expand Up @@ -142,7 +139,7 @@ private function createElement($name, StreamInterface $stream, $filename, array
return [$stream, $headers];
}

private function getHeader(array $headers, $key)
private function getHeader(array $headers, string $key)
{
$lowercaseHeader = strtolower($key);
foreach ($headers as $k => $v) {
Expand Down
8 changes: 4 additions & 4 deletions src/PumpStream.php
Expand Up @@ -18,10 +18,10 @@
*/
class PumpStream implements StreamInterface
{
/** @var callable */
/** @var callable|null */
private $source;

/** @var int */
/** @var int|null */
private $size;

/** @var int */
Expand Down Expand Up @@ -68,7 +68,7 @@ public function close()

public function detach()
{
$this->tellPos = false;
$this->tellPos = 0;
gmponos marked this conversation as resolved.
Show resolved Hide resolved
$this->source = null;
}

Expand Down Expand Up @@ -152,7 +152,7 @@ public function getMetadata($key = null)
return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
}

private function pump($length)
private function pump(int $length): void
{
if ($this->source) {
do {
Expand Down
4 changes: 2 additions & 2 deletions src/Request.php
Expand Up @@ -33,11 +33,11 @@ class Request implements RequestInterface
* @param string $version Protocol version
*/
public function __construct(
$method,
string $method,
$uri,
array $headers = [],
$body = null,
$version = '1.1'
string $version = '1.1'
) {
$this->assertMethod($method);
if (!($uri instanceof UriInterface)) {
Expand Down
10 changes: 4 additions & 6 deletions src/Response.php
Expand Up @@ -90,14 +90,12 @@ class Response implements ResponseInterface
* @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
*/
public function __construct(
$status = 200,
int $status = 200,
array $headers = [],
$body = null,
$version = '1.1',
$reason = null
string $version = '1.1',
string $reason = null
) {
$this->assertStatusCodeIsInteger($status);
$status = (int) $status;
$this->assertStatusCodeRange($status);

$this->statusCode = $status;
Expand Down Expand Up @@ -148,7 +146,7 @@ private function assertStatusCodeIsInteger($statusCode)
}
}

private function assertStatusCodeRange($statusCode)
private function assertStatusCodeRange(int $statusCode)
{
if ($statusCode < 100 || $statusCode >= 600) {
throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.');
Expand Down
4 changes: 2 additions & 2 deletions src/ServerRequest.php
Expand Up @@ -65,11 +65,11 @@ class ServerRequest extends Request implements ServerRequestInterface
* @param array $serverParams Typically the $_SERVER superglobal
*/
public function __construct(
$method,
string $method,
$uri,
array $headers = [],
$body = null,
$version = '1.1',
string $version = '1.1',
array $serverParams = []
) {
$this->serverParams = $serverParams;
Expand Down
4 changes: 1 addition & 3 deletions src/Stream.php
Expand Up @@ -8,8 +8,6 @@

/**
* PHP stream implementation.
*
* @var $stream
*/
class Stream implements StreamInterface
{
Expand Down Expand Up @@ -46,7 +44,7 @@ class Stream implements StreamInterface
*
* @throws \InvalidArgumentException if the stream is not a stream resource
*/
public function __construct($stream, $options = [])
public function __construct($stream, array $options = [])
{
if (!is_resource($stream)) {
throw new \InvalidArgumentException('Stream must be a resource');
Expand Down