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
declare(strict_types=1);
namespace Loom\HttpComponent;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
@ -9,11 +12,6 @@ use Psr\Http\Message\UriInterface;
class HttpClient implements ClientInterface
{
/**
* @param RequestInterface $request
*
* @return ResponseInterface
*/
public function sendRequest(RequestInterface $request): ResponseInterface
{
$curl = curl_init();
@ -29,73 +27,33 @@ class HttpClient implements ClientInterface
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
{
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
{
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
{
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
{
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
{
return $this->buildAndSend('DELETE', $endpoint, $headers, $body);
}
/**
* @param string $method
* @param string $endpoint
* @param array $headers
* @param string $body
*
* @return ResponseInterface
* @throws ClientExceptionInterface
*/
private function buildAndSend(string $method, string $endpoint, array $headers, string $body): ResponseInterface
{
@ -104,11 +62,6 @@ class HttpClient implements ClientInterface
return $this->sendRequest($request);
}
/**
* @param string $endpoint
*
* @return UriInterface
*/
private function buildUriFromEndpoint(string $endpoint): UriInterface
{
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
{
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getMethod());
@ -145,10 +92,6 @@ class HttpClient implements ClientInterface
}
/**
* @param \CurlHandle $curl
*
* @return bool|string
*
* @throws \RuntimeException
*/
private function getRawResponse(\CurlHandle &$curl): bool|string
@ -164,13 +107,6 @@ class HttpClient implements ClientInterface
return $responseText;
}
/**
* @param ResponseInterface $response
* @param string $responseText
* @param int $headerSize
*
* @return ResponseInterface
*/
private function parseHeaders(ResponseInterface &$response, string $responseText, int $headerSize): ResponseInterface
{
$responseHeaders = substr($responseText, 0, $headerSize);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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