diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index cc243ef..eaeafa7 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -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', ], diff --git a/src/BasicShopifyAPI.php b/src/BasicShopifyAPI.php index f909183..b00223c 100755 --- a/src/BasicShopifyAPI.php +++ b/src/BasicShopifyAPI.php @@ -1,5 +1,7 @@ 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 @@ -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 @@ -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) ) ); @@ -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; } } diff --git a/src/Middleware/RateLimiting.php b/src/Middleware/RateLimiting.php index 55af020..2d69692 100644 --- a/src/Middleware/RateLimiting.php +++ b/src/Middleware/RateLimiting.php @@ -1,5 +1,7 @@ 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; } diff --git a/src/Traits/IsResponseType.php b/src/Traits/IsResponseType.php index c715104..47abf8a 100644 --- a/src/Traits/IsResponseType.php +++ b/src/Traits/IsResponseType.php @@ -1,5 +1,7 @@ '1/80', - 'link' => '; rel="next"', + 'link' => '; rel="next"', ], - file_get_contents(__DIR__.'/../fixtures/rest/admin__shop.json') + file_get_contents(__DIR__ . '/../fixtures/rest/admin__shop.json') ), ]; @@ -71,7 +73,7 @@ public function testRequestAccess(): void new GuzzleResponse( 200, [], - file_get_contents(__DIR__.'/../fixtures/admin__oauth__access_token.json') + file_get_contents(__DIR__ . '/../fixtures/admin__oauth__access_token.json') ), ]; @@ -87,9 +89,6 @@ public function testRequestAccess(): void // Request access $code = '!@#'; $result = $api->requestAccessToken($code); - /** @var \GuzzleHttp\Handler\MockHandler $handler */ - $handler = $api->getOptions()->getGuzzleHandler(); - $data = json_decode($handler->getLastRequest()->getBody(), true); $this->assertSame('f85632530bf277ec9ac6f649fc327f17', $result); } @@ -110,7 +109,7 @@ public function testRequestAndSetAccess(): void new GuzzleResponse( 200, [], - file_get_contents(__DIR__.'/../fixtures/admin__oauth__access_token__grant.json') + file_get_contents(__DIR__ . '/../fixtures/admin__oauth__access_token__grant.json') ), ]; @@ -176,7 +175,7 @@ public function testRequestSuccess(): void new GuzzleResponse( 200, ['http_x_shopify_shop_api_call_limit' => '2/80'], - file_get_contents(__DIR__.'/../fixtures/rest/admin__shop.json') + file_get_contents(__DIR__ . '/../fixtures/rest/admin__shop.json') ), ]; @@ -210,7 +209,7 @@ public function testRequestForceRequestType(): void new GuzzleResponse( 200, [], - file_get_contents(__DIR__.'/../fixtures/rest/admin__shop.json') + file_get_contents(__DIR__ . '/../fixtures/rest/admin__shop.json') ), ]; @@ -236,7 +235,7 @@ public function testRequestFailure(): void new GuzzleResponse( 404, ['http_x_shopify_shop_api_call_limit' => '2/80'], - file_get_contents(__DIR__.'/../fixtures/rest/admin__shop_oops.json') + file_get_contents(__DIR__ . '/../fixtures/rest/admin__shop_oops.json') ), ]; @@ -286,7 +285,7 @@ public function testRequestAsync(): void new GuzzleResponse( 200, ['http_x_shopify_shop_api_call_limit' => '2/80'], - file_get_contents(__DIR__.'/../fixtures/rest/admin__shop.json') + file_get_contents(__DIR__ . '/../fixtures/rest/admin__shop.json') ), ]; diff --git a/test/Middleware/AuthRequestTest.php b/test/Middleware/AuthRequestTest.php index e2c2b5e..ff40003 100644 --- a/test/Middleware/AuthRequestTest.php +++ b/test/Middleware/AuthRequestTest.php @@ -1,5 +1,7 @@ self->assertTrue($this->isGraphRequest('/admin/api/graphql.json')); - $this->self->assertFalse($this->isGraphRequest('/admin/api/unstable/shop.json')); + $this->self->assertTrue($this->isGraphRequest(new Uri('/admin/api/graphql.json'))); + $this->self->assertFalse($this->isGraphRequest(new Uri('/admin/api/unstable/shop.json'))); } public function testRest(): void { - $this->self->assertFalse($this->isRestRequest('/admin/api/graphql.json')); - $this->self->assertTrue($this->isRestRequest('/admin/api/unstable/shop.json')); + $this->self->assertFalse($this->isRestRequest(new Uri('/admin/api/graphql.json'))); + $this->self->assertTrue($this->isRestRequest(new Uri('/admin/api/unstable/shop.json'))); } }; diff --git a/test/Traits/IsResponseTypeTest.php b/test/Traits/IsResponseTypeTest.php index d92052a..d6a1627 100644 --- a/test/Traits/IsResponseTypeTest.php +++ b/test/Traits/IsResponseTypeTest.php @@ -1,5 +1,7 @@ toResponse($response->getBody());