More code cleanup, removing docblocks etc

This commit is contained in:
Daniel Winning 2025-05-05 14:48:14 +01:00
parent a09f953058
commit 64c5c1b508
8 changed files with 28 additions and 280 deletions

View file

@ -1,7 +1,10 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponent; namespace Loom\HttpComponent;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@ -9,11 +12,6 @@ use Psr\Http\Message\UriInterface;
class HttpClient implements ClientInterface class HttpClient implements ClientInterface
{ {
/**
* @param RequestInterface $request
*
* @return ResponseInterface
*/
public function sendRequest(RequestInterface $request): ResponseInterface public function sendRequest(RequestInterface $request): ResponseInterface
{ {
$curl = curl_init(); $curl = curl_init();
@ -29,73 +27,33 @@ class HttpClient implements ClientInterface
return $response->withBody(StreamBuilder::build(substr($responseText, $headerSize))); return $response->withBody(StreamBuilder::build(substr($responseText, $headerSize)));
} }
/**
* @param string $endpoint
* @param array $headers
* @param string $body
*
* @return ResponseInterface
*/
public function get(string $endpoint, array $headers = [], string $body = ''): ResponseInterface public function get(string $endpoint, array $headers = [], string $body = ''): ResponseInterface
{ {
return $this->buildAndSend('GET', $endpoint, $headers, $body); return $this->buildAndSend('GET', $endpoint, $headers, $body);
} }
/**
* @param string $endpoint
* @param array $headers
* @param string $body
*
* @return ResponseInterface
*/
public function post(string $endpoint, array $headers = [], string $body = ''): ResponseInterface public function post(string $endpoint, array $headers = [], string $body = ''): ResponseInterface
{ {
return $this->buildAndSend('POST', $endpoint, $headers, $body); return $this->buildAndSend('POST', $endpoint, $headers, $body);
} }
/**
* @param string $endpoint
* @param array $headers
* @param string $body
*
* @return ResponseInterface
*/
public function put(string $endpoint, array $headers = [], string $body = ''): ResponseInterface public function put(string $endpoint, array $headers = [], string $body = ''): ResponseInterface
{ {
return $this->buildAndSend('PUT', $endpoint, $headers, $body); return $this->buildAndSend('PUT', $endpoint, $headers, $body);
} }
/**
* @param string $endpoint
* @param array $headers
* @param string $body
*
* @return ResponseInterface
*/
public function patch(string $endpoint, array $headers = [], string $body = ''): ResponseInterface public function patch(string $endpoint, array $headers = [], string $body = ''): ResponseInterface
{ {
return $this->buildAndSend('PATCH', $endpoint, $headers, $body); return $this->buildAndSend('PATCH', $endpoint, $headers, $body);
} }
/**
* @param string $endpoint
* @param array $headers
* @param string $body
*
* @return ResponseInterface
*/
public function delete(string $endpoint, array $headers = [], string $body = ''): ResponseInterface public function delete(string $endpoint, array $headers = [], string $body = ''): ResponseInterface
{ {
return $this->buildAndSend('DELETE', $endpoint, $headers, $body); return $this->buildAndSend('DELETE', $endpoint, $headers, $body);
} }
/** /**
* @param string $method * @throws ClientExceptionInterface
* @param string $endpoint
* @param array $headers
* @param string $body
*
* @return ResponseInterface
*/ */
private function buildAndSend(string $method, string $endpoint, array $headers, string $body): ResponseInterface private function buildAndSend(string $method, string $endpoint, array $headers, string $body): ResponseInterface
{ {
@ -104,11 +62,6 @@ class HttpClient implements ClientInterface
return $this->sendRequest($request); return $this->sendRequest($request);
} }
/**
* @param string $endpoint
*
* @return UriInterface
*/
private function buildUriFromEndpoint(string $endpoint): UriInterface private function buildUriFromEndpoint(string $endpoint): UriInterface
{ {
if (!parse_url($endpoint, PHP_URL_SCHEME)) { if (!parse_url($endpoint, PHP_URL_SCHEME)) {
@ -126,12 +79,6 @@ class HttpClient implements ClientInterface
); );
} }
/**
* @param \CurlHandle $curl
* @param RequestInterface $request
*
* @return void
*/
private function setCurlOptions(\CurlHandle &$curl, RequestInterface $request): void private function setCurlOptions(\CurlHandle &$curl, RequestInterface $request): void
{ {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getMethod()); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getMethod());
@ -145,10 +92,6 @@ class HttpClient implements ClientInterface
} }
/** /**
* @param \CurlHandle $curl
*
* @return bool|string
*
* @throws \RuntimeException * @throws \RuntimeException
*/ */
private function getRawResponse(\CurlHandle &$curl): bool|string private function getRawResponse(\CurlHandle &$curl): bool|string
@ -164,13 +107,6 @@ class HttpClient implements ClientInterface
return $responseText; return $responseText;
} }
/**
* @param ResponseInterface $response
* @param string $responseText
* @param int $headerSize
*
* @return ResponseInterface
*/
private function parseHeaders(ResponseInterface &$response, string $responseText, int $headerSize): ResponseInterface private function parseHeaders(ResponseInterface &$response, string $responseText, int $headerSize): ResponseInterface
{ {
$responseHeaders = substr($responseText, 0, $headerSize); $responseHeaders = substr($responseText, 0, $headerSize);

View file

@ -1,34 +1,27 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponent; namespace Loom\HttpComponent;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
class Stream implements StreamInterface class Stream implements StreamInterface
{ {
private $resource; public function __construct(private $resource)
public function __construct($resource)
{ {
if (!is_resource($resource)) { if (!is_resource($this->resource)) {
throw new \InvalidArgumentException('Invalid resource provided for Stream'); throw new \InvalidArgumentException('Invalid resource provided for Stream');
} }
$this->resource = $resource;
$this->rewind(); $this->rewind();
} }
/**
* @return string
*/
public function __toString(): string public function __toString(): string
{ {
return $this->getContents(); return $this->getContents();
} }
/**
* @return void
*/
public function close(): void public function close(): void
{ {
if (is_resource($this->resource)) { if (is_resource($this->resource)) {
@ -47,9 +40,6 @@ class Stream implements StreamInterface
return $resource; return $resource;
} }
/**
* @return int|null
*/
public function getSize(): ?int public function getSize(): ?int
{ {
if (!$this->resource) { if (!$this->resource) {
@ -61,9 +51,6 @@ class Stream implements StreamInterface
return $stats['size'] ?? null; return $stats['size'] ?? null;
} }
/**
* @return int
*/
public function tell(): int public function tell(): int
{ {
if (!$this->resource) { if (!$this->resource) {
@ -79,9 +66,6 @@ class Stream implements StreamInterface
return $position; return $position;
} }
/**
* @return bool
*/
public function eof(): bool public function eof(): bool
{ {
if (!$this->resource) { if (!$this->resource) {
@ -91,9 +75,6 @@ class Stream implements StreamInterface
return feof($this->resource); return feof($this->resource);
} }
/**
* @return bool
*/
public function isSeekable(): bool public function isSeekable(): bool
{ {
if (!$this->resource) { if (!$this->resource) {
@ -105,12 +86,6 @@ class Stream implements StreamInterface
return isset($meta['seekable']) && $meta['seekable']; return isset($meta['seekable']) && $meta['seekable'];
} }
/**
* @param int $offset
* @param int $whence
*
* @return void
*/
public function seek(int $offset, int $whence = SEEK_SET): void public function seek(int $offset, int $whence = SEEK_SET): void
{ {
if (!$this->isSeekable()) { if (!$this->isSeekable()) {
@ -122,17 +97,11 @@ class Stream implements StreamInterface
} }
} }
/**
* @return void
*/
public function rewind(): void public function rewind(): void
{ {
$this->seek(0); $this->seek(0);
} }
/**
* @return bool
*/
public function isWritable(): bool public function isWritable(): bool
{ {
if (!$this->resource) { if (!$this->resource) {
@ -144,11 +113,6 @@ class Stream implements StreamInterface
return isset($meta['mode']) && (str_contains($meta['mode'], 'w') || str_contains($meta['mode'], 'a')); return isset($meta['mode']) && (str_contains($meta['mode'], 'w') || str_contains($meta['mode'], 'a'));
} }
/**
* @param string $string
*
* @return int
*/
public function write(string $string): int public function write(string $string): int
{ {
if (!$this->isWritable()) { if (!$this->isWritable()) {
@ -164,9 +128,6 @@ class Stream implements StreamInterface
return $result; return $result;
} }
/**
* @return bool
*/
public function isReadable(): bool public function isReadable(): bool
{ {
if (!$this->resource) { if (!$this->resource) {
@ -178,11 +139,6 @@ class Stream implements StreamInterface
return isset($meta['mode']) && (str_contains($meta['mode'], 'r') || str_contains($meta['mode'], 'a') || str_contains($meta['mode'], '+')); return isset($meta['mode']) && (str_contains($meta['mode'], 'r') || str_contains($meta['mode'], 'a') || str_contains($meta['mode'], '+'));
} }
/**
* @param int $length
*
* @return string
*/
public function read(int $length): string public function read(int $length): string
{ {
if (!$this->isReadable()) { if (!$this->isReadable()) {
@ -198,9 +154,6 @@ class Stream implements StreamInterface
return $data; return $data;
} }
/**
* @return string
*/
public function getContents(): string public function getContents(): string
{ {
if (!$this->isReadable()) { if (!$this->isReadable()) {
@ -218,11 +171,6 @@ class Stream implements StreamInterface
return $contents; return $contents;
} }
/**
* @param mixed $key
*
* @return mixed
*/
public function getMetadata(mixed $key = null): mixed public function getMetadata(mixed $key = null): mixed
{ {
if (!$this->resource) { if (!$this->resource) {

View file

@ -1,16 +1,13 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponent; namespace Loom\HttpComponent;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
class StreamBuilder class StreamBuilder
{ {
/**
* @param string $body
*
* @return StreamInterface
*/
public static function build(string $body): StreamInterface public static function build(string $body): StreamInterface
{ {
$stream = new Stream(fopen('php://temp', 'r+')); $stream = new Stream(fopen('php://temp', 'r+'));

View file

@ -1,14 +1,11 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponent\Traits; namespace Loom\HttpComponent\Traits;
trait ResolveHeadersTrait trait ResolveHeadersTrait
{ {
/**
* @param array $headers
*
* @return array
*/
protected function setHeaders(array $headers): array protected function setHeaders(array $headers): array
{ {
$sortedHeaders = []; $sortedHeaders = [];

View file

@ -1,41 +1,34 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponent; namespace Loom\HttpComponent;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
class Uri implements UriInterface class Uri implements UriInterface
{ {
private string $scheme;
private string $host;
private ?string $port; private ?string $port;
private string $path;
private string $query;
private string $fragment; private string $fragment;
private string $userInfo; private string $userInfo;
public function __construct(string $scheme, string $host, string $path, string $query, string|int|null $port = null) public function __construct(
{ private string $scheme,
$this->scheme = $scheme; private string $host,
$this->host = $host; private string $path,
private string $query,
string|int|null $port = null
) {
$this->port = (string) $port; $this->port = (string) $port;
$this->path = $path;
$this->query = $query;
$this->fragment = ''; $this->fragment = '';
$this->userInfo = ''; $this->userInfo = '';
} }
/**
* @return string
*/
public function getScheme(): string public function getScheme(): string
{ {
return $this->scheme; return $this->scheme;
} }
/**
* @return string
*/
public function getAuthority(): string public function getAuthority(): string
{ {
$authority = $this->host; $authority = $this->host;
@ -51,59 +44,36 @@ class Uri implements UriInterface
return $authority; return $authority;
} }
/**
* @return string
*/
public function getUserInfo(): string public function getUserInfo(): string
{ {
return $this->userInfo; return $this->userInfo;
} }
/**
* @return string
*/
public function getHost(): string public function getHost(): string
{ {
return $this->host; return $this->host;
} }
/**
* @return int|null
*/
public function getPort(): ?int public function getPort(): ?int
{ {
return (int) $this->port; return (int) $this->port;
} }
/**
* @return string
*/
public function getPath(): string public function getPath(): string
{ {
return $this->path; return $this->path;
} }
/**
* @return string
*/
public function getQuery(): string public function getQuery(): string
{ {
return $this->query; return $this->query;
} }
/**
* @return string
*/
public function getFragment(): string public function getFragment(): string
{ {
return $this->fragment; return $this->fragment;
} }
/**
* @param string $scheme
*
* @return UriInterface
*/
public function withScheme(string $scheme): UriInterface public function withScheme(string $scheme): UriInterface
{ {
$uri = clone $this; $uri = clone $this;
@ -112,12 +82,6 @@ class Uri implements UriInterface
return $uri; return $uri;
} }
/**
* @param string $user
* @param string|null $password
*
* @return UriInterface
*/
public function withUserInfo(string $user, ?string $password = null): UriInterface public function withUserInfo(string $user, ?string $password = null): UriInterface
{ {
$uri = clone $this; $uri = clone $this;
@ -126,11 +90,6 @@ class Uri implements UriInterface
return $uri; return $uri;
} }
/**
* @param string $host
*
* @return UriInterface
*/
public function withHost(string $host): UriInterface public function withHost(string $host): UriInterface
{ {
$uri = clone $this; $uri = clone $this;
@ -139,24 +98,14 @@ class Uri implements UriInterface
return $uri; return $uri;
} }
/**
* @param int|null $port
*
* @return UriInterface
*/
public function withPort(?int $port): UriInterface public function withPort(?int $port): UriInterface
{ {
$uri = clone $this; $uri = clone $this;
$uri->port = $port; $uri->port = (string) $port;
return $uri; return $uri;
} }
/**
* @param string $path
*
* @return UriInterface
*/
public function withPath(string $path): UriInterface public function withPath(string $path): UriInterface
{ {
$uri = clone $this; $uri = clone $this;
@ -165,11 +114,6 @@ class Uri implements UriInterface
return $uri; return $uri;
} }
/**
* @param string $query
*
* @return UriInterface
*/
public function withQuery(string $query): UriInterface public function withQuery(string $query): UriInterface
{ {
$uri = clone $this; $uri = clone $this;
@ -178,11 +122,6 @@ class Uri implements UriInterface
return $uri; return $uri;
} }
/**
* @param string $fragment
*
* @return UriInterface
*/
public function withFragment(string $fragment): UriInterface public function withFragment(string $fragment): UriInterface
{ {
$uri = clone $this; $uri = clone $this;
@ -191,9 +130,6 @@ class Uri implements UriInterface
return $uri; return $uri;
} }
/**
* @return string
*/
public function __toString(): string public function __toString(): string
{ {
$uri = sprintf('%s://%s', $this->scheme, $this->host); $uri = sprintf('%s://%s', $this->scheme, $this->host);

View file

@ -1,14 +1,13 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponent\Web; namespace Loom\HttpComponent\Web;
use Loom\HttpComponent\Uri; use Loom\HttpComponent\Uri;
class WebServerUri class WebServerUri
{ {
/**
* @return Uri
*/
public static function generate(): Uri public static function generate(): Uri
{ {
$scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'; $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';

View file

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponentTests; namespace Loom\HttpComponentTests;
use Loom\HttpComponent\Stream; use Loom\HttpComponent\Stream;
@ -7,9 +9,6 @@ use PHPUnit\Framework\TestCase;
class StreamTest extends TestCase class StreamTest extends TestCase
{ {
/**
* @return void
*/
public function testToString(): void public function testToString(): void
{ {
[$stream, $data] = $this->getWritableStreamWithData(); [$stream, $data] = $this->getWritableStreamWithData();
@ -19,9 +18,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testGetSize(): void public function testGetSize(): void
{ {
[$stream, $data] = $this->getWritableStreamWithData(); [$stream, $data] = $this->getWritableStreamWithData();
@ -31,9 +27,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testTell(): void public function testTell(): void
{ {
[$stream] = $this->getWritableStreamWithData(); [$stream] = $this->getWritableStreamWithData();
@ -45,9 +38,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testClose(): void public function testClose(): void
{ {
$resource = fopen('php://temp', 'r+'); $resource = fopen('php://temp', 'r+');
@ -60,9 +50,6 @@ class StreamTest extends TestCase
$this->assertFalse(is_resource($resource)); $this->assertFalse(is_resource($resource));
} }
/**
* @return void
*/
public function testDetach(): void public function testDetach(): void
{ {
$resource = fopen('php://temp', 'r+'); $resource = fopen('php://temp', 'r+');
@ -76,9 +63,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testEof(): void public function testEof(): void
{ {
[$stream, $data] = $this->getWritableStreamWithData(); [$stream, $data] = $this->getWritableStreamWithData();
@ -92,9 +76,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testIsSeekableWithSeekableResource(): void public function testIsSeekableWithSeekableResource(): void
{ {
[$stream] = $this->getWritableStreamWithData(); [$stream] = $this->getWritableStreamWithData();
@ -104,9 +85,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testSeek(): void public function testSeek(): void
{ {
[$stream] = $this->getWritableStreamWithData(); [$stream] = $this->getWritableStreamWithData();
@ -118,9 +96,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testRead(): void public function testRead(): void
{ {
[$stream] = $this->getWritableStreamWithData(); [$stream] = $this->getWritableStreamWithData();
@ -136,9 +111,6 @@ class StreamTest extends TestCase
$this->assertSame('', $read[2]); $this->assertSame('', $read[2]);
} }
/**
* @return void
*/
public function testWrite(): void public function testWrite(): void
{ {
$resource = fopen('php://temp', 'r+'); $resource = fopen('php://temp', 'r+');
@ -155,9 +127,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return void
*/
public function testGetMetadata(): void public function testGetMetadata(): void
{ {
[$stream] = $this->getWritableStreamWithData(); [$stream] = $this->getWritableStreamWithData();
@ -167,9 +136,6 @@ class StreamTest extends TestCase
$stream->close(); $stream->close();
} }
/**
* @return array
*/
private function getWritableStreamWithData(): array private function getWritableStreamWithData(): array
{ {
$data = 'Test string'; $data = 'Test string';

View file

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace Loom\HttpComponentTests; namespace Loom\HttpComponentTests;
use Loom\HttpComponent\Uri; use Loom\HttpComponent\Uri;
@ -8,17 +10,11 @@ use Psr\Http\Message\UriInterface;
class UriTest extends TestCase class UriTest extends TestCase
{ {
/**
* @return void
*/
public function testGetScheme(): void public function testGetScheme(): void
{ {
$this->assertEquals('https', ($this->getDefaultUri())->getScheme()); $this->assertEquals('https', ($this->getDefaultUri())->getScheme());
} }
/**
* @return void
*/
public function testGetAuthority(): void public function testGetAuthority(): void
{ {
$uri = new Uri('https', 'test.com', '/path', 'var=1', 443); $uri = new Uri('https', 'test.com', '/path', 'var=1', 443);
@ -28,26 +24,17 @@ class UriTest extends TestCase
$this->assertEquals('user:password@test.com', $uri->getAuthority()); $this->assertEquals('user:password@test.com', $uri->getAuthority());
} }
/**
* @return void
*/
public function testWithPath(): void public function testWithPath(): void
{ {
$this->assertEquals('/', ($this->getDefaultUri())->getPath()); $this->assertEquals('/', ($this->getDefaultUri())->getPath());
$this->assertEquals('/new', ($this->getDefaultUri())->withPath('/new')->getPath()); $this->assertEquals('/new', ($this->getDefaultUri())->withPath('/new')->getPath());
} }
/**
* @return void
*/
public function testToString(): void public function testToString(): void
{ {
$this->assertEquals('https://test.com/', ($this->getDefaultUri())->__toString()); $this->assertEquals('https://test.com/', ($this->getDefaultUri())->__toString());
} }
/**
* @return void
*/
public function testWithUserInfo(): void public function testWithUserInfo(): void
{ {
$this->assertEquals( $this->assertEquals(
@ -56,33 +43,21 @@ class UriTest extends TestCase
); );
} }
/**
* @return void
*/
public function testWithScheme(): void public function testWithScheme(): void
{ {
$this->assertEquals('ftp', ($this->getDefaultUri())->withScheme('ftp')->getScheme()); $this->assertEquals('ftp', ($this->getDefaultUri())->withScheme('ftp')->getScheme());
} }
/**
* @return void
*/
public function testWithHost(): void public function testWithHost(): void
{ {
$this->assertEquals('localhost', ($this->getDefaultUri())->withHost('localhost')->getHost()); $this->assertEquals('localhost', ($this->getDefaultUri())->withHost('localhost')->getHost());
} }
/**
* @return void
*/
public function testWithPort(): void public function testWithPort(): void
{ {
$this->assertEquals(8080, ($this->getDefaultUri())->withPort(8080)->getPort()); $this->assertEquals(8080, ($this->getDefaultUri())->withPort(8080)->getPort());
} }
/**
* @return void
*/
public function testWithQuery(): void public function testWithQuery(): void
{ {
$this->assertEquals( $this->assertEquals(
@ -91,17 +66,11 @@ class UriTest extends TestCase
); );
} }
/**
* @return void
*/
public function testWithFragment(): void public function testWithFragment(): void
{ {
$this->assertEquals('news', ($this->getDefaultUri())->withFragment('news')->getFragment()); $this->assertEquals('news', ($this->getDefaultUri())->withFragment('news')->getFragment());
} }
/**
* @return UriInterface
*/
private function getDefaultUri(): UriInterface private function getDefaultUri(): UriInterface
{ {
return new Uri('https', 'test.com', '/', ''); return new Uri('https', 'test.com', '/', '');