Skip to content

Commit

Permalink
[TASK] Use PHP 8.1 language features
Browse files Browse the repository at this point in the history
Related: #2
  • Loading branch information
brotkrueml committed Dec 11, 2022
1 parent 1229e16 commit 17f32d7
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 124 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"kriswallsmith/buzz": "^1.2",
"nyholm/psr7": "^1.5",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0",
"symfony/polyfill-php80": "^1.18"
"psr/http-message": "^1.0"
},
"require-dev": {
"brotkrueml/coding-standards": "~3.0.0",
Expand Down
4 changes: 1 addition & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ includes:
- vendor/symplify/phpstan-rules/config/regex-rules.neon

parameters:
phpVersion: 70400
phpVersion: 80100
level: 8
paths:
- src
# This can be removed once PHP 8.0 is minimum requirement and union types are added
treatPhpDocTypesAsCertain: false
ignoreErrors:
- "#^Class has a static method must so must contains \"Static\" in its name$#"

Expand Down
6 changes: 2 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersion::PHP_74);
$rectorConfig->phpVersion(PhpVersion::PHP_81);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_74,
LevelSetList::UP_TO_PHP_81,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::EARLY_RETURN,
Expand All @@ -39,7 +38,6 @@
$rectorConfig->importShortClasses(false);
$rectorConfig->skip([
AddLiteralSeparatorToNumberRector::class,
RemoveUnusedPromotedPropertyRector::class, // Skip until compatibility with PHP >= 8.0
TypedPropertyFromAssignsRector::class => [
__DIR__ . '/tests',
],
Expand Down
11 changes: 3 additions & 8 deletions src/Client/ClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@

abstract class ClientDecorator implements ClientInterface
{
/**
* @var ClientInterface
*/
protected $client;

public function __construct(ClientInterface $client)
{
$this->client = $client;
public function __construct(
protected readonly ClientInterface $client
) {
}

public function authenticate(): void
Expand Down
3 changes: 2 additions & 1 deletion src/Client/DocumentsClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private function buildMultipart(Document $document): array
$multipartKeywordFields = $this->buildFieldsForMultipart('keyword', $document->getKeywordFields());
$multipartFiles = $this->buildFilesForMultipart($document->getFiles());

return \array_merge($multipartIndexFields, $multipartKeywordFields, $multipartFiles);
// @phpstan-ignore-next-line Use value object over return of values
return [...$multipartIndexFields, ...$multipartKeywordFields, ...$multipartFiles];
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Client/IncidentsClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ private function buildProcessTableFieldsForMultipart(array $processTableFields):
return $multipartProcessTableFields;
}

/**
* @param bool|int|string|FileInterface $value
* @return string|FileInterface
*/
private function prepareFieldValue($value)
private function prepareFieldValue(bool|int|string|FileInterface $value): string|FileInterface
{
if (\is_bool($value)) {
$value = (int)$value;
Expand Down
20 changes: 8 additions & 12 deletions src/Client/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@
*/
final class RestClient implements ClientInterface
{
/**
* @readonly
*/
private ClientConfiguration $configuration;
private Psr17Factory $psr17factory;
private Browser $browser;
private AuthorisationMiddleware $authorisationMiddleware;
private RouteContentTypeMapper $routeContentTypeMapper;
private readonly Psr17Factory $psr17factory;
private readonly Browser $browser;
private readonly AuthorisationMiddleware $authorisationMiddleware;
private readonly RouteContentTypeMapper $routeContentTypeMapper;
private string $jobRouterVersion = '';

/**
Expand All @@ -52,9 +48,9 @@ final class RestClient implements ClientInterface
* @throws AuthenticationException
* @throws HttpException
*/
public function __construct(ClientConfiguration $configuration)
{
$this->configuration = $configuration;
public function __construct(
private readonly ClientConfiguration $configuration
) {
$this->psr17factory = new Psr17Factory();

$client = new Curl($this->psr17factory, $this->configuration->getClientOptions()->toArray());
Expand Down Expand Up @@ -195,7 +191,7 @@ private function sendForm(string $method, string $resource, array $multipart): R
/**
* @param string|array<string, mixed> $jsonPayload
*/
private function sendJson(string $method, string $resource, $jsonPayload): ResponseInterface
private function sendJson(string $method, string $resource, string|array $jsonPayload): ResponseInterface
{
$request = $this->buildRequest($method, $resource);
$request = $request->withHeader('content-type', 'application/json');
Expand Down
6 changes: 3 additions & 3 deletions src/Configuration/ClientConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ final class ClientConfiguration
public const MINIMUM_ALLOWED_TOKEN_LIFETIME_IN_SECONDS = 0;
public const MAXIMUM_ALLOWED_TOKEN_LIFETIME_IN_SECONDS = 3600;

private JobRouterSystem $jobRouterSystem;
private string $username;
private string $password;
private readonly JobRouterSystem $jobRouterSystem;
private readonly string $username;
private readonly string $password;
private int $lifetime = self::DEFAULT_TOKEN_LIFETIME_IN_SECONDS;
private string $userAgentAddition = '';
private ClientOptions $clientOptions;
Expand Down
21 changes: 5 additions & 16 deletions src/Configuration/ClientOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,13 @@
*/
final class ClientOptions
{
private bool $allowRedirects;
private int $maxRedirects;
private int $timeout;
private bool $verify;
private ?string $proxy;

public function __construct(
bool $allowRedirects = false,
int $maxRedirects = 5,
int $timeout = 0,
bool $verify = true,
?string $proxy = null
private readonly bool $allowRedirects = false,
private readonly int $maxRedirects = 5,
private readonly int $timeout = 0,
private readonly bool $verify = true,
private readonly ?string $proxy = null
) {
$this->allowRedirects = $allowRedirects;
$this->maxRedirects = $maxRedirects;
$this->timeout = $timeout;
$this->verify = $verify;
$this->proxy = $proxy;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/UserAgentMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ class UserAgentMiddleware implements MiddlewareInterface
{
private const USER_AGENT_TEMPLATE = 'JobRouterClient/%s (https://jobrouter-client.rtfd.io/) %s';

/**
* @readonly
*/
private string $userAgent;
private readonly string $userAgent;

public function __construct(string $userAgentAddition = '')
{
Expand Down
17 changes: 1 addition & 16 deletions src/Model/Incident.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,8 @@ public function getProcessTableField(string $name)
return $this->processTableFields[$name] ?? null;
}

/**
* @param string|int|bool|FileInterface $value
* @throws \InvalidArgumentException
*/
public function setProcessTableField(string $name, $value): self
public function setProcessTableField(string $name, string|int|bool|FileInterface $value): self
{
if (! \is_string($value) && ! \is_int($value) && ! \is_bool($value) && ! $value instanceof FileInterface) {
throw new \InvalidArgumentException(
\sprintf(
'value has to be either a string, an integer, a boolean or an instance of %s, "%s" given',
FileInterface::class,
\get_debug_type($value)
),
1578225863
);
}

$this->processTableFields[$name] = $value;

return $this;
Expand Down
13 changes: 7 additions & 6 deletions src/Resource/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
*/
final class File implements FileInterface
{
private string $path;
private string $fileName;
private string $contentType;
private readonly string $path;
private readonly string $fileName;

public function __construct(string $path, string $fileName = '', string $contentType = '')
{
public function __construct(
string $path,
string $fileName = '',
private readonly string $contentType = ''
) {
if (! \file_exists($path)) {
throw new InvalidResourceException(
\sprintf(
Expand All @@ -39,7 +41,6 @@ public function __construct(string $path, string $fileName = '', string $content

$this->path = $path;
$this->fileName = $fileName ?: \basename($path);
$this->contentType = $contentType;
}

public function getPath(): string
Expand Down
6 changes: 1 addition & 5 deletions src/Resource/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ public function count(): int
return \count($this->files);
}

/**
* @return FileInterface|false
*/
#[\ReturnTypeWillChange]
public function current()
public function current(): FileInterface|false
{
return \current($this->files);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Resource/JobRouterSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class JobRouterSystem implements \Stringable
{
private const API_ENDPOINT = 'api/rest/v2/';

private string $baseUrl;
private readonly string $baseUrl;

public function __construct(string $baseUrl)
{
Expand Down Expand Up @@ -115,7 +115,7 @@ public function getResourceUrl(string $resource): string
return $this->getApiUrl() . \ltrim($resource, '/');
}

public function __toString()
public function __toString(): string
{
return $this->getBaseUrl();
}
Expand Down
5 changes: 1 addition & 4 deletions tests/Unit/Client/ClientDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@

class ClientDecoratorTest extends TestCase
{
/**
* @var ClientInterface&MockObject
*/
private $client;
private ClientInterface&MockObject $client;
private ClientDecorator $subject;

protected function setUp(): void
Expand Down
5 changes: 1 addition & 4 deletions tests/Unit/Client/DocumentsClientDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@

class DocumentsClientDecoratorTest extends TestCase
{
/**
* @var ClientInterface&MockObject
*/
private $clientMock;
private ClientInterface&MockObject $clientMock;
private DocumentsClientDecorator $subject;

protected function setUp(): void
Expand Down
5 changes: 1 addition & 4 deletions tests/Unit/Client/IncidentsClientDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@

class IncidentsClientDecoratorTest extends TestCase
{
/**
* @var ClientInterface&MockObject
*/
private $clientMock;
private ClientInterface&MockObject $clientMock;
private IncidentsClientDecorator $subject;

protected function setUp(): void
Expand Down
25 changes: 0 additions & 25 deletions tests/Unit/Model/IncidentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,31 +367,6 @@ public function setAndGetProcessTableFieldAreImplementedCorrectlyForAFile(): voi
self::assertSame($fileStub, $this->subject->getProcessTableField('some name'));
}

/**
* @test
*/
public function setProcessTableFieldThrowsExceptionWhenValueTypeIsAClass(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1578225863);
$this->expectExceptionMessage('value has to be either a string, an integer, a boolean or an instance of Brotkrueml\JobRouterClient\Resource\FileInterface, "stdClass" given');

$this->subject->setProcessTableField('some name', new \stdClass());
}

/**
* @test
*/
public function setProcessTableFieldThrowsExceptionWhenValueTypeIsAnArray(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1578225863);
$this->expectExceptionMessage('value has to be either a string, an integer, a boolean or an instance of Brotkrueml\JobRouterClient\Resource\FileInterface, "array" given');

/** @noinspection PhpParamsInspection */
$this->subject->setProcessTableField('some name', ['invalid']);
}

/**
* @test
*/
Expand Down

0 comments on commit 17f32d7

Please sign in to comment.