Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Run PHPStan with activated strict rules.
- Run tests with PHPUnit 11 and 12.
- Add `rector.php` config file to run [rector](https://getrector.com) for code improvements.

### Changed

Expand Down
2 changes: 2 additions & 0 deletions examples/get.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

// First, include the Composer Autoloader.
require_once dirname(__DIR__) . '/vendor/autoload.php';

Expand Down
2 changes: 2 additions & 0 deletions examples/post.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

// First, include the Composer Autoloader.
require_once dirname(__DIR__) . '/vendor/autoload.php';

Expand Down
36 changes: 36 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector;
use Rector\Config\RectorConfig;
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/examples',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets()
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
privatization: true,
instanceOf: true,
earlyReturn: true,
strictBooleans: true,
rectorPreset: true,
phpunitCodeQuality: true,
)
->withSkip([
CatchExceptionNameMatchingTypeRector::class,
PostIncDecToPreIncDecRector::class,
SymplifyQuoteEscapeRector::class,
YieldDataProviderRector::class,
])
;
3 changes: 0 additions & 3 deletions src/Exception/Psr/NetworkException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ class NetworkException extends Exception implements NetworkExceptionInterface

/**
* Create a new exception
*
* @param RequestInterface $request
* @param Transport $previous
*/
public function __construct(RequestInterface $request, Transport $previous)
{
Expand Down
3 changes: 0 additions & 3 deletions src/Exception/Psr/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class RequestException extends Exception implements RequestExceptionInterface

/**
* Create a new exception
*
* @param RequestInterface $request
* @param Exception $previous
*/
public function __construct(RequestInterface $request, Exception $previous)
{
Expand Down
6 changes: 1 addition & 5 deletions src/Psr/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function createRequest(string $method, $uri): RequestInterface
* The stream SHOULD be created with a temporary resource.
*
* @param string $content String content with which to populate the stream.
* @return StreamInterface
*/
public function createStream(string $content = ''): StreamInterface
{
Expand All @@ -88,7 +87,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
{
$headers = [];

foreach ($request->getHeaders() as $key => $header) {
foreach (array_keys($request->getHeaders()) as $key) {
$headers[$key] = $request->getHeaderLine($key);
}

Expand Down Expand Up @@ -120,7 +119,6 @@ public function sendRequest(RequestInterface $request): ResponseInterface
* @param string $filename Filename or stream URI to use as basis of stream.
* @param string $mode Mode with which to open the underlying filename/stream.
*
* @return StreamInterface
* @throws \RuntimeException If the file cannot be opened.
* @throws \InvalidArgumentException If the mode is invalid.
*/
Expand All @@ -135,8 +133,6 @@ public function createStreamFromFile(string $filename, string $mode = 'r'): Stre
* The stream MUST be readable and may be writable.
*
* @param resource $resource PHP resource to use as basis of stream.
*
* @return StreamInterface
*/
public function createStreamFromResource($resource): StreamInterface
{
Expand Down
11 changes: 2 additions & 9 deletions src/Psr/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ final class Request implements RequestInterface
/**
* create Request with method and uri
*
* @param string $method
* @param UriInterface $uri
*
* @return Request
*/
public static function withMethodAndUri(string $method, UriInterface $uri)
public static function withMethodAndUri(string $method, UriInterface $uri): self
{
if ($method === '') {
throw new \InvalidArgumentException('Method must be a non-empty string');
Expand Down Expand Up @@ -87,7 +84,6 @@ public static function withMethodAndUri(string $method, UriInterface $uri)
* Constructor
*
* @param string $method
* @param UriInterface $uri
*
* @return Request
*/
Expand All @@ -110,8 +106,6 @@ private function __construct($method, UriInterface $uri)
*
* If no URI is available, and no request-target has been specifically
* provided, this method MUST return the string "/".
*
* @return string
*/
public function getRequestTarget(): string
{
Expand Down Expand Up @@ -148,7 +142,6 @@ public function getRequestTarget(): string
*
* @see http://tools.ietf.org/html/rfc7230#section-5.3 (for the various
* request-target forms allowed in request messages)
* @param string $requestTarget
* @return static
*/
public function withRequestTarget(string $requestTarget): RequestInterface
Expand Down Expand Up @@ -322,7 +315,7 @@ public function withBody(StreamInterface $body): MessageInterface
* @param UriInterface $uri New request URI to use.
* @param bool $preserveHost Preserve the original state of the Host header.
*/
private function setUri(UriInterface $uri, $preserveHost): void
private function setUri(UriInterface $uri, bool $preserveHost): void
{
$this->uri = $uri;

Expand Down
15 changes: 3 additions & 12 deletions src/Psr/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ final class Response implements ResponseInterface
/**
* create Response
*
* @param RequestsResponse $response
*
* @return Response
*/
public static function fromResponse(RequestsResponse $response)
public static function fromResponse(RequestsResponse $response): self
{
if (is_bool($response->protocol_version)) {
$protocol_version = '1.1';
Expand Down Expand Up @@ -85,7 +83,7 @@ public static function fromResponse(RequestsResponse $response)
/**
* @var null|string
*/
private $reasonPhrase = null;
private $reasonPhrase;

/**
* @var string
Expand Down Expand Up @@ -169,10 +167,7 @@ public static function fromResponse(RequestsResponse $response)
/**
* Constructor
*
* @param StreamInterface $body
* @param array<string,string[]> $headers
* @param int $status_code
* @param string $protocol_version
*/
private function __construct(StreamInterface $body, array $headers, int $status_code, string $protocol_version)
{
Expand Down Expand Up @@ -228,11 +223,7 @@ public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterf
$response = clone($this);
$response->status_code = $code;

if ($reasonPhrase === '') {
$response->reasonPhrase = null;
} else {
$response->reasonPhrase = $reasonPhrase;
}
$response->reasonPhrase = $reasonPhrase === '' ? null : $reasonPhrase;

return $response;
}
Expand Down
22 changes: 2 additions & 20 deletions src/Psr/StringBasedStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ final class StringBasedStream implements StreamInterface
{
/**
* Create StringBasedStream from string
*
* @param string $content
* @return static
*/
public static function createFromString(string $content)
public static function createFromString(string $content): self
{
return new self($content);
}
Expand All @@ -40,8 +37,6 @@ public static function createFromString(string $content)

/**
* Constructor
*
* @param string $content
*/
private function __construct(string $content)
{
Expand All @@ -60,7 +55,6 @@ private function __construct(string $content)
* string casting operations.
*
* @see http://php.net/manual/en/language.oop5.magic.php#object.tostring
* @return string
*/
public function __toString(): string
{
Expand All @@ -70,10 +64,7 @@ public function __toString(): string
/**
* Closes the stream and any underlying resources.
*/
public function close(): void
{
return;
}
public function close(): void {}

/**
* Separates any underlying resources from the stream.
Expand Down Expand Up @@ -110,8 +101,6 @@ public function tell(): int

/**
* Returns true if the stream is at the end of the stream.
*
* @return bool
*/
public function eof(): bool
{
Expand All @@ -120,8 +109,6 @@ public function eof(): bool

/**
* Returns whether or not the stream is seekable.
*
* @return bool
*/
public function isSeekable(): bool
{
Expand Down Expand Up @@ -162,8 +149,6 @@ public function rewind(): void

/**
* Returns whether or not the stream is writable.
*
* @return bool
*/
public function isWritable(): bool
{
Expand All @@ -184,8 +169,6 @@ public function write(string $string): int

/**
* Returns whether or not the stream is readable.
*
* @return bool
*/
public function isReadable(): bool
{
Expand All @@ -210,7 +193,6 @@ public function read(int $length): string
/**
* Returns the remaining contents in a string
*
* @return string
* @throws \RuntimeException if unable to read or an error occurs while
* reading.
*/
Expand Down
9 changes: 2 additions & 7 deletions src/Psr/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ final class Uri implements UriInterface
/**
* create Uri from Iri instance
*
* @param Iri $iri
*
* @return Uri
*/
public static function fromIri(Iri $iri)
public static function fromIri(Iri $iri): self
{
return new self(clone($iri));
}
Expand All @@ -56,8 +54,6 @@ public static function fromIri(Iri $iri)

/**
* Constructor
*
* @param Iri $iri
*/
private function __construct(Iri $iri)
{
Expand Down Expand Up @@ -313,7 +309,7 @@ public function withUserInfo($user, $password = null): UriInterface

if ($user === '') {
$iri->userinfo = $user;
} elseif ($password === null or $password === '') {
} elseif ($password === null || $password === '') {
$iri->userinfo = $user;
} else {
$iri->userinfo = $user . ':' . $password;
Expand Down Expand Up @@ -494,7 +490,6 @@ public function withFragment($fragment): UriInterface
* - If a fragment is present, it MUST be prefixed by "#".
*
* @see http://tools.ietf.org/html/rfc3986#section-4.1
* @return string
*/
public function __toString(): string
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Psr/HttpClient/CreateRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testCreateRequestWithoutUriStringThrowsException($input): void
*
* @return array<string, mixed>
*/
public static function dataInvalidTypeNotString()
public static function dataInvalidTypeNotString(): array
{
return TypeProviderHelper::getAllExcept(TypeProviderHelper::GROUP_STRING);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Psr/HttpClient/CreateStreamFromFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testCreateStreamFromFileThrowsException(): void
$httpClient = new HttpClient();

$this->expectException(Exception::class);
$this->expectExceptionMessage('Art4\Requests\Psr\HttpClient::createStreamFromFile() is not yet implemented.');
$this->expectExceptionMessage(HttpClient::class . '::createStreamFromFile() is not yet implemented.');

$httpClient->createStreamFromFile('path/to/filename.txt');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Psr/HttpClient/CreateStreamFromResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testCreateStreamFromResourceThrowsException(): void
}

$this->expectException(Exception::class);
$this->expectExceptionMessage('Art4\Requests\Psr\HttpClient::createStreamFromResource() is not yet implemented.');
$this->expectExceptionMessage(HttpClient::class . '::createStreamFromResource() is not yet implemented.');

$httpClient->createStreamFromResource($resource);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Psr/HttpClient/SendRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SendRequestTest extends TestCase
public function testSendRequestWithGetSendsCorrectDataAndReturnsCorrectResponseData(): void
{
$transport = $this->createMock(Transport::class);
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, $options) {
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, array $options): string {
TestCase::assertSame('https://example.org/', $url);
TestCase::assertSame(['Host' => 'example.org'], $headers);
TestCase::assertSame('', $data);
Expand Down Expand Up @@ -59,7 +59,7 @@ public function testSendRequestWithGetSendsCorrectDataAndReturnsCorrectResponseD
public function testSendRequestWithPostSendsCorrectDataAndReturnsCorrectResponseData(): void
{
$transport = $this->createMock(Transport::class);
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, $options) {
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, array $options): string {
TestCase::assertSame('https://example.org/posts', $url);
TestCase::assertSame(['Host' => 'example.org'], $headers);
TestCase::assertSame('{"title":"Post title"}', $data);
Expand Down Expand Up @@ -96,7 +96,7 @@ public function testSendRequestWithPostSendsCorrectDataAndReturnsCorrectResponse
public function testSendRequestReturnsResponseOn404Error(): void
{
$transport = $this->createMock(Transport::class);
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, $options) {
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, array $options): string {
TestCase::assertSame('https://example.org/not-found', $url);
TestCase::assertSame(['Host' => 'example.org'], $headers);
TestCase::assertSame('', $data);
Expand Down Expand Up @@ -132,7 +132,7 @@ public function testSendRequestReturnsResponseOn404Error(): void
public function testSendRequestReturnsResponseOn503Error(): void
{
$transport = $this->createMock(Transport::class);
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, $options) {
$transport->expects(TestCase::once())->method('request')->willReturnCallback(function ($url, $headers, $data, array $options): string {
TestCase::assertSame('https://example.org/not-available', $url);
TestCase::assertSame(['Host' => 'example.org'], $headers);
TestCase::assertSame('', $data);
Expand Down
Loading