Skip to content

Commit

Permalink
refactor: drop support for php 7.4 and optimize code (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-doehring committed Jul 10, 2021
1 parent 9175ac6 commit 30af009
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 100 deletions.
1 change: 0 additions & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
os:
- ubuntu-latest
php:
- 7.4
- 8.0
dependency-version: [prefer-lowest, prefer-stable]

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.1.0 - 2021-07-XX

- drop support for php 7.4
- some code optimizations

## v1.0.1 - 2021-07-05

- remove `illuminate/support` as dependency
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"license": "MIT",
"homepage": "https://github.com/dogado-group/json-api-client",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"ext-json": "*",
"dogado/json-api-common": "^1.0",
"psr/http-factory": "^1.0",
Expand Down
17 changes: 4 additions & 13 deletions src/Action/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,15 @@

abstract class AbstractAction implements ActionInterface
{
protected JsonApiClient $client;
protected RequestFactoryInterface $requestFactory;
protected UriFactoryInterface $uriFactory;
protected ResponseValidator $responseValidator;

/** @var callable[] */
protected array $preExecutionCallStack = [];

public function __construct(
JsonApiClient $client,
RequestFactoryInterface $requestFactory,
UriFactoryInterface $uriFactory,
ResponseValidator $responseValidator
protected JsonApiClient $client,
protected RequestFactoryInterface $requestFactory,
protected UriFactoryInterface $uriFactory,
protected ResponseValidator $responseValidator
) {
$this->client = $client;
$this->requestFactory = $requestFactory;
$this->uriFactory = $uriFactory;
$this->responseValidator = $responseValidator;
}

abstract public function execute(): ResponseInterface;
Expand Down
4 changes: 1 addition & 3 deletions src/Action/FiltersResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ trait FiltersResource

/**
* @param string|array $filterOrKey
* @param mixed $value
* @return $this
*/
public function filter($filterOrKey, $value = null): self
public function filter(mixed $filterOrKey, mixed $value = null): self
{
$this->beforeSend('filter', function (RequestInterface $request) {
$this->applyFilter($request);
Expand Down
3 changes: 1 addition & 2 deletions src/Action/PaginatesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ trait PaginatesResource

/**
* @param string|array $paginationOrKey
* @param mixed $value
*/
public function pagination($paginationOrKey, $value = null): self
public function pagination(mixed $paginationOrKey, mixed $value = null): self
{
$this->beforeSend('paginate', function (RequestInterface $request) {
$this->applyPagination($request);
Expand Down
4 changes: 1 addition & 3 deletions src/Action/SortsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ trait SortsResource

/**
* @param string|array $sortOrKey
* @param mixed $direction
* @return $this
*/
public function sort($sortOrKey, $direction = null): self
public function sort(mixed $sortOrKey, mixed $direction = null): self
{
$this->beforeSend('sort', function (RequestInterface $request) {
$this->applySorting($request);
Expand Down
17 changes: 4 additions & 13 deletions src/Exception/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ class ResponseException extends Exception
{
public const CODE_UNSUCCESSFUL_HTTP_STATUS = 100;

protected RequestInterface $request;
protected PsrRequestInterface $psrRequest;
protected ResponseInterface $response;
protected PsrResponseInterface $psrResponse;

public static function unsuccessfulHttpStatusReturned(
RequestInterface $request,
PsrRequestInterface $psrRequest,
Expand All @@ -41,10 +36,10 @@ public static function unsuccessfulHttpStatusReturned(
public function __construct(
string $message,
int $code,
RequestInterface $request,
PsrRequestInterface $psrRequest,
ResponseInterface $response,
PsrResponseInterface $psrResponse,
protected RequestInterface $request,
protected PsrRequestInterface $psrRequest,
protected ResponseInterface $response,
protected PsrResponseInterface $psrResponse,
Throwable $previous = null
) {
$document = $response->document();
Expand All @@ -55,10 +50,6 @@ public function __construct(
}

parent::__construct($message, $code, $previous);
$this->request = $request;
$this->psrRequest = $psrRequest;
$this->response = $response;
$this->psrResponse = $psrResponse;
}

public function request(): RequestInterface
Expand Down
5 changes: 1 addition & 4 deletions src/Factory/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@

class RequestFactory implements RequestFactoryInterface
{
protected UriInterface $baseUrl;

public function __construct(UriInterface $baseUrl)
public function __construct(protected UriInterface $baseUrl)
{
$this->baseUrl = $baseUrl;
}

/**
Expand Down
25 changes: 6 additions & 19 deletions src/JsonApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,14 @@

class JsonApiClient
{
protected ClientInterface $httpClient;
protected RequestFactoryInterface $requestFactory;
protected StreamFactoryInterface $streamFactory;
protected DocumentSerializerInterface $serializer;
protected ResponseFactoryInterface $responseFactory;
protected ?AuthenticationMiddlewareInterface $authMiddleware;

public function __construct(
ClientInterface $httpClient,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
DocumentSerializerInterface $serializer,
ResponseFactoryInterface $responseFactory,
?AuthenticationMiddlewareInterface $authMiddleware = null
protected ClientInterface $httpClient,
protected RequestFactoryInterface $requestFactory,
protected StreamFactoryInterface $streamFactory,
protected DocumentSerializerInterface $serializer,
protected ResponseFactoryInterface $responseFactory,
protected ?AuthenticationMiddlewareInterface $authMiddleware = null
) {
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->serializer = $serializer;
$this->responseFactory = $responseFactory;
$this->authMiddleware = $authMiddleware;
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Model/BasicCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
*/
class BasicCredentials
{
public string $username;
public string $password;

public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
public function __construct(
public string $username,
public string $password
) {
}
}
14 changes: 5 additions & 9 deletions src/Model/OAuth2Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@

class OAuth2Credentials
{
public string $tokenType;
public string $accessToken;
public ?DateTimeInterface $expiresAt;

public function __construct(string $tokenType, string $accessToken, DateTimeInterface $expiresAt = null)
{
$this->tokenType = $tokenType;
$this->accessToken = $accessToken;
$this->expiresAt = $expiresAt;
public function __construct(
public string $tokenType,
public string $accessToken,
public ?DateTimeInterface $expiresAt = null
) {
}

public function isExpired(): bool
Expand Down
9 changes: 2 additions & 7 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ class Response implements ResponseInterface
{
private int $status;
private KeyValueCollectionInterface $headers;
private ?DocumentInterface $document;
protected ?PsrResponseInterface $psrResponse;

public function __construct(
PsrResponseInterface $psrResponse,
?DocumentInterface $document = null
protected PsrResponseInterface $psrResponse,
private ?DocumentInterface $document = null
) {
$this->status = $psrResponse->getStatusCode();
$this->headers = new KeyValueCollection();
Expand All @@ -29,9 +27,6 @@ public function __construct(
}
$this->headers->set($header, $value);
}

$this->document = $document;
$this->psrResponse = $psrResponse;
}

public function status(): int
Expand Down
7 changes: 2 additions & 5 deletions src/Response/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

class ResponseFactory implements ResponseFactoryInterface
{
protected DocumentDeserializerInterface $deserializer;

public function __construct(DocumentDeserializerInterface $deserializer)
public function __construct(protected DocumentDeserializerInterface $deserializer)
{
$this->deserializer = $deserializer;
}

/**
Expand All @@ -37,7 +34,7 @@ public function createResponse(
if ($psrResponse->getStatusCode() >= 400) {
try {
$responseDocument = $this->createResponseBody($responseBody);
} catch (BadResponseException $e) {
} catch (BadResponseException) {
$responseDocument = null;
}

Expand Down
17 changes: 4 additions & 13 deletions src/Service/OAuth2Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,12 @@

class OAuth2Authenticator
{
protected ClientInterface $httpClient;
protected RequestFactoryInterface $requestFactory;
protected StreamFactoryInterface $streamFactory;
protected CredentialFactoryInterface $authStorageFactory;

public function __construct(
ClientInterface $httpClient,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
CredentialFactoryInterface $authStorageFactory
protected ClientInterface $httpClient,
protected RequestFactoryInterface $requestFactory,
protected StreamFactoryInterface $streamFactory,
protected CredentialFactoryInterface $authStorageFactory
) {
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->authStorageFactory = $authStorageFactory;
}

/**
Expand Down

0 comments on commit 30af009

Please sign in to comment.