Skip to content

Adding strict types #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
use PhpCsFixer\Finder;

$rules = [
'@PSR2' => true,
'@PSR12' => true,
'@PER-CS2.0' => true,
'@PER-CS2.0:risky' => true,
'declare_strict_types' => true,
'strict_param' => true,
'array_syntax' => [
'syntax' => 'short',
],
Expand Down
4 changes: 3 additions & 1 deletion src/BasicShopifyAPI.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI;

use Closure;
Expand Down Expand Up @@ -377,7 +379,7 @@ public function verifyRequest(array $params): bool
$value = '["'.implode('", "', $value).'"]';
}
}

ksort($params);

// Encode and hash the params (without HMAC), add the API secret, and compare to the HMAC from params
Expand Down
2 changes: 2 additions & 0 deletions src/Clients/AbstractClient.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Clients;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Clients/Graph.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Clients;

use Gnikyt\BasicShopifyAPI\Contracts\GraphRequester;
Expand Down
4 changes: 3 additions & 1 deletion src/Clients/Rest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Clients;

use Exception;
Expand Down Expand Up @@ -112,7 +114,7 @@ public function request(string $type, string $path, ?array $params = null, array
$guzzleParams = [];
if ($params !== null) {
$keys = array_keys($params);
if (isset($keys[0]) && in_array($keys[0], ['query', 'json'])) {
if (isset($keys[0]) && in_array($keys[0], ['query', 'json'], true)) {
// Inputted type
$guzzleParams = $params;
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/ClientAware.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

use Gnikyt\BasicShopifyAPI\Options;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/GraphRequester.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

use GuzzleHttp\Promise\Promise;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/LimitAccesser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

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

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

use Gnikyt\BasicShopifyAPI\ResponseAccess;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/RestRequester.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/SessionAware.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

use Gnikyt\BasicShopifyAPI\Session;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/StateStorage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

use Gnikyt\BasicShopifyAPI\Session;
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/TimeAccesser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

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

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Contracts;

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

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Deferrers;

use Gnikyt\BasicShopifyAPI\Contracts\TimeDeferrer;
Expand Down
2 changes: 2 additions & 0 deletions src/Middleware/AbstractMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Middleware;

use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
Expand Down
48 changes: 20 additions & 28 deletions src/Middleware/AuthRequest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Middleware;

use Exception;
use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
use Gnikyt\BasicShopifyAPI\Options;
use Gnikyt\BasicShopifyAPI\Traits\IsRequestType;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;

/**
* Ensures we have the proper request for private and public calls.
Expand Down Expand Up @@ -38,8 +41,8 @@ public function __invoke(callable $handler): callable
$apiPassword = $self->api->getOptions()->getApiPassword();
$accessToken = $self->api->getSession()->getAccessToken();

if ($self->isAuthableRequest((string) $uri)) {
if ($self->isRestRequest((string) $uri)) {
if ($self->isAuthableRequest($uri)) {
if ($self->isRestRequest($uri)) {
// Checks for REST
if ($isPrivate && ($apiKey === null || $apiPassword === null)) {
// Key and password are required for private API calls
Expand All @@ -50,7 +53,7 @@ public function __invoke(callable $handler): callable
// Private: Add auth for REST calls, add the basic auth header
$request = $request->withHeader(
'Authorization',
'Basic '.base64_encode("{$apiKey}:{$apiPassword}")
'Basic ' . base64_encode("{$apiKey}:{$apiPassword}")
);
} else {
// Public: Add the token header
Expand Down Expand Up @@ -78,7 +81,7 @@ public function __invoke(callable $handler): callable
$uri = $request->getUri();
$request = $request->withUri(
$uri->withPath(
$this->versionPath($uri->getPath())
$this->versionPath($uri)
)
);

Expand All @@ -88,53 +91,42 @@ public function __invoke(callable $handler): callable

/**
* Determines if the request requires auth headers.
*
* @param string $uri The request URI.
*
* @return bool
*/
protected function isAuthableRequest(string $uri): bool
protected function isAuthableRequest(UriInterface $uri): bool
{
return preg_match('/\/admin\/oauth\/(authorize|access_token)/', $uri) === 0;
return preg_match('/\/admin\/oauth\/(authorize|access_token)/', $uri->getPath()) === 0;
}

/**
* Versions the API call with the set version.
*
* @param string $uri The request URI.
*
* @return string
*/
protected function versionPath(string $uri): string
protected function versionPath(UriInterface $uri): string
{
$version = $this->api->getOptions()->getVersion();
if ($version === null ||
preg_match(Options::VERSION_PATTERN, $uri) ||
!$this->isAuthableRequest($uri) ||
!$this->isVersionableRequest($uri)
if (
$version === null
|| preg_match(Options::VERSION_PATTERN, $uri->getPath())
|| !$this->isAuthableRequest($uri)
|| !$this->isVersionableRequest($uri)
) {
// No version set, or already versioned... nothing to do
return $uri;
return $uri->getPath();
}

// Graph request
if ($this->isGraphRequest($uri)) {
return str_replace('/admin/api', "/admin/api/{$version}", $uri);
return preg_replace('/\/admin(\/api)?\//', "/admin/api/{$version}/", $uri->getPath());
}

// REST request
return preg_replace('/\/admin(\/api)?\//', "/admin/api/{$version}/", $uri);
return preg_replace('/\/admin(\/api)?\//', "/admin/api/{$version}/", $uri->getPath());
}

/**
* Determines if the request requires versioning.
*
* @param string $uri The request URI.
*
* @return bool
*/
protected function isVersionableRequest(string $uri): bool
protected function isVersionableRequest(UriInterface $uri): bool
{
return preg_match('/\/admin\/(oauth\/access_scopes)/', $uri) === 0;
return preg_match('/\/admin\/(oauth\/access_scopes)/', $uri->getPath()) === 0;
}
}
2 changes: 2 additions & 0 deletions src/Middleware/RateLimiting.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Middleware;

use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
Expand Down
2 changes: 2 additions & 0 deletions src/Middleware/UpdateApiLimits.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Middleware;

use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
Expand Down
2 changes: 2 additions & 0 deletions src/Middleware/UpdateRequestTime.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Middleware;

use Gnikyt\BasicShopifyAPI\Traits\IsRequestType;
Expand Down
2 changes: 2 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI;

use Exception;
Expand Down
2 changes: 2 additions & 0 deletions src/ResponseAccess.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI;

use ArrayAccess;
Expand Down
2 changes: 2 additions & 0 deletions src/Session.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI;

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

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Store;

use Gnikyt\BasicShopifyAPI\Contracts\StateStorage;
Expand Down
18 changes: 7 additions & 11 deletions src/Traits/IsRequestType.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Traits;

use Psr\Http\Message\UriInterface;

/**
* Determine GraphQL or REST request type.
*/
trait IsRequestType
{
/**
* Determines if the request is to Graph API.
*
* @param string $uri The request URI.
*
* @return bool
*/
protected function isGraphRequest(string $uri): bool
protected function isGraphRequest(UriInterface $uri): bool
{
return strpos($uri, 'graphql.json') !== false;
return strpos($uri->getPath(), 'graphql.json') !== false;
}

/**
* Determines if the request is to REST API.
*
* @param string $uri The request URI.
*
* @return bool
*/
protected function isRestRequest(string $uri): bool
protected function isRestRequest(UriInterface $uri): bool
{
return $this->isGraphRequest($uri) === false;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Traits/IsResponseType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Traits;

use Gnikyt\BasicShopifyAPI\BasicShopifyAPI;
Expand Down
9 changes: 7 additions & 2 deletions src/Traits/ResponseTransform.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Traits;

use Gnikyt\BasicShopifyAPI\ResponseAccess;
Expand All @@ -15,8 +17,11 @@ trait ResponseTransform
*/
public function toResponse(StreamInterface $body): ResponseAccess
{
$decoded = json_decode($body, true, 512, JSON_BIGINT_AS_STRING);

$decoded = json_decode(
json: (string) $body,
associative: true,
flags: JSON_BIGINT_AS_STRING
);
return new ResponseAccess($decoded);
}
}
2 changes: 2 additions & 0 deletions test/BaseTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Gnikyt\BasicShopifyAPI\Test;

use Closure;
Expand Down
Loading