Skip to content

Commit

Permalink
Merge pull request #328 from shakaran/feat/improve-code-quality-rector
Browse files Browse the repository at this point in the history
feat: improve code quality with rector rules
  • Loading branch information
matteodepalo authored Apr 18, 2024
2 parents 43d7cbf + 5ffc0cf commit d3690a6
Show file tree
Hide file tree
Showing 33 changed files with 201 additions and 207 deletions.
15 changes: 5 additions & 10 deletions src/Auth/AccessTokenOnlineResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@

final class AccessTokenOnlineResponse extends AccessTokenResponse
{
/** @var string */
protected $accessToken;
/** @var string */
protected $scope;
/** @var int */
private $expiresIn;
/** @var string */
private $associatedUserScope;
/** @var AccessTokenOnlineUserInfo|null */
private $associatedUser = null;
protected string $accessToken;
protected string $scope;
private readonly int $expiresIn;
private readonly string $associatedUserScope;
private readonly ?AccessTokenOnlineUserInfo $associatedUser;

public function __construct(
string $accessToken,
Expand Down
24 changes: 8 additions & 16 deletions src/Auth/AccessTokenOnlineUserInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@

final class AccessTokenOnlineUserInfo
{
/** @var int */
private $id;
/** @var string */
private $firstName;
/** @var string */
private $lastName;
/** @var string */
private $email;
/** @var bool */
private $emailVerified;
/** @var bool */
private $accountOwner;
/** @var string */
private $locale;
/** @var bool */
private $collaborator;
private readonly int $id;
private readonly string $firstName;
private readonly string $lastName;
private readonly string $email;
private readonly bool $emailVerified;
private readonly bool $accountOwner;
private readonly string $locale;
private readonly bool $collaborator;

public function __construct(
int $id,
Expand Down
6 changes: 2 additions & 4 deletions src/Auth/AccessTokenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

class AccessTokenResponse
{
/** @var string */
protected $accessToken;
/** @var string */
protected $scope;
protected string $accessToken;
protected string $scope;

public function __construct(
string $accessToken,
Expand Down
3 changes: 1 addition & 2 deletions src/Auth/FileSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
*/
class FileSessionStorage implements SessionStorage
{
/** @var string */
private $path;
private readonly string $path;

/**
* Initializes FileSessionStorage object
Expand Down
50 changes: 27 additions & 23 deletions src/Auth/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Shopify\Auth;

use Shopify\Exception\PrivateAppException;
use Shopify\Exception\UninitializedContextException;
use Shopify\Exception\OAuthCookieNotFoundException;
use Psr\Http\Client\ClientExceptionInterface;
use Shopify\Clients\Http;
use Shopify\Clients\HttpHeaders;
use Shopify\Clients\HttpResponse;
Expand Down Expand Up @@ -37,10 +41,10 @@ class OAuth
* @param null|callable $setCookieFunction An optional override for setting cookie in response
*
* @return string The URL for OAuth redirection
* @throws \Shopify\Exception\CookieSetException
* @throws \Shopify\Exception\PrivateAppException
* @throws \Shopify\Exception\SessionStorageException
* @throws \Shopify\Exception\UninitializedContextException
* @throws CookieSetException
* @throws PrivateAppException
* @throws SessionStorageException
* @throws UninitializedContextException
*/
public static function begin(
string $shop,
Expand Down Expand Up @@ -108,13 +112,13 @@ public static function begin(
* @param null|callable $setCookieFunction An optional override for setting cookie in response.
*
* @return Session
* @throws \Shopify\Exception\HttpRequestException
* @throws \Shopify\Exception\InvalidOAuthException
* @throws \Shopify\Exception\OAuthCookieNotFoundException
* @throws \Shopify\Exception\OAuthSessionNotFoundException
* @throws \Shopify\Exception\PrivateAppException
* @throws \Shopify\Exception\SessionStorageException
* @throws \Shopify\Exception\UninitializedContextException
* @throws HttpRequestException
* @throws InvalidOAuthException
* @throws OAuthCookieNotFoundException
* @throws OAuthSessionNotFoundException
* @throws PrivateAppException
* @throws SessionStorageException
* @throws UninitializedContextException
*/
public static function callback(array $cookies, array $query, ?callable $setCookieFunction = null): Session
{
Expand Down Expand Up @@ -213,8 +217,8 @@ public static function getOfflineSessionId(string $shop): string
* @param bool $isOnline Whether to load online or offline sessions
*
* @return string The ID of the current session
* @throws \Shopify\Exception\MissingArgumentException
* @throws \Shopify\Exception\CookieNotFoundException
* @throws MissingArgumentException
* @throws CookieNotFoundException
*/
public static function getCurrentSessionId(array $rawHeaders, array $cookies, bool $isOnline): string
{
Expand All @@ -226,13 +230,13 @@ public static function getCurrentSessionId(array $rawHeaders, array $cookies, bo
throw new MissingArgumentException('Missing Authorization key in headers array');
}
$auth = $headers->get('authorization');
preg_match('/^Bearer (.+)$/', $auth, $matches);
preg_match('/^Bearer (.+)$/', (string) $auth, $matches);
if (!$matches) {
throw new MissingArgumentException('Missing Bearer token in authorization header');
}

$jwtPayload = Utils::decodeSessionToken($matches[1]);
$shop = preg_replace('/^https:\/\//', '', $jwtPayload['dest']);
$shop = preg_replace('/^https:\/\//', '', (string) $jwtPayload['dest']);
if ($isOnline) {
$currentSessionId = self::getJwtSessionId($shop, $jwtPayload['sub']);
} else {
Expand All @@ -257,7 +261,7 @@ public static function getCurrentSessionId(array $rawHeaders, array $cookies, bo
* @param array $cookies The $cookies param from `callback`
*
* @return string The ID of the current session
* @throws \Shopify\Exception\CookieNotFoundException
* @throws CookieNotFoundException
*/
private static function getCookieSessionId(array $cookies): string
{
Expand All @@ -266,7 +270,7 @@ private static function getCookieSessionId(array $cookies): string

$sessionId = null;
if ($signature && $cookieId) {
$expectedSignature = hash_hmac('sha256', $cookieId, Context::$API_SECRET_KEY);
$expectedSignature = hash_hmac('sha256', (string) $cookieId, Context::$API_SECRET_KEY);

if ($signature === $expectedSignature) {
$sessionId = $cookieId;
Expand Down Expand Up @@ -333,7 +337,7 @@ private static function setCookieSessionId(?callable $setCookieFunction, $sessio
* @param Session $session The current session
*
* @return bool
* @throws \Shopify\Exception\UninitializedContextException
* @throws UninitializedContextException
*/
private static function isCallbackQueryValid(array $query, Session $session): bool
{
Expand All @@ -344,7 +348,7 @@ private static function isCallbackQueryValid(array $query, Session $session): bo
return (
($code) &&
($sanitizedShop && strcmp($session->getShop(), $sanitizedShop) === 0) &&
($state && strcmp($session->getState(), $state) === 0) &&
($state && strcmp($session->getState(), (string) $state) === 0) &&
Utils::validateHmac($query, Context::$API_SECRET_KEY)
);
}
Expand All @@ -356,7 +360,7 @@ private static function isCallbackQueryValid(array $query, Session $session): bo
* @param Session $session The OAuth session
*
* @return AccessTokenResponse|AccessTokenOnlineResponse The access token exchanged for the OAuth code
* @throws \Shopify\Exception\HttpRequestException
* @throws HttpRequestException
*/
private static function fetchAccessToken(
array $query,
Expand Down Expand Up @@ -424,9 +428,9 @@ private static function buildAccessTokenResponse(array $body): AccessTokenRespon
* @param Http $client
* @param array $post The POST payload
*
* @return \Shopify\Clients\HttpResponse
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \Shopify\Exception\UninitializedContextException
* @return HttpResponse
* @throws ClientExceptionInterface
* @throws UninitializedContextException
* @codeCoverageIgnore
*/
public static function requestAccessToken(Http $client, array $post): HttpResponse
Expand Down
15 changes: 5 additions & 10 deletions src/Auth/OAuthCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@
*/
class OAuthCookie
{
/** @var string */
private $value;
/** @var string */
private $name;
/** @var int|null */
private $expire = 0;
/** @var bool */
private $secure = true;
/** @var bool */
private $httpOnly = true;
private readonly string $value;
private readonly string $name;
private readonly ?int $expire;
private readonly bool $secure;
private readonly bool $httpOnly;

public function __construct(
string $value,
Expand Down
8 changes: 3 additions & 5 deletions src/Auth/Scopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ final class Scopes
{
public const SCOPE_DELIMITER = ',';

/** @var array */
private $compressedScopes;
/** @var array */
private $expandedScopes;
private readonly array $compressedScopes;
private readonly array $expandedScopes;

/**
* @param string|array $scopes
Expand Down Expand Up @@ -98,7 +96,7 @@ private function getImpliedScopes(array $scopes): array
{
$impliedScopes = [];
foreach ($scopes as $scope) {
if (preg_match('/^(unauthenticated_)?write_(.*)$/', $scope, $matches)) {
if (preg_match('/^(unauthenticated_)?write_(.*)$/', (string) $scope, $matches)) {
$impliedScopes[] = ($matches[1] ?? '') . "read_{$matches[2]}";
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Auth/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Shopify\Auth;

use Exception;
use DateTime;
use Shopify\Context;
use Shopify\Utils;
Expand Down Expand Up @@ -94,7 +95,7 @@ public function setScope(string $scope): void
/**
* @param string|int|DateTime $expires
*
* @throws \Exception
* @throws Exception
*/
public function setExpires($expires): void
{
Expand Down
23 changes: 12 additions & 11 deletions src/Clients/Graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

namespace Shopify\Clients;

use Shopify\Exception\HttpRequestException;
use Psr\Http\Client\ClientExceptionInterface;
use Shopify\Exception\UninitializedContextException;
use Shopify\Context;
use Shopify\Exception\MissingArgumentException;

class Graphql
{
/** @var Http */
private $client;
/** @var string|null */
protected $token;
private readonly Http $client;
protected ?string $token;

/**
* GraphQL Client constructor.
*
* @param string $domain
* @param string|null $token
*
* @throws \Shopify\Exception\MissingArgumentException
* @throws MissingArgumentException
*/
public function __construct(
string $domain,
Expand All @@ -42,8 +43,8 @@ public function __construct(
* @param int|null $tries How many times to attempt the request
*
* @return HttpResponse
* @throws \Shopify\Exception\HttpRequestException
* @throws \Shopify\Exception\MissingArgumentException
* @throws HttpRequestException
* @throws MissingArgumentException
*/
public function query(
$data,
Expand Down Expand Up @@ -82,10 +83,10 @@ public function query(
* @param array $extraHeaders Any extra headers to send along with the request
* @param int|null $tries How many times to attempt the request
*
* @return \Shopify\Clients\HttpResponse
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \Shopify\Exception\MissingArgumentException
* @throws \Shopify\Exception\UninitializedContextException
* @return HttpResponse
* @throws ClientExceptionInterface
* @throws MissingArgumentException
* @throws UninitializedContextException
*/
public function proxy(
string $data,
Expand Down
Loading

0 comments on commit d3690a6

Please sign in to comment.