Skip to content

Commit

Permalink
Merge pull request #5 from pnx/phpstan
Browse files Browse the repository at this point in the history
Adding support for phpstan
  • Loading branch information
olssonm authored Jan 8, 2024
2 parents cb67cf1 + cedcba3 commit d4861ab
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 65 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"phpunit/phpunit": "^8.3 || ^9.3",
"orchestra/testbench": ">=4.0",
"squizlabs/php_codesniffer": "^3.5",
"pestphp/pest": "^1.0"
"pestphp/pest": "^1.0",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand All @@ -33,6 +34,7 @@
},
"scripts": {
"test": "./vendor/bin/pest",
"phpstan": "./vendor/bin/phpstan",
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/pest --coverage",
"phpsniff": "vendor/bin/phpcs --standard=\"PSR12\" ./src",
"phpfix": "vendor/bin/phpcbf --standard=\"PSR12\" ./src"
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
paths:
- src
checkGenericClassInNonGenericObjectType: false
38 changes: 26 additions & 12 deletions src/Api/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Olssonm\Swish\Api;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request as Psr7Request;
use GuzzleHttp\Psr7\Response;
use Olssonm\Swish\Exceptions\ClientException;
use Olssonm\Swish\Exceptions\ServerException;
use Olssonm\Swish\Exceptions\ValidationException;
Expand All @@ -14,40 +12,53 @@

abstract class AbstractResource
{
protected Client $client;
protected ClientInterface $client;

public function __construct(Client $client)
public function __construct(ClientInterface $client)
{
$this->client = $client;
}

/**
* Retrieve resource
*
* @param $transaction
* @return mixed
*/
abstract public function get($transaction);
abstract public function get($transaction); // @phpstan-ignore-line

/**
* Create resource
*
* @param $transaction
* @return mixed
*/
abstract public function create($transaction);
abstract public function create($transaction); // @phpstan-ignore-line

/**
* Cancel transaction
*
* @param $transaction
* @return mixed
*/
abstract public function cancel($transaction);
abstract public function cancel($transaction); // @phpstan-ignore-line

/**
* Main API caller
*
* @param string $verb
* @param string $uri
* @param array $headers
* @param array<string, string> $headers
* @param string|null $payload
* @return Response
* @return ResponseInterface
* @throws ClientException|ServerException|ValidationException
*/
protected function request(string $verb, string $uri, array $headers = [], $payload = null): Response
{
protected function request(
string $verb,
string $uri,
array $headers = [],
string|null $payload = null
): ResponseInterface {
$request = new Psr7Request(
$verb,
$uri,
Expand Down Expand Up @@ -118,6 +129,9 @@ protected function triggerException(
$response->getReasonPhrase()
);

/**
* @var \Exception
*/
throw new $class(
$message,
$request,
Expand Down
16 changes: 12 additions & 4 deletions src/Api/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ public function get($payment): Payment
*/
public function create($payment): PaymentResult
{
$response = $this->request('PUT', sprintf('v2/paymentrequests/%s', $payment->id), [], json_encode($payment));
$response = $this->request(
'PUT',
sprintf('v2/paymentrequests/%s', $payment->id),
[],
(string) json_encode($payment)
);

$location = $response->getHeaderLine('Location');
$token = $response->getHeaderLine('PaymentRequestToken');

return new PaymentResult([
'id' => Id::parse($response),
'location' => $response->getHeaderLine('Location') ?? null,
'paymentRequestToken' => $response->getHeaderLine('PaymentRequestToken') ?? null,
'location' => strlen($location) > 0 ? $location : null,
'paymentRequestToken' => strlen($token) > 0 ? $token : null,
]);
}

Expand All @@ -51,7 +59,7 @@ public function cancel($payment): Payment
{
$response = $this->request('PATCH', sprintf('v1/paymentrequests/%s', $payment->id), [
'Content-Type' => 'application/json-patch+json',
], json_encode([[
], (string) json_encode([[
'op' => 'replace',
'path' => '/status',
'value' => 'cancelled',
Expand Down
8 changes: 5 additions & 3 deletions src/Api/Refunds.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ public function get($refund): Refund
*/
public function create($refund): RefundResult
{
$response = $this->request('PUT', sprintf('v2/refunds/%s', $refund->id), [], json_encode($refund));
$response = $this->request('PUT', sprintf('v2/refunds/%s', $refund->id), [], (string) json_encode($refund));

$location = $response->getHeaderLine('Location');

return new RefundResult([
'id' => Id::parse($response),
'location' => $response->getHeaderLine('Location') ?? null,
'location' => strlen($location) > 0 ? $location : null,
]);
}

/**
* Cancel a refund.
*
* @param Refund $transaction
* @throws BadMethodCallException
* @throws \BadMethodCallException
*/
public function cancel($transaction): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Callback
public static function parse($content = null)
{
if (is_null($content)) {
$content = file_get_contents('php://input');
$content = (string) file_get_contents('php://input');
}

try {
Expand Down
11 changes: 7 additions & 4 deletions src/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

class Certificate
{
private $client;
private ?string $client;

private $passphrase;
private ?string $passphrase;

private $root;
private bool|string $root;

/**
* Certificate constructor
Expand All @@ -24,7 +24,10 @@ public function __construct(?string $clientPath, ?string $passphrase = null, boo
$this->root = $rootPath;
}

public function getClientCertificate(): ?array
/**
* @return array<string|null>
*/
public function getClientCertificate(): array
{
return [
$this->client,
Expand Down
19 changes: 13 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ class Client
{
protected string $endpoint;

/**
* @var array<mixed>
*/
protected array $history = [];

public const PRODUCTION_ENDPOINT = 'https://cpc.getswish.net/swish-cpcapi/api/';

public const TEST_ENDPOINT = 'https://mss.cpc.getswish.net/swish-cpcapi/api/';

protected GuzzleHttpClient $client;
protected ClientInterface $client;

public function __construct(
Certificate $certificate = null,
Expand Down Expand Up @@ -53,8 +56,8 @@ public function setup(
CURLOPT_TIMEOUT => 0,
CURLOPT_CONNECTTIMEOUT => 20,
],
'verify' => $certificate->getRootCertificate(),
'cert' => $certificate->getClientCertificate(),
'verify' => $certificate?->getRootCertificate(),
'cert' => $certificate?->getClientCertificate(),
'base_uri' => $endpoint,
'http_errors' => false,
]);
Expand All @@ -63,14 +66,17 @@ public function setup(
/**
* Return the clients call-history
*
* @return array
* @return array<mixed>
*/
public function getHistory()
public function getHistory(): array
{
return $this->history;
}

public function __call($method, $args)
/**
* @param array<mixed> $args
*/
public function __call(string $method, array $args): mixed
{
if (
!is_object($args[0]) ||
Expand All @@ -89,6 +95,7 @@ public function __call($method, $args)
break;
}

// @phpstan-ignore-next-line
return call_user_func_array([$class, $method], $args);
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidUuidException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class InvalidUuidException extends \InvalidArgumentException
{
public function __construct($message = null, $code = 0)
public function __construct(string $message = null, int $code = 0)
{
if (!$message) {
throw new $this('Invalid UUID ' . get_class($this), $code);
Expand Down
14 changes: 12 additions & 2 deletions src/Exceptions/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@

class ValidationException extends RequestException
{
/**
* @var array<Error>
*/
private array $errors = [];

/**
* Undocumented function
*
* @param array<mixed> $handlerContext
*/
public function __construct(
string $message,
RequestInterface $request,
Expand All @@ -19,7 +27,7 @@ public function __construct(
array $handlerContext = []
) {

$data = json_decode((string) $response->getBody()->getContents());
$data = json_decode((string) $response?->getBody()->getContents());

if (is_array($data)) {
foreach ($data as $error) {
Expand All @@ -36,9 +44,11 @@ public function __construct(
parent::__construct($message, $request, $response, $previous, $handlerContext);
}

/**
* @return array<Error>
*/
public function getErrors(): array
{

return $this->errors;
}
}
5 changes: 4 additions & 1 deletion src/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*/
class Payment extends Resource
{
public function __construct($attributes = [])
/**
* @param array<string, mixed> $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->id = $this->id ?? Uuid::make();
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/SwishServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function register(): void
/**
* Get the services provided by the provider.
*
* @return array
* @return array<string>
* @codeCoverageIgnore
*/
public function provides()
Expand Down
33 changes: 18 additions & 15 deletions src/Refund.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
use Olssonm\Swish\Util\Uuid;

/**
* @property string id
* @property string originalPaymentReference
* @property string payerPaymentReference
* @property string callbackUrl
* @property string payerAlias
* @property string payeeAlias
* @property string amount
* @property string currency
* @property string message
* @property string status
* @property string dateCreated
* @property string datePaid
* @property string errorCode
* @property string errorMessage
* @property string additionalInformation
* @property string $id
* @property string $originalPaymentReference
* @property string $payerPaymentReference
* @property string $callbackUrl
* @property string $payerAlias
* @property string $payeeAlias
* @property string $amount
* @property string $currency
* @property string $message
* @property string $status
* @property string $dateCreated
* @property string $datePaid
* @property string $errorCode
* @property string $errorMessage
* @property string $additionalInformation
*/
class Refund extends Resource
{
/**
* @param array<string, mixed> $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
Expand Down
Loading

0 comments on commit d4861ab

Please sign in to comment.