From 57a6a49d2f7c4d705bb1f8e06fc18b3788cd6bce Mon Sep 17 00:00:00 2001 From: Mike Russell <3056352+MichaelJ2324@users.noreply.github.com> Date: Tue, 17 Dec 2024 15:57:06 -0500 Subject: [PATCH] PHP8+ Type Support --- composer.json | 2 +- rector.php | 2 +- src/Auth/Abstracts/AbstractAuthController.php | 56 +++++-------- .../Abstracts/AbstractBasicController.php | 4 +- .../Abstracts/AbstractOAuth2Controller.php | 42 ++++------ src/Auth/AuthControllerInterface.php | 15 ++-- src/Client/AbstractClient.php | 42 +++++----- src/Client/AuthControllerAwareInterface.php | 9 +- src/Client/AuthControllerAwareTrait.php | 11 +-- src/Client/ClientAwareInterface.php | 2 +- src/Client/ClientInterface.php | 23 ++--- src/Client/EndpointProviderAwareInterface.php | 9 +- src/Client/EndpointProviderAwareTrait.php | 13 ++- .../Abstracts/AbstractCollectionEndpoint.php | 23 +++-- src/Endpoint/Abstracts/AbstractEndpoint.php | 84 +++++++------------ .../Abstracts/AbstractModelEndpoint.php | 34 ++++---- .../Abstracts/AbstractSmartEndpoint.php | 33 ++++---- src/Endpoint/Data/AbstractEndpointData.php | 13 ++- src/Endpoint/Event/EventTriggerInterface.php | 3 +- .../Interfaces/ClearableInterface.php | 2 +- .../Interfaces/CollectionInterface.php | 7 +- src/Endpoint/Interfaces/EndpointInterface.php | 15 ++-- src/Endpoint/Interfaces/GetInterface.php | 3 +- src/Endpoint/Interfaces/ModelInterface.php | 6 +- .../Interfaces/PropertiesInterface.php | 8 +- .../Interfaces/ResettableInterface.php | 2 +- src/Endpoint/Interfaces/SetInterface.php | 3 +- .../Provider/AbstractEndpointProvider.php | 11 +-- .../Provider/DefaultEndpointProvider.php | 6 +- .../Provider/EndpointProviderInterface.php | 2 +- src/Endpoint/SmartEndpoint.php | 2 +- .../Traits/ArrayObjectAttributesTrait.php | 10 +-- src/Endpoint/Traits/ClearAttributesTrait.php | 3 +- src/Endpoint/Traits/GetAttributesTrait.php | 4 +- src/Endpoint/Traits/JsonHandlerTrait.php | 2 +- src/Endpoint/Traits/PropertiesTrait.php | 12 +-- .../Traits/ProtectedAttributesTrait.php | 8 ++ src/Endpoint/Traits/SetAttributesTrait.php | 3 +- src/Traits/PsrLoggerTrait.php | 11 +-- src/Traits/PsrSimpleCacheTrait.php | 4 +- tests/Client/AbstractClientTest.php | 6 -- tests/Stubs/Auth/AuthController.php | 4 +- tests/Stubs/Client/Client.php | 2 +- tests/Stubs/Endpoint/AuthEndpoint.php | 4 +- .../CollectionEndpointWithoutModel.php | 2 +- tests/Stubs/Endpoint/LogoutEndpoint.php | 4 +- tests/Stubs/Endpoint/ModelEndpoint.php | 2 +- .../Endpoint/ModelEndpointWithActions.php | 4 +- tests/Stubs/Endpoint/PingEndpoint.php | 4 +- tests/Stubs/Endpoint/RefreshEndpoint.php | 4 +- tests/Stubs/Endpoint/SmartEndpointNoData.php | 2 +- 51 files changed, 244 insertions(+), 338 deletions(-) create mode 100644 src/Endpoint/Traits/ProtectedAttributesTrait.php diff --git a/composer.json b/composer.json index b4c35e1..4187bb3 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "api" ], "require": { - "php": ">=7.4", + "php": ">=8.0", "guzzlehttp/guzzle": ">=6.3.3", "psr/log": ">=1", "psr/simple-cache": "1.*|2.*", diff --git a/rector.php b/rector.php index fc35eeb..5f3c0c8 100644 --- a/rector.php +++ b/rector.php @@ -14,7 +14,7 @@ SimplifyEmptyCheckOnEmptyArrayRector::class, ]) ->withSets([ - SetList::PHP_74, + SetList::PHP_80, SetList::DEAD_CODE, SetList::CODE_QUALITY, SetList::CODING_STYLE, diff --git a/src/Auth/Abstracts/AbstractAuthController.php b/src/Auth/Abstracts/AbstractAuthController.php index cdc2d63..426102d 100644 --- a/src/Auth/Abstracts/AbstractAuthController.php +++ b/src/Auth/Abstracts/AbstractAuthController.php @@ -25,33 +25,28 @@ abstract class AbstractAuthController implements AuthControllerInterface /** * Configured Actions on the Controlller - * @var array */ - protected $actions = []; + protected array $actions = []; /** * Configured Endpoints for configured actions - * @var array */ - protected $endpoints = []; + protected array $endpoints = []; /** * The credentials used for authentication - * @var array */ - protected $credentials = []; + protected array $credentials = []; /** * The authentication token - * @var mixed */ - protected $token; + protected mixed $token; /** * The Cache Key to store the token - * @var string */ - protected $cacheKey; + protected string $cacheKey; public function __construct() { @@ -63,7 +58,7 @@ public function __construct() /** * @inheritdoc */ - public function setCredentials(array $credentials) + public function setCredentials(array $credentials): static { $this->credentials = $credentials; $this->cacheKey = ''; @@ -87,7 +82,7 @@ public function getCacheKey(): string /** * @inheritdoc */ - public function updateCredentials(array $credentials): AuthControllerInterface + public function updateCredentials(array $credentials): static { return $this->setCredentials(array_replace($this->getCredentials(), $credentials)); } @@ -103,7 +98,7 @@ public function getCredentials(): array /** * @inheritDoc */ - public function setToken($token) + public function setToken($token): static { $this->token = $token; $this->cacheToken(); @@ -113,25 +108,25 @@ public function setToken($token) /** * @inheritdoc */ - public function getToken() + public function getToken(): mixed { - return $this->token; + return $this->token ?? null; } /** * Clear the token property to null * @return $this */ - public function clearToken() + public function clearToken(): static { - $this->token = null; + unset($this->token); return $this; } /** * @inheritdoc */ - public function setActions(array $actions) + public function setActions(array $actions): static { $this->actions = $actions; return $this; @@ -148,7 +143,7 @@ public function getActions(): array /** * @inheritdoc */ - public function setActionEndpoint(string $action, EndpointInterface $Endpoint): AuthControllerInterface + public function setActionEndpoint(string $action, EndpointInterface $Endpoint): static { if (in_array($action, $this->actions)) { $this->endpoints[$action] = $Endpoint; @@ -224,7 +219,7 @@ public function logout(): bool /** * @inheritDoc **/ - public function reset(): AuthControllerInterface + public function reset(): static { $this->credentials = []; return $this->clearToken(); @@ -240,9 +235,8 @@ protected function cacheToken(): bool /** * Get the cached token for the Auth Controller - * @return mixed */ - protected function getCachedToken() + protected function getCachedToken(): mixed { return $this->getCache()->get($this->getCacheKey(), null); } @@ -261,16 +255,11 @@ protected function removeCachedToken(): bool */ protected function configureEndpoint(EndpointInterface $Endpoint, $action): EndpointInterface { - switch ($action) { - case self::ACTION_AUTH: - $Endpoint = $this->configureAuthenticationEndpoint($Endpoint); - break; - case self::ACTION_LOGOUT: - $Endpoint = $this->configureLogoutEndpoint($Endpoint); - break; - } - - return $Endpoint; + return match ($action) { + self::ACTION_AUTH => $this->configureAuthenticationEndpoint($Endpoint), + self::ACTION_LOGOUT => $this->configureLogoutEndpoint($Endpoint), + default => $Endpoint, + }; } /** @@ -290,10 +279,9 @@ protected function configureLogoutEndpoint(EndpointInterface $Endpoint): Endpoin /** * Given a response from Authentication endpoint, parse the response * - * @return mixed * @codeCoverageIgnore */ - protected function parseResponseToToken(string $action, Response $response) + protected function parseResponseToToken(string $action, Response $response): mixed { return null; } diff --git a/src/Auth/Abstracts/AbstractBasicController.php b/src/Auth/Abstracts/AbstractBasicController.php index 985beaf..57e4866 100644 --- a/src/Auth/Abstracts/AbstractBasicController.php +++ b/src/Auth/Abstracts/AbstractBasicController.php @@ -14,9 +14,9 @@ abstract class AbstractBasicController extends AbstractAuthController public const DEFAULT_AUTH_TYPE = 'Basic'; - protected static $_AUTH_HEADER = self::DEFAULT_AUTH_HEADER; + protected static string $_AUTH_HEADER = self::DEFAULT_AUTH_HEADER; - protected static $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE; + protected static string $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE; /** * @inheritdoc diff --git a/src/Auth/Abstracts/AbstractOAuth2Controller.php b/src/Auth/Abstracts/AbstractOAuth2Controller.php index 9003942..1239795 100644 --- a/src/Auth/Abstracts/AbstractOAuth2Controller.php +++ b/src/Auth/Abstracts/AbstractOAuth2Controller.php @@ -24,15 +24,12 @@ abstract class AbstractOAuth2Controller extends AbstractBasicController public const OAUTH_REFRESH_GRANT = 'refresh_token'; - /** - * @var string - */ - protected static $_DEFAULT_GRANT_TYPE = self::OAUTH_CLIENT_CREDENTIALS_GRANT; + protected static string $_DEFAULT_GRANT_TYPE = self::OAUTH_CLIENT_CREDENTIALS_GRANT; /** - * @inheritdoc + * The type of OAuth token we are receiving */ - protected static $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE; + protected static string $_AUTH_TYPE = self::DEFAULT_AUTH_TYPE; /** * @inheritdoc @@ -43,12 +40,9 @@ abstract class AbstractOAuth2Controller extends AbstractBasicController * The OAuth2 Full token * @var array */ - protected $token = []; + protected mixed $token; - /** - * @var - */ - protected $grant_type; + protected string $grant_type; public function __construct() { @@ -60,7 +54,7 @@ public function __construct() * @param $grant_type * @return $this */ - public function setGrantType($grant_type): AuthControllerInterface + public function setGrantType(string $grant_type): AuthControllerInterface { $this->grant_type = $grant_type; return $this; @@ -75,7 +69,7 @@ public function getGrantType(): string * Get/Set the OAuth Authorization header * @param $header */ - public static function oauthHeader($header = null): string + public static function oauthHeader(string $header = null): string { if ($header !== null) { static::$_AUTH_HEADER = $header; @@ -88,7 +82,7 @@ public static function oauthHeader($header = null): string * @inheritdoc * @throws InvalidToken */ - public function setToken($token) + public function setToken($token): static { if (is_array($token) && isset($token['access_token'])) { $token = $this->configureToken($token); @@ -127,9 +121,9 @@ protected function configureToken($token) * @param $prop * @return mixed|null */ - public function getTokenProp($prop) + public function getTokenProp(string $prop): mixed { - if ($this->token) { + if (isset($this->token)) { if (is_object($this->token) && $this->token->$prop) { return $this->token->$prop; } elseif (is_array($this->token) && isset($this->token[$prop])) { @@ -236,12 +230,10 @@ protected function isTokenExpired(): int */ protected function configureEndpoint(EndpointInterface $Endpoint, $action): EndpointInterface { - switch ($action) { - case self::ACTION_OAUTH_REFRESH: - return $this->configureRefreshEndpoint($Endpoint); - default: - return parent::configureEndpoint($Endpoint, $action); - } + return match ($action) { + self::ACTION_OAUTH_REFRESH => $this->configureRefreshEndpoint($Endpoint), + default => parent::configureEndpoint($Endpoint, $action), + }; } /** @@ -271,7 +263,7 @@ protected function configureAuthenticationEndpoint(EndpointInterface $Endpoint): /** * @inheritDoc */ - public function reset(): AuthControllerInterface + public function reset(): static { $this->setGrantType(static::$_DEFAULT_GRANT_TYPE); return parent::reset(); @@ -279,10 +271,8 @@ public function reset(): AuthControllerInterface /** * Parse token responses as string - * - * @return mixed */ - protected function parseResponseToToken(string $action, Response $response) + protected function parseResponseToToken(string $action, Response $response): mixed { $tokenStr = $response->getBody()->getContents(); $response->getBody()->rewind(); diff --git a/src/Auth/AuthControllerInterface.php b/src/Auth/AuthControllerInterface.php index ec7a46d..addddfd 100644 --- a/src/Auth/AuthControllerInterface.php +++ b/src/Auth/AuthControllerInterface.php @@ -16,25 +16,25 @@ public function getCredentials(): array; * Set the credentials used for authentication * @return $this */ - public function setCredentials(array $credentials); + public function setCredentials(array $credentials): static; /** * Update parts of credentials used for authentication * @return $this */ - public function updateCredentials(array $credentials); + public function updateCredentials(array $credentials): static; /** * @return $this */ - public function setActions(array $actions); + public function setActions(array $actions): static; public function getActions(): array; /** * @return $this */ - public function setActionEndpoint(string $action, EndpointInterface $Endpoint); + public function setActionEndpoint(string $action, EndpointInterface $Endpoint): static; /** * Get the Endpoint configured for an action @@ -62,7 +62,7 @@ public function logout(): bool; * Reset the auth controller to default state. Does not call 'logout' but does clear current token/credentials * @return $this */ - public function reset(); + public function reset(): static; /** * Is currently authenticated @@ -74,11 +74,10 @@ public function isAuthenticated(): bool; * @param $token mixed * @return $this */ - public function setToken($token); + public function setToken(mixed $token): static; /** * Get the current token on the Auth Controller - * @return mixed */ - public function getToken(); + public function getToken(): mixed; } diff --git a/src/Client/AbstractClient.php b/src/Client/AbstractClient.php index 5b5ab26..e96667f 100644 --- a/src/Client/AbstractClient.php +++ b/src/Client/AbstractClient.php @@ -103,7 +103,7 @@ public function setHandlerStack(HandlerStack $stackHandler) { $this->guzzleHandlerStack = $stackHandler; $this->initHttpClient(); - if ($this->Auth) { + if (isset($this->auth)) { $this->configureAuth(); } @@ -122,7 +122,7 @@ protected function configureAuth() $Auth = $api->getAuth(); if ($Auth) { $EP = $api->current(); - if (!$EP || ($EP && $EP->useAuth())) { + if ($EP && $EP->useAuth()) { return $Auth->configureRequest($request); } } @@ -134,7 +134,7 @@ protected function configureAuth() /** * @inheritdoc */ - public function setServer($server) + public function setServer($server): static { $this->server = $server; $this->setAPIUrl(); @@ -144,15 +144,15 @@ public function setServer($server) /** * @inheritdoc */ - public function getServer() + public function getServer(): string { - return $this->server; + return $this->server ?? ""; } /** * @inheritdoc */ - protected function setAPIUrl() + protected function setAPIUrl(): static { $this->apiURL = $this->server; return $this; @@ -161,15 +161,15 @@ protected function setAPIUrl() /** * @inheritdoc */ - public function getAPIUrl() + public function getAPIUrl(): string { - return $this->apiURL; + return $this->apiURL ?? ""; } /** * @inheritdoc */ - public function setVersion($version) + public function setVersion(string $version): static { $this->version = $version; $this->setAPIUrl(); @@ -179,15 +179,15 @@ public function setVersion($version) /** * @inheritdoc */ - public function getVersion() + public function getVersion(): string { - return $this->version; + return $this->version ?? ""; } /** * @inheritdoc */ - public function current() + public function current(): EndpointInterface { return $this->currentEndPoint; } @@ -195,7 +195,7 @@ public function current() /** * @inheritdoc */ - public function last() + public function last(): EndpointInterface { return $this->lastEndPoint; } @@ -205,16 +205,16 @@ public function last() */ public function __call(string $name, $arguments) { - $provider = $this->getEndpointProvider(); - if ($provider) { - $this->setCurrentEndpoint($provider->getEndpoint($name, $this->version)) - ->current() - ->setClient($this) - ->setUrlArgs($arguments); - return $this->currentEndPoint; - } else { + if (!isset($this->endpointProvider)){ throw new EndpointProviderMissing(); } + + $provider = $this->getEndpointProvider(); + $this->setCurrentEndpoint($provider->getEndpoint($name, $this->version)) + ->current() + ->setClient($this) + ->setUrlArgs($arguments); + return $this->currentEndPoint; } /** diff --git a/src/Client/AuthControllerAwareInterface.php b/src/Client/AuthControllerAwareInterface.php index 09a1b21..a0aedde 100644 --- a/src/Client/AuthControllerAwareInterface.php +++ b/src/Client/AuthControllerAwareInterface.php @@ -10,11 +10,8 @@ interface AuthControllerAwareInterface * Set the Auth Controller that handles Auth for the API * @return $this */ - public function setAuth(AuthControllerInterface $Auth); + public function setAuth(AuthControllerInterface $Auth): static; - /** - * - * @return AuthControllerInterface - */ - public function getAuth(); + + public function getAuth(): AuthControllerInterface; } diff --git a/src/Client/AuthControllerAwareTrait.php b/src/Client/AuthControllerAwareTrait.php index b26c9bb..0187c3f 100644 --- a/src/Client/AuthControllerAwareTrait.php +++ b/src/Client/AuthControllerAwareTrait.php @@ -6,17 +6,14 @@ trait AuthControllerAwareTrait { - /** - * @var AuthControllerInterface - */ - protected $Auth; + protected AuthControllerInterface $auth; /** * @inheritdoc */ - public function setAuth(AuthControllerInterface $Auth) + public function setAuth(AuthControllerInterface $auth): static { - $this->Auth = $Auth; + $this->auth = $auth; $this->configureAuth(); return $this; } @@ -31,6 +28,6 @@ abstract protected function configureAuth(); */ public function getAuth(): AuthControllerInterface { - return $this->Auth; + return $this->auth; } } diff --git a/src/Client/ClientAwareInterface.php b/src/Client/ClientAwareInterface.php index 490074e..3b63a38 100644 --- a/src/Client/ClientAwareInterface.php +++ b/src/Client/ClientAwareInterface.php @@ -9,5 +9,5 @@ public function getClient(): ClientInterface; /** * @return $this */ - public function setClient(ClientInterface $client); + public function setClient(ClientInterface $client): static; } diff --git a/src/Client/ClientInterface.php b/src/Client/ClientInterface.php index 3e8af72..9451ca9 100644 --- a/src/Client/ClientInterface.php +++ b/src/Client/ClientInterface.php @@ -7,52 +7,45 @@ interface ClientInterface { - /** - * @return Client - */ - public function getHttpClient(); + public function getHttpClient(): Client; /** * Set the server on the Client, and configure the API Url if necessary * @param $server * @return $this */ - public function setServer($server); + public function setServer(string $server): static; /** * Get the server configured on SDK Client - * @return mixed */ - public function getServer(); + public function getServer(): string; /** * Get the configured API Url on the SDK Client - * @return string */ - public function getAPIUrl(); + public function getAPIUrl(): string; /** * Set the API Version to use * @param $version * @return $this */ - public function setVersion($version); + public function setVersion(string $version): static; /** * Set the Client API Version that is to be used * @return string $version */ - public function getVersion(); + public function getVersion(): string; /** * Get the Endpoint currently being used - * @return EndpointInterface */ - public function current(); + public function current(): EndpointInterface; /** * Get the last Endpoint Used - * @return EndpointInterface */ - public function last(); + public function last(): EndpointInterface; } diff --git a/src/Client/EndpointProviderAwareInterface.php b/src/Client/EndpointProviderAwareInterface.php index 3040107..197f8d1 100644 --- a/src/Client/EndpointProviderAwareInterface.php +++ b/src/Client/EndpointProviderAwareInterface.php @@ -10,11 +10,8 @@ interface EndpointProviderAwareInterface * Set the Endpoint Provider that is to be used by the REST Client * @return $this */ - public function setEndpointProvider(EndpointProviderInterface $EndpointProvider); + public function setEndpointProvider(EndpointProviderInterface $EndpointProvider): static; - /** - * - * @return EndpointProviderInterface - */ - public function getEndpointProvider(); + + public function getEndpointProvider(): EndpointProviderInterface; } diff --git a/src/Client/EndpointProviderAwareTrait.php b/src/Client/EndpointProviderAwareTrait.php index cf068f8..95769ad 100644 --- a/src/Client/EndpointProviderAwareTrait.php +++ b/src/Client/EndpointProviderAwareTrait.php @@ -6,18 +6,15 @@ trait EndpointProviderAwareTrait { - /** - * @var EndpointProviderInterface - */ - protected $EndpointProvider; + protected EndpointProviderInterface $endpointProvider; /** * @inheritdoc * @implements EndpointProviderAwareInterface */ - public function setEndpointProvider(EndpointProviderInterface $EndpointProvider) + public function setEndpointProvider(EndpointProviderInterface $endpointProvider): static { - $this->EndpointProvider = $EndpointProvider; + $this->endpointProvider = $endpointProvider; return $this; } @@ -25,8 +22,8 @@ public function setEndpointProvider(EndpointProviderInterface $EndpointProvider) * @inheritdoc * @implements EndpointProviderAwareInterface */ - public function getEndpointProvider() + public function getEndpointProvider(): EndpointProviderInterface { - return $this->EndpointProvider; + return $this->endpointProvider; } } diff --git a/src/Endpoint/Abstracts/AbstractCollectionEndpoint.php b/src/Endpoint/Abstracts/AbstractCollectionEndpoint.php index 8f17bc5..4390ce7 100644 --- a/src/Endpoint/Abstracts/AbstractCollectionEndpoint.php +++ b/src/Endpoint/Abstracts/AbstractCollectionEndpoint.php @@ -6,7 +6,6 @@ use GuzzleHttp\Psr7\Response; use MRussell\REST\Endpoint\Data\DataInterface; use MRussell\REST\Endpoint\Interfaces\CollectionInterface; -use MRussell\REST\Endpoint\Interfaces\EndpointInterface; use MRussell\REST\Endpoint\Interfaces\ModelInterface; use MRussell\REST\Endpoint\Traits\ParseResponseBodyToArrayTrait; use MRussell\REST\Exception\Endpoint\UnknownEndpoint; @@ -59,7 +58,7 @@ abstract class AbstractCollectionEndpoint extends AbstractSmartEndpoint implemen * @param mixed $value - The value to set * @abstracting ArrayAccess */ - public function offsetSet($offset, $value): void + public function offsetSet($offset, mixed $value): void { if (is_null($offset)) { $this->models[] = $value; @@ -114,7 +113,7 @@ public function toArray(): array * @return $this * @implements ResettableInterface */ - public function reset() + public function reset(): static { parent::reset(); return $this->clear(); @@ -125,7 +124,7 @@ public function reset() * @return $this * @implements ClearableInterface */ - public function clear() + public function clear(): static { $this->models = []; return $this; @@ -183,7 +182,7 @@ public function valid(): bool * @inheritdoc * @throws InvalidRequest */ - public function fetch() + public function fetch(): static { $this->setProperty(self::PROPERTY_HTTP_METHOD, "GET"); return $this->execute(); @@ -192,11 +191,11 @@ public function fetch() /** * @inheritdoc */ - public function get($id) + public function get(string|int $key): ModelInterface|array|\ArrayAccess|null { $data = null; - if ($this->offsetExists($id)) { - $data = $this->models[$id]; + if ($this->offsetExists($key)) { + $data = $this->models[$key]; $Model = $this->buildModel($data); if ($Model !== null) { $data = $Model; @@ -288,19 +287,19 @@ public function length(): int * @inheritdoc * @throws UnknownEndpoint */ - public function setModelEndpoint($model): CollectionInterface + public function setModelEndpoint($model): static { try { $implements = class_implements($model); if (is_array($implements) && isset($implements[ModelInterface::class])) { if (is_object($model)) { - $model = get_class($model); + $model = $model::class; } $this->setProperty(self::PROPERTY_MODEL_CLASS, $model); return $this; } - } catch (\Exception $exception) { + } catch (\Exception) { //If class_implements cannot load class } @@ -327,7 +326,7 @@ public function getEndPointUrl($full = false): string return $epURL; } - protected function setResponse(Response $response): EndpointInterface + protected function setResponse(Response $response): static { parent::setResponse($response); $this->parseResponse($response); diff --git a/src/Endpoint/Abstracts/AbstractEndpoint.php b/src/Endpoint/Abstracts/AbstractEndpoint.php index 5743154..e02eebc 100644 --- a/src/Endpoint/Abstracts/AbstractEndpoint.php +++ b/src/Endpoint/Abstracts/AbstractEndpoint.php @@ -7,7 +7,6 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\InvalidArgumentException; use GuzzleHttp\Exception\RequestException; -use GuzzleHttp\Promise\Promise; use GuzzleHttp\Psr7\Stream; use GuzzleHttp\Psr7\Utils; use MRussell\REST\Client\ClientAwareTrait; @@ -57,52 +56,45 @@ abstract class AbstractEndpoint implements EndpointInterface, EventTriggerInterf public const AUTH_REQUIRED = 2; - protected static $_DEFAULT_PROPERTIES = [self::PROPERTY_URL => '', self::PROPERTY_HTTP_METHOD => '', self::PROPERTY_AUTH => self::AUTH_EITHER]; + protected static array $_DEFAULT_PROPERTIES = [self::PROPERTY_URL => '', self::PROPERTY_HTTP_METHOD => '', self::PROPERTY_AUTH => self::AUTH_EITHER]; - private ?PromiseInterface $promise = null; + private PromiseInterface $promise; /** * The Variable Identifier to parse Endpoint URL - * @var string */ - protected static $_URL_VAR_CHARACTER = '$'; + protected static string $_URL_VAR_CHARACTER = '$'; /** * The Endpoint Relative URL to the API - * @var string */ - protected static $_ENDPOINT_URL = ''; + protected static string $_ENDPOINT_URL = ''; /** * The initial URL passed into the Endpoint - * @var string */ - protected $baseUrl = ''; + protected string $baseUrl = ''; /** * The passed in Options for the Endpoint, mainly used for parsing URL Variables - * @var array */ - protected $urlArgs = []; + protected array $urlArgs = []; /** * The data being passed to the API Endpoint. * Defaults to Array, but can be mixed based on how you want to use Endpoint. - * @var mixed */ - protected $data; + protected string|array|\ArrayAccess|null $data; /** * The Request Object used by the Endpoint to submit the data - * @var Request */ - protected $request; + protected Request $request; /** * The Response Object used by the Endpoint - * @var Response */ - protected $response; + protected Response $response; public function __construct(array $urlArgs = [], array $properties = []) { @@ -121,7 +113,7 @@ public function __construct(array $urlArgs = [], array $properties = []) /** * @inheritdoc */ - public function setUrlArgs(array $args): EndpointInterface + public function setUrlArgs(array $args): static { $this->urlArgs = $args; return $this; @@ -138,7 +130,7 @@ public function getUrlArgs(): array /** * @inheritdoc */ - public function setBaseUrl($url): EndpointInterface + public function setBaseUrl($url): static { $this->baseUrl = $url; return $this; @@ -159,10 +151,10 @@ public function getBaseUrl(): string /** * @inheritdoc */ - public function getEndPointUrl($full = false): string + public function getEndPointUrl(bool $full = false): string { $url = static::$_ENDPOINT_URL; - if (isset($this->_properties[self::PROPERTY_URL]) && $this->_properties[self::PROPERTY_URL] !== '') { + if (!empty($this->_properties[self::PROPERTY_URL])) { $url = $this->_properties[self::PROPERTY_URL]; } @@ -176,7 +168,7 @@ public function getEndPointUrl($full = false): string /** * @inheritdoc */ - public function setData($data) + public function setData(string|array|\ArrayAccess|null $data): static { $this->data = $data; return $this; @@ -185,28 +177,15 @@ public function setData($data) /** * @inheritdoc */ - public function getData() + public function getData(): string|array|\ArrayAccess|null { - return $this->data; - } - - /** - * Due to how Guzzle Requests work, this may not return the actual Request object used - * - Use Middleware::history() if you need the request that was sent to server - * - * May deprecate in the future, just leaving it in right now to assess if its still needed - * TODO:Deprecate me - * @codeCoverageIgnore - */ - protected function getRequest(): Request - { - return $this->request; + return $this->data ?? null; } /** * @return $this|EndpointInterface */ - protected function setResponse(Response $response) + protected function setResponse(Response $response): static { $this->response = $response; $this->respContent = null; @@ -217,7 +196,7 @@ protected function setResponse(Response $response) /** * @inheritdoc */ - public function getResponse() + public function getResponse(): Response { return $this->response; } @@ -243,7 +222,7 @@ public function getHttpClient(): Client * @return $this * @throws GuzzleException */ - public function execute(array $options = []): EndpointInterface + public function execute(array $options = []): static { $this->setResponse($this->getHttpClient()->send($this->buildRequest(), $options)); return $this; @@ -275,12 +254,9 @@ function (RequestException $e) use ($options): void { return $this; } - /** - * @return Promise - */ - public function getPromise() + public function getPromise(): ?PromiseInterface { - return $this->promise; + return $this->promise ?? null; } /** @@ -399,8 +375,8 @@ protected function configureURL(array $urlArgs): string foreach ($urlArr as $key => $urlPart) { $replace = null; - if (strpos($urlPart, static::$_URL_VAR_CHARACTER) !== false) { - if (strpos($urlPart, ':') !== false) { + if (str_contains($urlPart, static::$_URL_VAR_CHARACTER)) { + if (str_contains($urlPart, ':')) { $optional = true; $replace = ''; } @@ -439,8 +415,8 @@ protected function configureURL(array $urlArgs): string */ private function verifyUrl(string $url): bool { - if (strpos($url, static::$_URL_VAR_CHARACTER) !== false) { - throw new InvalidUrl([get_class($this), $url]); + if (str_contains($url, static::$_URL_VAR_CHARACTER)) { + throw new InvalidUrl([static::class, $url]); } return true; @@ -463,7 +439,7 @@ protected function hasUrlArgs(): bool protected function extractUrlVariables($url): array { $variables = []; - $pattern = "/(\\" . static::$_URL_VAR_CHARACTER . ".*?[^\\/]*)/"; + $pattern = "/(" . preg_quote(static::$_URL_VAR_CHARACTER) . ".*?[^\\/]*)/"; if (preg_match_all($pattern, $url, $matches)) { foreach ($matches as $match) { $variables[] = $match[0]; @@ -476,10 +452,10 @@ protected function extractUrlVariables($url): array /** * @return $this */ - public function reset() + public function reset(): static { - $this->request = null; - $this->response = null; + unset($this->request); + unset($this->response); $this->urlArgs = []; $this->setData(null); $this->setProperties([]); @@ -489,7 +465,7 @@ public function reset() /** * @inheritdoc */ - public function setProperties(array $properties) + public function setProperties(array $properties): static { if (!isset($properties[self::PROPERTY_HTTP_METHOD])) { $properties[self::PROPERTY_HTTP_METHOD] = ''; diff --git a/src/Endpoint/Abstracts/AbstractModelEndpoint.php b/src/Endpoint/Abstracts/AbstractModelEndpoint.php index 20b0245..cbdc03c 100644 --- a/src/Endpoint/Abstracts/AbstractModelEndpoint.php +++ b/src/Endpoint/Abstracts/AbstractModelEndpoint.php @@ -6,7 +6,6 @@ use GuzzleHttp\Psr7\Response; use MRussell\REST\Endpoint\Data\AbstractEndpointData; use MRussell\REST\Endpoint\Data\DataInterface; -use MRussell\REST\Endpoint\Interfaces\EndpointInterface; use MRussell\REST\Endpoint\Interfaces\ModelInterface; use MRussell\REST\Endpoint\Traits\ArrayObjectAttributesTrait; use MRussell\REST\Endpoint\Traits\ClearAttributesTrait; @@ -58,33 +57,28 @@ abstract class AbstractModelEndpoint extends AbstractSmartEndpoint implements Mo /** * The ID Field used by the Model - * @var string */ - protected static $_MODEL_ID_KEY = 'id'; + protected static string $_MODEL_ID_KEY = 'id'; /** * The response property where the model data is located - * @var string */ - protected static $_RESPONSE_PROP = ''; + protected static string $_RESPONSE_PROP = ''; /** * List of actions - * @var array */ - protected static $_DEFAULT_ACTIONS = [self::MODEL_ACTION_CREATE => 'POST', self::MODEL_ACTION_RETRIEVE => 'GET', self::MODEL_ACTION_UPDATE => 'PUT', self::MODEL_ACTION_DELETE => 'DELETE']; + protected static array $_DEFAULT_ACTIONS = [self::MODEL_ACTION_CREATE => 'POST', self::MODEL_ACTION_RETRIEVE => 'GET', self::MODEL_ACTION_UPDATE => 'PUT', self::MODEL_ACTION_DELETE => 'DELETE']; /** * List of available actions and their associated Request Method - * @var array */ - protected $actions = []; + protected array $actions = []; /** * Current action being executed - * @var string */ - protected $action = self::MODEL_ACTION_RETRIEVE; + protected string $action = self::MODEL_ACTION_RETRIEVE; //Static public static function modelIdKey($id = null): string @@ -111,13 +105,13 @@ public function __call($name, $arguments) return $this->setCurrentAction($name, $arguments)->execute(); } - throw new UnknownModelAction([get_class($this), $name]); + throw new UnknownModelAction([static::class, $name]); } /** * @inheritdoc */ - public function reset() + public function reset(): static { parent::reset(); return $this->clear(); @@ -127,7 +121,7 @@ public function reset() * @inheritdoc * @throws InvalidRequest */ - public function retrieve($id = null): ModelInterface + public function retrieve($id = null): static { $this->setCurrentAction(self::MODEL_ACTION_RETRIEVE); $idKey = static::modelIdKey(); @@ -138,7 +132,7 @@ public function retrieve($id = null): ModelInterface $this->set($idKey, $id); } elseif (!isset($this->_attributes[$idKey])) { - throw new MissingModelId([$this->action, get_class($this)]); + throw new MissingModelId([$this->action, static::class]); } $this->triggerEvent(self::EVENT_BEFORE_RETRIEVE); @@ -151,7 +145,7 @@ public function retrieve($id = null): ModelInterface * @inheritdoc * @throws InvalidRequest */ - public function save(): ModelInterface + public function save(): static { if (isset($this->_attributes[static::modelIdKey()])) { $this->setCurrentAction(self::MODEL_ACTION_UPDATE); @@ -168,7 +162,7 @@ public function save(): ModelInterface /** * @inheritdoc */ - public function delete(): ModelInterface + public function delete(): static { $this->setCurrentAction(self::MODEL_ACTION_DELETE); $this->triggerEvent(self::EVENT_BEFORE_DELETE); @@ -180,7 +174,7 @@ public function delete(): ModelInterface /** * Set the current action taking place on the Model */ - public function setCurrentAction(string $action, array $actionArgs = []): AbstractModelEndpoint + public function setCurrentAction(string $action, array $actionArgs = []): static { if (array_key_exists($action, $this->actions)) { $this->action = $action; @@ -203,7 +197,7 @@ public function getCurrentAction(): string * - Called when setting the Current Action * @param $action */ - protected function configureAction($action, array $arguments = []) + protected function configureAction(string $action, array $arguments = []): void { $this->setProperty(self::PROPERTY_HTTP_METHOD, $this->actions[$action]); } @@ -231,7 +225,7 @@ protected function configurePayload() return $data; } - protected function setResponse(Response $response): EndpointInterface + protected function setResponse(Response $response): static { parent::setResponse($response); $this->parseResponse($response); diff --git a/src/Endpoint/Abstracts/AbstractSmartEndpoint.php b/src/Endpoint/Abstracts/AbstractSmartEndpoint.php index dddc9f3..ae1c707 100644 --- a/src/Endpoint/Abstracts/AbstractSmartEndpoint.php +++ b/src/Endpoint/Abstracts/AbstractSmartEndpoint.php @@ -6,7 +6,6 @@ use MRussell\REST\Endpoint\Data\AbstractEndpointData; use MRussell\REST\Endpoint\Data\DataInterface; use MRussell\REST\Endpoint\Data\EndpointData; -use MRussell\REST\Endpoint\Interfaces\EndpointInterface; use MRussell\REST\Exception\Endpoint\InvalidData; use MRussell\REST\Exception\Endpoint\InvalidDataType; @@ -17,16 +16,15 @@ abstract class AbstractSmartEndpoint extends AbstractEndpoint /** * @inheritdoc */ - protected static $_DEFAULT_PROPERTIES = [self::PROPERTY_URL => '', self::PROPERTY_HTTP_METHOD => '', self::PROPERTY_AUTH => false, self::PROPERTY_DATA => ['required' => [], 'defaults' => []]]; + protected static array $_DEFAULT_PROPERTIES = [self::PROPERTY_URL => '', self::PROPERTY_HTTP_METHOD => '', self::PROPERTY_AUTH => false, self::PROPERTY_DATA => ['required' => [], 'defaults' => []]]; - protected static $_DATA_CLASS = EndpointData::class; + protected static string $_DATA_CLASS = EndpointData::class; /** * The data being passed to the API Endpoint. * Uses the DataInterface to provide a more robust way of configuring data and an automation API - * @var DataInterface */ - protected $data; + protected string|array|\ArrayAccess|null $data; public function __construct(array $urlArgs = [], array $properties = []) { @@ -39,7 +37,7 @@ public function __construct(array $urlArgs = [], array $properties = []) * Passes through the data properties on the Data Object * @return $this */ - public function setProperties(array $properties) + public function setProperties(array $properties): static { if (!isset($properties[self::PROPERTY_DATA])) { $properties[self::PROPERTY_DATA] = [ @@ -56,7 +54,7 @@ public function setProperties(array $properties) /** * @inheritdoc */ - public function setProperty(string $name, $value) + public function setProperty(string $name, $value): static { parent::setProperty($name, $value); if ($name === self::PROPERTY_DATA && isset($this->data)) { @@ -69,7 +67,7 @@ public function setProperty(string $name, $value) /** * @inheritdoc */ - public function setData($data): EndpointInterface + public function setData(string|array|\ArrayAccess|null $data): static { if ($data instanceof AbstractEndpointData) { $this->data = $data; @@ -79,7 +77,7 @@ public function setData($data): EndpointInterface } elseif (is_null($data)) { $this->data = $this->buildDataObject(); } else { - throw new InvalidDataType(get_class($this)); + throw new InvalidDataType(static::class); } return $this; @@ -87,22 +85,27 @@ public function setData($data): EndpointInterface /** * Get the current data object, or build out a new one if one is not set - * @return DataInterface */ - public function getData() + public function getData(): DataInterface { - if (!$this->data) { + if (!isset($this->data)) { $this->data = $this->buildDataObject(); } - return parent::getData(); + $data = parent::getData(); + if (!($data instanceof DataInterface)) { + $di = $this->buildDataObject(); + $this->data = $di->set($data); + } + + return $this->data; } /** * Passes Data properties to Endpoint Data object * @return $this */ - protected function configureDataProperties(): EndpointInterface + protected function configureDataProperties(): static { $dataProps = $this->getProperty(self::PROPERTY_DATA); if (!empty($dataProps)) { @@ -134,7 +137,7 @@ protected function configureRequest(Request $request, $data): Request * Reset data * @return $this */ - public function reset() + public function reset(): static { $this->getData()->reset(); return parent::reset(); diff --git a/src/Endpoint/Data/AbstractEndpointData.php b/src/Endpoint/Data/AbstractEndpointData.php index 0bbdcf0..4d0e124 100644 --- a/src/Endpoint/Data/AbstractEndpointData.php +++ b/src/Endpoint/Data/AbstractEndpointData.php @@ -60,7 +60,7 @@ public function __set($key, $value) * @param mixed $value - The value to set * @abstracting ArrayAccess */ - public function offsetSet($offset, $value): void + public function offsetSet($offset, mixed $value): void { $this->isNull = false; if (is_null($offset)) { @@ -75,7 +75,7 @@ public function offsetSet($offset, $value): void * @param $value * @return $this */ - public function set($key, $value = null) + public function set($key, $value = null): static { if ((is_array($key) && !empty($key)) || !is_array($key)) { $this->isNull = false; @@ -87,7 +87,7 @@ public function set($key, $value = null) /** * Set properties for data */ - public function setProperties(array $properties): void + public function setProperties(array $properties): static { if (!isset($properties[self::DATA_PROPERTY_REQUIRED])) { $properties[self::DATA_PROPERTY_REQUIRED] = []; @@ -97,15 +97,14 @@ public function setProperties(array $properties): void $properties[self::DATA_PROPERTY_DEFAULTS] = []; } - $this->rawSetProperties($properties); + return $this->rawSetProperties($properties); } /** * Set Data back to Defaults and clear out data - * @return AbstractEndpointData * @implements ResettableInterface */ - public function reset(): DataInterface + public function reset(): static { $this->setProperties(static::$_DEFAULT_PROPERTIES); $this->null(); @@ -116,7 +115,7 @@ public function reset(): DataInterface * Set data to null * @return $this */ - public function null() + public function null(): static { $this->clear(); $this->isNull = true; diff --git a/src/Endpoint/Event/EventTriggerInterface.php b/src/Endpoint/Event/EventTriggerInterface.php index 0e3d3b8..d19efee 100644 --- a/src/Endpoint/Event/EventTriggerInterface.php +++ b/src/Endpoint/Event/EventTriggerInterface.php @@ -6,9 +6,8 @@ interface EventTriggerInterface { /** * Trigger a specific event to run - * @param mixed $data */ - public function triggerEvent(string $event, &$data = null): void; + public function triggerEvent(string $event, mixed &$data = null): void; /** * Register a function to run when event is triggered diff --git a/src/Endpoint/Interfaces/ClearableInterface.php b/src/Endpoint/Interfaces/ClearableInterface.php index c792199..334e8f5 100644 --- a/src/Endpoint/Interfaces/ClearableInterface.php +++ b/src/Endpoint/Interfaces/ClearableInterface.php @@ -8,5 +8,5 @@ interface ClearableInterface * Clear an object of data * @return $this */ - public function clear(); + public function clear(): static; } diff --git a/src/Endpoint/Interfaces/CollectionInterface.php b/src/Endpoint/Interfaces/CollectionInterface.php index 08308af..d2b64e8 100644 --- a/src/Endpoint/Interfaces/CollectionInterface.php +++ b/src/Endpoint/Interfaces/CollectionInterface.php @@ -8,21 +8,20 @@ interface CollectionInterface extends EndpointInterface, ClearableInterface, Get * Retrieve the Endpoint Collection * @return $this */ - public function fetch(); + public function fetch(): static; /** * Set the Model Endpoint * @param mixed $model * @return $this */ - public function setModelEndpoint($model); + public function setModelEndpoint(ModelInterface $model): static; /** * Get a Model Endpoint based on Id * @param $id - * @return ModelInterface|null */ - public function get($id); + public function get(string|int $key): ModelInterface|array|\ArrayAccess|null; /** * Get a Model Endpoint based on numerical index diff --git a/src/Endpoint/Interfaces/EndpointInterface.php b/src/Endpoint/Interfaces/EndpointInterface.php index 942adc2..c7d6abd 100644 --- a/src/Endpoint/Interfaces/EndpointInterface.php +++ b/src/Endpoint/Interfaces/EndpointInterface.php @@ -10,7 +10,7 @@ interface EndpointInterface extends PropertiesInterface, ResettableInterface * Set the urlArgs property to configure the URL variables * @return $this */ - public function setUrlArgs(array $args); + public function setUrlArgs(array $args): static; /** * Get the configured Url Arguments @@ -19,22 +19,20 @@ public function getUrlArgs(): array; /** * Sets the data on the Endpoint Object, that will be passed to Request Object - * @param mixed $data * @return $this */ - public function setData($data); + public function setData(string|array|\ArrayAccess|null $data): static; /** * Get the data being used by the Endpoint - * @return array|\ArrayAccess */ - public function getData(); + public function getData(): string|array|\ArrayAccess|null; /** * Set the Base URL that the Endpoint uses in regards to it's pre-configured Endpoint URL * @return $this */ - public function setBaseUrl(string $url); + public function setBaseUrl(string $url): static; /** * Get the Base URL that is currently configured on the Endpoint @@ -50,13 +48,12 @@ public function getEndPointUrl(): string; * Execute the Endpoint Object using the desired action * @return $this */ - public function execute(); + public function execute(): static; /** * Get the Response Object being used by the Endpoint - * @return Response */ - public function getResponse(); + public function getResponse(): Response; /** * Check if authentication should be applied diff --git a/src/Endpoint/Interfaces/GetInterface.php b/src/Endpoint/Interfaces/GetInterface.php index 2b20617..68313a5 100644 --- a/src/Endpoint/Interfaces/GetInterface.php +++ b/src/Endpoint/Interfaces/GetInterface.php @@ -10,7 +10,6 @@ interface GetInterface /** * Get a specific attribute from the object * @param $key - * @return mixed */ - public function get($key); + public function get(string|int $key): mixed; } diff --git a/src/Endpoint/Interfaces/ModelInterface.php b/src/Endpoint/Interfaces/ModelInterface.php index 986b8e8..41f3165 100644 --- a/src/Endpoint/Interfaces/ModelInterface.php +++ b/src/Endpoint/Interfaces/ModelInterface.php @@ -14,7 +14,7 @@ public static function modelIdKey($id = null): string; * @param $id * @return $this */ - public function retrieve($id = null): self; + public function retrieve($id = null): static; /** * Save the current Model @@ -22,10 +22,10 @@ public function retrieve($id = null): self; * - Uses a PUT request if Model ID is set * @return $this */ - public function save(): self; + public function save(): static; /** * Delete the current Model using a DELETE Request */ - public function delete(): self; + public function delete(): static; } diff --git a/src/Endpoint/Interfaces/PropertiesInterface.php b/src/Endpoint/Interfaces/PropertiesInterface.php index 74ab043..05c2a19 100644 --- a/src/Endpoint/Interfaces/PropertiesInterface.php +++ b/src/Endpoint/Interfaces/PropertiesInterface.php @@ -11,14 +11,13 @@ interface PropertiesInterface * Set the properties that control the object * @return $this */ - public function setProperties(array $properties); + public function setProperties(array $properties): static; /** * Set a specific property on an object - * @param mixed $value * @return $this */ - public function setProperty(string $name, $value); + public function setProperty(string $name, mixed $value): static; /** * Get the properties configured on the Data @@ -27,7 +26,6 @@ public function getProperties(): array; /** * Get the properties configured on the Data - * @return mixed */ - public function getProperty(string $name); + public function getProperty(string $name): mixed; } diff --git a/src/Endpoint/Interfaces/ResettableInterface.php b/src/Endpoint/Interfaces/ResettableInterface.php index d90c7e5..b446b74 100644 --- a/src/Endpoint/Interfaces/ResettableInterface.php +++ b/src/Endpoint/Interfaces/ResettableInterface.php @@ -8,5 +8,5 @@ interface ResettableInterface * Reset Object back to initial state * @return $this */ - public function reset(); + public function reset(): static; } diff --git a/src/Endpoint/Interfaces/SetInterface.php b/src/Endpoint/Interfaces/SetInterface.php index d716fb0..8487ad2 100644 --- a/src/Endpoint/Interfaces/SetInterface.php +++ b/src/Endpoint/Interfaces/SetInterface.php @@ -10,8 +10,7 @@ interface SetInterface /** * Set a property or multiple attributes on an object * @param string|array $key - * @param mixed $value * @return $this */ - public function set($key, $value = null); + public function set(string|array|\ArrayAccess $key, mixed $value = null): static; } diff --git a/src/Endpoint/Provider/AbstractEndpointProvider.php b/src/Endpoint/Provider/AbstractEndpointProvider.php index a97ccaf..058a263 100644 --- a/src/Endpoint/Provider/AbstractEndpointProvider.php +++ b/src/Endpoint/Provider/AbstractEndpointProvider.php @@ -8,16 +8,13 @@ abstract class AbstractEndpointProvider implements EndpointProviderInterface { - /** - * @var array - */ - protected $registry = []; + protected array $registry = []; /** * @inheritdoc * @throws InvalidRegistration */ - public function registerEndpoint($name, $className, array $properties = []): EndpointProviderInterface + public function registerEndpoint($name, $className, array $properties = []): static { try { $implements = class_implements($className); @@ -25,7 +22,7 @@ public function registerEndpoint($name, $className, array $properties = []): End $this->registry[$name] = ['class' => $className, 'properties' => $properties]; return $this; } - } catch (\Exception $exception) { + } catch (\Exception) { //Class Implements failed to Load Class completely } @@ -56,7 +53,7 @@ public function getEndpoint($name, $version = null): EndpointInterface /** * @param $name */ - protected function buildEndpoint($name, $version = null): EndpointInterface + protected function buildEndpoint(string $name, string $version = null): EndpointInterface { $endPointArray = $this->registry[$name]; $Class = $endPointArray['class']; diff --git a/src/Endpoint/Provider/DefaultEndpointProvider.php b/src/Endpoint/Provider/DefaultEndpointProvider.php index f4620d3..1c63838 100644 --- a/src/Endpoint/Provider/DefaultEndpointProvider.php +++ b/src/Endpoint/Provider/DefaultEndpointProvider.php @@ -12,11 +12,7 @@ class DefaultEndpointProvider extends AbstractEndpointProvider public function __construct() { foreach (static::$_DEFAULT_ENDPOINTS as $name => $epData) { - if (!isset($epData['properties'])) { - $epData['properties'] = []; - } - - $this->registerEndpoint($name, $epData['class'], $epData['properties']); + $this->registerEndpoint($name, $epData['class'], $epData['properties'] ?? []); } } } diff --git a/src/Endpoint/Provider/EndpointProviderInterface.php b/src/Endpoint/Provider/EndpointProviderInterface.php index 338b0cb..19fb257 100644 --- a/src/Endpoint/Provider/EndpointProviderInterface.php +++ b/src/Endpoint/Provider/EndpointProviderInterface.php @@ -15,7 +15,7 @@ public function getEndpoint(string $name, string $version = null): EndpointInter * * @return $this */ - public function registerEndpoint(string $name, string $className, array $properties = []): self; + public function registerEndpoint(string $name, string $className, array $properties = []): static; /** * Check if Endpoint is registered diff --git a/src/Endpoint/SmartEndpoint.php b/src/Endpoint/SmartEndpoint.php index 91033f5..695051f 100644 --- a/src/Endpoint/SmartEndpoint.php +++ b/src/Endpoint/SmartEndpoint.php @@ -7,5 +7,5 @@ class SmartEndpoint extends AbstractSmartEndpoint { - protected static $_DATA_CLASS = EndpointData::class; + protected static string $_DATA_CLASS = EndpointData::class; } diff --git a/src/Endpoint/Traits/ArrayObjectAttributesTrait.php b/src/Endpoint/Traits/ArrayObjectAttributesTrait.php index ba39343..96c5210 100644 --- a/src/Endpoint/Traits/ArrayObjectAttributesTrait.php +++ b/src/Endpoint/Traits/ArrayObjectAttributesTrait.php @@ -4,11 +4,7 @@ trait ArrayObjectAttributesTrait { - /** - * @var array - */ - protected $_attributes = []; - + use ProtectedAttributesTrait; //Object Access /** * Get a data by key @@ -25,7 +21,7 @@ public function &__get($key) * @param string $key - The data key to assign the value to * @param mixed $value - The value to set */ - public function __set($key, $value) + public function __set($key, mixed $value) { $this->_attributes[$key] = $value; } @@ -56,7 +52,7 @@ public function __unset($key) * @param mixed $value - The value to set * @abstracting ArrayAccess */ - public function offsetSet($offset, $value): void + public function offsetSet($offset, mixed $value): void { if (is_null($offset)) { $this->_attributes[] = $value; diff --git a/src/Endpoint/Traits/ClearAttributesTrait.php b/src/Endpoint/Traits/ClearAttributesTrait.php index 53a69eb..bc0db4c 100644 --- a/src/Endpoint/Traits/ClearAttributesTrait.php +++ b/src/Endpoint/Traits/ClearAttributesTrait.php @@ -4,11 +4,12 @@ trait ClearAttributesTrait { + use ProtectedAttributesTrait; /** * Clear the attributes array * @implements ClearableInterface */ - public function clear() + public function clear(): static { $this->_attributes = []; return $this; diff --git a/src/Endpoint/Traits/GetAttributesTrait.php b/src/Endpoint/Traits/GetAttributesTrait.php index 2870abf..ca684c8 100644 --- a/src/Endpoint/Traits/GetAttributesTrait.php +++ b/src/Endpoint/Traits/GetAttributesTrait.php @@ -4,13 +4,13 @@ trait GetAttributesTrait { + use ProtectedAttributesTrait; /** * Get an attribute by Key * @param $key - * @return mixed * @implements GetInterface */ - public function get($key) + public function get(string|int $key): mixed { return $this->_attributes[$key] ?? null; } diff --git a/src/Endpoint/Traits/JsonHandlerTrait.php b/src/Endpoint/Traits/JsonHandlerTrait.php index 37da033..bdf83df 100644 --- a/src/Endpoint/Traits/JsonHandlerTrait.php +++ b/src/Endpoint/Traits/JsonHandlerTrait.php @@ -30,7 +30,7 @@ public function getResponseContent(Response $response, bool $associative = true) try { $body = json_decode($this->respContent, $associative); // @codeCoverageIgnoreStart - } catch (\Exception $exception) { + } catch (\Exception) { } // @codeCoverageIgnoreEnd diff --git a/src/Endpoint/Traits/PropertiesTrait.php b/src/Endpoint/Traits/PropertiesTrait.php index 5f71200..5b9ee98 100644 --- a/src/Endpoint/Traits/PropertiesTrait.php +++ b/src/Endpoint/Traits/PropertiesTrait.php @@ -4,10 +4,7 @@ trait PropertiesTrait { - /** - * @var array - */ - protected $_properties = []; + protected array $_properties = []; /** * Get the current Data Properties @@ -23,7 +20,7 @@ public function getProperties(): array * @return $this * @implements PropertiesInterface */ - public function setProperties(array $properties) + public function setProperties(array $properties): static { $this->_properties = $properties; return $this; @@ -35,7 +32,7 @@ public function setProperties(array $properties) * @return $this * @implements PropertiesInterface */ - public function setProperty(string $name, $value) + public function setProperty(string $name, $value): static { $properties = $this->getProperties(); $properties[$name] = $value; @@ -44,10 +41,9 @@ public function setProperty(string $name, $value) /** * Get a specific property from properties array - * @return mixed * @implements PropertiesInterface */ - public function getProperty(string $name) + public function getProperty(string $name): mixed { return $this->_properties[$name] ?? null; } diff --git a/src/Endpoint/Traits/ProtectedAttributesTrait.php b/src/Endpoint/Traits/ProtectedAttributesTrait.php new file mode 100644 index 0000000..42c19ef --- /dev/null +++ b/src/Endpoint/Traits/ProtectedAttributesTrait.php @@ -0,0 +1,8 @@ + $value) { diff --git a/src/Traits/PsrLoggerTrait.php b/src/Traits/PsrLoggerTrait.php index ace9734..adb43ba 100644 --- a/src/Traits/PsrLoggerTrait.php +++ b/src/Traits/PsrLoggerTrait.php @@ -9,25 +9,22 @@ trait PsrLoggerTrait { /** * The logger instance. - * - * @var LoggerInterface|null */ - protected $logger; + protected LoggerInterface $logger; /** * Sets a logger. */ - public function setLogger(LoggerInterface $logger): void + public function setLogger(LoggerInterface $logger): static { $this->logger = $logger; + return $this; } /** * Get the Logger instance - * - * @return LoggerInterface */ - public function getLogger() + public function getLogger(): LoggerInterface { if (!isset($this->logger)) { $this->setLogger(new NullLogger()); diff --git a/src/Traits/PsrSimpleCacheTrait.php b/src/Traits/PsrSimpleCacheTrait.php index 110f2f2..1d8c5e8 100644 --- a/src/Traits/PsrSimpleCacheTrait.php +++ b/src/Traits/PsrSimpleCacheTrait.php @@ -10,7 +10,7 @@ trait PsrSimpleCacheTrait /** * @var */ - protected $cache; + protected CacheInterface $cache; /** * Get the Simple Cache object @@ -28,7 +28,7 @@ public function getCache(): CacheInterface * Set the Simple Cache object * @return $this */ - public function setCache(CacheInterface $cache) + public function setCache(CacheInterface $cache): static { $this->cache = $cache; return $this; diff --git a/tests/Client/AbstractClientTest.php b/tests/Client/AbstractClientTest.php index e226826..172174c 100644 --- a/tests/Client/AbstractClientTest.php +++ b/tests/Client/AbstractClientTest.php @@ -193,12 +193,6 @@ public function testSetServer(): void */ public function testSetVersion(): void { - $this->assertEquals($this->Client, $this->Client->setVersion(1)); - $this->assertEquals(1, $this->Client->getVersion()); - $this->assertEquals($this->Client, $this->Client->setVersion(null)); - $this->assertEquals(null, $this->Client->getVersion()); - $this->assertEquals($this->Client, $this->Client->setVersion([])); - $this->assertEquals([], $this->Client->getVersion()); $this->assertEquals($this->Client, $this->Client->setVersion($this->version)); $this->assertEquals($this->version, $this->Client->getVersion()); } diff --git a/tests/Stubs/Auth/AuthController.php b/tests/Stubs/Auth/AuthController.php index 02d8e30..520dc95 100644 --- a/tests/Stubs/Auth/AuthController.php +++ b/tests/Stubs/Auth/AuthController.php @@ -8,14 +8,14 @@ class AuthController extends AbstractAuthController { - protected $token = '12345'; + protected mixed $token = '12345'; public function configureRequest(Request $Request): Request { return $Request->withHeader('token', $this->token); } - protected function parseResponseToToken(string $action, Response $response) + protected function parseResponseToToken(string $action, Response $response): string { return $response->getBody()->getContents(); } diff --git a/tests/Stubs/Client/Client.php b/tests/Stubs/Client/Client.php index f0461ed..22d3516 100644 --- a/tests/Stubs/Client/Client.php +++ b/tests/Stubs/Client/Client.php @@ -28,7 +28,7 @@ public function __construct() parent::__construct(); } - public function current(EndpointInterface $endpoint = null) + public function current(EndpointInterface $endpoint = null): EndpointInterface { if ($endpoint instanceof EndpointInterface) { $this->setCurrentEndpoint($endpoint); diff --git a/tests/Stubs/Endpoint/AuthEndpoint.php b/tests/Stubs/Endpoint/AuthEndpoint.php index f650630..8ed095b 100644 --- a/tests/Stubs/Endpoint/AuthEndpoint.php +++ b/tests/Stubs/Endpoint/AuthEndpoint.php @@ -6,7 +6,7 @@ class AuthEndpoint extends SmartEndpoint { - protected static $_ENDPOINT_URL = 'authenticate'; + protected static string $_ENDPOINT_URL = 'authenticate'; - protected static $_DEFAULT_PROPERTIES = ['httpMethod' => "POST"]; + protected static array $_DEFAULT_PROPERTIES = ['httpMethod' => "POST"]; } diff --git a/tests/Stubs/Endpoint/CollectionEndpointWithoutModel.php b/tests/Stubs/Endpoint/CollectionEndpointWithoutModel.php index 921dcd4..676f10f 100644 --- a/tests/Stubs/Endpoint/CollectionEndpointWithoutModel.php +++ b/tests/Stubs/Endpoint/CollectionEndpointWithoutModel.php @@ -6,5 +6,5 @@ class CollectionEndpointWithoutModel extends AbstractCollectionEndpoint { - protected static $_ENDPOINT_URL = 'accounts'; + protected static string $_ENDPOINT_URL = 'accounts'; } diff --git a/tests/Stubs/Endpoint/LogoutEndpoint.php b/tests/Stubs/Endpoint/LogoutEndpoint.php index 0a0174d..e0091b4 100644 --- a/tests/Stubs/Endpoint/LogoutEndpoint.php +++ b/tests/Stubs/Endpoint/LogoutEndpoint.php @@ -6,7 +6,7 @@ class LogoutEndpoint extends Endpoint { - protected static $_ENDPOINT_URL = 'logout'; + protected static string $_ENDPOINT_URL = 'logout'; - protected static $_DEFAULT_PROPERTIES = ['httpMethod' => "POST"]; + protected static array $_DEFAULT_PROPERTIES = [self::PROPERTY_HTTP_METHOD => "POST"]; } diff --git a/tests/Stubs/Endpoint/ModelEndpoint.php b/tests/Stubs/Endpoint/ModelEndpoint.php index 968ab91..16e4556 100644 --- a/tests/Stubs/Endpoint/ModelEndpoint.php +++ b/tests/Stubs/Endpoint/ModelEndpoint.php @@ -6,5 +6,5 @@ class ModelEndpoint extends AbstractModelEndpoint { - protected static $_ENDPOINT_URL = 'account/$:id'; + protected static string $_ENDPOINT_URL = 'account/$:id'; } diff --git a/tests/Stubs/Endpoint/ModelEndpointWithActions.php b/tests/Stubs/Endpoint/ModelEndpointWithActions.php index 73c9985..94777b2 100644 --- a/tests/Stubs/Endpoint/ModelEndpointWithActions.php +++ b/tests/Stubs/Endpoint/ModelEndpointWithActions.php @@ -4,7 +4,7 @@ class ModelEndpointWithActions extends ModelEndpoint { - protected static $_RESPONSE_PROP = 'account'; + protected static string $_RESPONSE_PROP = 'account'; - protected $actions = ['foo' => "GET"]; + protected array $actions = ['foo' => "GET"]; } diff --git a/tests/Stubs/Endpoint/PingEndpoint.php b/tests/Stubs/Endpoint/PingEndpoint.php index f15f1f3..904d9da 100644 --- a/tests/Stubs/Endpoint/PingEndpoint.php +++ b/tests/Stubs/Endpoint/PingEndpoint.php @@ -6,7 +6,7 @@ class PingEndpoint extends SmartEndpoint { - protected static $_ENDPOINT_URL = 'ping'; + protected static string $_ENDPOINT_URL = 'ping'; - protected static $_DEFAULT_PROPERTIES = ['httpMethod' => "GET"]; + protected static array $_DEFAULT_PROPERTIES = [self::PROPERTY_HTTP_METHOD => "GET"]; } diff --git a/tests/Stubs/Endpoint/RefreshEndpoint.php b/tests/Stubs/Endpoint/RefreshEndpoint.php index b5f4403..c0cd849 100644 --- a/tests/Stubs/Endpoint/RefreshEndpoint.php +++ b/tests/Stubs/Endpoint/RefreshEndpoint.php @@ -6,7 +6,7 @@ class RefreshEndpoint extends Endpoint { - protected static $_ENDPOINT_URL = 'refresh'; + protected static string $_ENDPOINT_URL = 'refresh'; - protected static $_DEFAULT_PROPERTIES = ['httpMethod' => "POST"]; + protected static array $_DEFAULT_PROPERTIES = [ self::PROPERTY_HTTP_METHOD => "POST"]; } diff --git a/tests/Stubs/Endpoint/SmartEndpointNoData.php b/tests/Stubs/Endpoint/SmartEndpointNoData.php index 7079b82..dbbd913 100644 --- a/tests/Stubs/Endpoint/SmartEndpointNoData.php +++ b/tests/Stubs/Endpoint/SmartEndpointNoData.php @@ -8,7 +8,7 @@ class SmartEndpointNoData extends AbstractSmartEndpoint { - protected static $_DATA_CLASS = EndpointData::class; + protected static string $_DATA_CLASS = EndpointData::class; //Override constructor to prevent building out of Data Object right away public function __construct(array $urlArgs = [], array $properties = [])