Skip to content

Commit

Permalink
Various fixes (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored Sep 10, 2023
1 parent 2ce93c4 commit 38ef514
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 131 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class EofCallbackStream implements StreamInterface

// Invoke the callback when EOF is hit.
if ($this->eof()) {
call_user_func($this->callback);
($this->callback)();
}

return $result;
Expand Down
71 changes: 0 additions & 71 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -90,71 +90,11 @@ parameters:
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:__toString\\(\\) should return string but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:detach\\(\\) should return resource\\|null but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:eof\\(\\) should return bool but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:getContents\\(\\) should return string but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:getSize\\(\\) should return int\\|null but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:isReadable\\(\\) should return bool but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:isSeekable\\(\\) should return bool but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:isWritable\\(\\) should return bool but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:read\\(\\) should return string but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:tell\\(\\) should return int but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\FnStream\\:\\:write\\(\\) should return int but returns mixed\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
path: src/FnStream.php

-
message: "#^Argument of an invalid type array\\<int, string\\>\\|false supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/Header.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
Expand Down Expand Up @@ -190,11 +130,6 @@ parameters:
count: 1
path: src/Message.php

-
message: "#^Method GuzzleHttp\\\\Psr7\\\\MultipartStream\\:\\:getHeader\\(\\) has no return type specified\\.$#"
count: 1
path: src/MultipartStream.php

-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
count: 1
Expand Down Expand Up @@ -295,11 +230,6 @@ parameters:
count: 1
path: src/Utils.php

-
message: "#^Parameter \\#1 \\$keys of static method GuzzleHttp\\\\Psr7\\\\Utils\\:\\:caselessRemove\\(\\) expects array\\<string\\>, array\\<int, int\\|string\\> given\\.$#"
count: 1
path: src/Utils.php

-
message: "#^Parameter \\#1 \\$source of class GuzzleHttp\\\\Psr7\\\\PumpStream constructor expects callable\\(int\\)\\: \\(string\\|false\\|null\\), Closure\\(\\)\\: mixed given\\.$#"
count: 1
Expand All @@ -319,4 +249,3 @@ parameters:
message: "#^Variable \\$handle might not be defined\\.$#"
count: 1
path: src/Utils.php

30 changes: 15 additions & 15 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<file src="src/FnStream.php">
<InvalidFunctionCall>
<code><![CDATA[call_user_func($this->_fn___toString)]]></code>
<code><![CDATA[call_user_func($this->_fn_close)]]></code>
<code><![CDATA[call_user_func($this->_fn_detach)]]></code>
<code><![CDATA[call_user_func($this->_fn_eof)]]></code>
<code><![CDATA[call_user_func($this->_fn_getContents)]]></code>
<code><![CDATA[call_user_func($this->_fn_getMetadata, $key)]]></code>
<code><![CDATA[call_user_func($this->_fn_getSize)]]></code>
<code><![CDATA[call_user_func($this->_fn_isReadable)]]></code>
<code><![CDATA[call_user_func($this->_fn_isSeekable)]]></code>
<code><![CDATA[call_user_func($this->_fn_isWritable)]]></code>
<code><![CDATA[call_user_func($this->_fn_read, $length)]]></code>
<code><![CDATA[call_user_func($this->_fn_rewind)]]></code>
<code><![CDATA[call_user_func($this->_fn_seek, $offset, $whence)]]></code>
<code><![CDATA[call_user_func($this->_fn_tell)]]></code>
<code><![CDATA[call_user_func($this->_fn_write, $string)]]></code>
<code><![CDATA[($this->_fn___toString)()]]></code>
<code><![CDATA[($this->_fn_close)()]]></code>
<code><![CDATA[($this->_fn_detach)()]]></code>
<code><![CDATA[($this->_fn_eof)()]]></code>
<code><![CDATA[($this->_fn_getContents)()]]></code>
<code><![CDATA[($this->_fn_getMetadata)($key)]]></code>
<code><![CDATA[($this->_fn_getSize)()]]></code>
<code><![CDATA[($this->_fn_isReadable)()]]></code>
<code><![CDATA[($this->_fn_isSeekable)()]]></code>
<code><![CDATA[($this->_fn_isWritable)()]]></code>
<code><![CDATA[($this->_fn_read)($length)]]></code>
<code><![CDATA[($this->_fn_rewind)()]]></code>
<code><![CDATA[($this->_fn_seek)($offset, $whence)]]></code>
<code><![CDATA[($this->_fn_tell)()]]></code>
<code><![CDATA[($this->_fn_write)($string)]]></code>
</InvalidFunctionCall>
</file>
<file src="src/Header.php">
Expand Down
33 changes: 17 additions & 16 deletions src/FnStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __get(string $name): void
public function __destruct()
{
if (isset($this->_fn_close)) {
call_user_func($this->_fn_close);
($this->_fn_close)();
}
}

Expand Down Expand Up @@ -93,7 +93,8 @@ public static function decorate(StreamInterface $stream, array $methods)
public function __toString(): string
{
try {
return call_user_func($this->_fn___toString);
/** @var string */
return ($this->_fn___toString)();
} catch (\Throwable $e) {
if (\PHP_VERSION_ID >= 70400) {
throw $e;
Expand All @@ -106,74 +107,74 @@ public function __toString(): string

public function close(): void
{
call_user_func($this->_fn_close);
($this->_fn_close)();
}

public function detach()
{
return call_user_func($this->_fn_detach);
return ($this->_fn_detach)();
}

public function getSize(): ?int
{
return call_user_func($this->_fn_getSize);
return ($this->_fn_getSize)();
}

public function tell(): int
{
return call_user_func($this->_fn_tell);
return ($this->_fn_tell)();
}

public function eof(): bool
{
return call_user_func($this->_fn_eof);
return ($this->_fn_eof)();
}

public function isSeekable(): bool
{
return call_user_func($this->_fn_isSeekable);
return ($this->_fn_isSeekable)();
}

public function rewind(): void
{
call_user_func($this->_fn_rewind);
($this->_fn_rewind)();
}

public function seek($offset, $whence = SEEK_SET): void
{
call_user_func($this->_fn_seek, $offset, $whence);
($this->_fn_seek)($offset, $whence);
}

public function isWritable(): bool
{
return call_user_func($this->_fn_isWritable);
return ($this->_fn_isWritable)();
}

public function write($string): int
{
return call_user_func($this->_fn_write, $string);
return ($this->_fn_write)($string);
}

public function isReadable(): bool
{
return call_user_func($this->_fn_isReadable);
return ($this->_fn_isReadable)();
}

public function read($length): string
{
return call_user_func($this->_fn_read, $length);
return ($this->_fn_read)($length);
}

public function getContents(): string
{
return call_user_func($this->_fn_getContents);
return ($this->_fn_getContents)();
}

/**
* @return mixed
*/
public function getMetadata($key = null)
{
return call_user_func($this->_fn_getMetadata, $key);
return ($this->_fn_getMetadata)($key);
}
}
2 changes: 1 addition & 1 deletion src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function parse($header): array
foreach ((array) $header as $value) {
foreach (self::splitList($value) as $val) {
$part = [];
foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) ?: [] as $kvp) {
if (preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
$m = $matches[0];
if (isset($m[1])) {
Expand Down
2 changes: 1 addition & 1 deletion src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function withBody(StreamInterface $body): MessageInterface
}

/**
* @param array<string|int, string|string[]> $headers
* @param (string|string[])[] $headers
*/
private function setHeaders(array $headers): void
{
Expand Down
20 changes: 14 additions & 6 deletions src/MultipartStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function isWritable(): bool
/**
* Get the headers needed before transferring the content of a POST file
*
* @param array<string, string> $headers
* @param string[] $headers
*/
private function getHeaders(array $headers): string
{
Expand Down Expand Up @@ -112,10 +112,15 @@ private function addElement(AppendStream $stream, array $element): void
$stream->addStream(Utils::streamFor("\r\n"));
}

/**
* @param string[] $headers
*
* @return array{0: StreamInterface, 1: string[]}
*/
private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers): array
{
// Set a default content-disposition header if one was no provided
$disposition = $this->getHeader($headers, 'content-disposition');
$disposition = self::getHeader($headers, 'content-disposition');
if (!$disposition) {
$headers['Content-Disposition'] = ($filename === '0' || $filename)
? sprintf(
Expand All @@ -127,27 +132,30 @@ private function createElement(string $name, StreamInterface $stream, ?string $f
}

// Set a default content-length header if one was no provided
$length = $this->getHeader($headers, 'content-length');
$length = self::getHeader($headers, 'content-length');
if (!$length) {
if ($length = $stream->getSize()) {
$headers['Content-Length'] = (string) $length;
}
}

// Set a default Content-Type if one was not supplied
$type = $this->getHeader($headers, 'content-type');
$type = self::getHeader($headers, 'content-type');
if (!$type && ($filename === '0' || $filename)) {
$headers['Content-Type'] = MimeType::fromFilename($filename) ?? 'application/octet-stream';
}

return [$stream, $headers];
}

private function getHeader(array $headers, string $key)
/**
* @param string[] $headers
*/
private static function getHeader(array $headers, string $key): ?string
{
$lowercaseHeader = strtolower($key);
foreach ($headers as $k => $v) {
if (strtolower($k) === $lowercaseHeader) {
if (strtolower((string) $k) === $lowercaseHeader) {
return $v;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/PumpStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
final class PumpStream implements StreamInterface
{
/** @var callable|null */
/** @var callable(int): (string|false|null)|null */
private $source;

/** @var int|null */
Expand Down Expand Up @@ -163,9 +163,9 @@ public function getMetadata($key = null)

private function pump(int $length): void
{
if ($this->source) {
if ($this->source !== null) {
do {
$data = call_user_func($this->source, $length);
$data = ($this->source)($length);
if ($data === false || $data === null) {
$this->source = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Request implements RequestInterface
/**
* @param string $method HTTP method
* @param string|UriInterface $uri URI
* @param array<string, string|string[]> $headers Request headers
* @param (string|string[])[] $headers Request headers
* @param string|resource|StreamInterface|null $body Request body
* @param string $version Protocol version
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Response implements ResponseInterface

/**
* @param int $status Status code
* @param array<string, string|string[]> $headers Response headers
* @param (string|string[])[] $headers Response headers
* @param string|resource|StreamInterface|null $body Response body
* @param string $version Protocol version
* @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
Expand Down
2 changes: 1 addition & 1 deletion src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ServerRequest extends Request implements ServerRequestInterface
/**
* @param string $method HTTP method
* @param string|UriInterface $uri URI
* @param array<string, string|string[]> $headers Request headers
* @param (string|string[])[] $headers Request headers
* @param string|resource|StreamInterface|null $body Request body
* @param string $version Protocol version
* @param array $serverParams Typically the $_SERVER superglobal
Expand Down
2 changes: 1 addition & 1 deletion src/StreamDecoratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __call(string $method, array $args)
{
/** @var callable $callable */
$callable = [$this->stream, $method];
$result = call_user_func_array($callable, $args);
$result = ($callable)(...$args);

// Always return the wrapped object if the result is a return $this
return $result === $this->stream ? $this : $result;
Expand Down
Loading

0 comments on commit 38ef514

Please sign in to comment.