Skip to content

Commit

Permalink
Strengthen static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
xHeaven committed Jul 10, 2024
1 parent 8c8a008 commit a9fb0f6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 5
level: 9
paths:
- src
- config
Expand Down
6 changes: 5 additions & 1 deletion src/Authenticators/Abstracts/AbstractAppAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function setShopDomain(string $shopDomain): void
{
$shopDomain = preg_replace('#^https?://|/$#', '', $shopDomain);

if ($shopDomain === null) {
throw new InvalidArgumentException('Invalid shop URL. The shop URL cannot be empty.');
}

if (!Str::endsWith($shopDomain, '.myshopify.com')) {
throw new InvalidArgumentException('Invalid shop URL. The shop URL must end with ".myshopify.com".');
}
Expand All @@ -31,7 +35,7 @@ public function getShopDomain(): string
return $this->shopDomain;
}

public function setApiVersion(?string $apiVersion): void
public function setApiVersion(string $apiVersion): void
{
if (!preg_match('/\d{4}-\d{2}/', $apiVersion)) {
throw new InvalidArgumentException('Invalid API version. The API version must match the pattern "YYYY-MM".');
Expand Down
5 changes: 4 additions & 1 deletion src/Authenticators/ShopifyApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public function __construct(
?string $apiVersion = null,
) {
$this->setShopDomain($shopDomain);
$this->setApiVersion($apiVersion ?? config('shopify-graphql.api_version'));

/** @var string $configApiVersion */
$configApiVersion = config('shopify-graphql.api_version');
$this->setApiVersion($apiVersion ?? $configApiVersion);
}
}
7 changes: 5 additions & 2 deletions src/GraphQLClientMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Luminarix\Shopify\GraphQLClient;

use Illuminate\Support\Arr;
use Luminarix\Shopify\GraphQLClient\Authenticators\Abstracts\AbstractAppAuthenticator;
use Luminarix\Shopify\GraphQLClient\Exceptions\ClientNotInitializedException;
use Luminarix\Shopify\GraphQLClient\Exceptions\ClientRequestFailedException;
Expand Down Expand Up @@ -31,11 +32,13 @@ public function query(string $query, bool $withExtensions = false, bool $detaile
throw_if($response->failed(), ClientRequestFailedException::class, $response);

return new GraphQLClientTransformer(
data: $withExtensions ? $response->json() : $response->json()['data']
data: $withExtensions ? Arr::wrap($response->json()) : Arr::wrap($response->json()['data'])
);
}

/**
* @param array<mixed, mixed> $variables
*
* @throws ClientNotInitializedException If the connector is not set
* @throws ClientRequestFailedException If the response contains errors
*/
Expand All @@ -48,7 +51,7 @@ public function mutate(string $query, array $variables, bool $withExtensions = f
throw_if($response->failed(), ClientRequestFailedException::class, $response);

return new GraphQLClientTransformer(
data: $withExtensions ? $response->json() : $response->json()['data']
data: $withExtensions ? Arr::wrap($response->json()) : Arr::wrap($response->json()['data'])
);
}
}
18 changes: 17 additions & 1 deletion src/GraphQLClientTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@

namespace Luminarix\Shopify\GraphQLClient;

use InvalidArgumentException;

readonly class GraphQLClientTransformer
{
/**
* @param array<mixed, mixed> $data
*/
public function __construct(private array $data) {}

/**
* @return array<mixed, mixed>
*/
public function toArray(): array
{
return $this->data;
}

public function toJson(int $flags = 0, int $depth = 512): string
{
return json_encode($this->data, $flags, $depth);
if ($depth < 1) {
throw new InvalidArgumentException('Depth must be greater than 0');
}

$jsonString = json_encode($this->data, $flags, $depth);

return $jsonString === false
? ''
: $jsonString;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Integrations/Requests/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ protected function defaultHeaders(): array
return $this->detailedCost ? $this->detailedCost() : [];
}

/**
* @return array<string, string>
*/
private function detailedCost(): array
{
return [
Expand Down
3 changes: 3 additions & 0 deletions src/Integrations/Requests/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class Mutation extends BaseRequest
{
/**
* @param mixed[] $variables
*/
public function __construct(
public string $graphqlQuery,
public array $variables,
Expand Down
3 changes: 3 additions & 0 deletions src/Integrations/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function query(string $graphqlQuery, bool $detailedCost = false): Respons
);
}

/**
* @param array<mixed, mixed> $variables
*/
public function mutation(string $graphqlQuery, array $variables, bool $detailedCost = false): Response
{
return $this->connector->send(
Expand Down

0 comments on commit a9fb0f6

Please sign in to comment.