Skip to content
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

Install phpstan and fix some errors #62

Merged
merged 14 commits into from
Feb 15, 2023
8 changes: 8 additions & 0 deletions .github/workflows/run-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ jobs:
- name: Run linter
run: composer lint

- name: Run phpstan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have phpstan in composer scripts as well. Could be as part of composer lint -command, since it mostly is for code quality. But since it has own existing action in github, maybe own command would be better in pipeline sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important to run static analysis with different PHP versions to ensure interoperability with promised legacy versions (ie not use too new language features). Not sure if this can be done without using the separate action? I'm not very familiar with GitHub Actions.

Maybe add composer analyse? This way it would still be separate in the pipeline. In our project we also use Psalm which could be added to the same composer script.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Analyze sounds good command.
Actions can have only one command if I recall correctly. But we can always add command like composer check or something that runs composer analyze and composer lint as pre commit hook, when adding those later in line.
But that is more sens to set when code quality is in level that it should be in order to ensuring maintaining it that way. This repo is really old, from Checkout era from different version control, so it still has probably quite few out dated conventions and such.

You can also add psalm in this PR as well, I don't think it breaks too much, and would anyway makes sense to have, since there's already plan to add it anyway when have time to do so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like some of the dependencies pulled in by psalm are not compatible with PHP 7.3 :)

I made another branch psalm based on this one, but it can't be merged right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the command composer analyze which now just runs phpstan.

Let me know when #59 is merged - I'll rebase afterwards

uses: php-actions/phpstan@v3.0.1
with:
php_version: ${{ matrix.php-version }}
version: 1.9.14
configuration: phpstan.neon
memory_limit: 256M

- name: Run tests
uses: php-actions/phpunit@v3.0.0
with:
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"squizlabs/php_codesniffer": "^3.6",
"phpcompatibility/php-compatibility": "^9.3",
"phpmd/phpmd": "^2.10",
"phpstan/phpstan": "1.9.14",
"mockery/mockery": "^1.5",
"guzzlehttp/guzzle": "^7.0|^6.0"
},
Expand All @@ -33,6 +34,9 @@
}
},
"scripts": {
"analyze": [
"vendor/bin/phpstan"
],
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility",
"post-update-cmd" : "\"vendor/bin/phpcs\" --config-set installed_paths vendor/phpcompatibility/php-compatibility",
"test": "\"vendor/bin/phpunit\"",
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: 3
paths:
- src
- tests
9 changes: 0 additions & 9 deletions src/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,13 @@

/**
* Class ValidationException
* This exception is a simple wrapper for
* a Respect\Validation NestedValidationException instance.
*
* This exception holds the general expection message
*
* @package Paytrail\SDK\Exception
*/
class ValidationException extends \Exception
{
/**
* Holds the previous NestedValidationException instance.
*
* $var NestedValidationException
*/
protected $previous;

/**
* Holds all error messages.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/AddressInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
interface AddressInterface
{
/**
* Validates with Respect\Validation library and throws exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/CallbackUrlInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
interface CallbackUrlInterface
{
/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/CommissionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
interface CommissionInterface
{
/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/CustomerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
interface CustomerInterface
{
/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Interfaces/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
interface ItemInterface
{
/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand All @@ -36,7 +36,7 @@ public function getUnitPrice(): ?int;
/**
* Set the unit price.
*
* @param int/null $unitPrice
* @param int|null $unitPrice
* @return ItemInterface Return self to enable chaining.
*/
public function setUnitPrice(?int $unitPrice): ItemInterface;
Expand All @@ -51,7 +51,7 @@ public function getUnits(): ?int;
/**
* Set the units.
*
* @param int/null $units
* @param int|null $units
* @return ItemInterface Return self to enable chaining.
*/
public function setUnits(?int $units): ItemInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/PaymentRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
interface PaymentRequestInterface
{
/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Address implements \JsonSerializable, AddressInterface
use JsonSerializable;

/**
* Validates with Respect\Validation library and throws exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Model/CallbackUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CallbackUrl implements \JsonSerializable, CallbackUrlInterface
use JsonSerializable;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Model/Commission.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public function setAmount(int $amount): Commission

/**
* The getter for the amount.
*
* @return string
*/
public function getAmount(): int
{
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Item implements \JsonSerializable, ItemInterface
* Merchant ID for the item.
* Required for Shop-in-Shop payments, do not use for normal payments.
*
* @var int
* @var string|null
*/
protected $merchant;

Expand Down Expand Up @@ -363,7 +363,7 @@ public function setCommission(?CommissionInterface $commission): ItemInterface
}

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down Expand Up @@ -393,7 +393,7 @@ public function validate()
}

/**
* Validates shop-in-shop props with Respect\Validation library and throws an exception for invalid objects
* Validates shop-in-shop props and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Model/RefundItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RefundItem implements \JsonSerializable
use JsonSerializable;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Token/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Customer implements \JsonSerializable
use ObjectPropertyConverter;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
15 changes: 15 additions & 0 deletions src/PaytrailClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ protected function createHttpClient()
$this->http_client = new RequestClient();
}

/**
* A proxy for the Signature class' static method
* to be used via a client instance.
*
* @param array $response The response parameters.
* @param string $body The response body.
* @param string $signature The response signature key.
*
* @throws HmacException
*/
abstract public function validateHmac(array $response = [], string $body = '', string $signature = '');

/**
* A wrapper for post requests.
*
Expand Down Expand Up @@ -86,8 +98,11 @@ protected function post(
$headers = $this->reduceHeaders($response->getHeaders());
$this->validateHmac($headers, $body, $headers['signature'] ?? '');
} else {
// @phpstan-ignore-next-line FIXME
$mac = $this->calculateHmac($data->toArray());
// @phpstan-ignore-next-line FIXME
$data->setSignature($mac);
// @phpstan-ignore-next-line FIXME
$body = json_encode($data->toArray(), JSON_UNESCAPED_SLASHES);

$response = $this->http_client->request('POST', $uri, [
Expand Down
16 changes: 8 additions & 8 deletions src/Request/AbstractPaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractPaymentRequest implements \JsonSerializable, PaymentReque
use JsonSerializable;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down Expand Up @@ -62,7 +62,7 @@ public function validate()
});

// Count the total amount of the payment.
$items_total = array_reduce($this->getItems(), function ($carry = 0, ?Item $item = null) {
$items_total = array_reduce($this->getItems(), function ($carry = 0, ?ItemInterface $item = null) {
if ($item === null) {
return $carry;
}
Expand Down Expand Up @@ -156,42 +156,42 @@ public function validate()
/**
* Array of items.
*
* @var Item[]
* @var ItemInterface[]
*/
protected $items;

/**
* Customer information.
*
* @var Customer
* @var CustomerInterface
*/
protected $customer;

/**
* Delivery address.
*
* @var Address
* @var AddressInterface
*/
protected $deliveryAddress;

/**
* Invoicing address.
*
* @var Address
* @var AddressInterface
*/
protected $invoicingAddress;

/**
* Where to redirect browser after a payment is paid or cancelled.
*
* @var CallbackUrl;
* @var CallbackUrlInterface
*/
protected $redirectUrls;

/**
* Which url to ping after this payment is paid or cancelled.
*
* @var CallbackUrl;
* @var CallbackUrlInterface
*/
protected $callbackUrls;

Expand Down
2 changes: 1 addition & 1 deletion src/Request/AddCardFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AddCardFormRequest implements \JsonSerializable
protected $signature;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Request/CitPaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CitPaymentRequest extends AbstractPaymentRequest implements TokenPaymentRe
protected $token;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
3 changes: 1 addition & 2 deletions src/Request/EmailRefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class EmailRefundRequest extends RefundRequest
protected $email;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws NestedValidationException Thrown when the validate() fails.
* @throws ValidationException
*/
public function validate()
Expand Down
2 changes: 1 addition & 1 deletion src/Request/GetTokenRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GetTokenRequest implements \JsonSerializable
protected $checkoutTokenizationId;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Request/MitPaymentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MitPaymentRequest extends AbstractPaymentRequest implements TokenPaymentRe
protected $token;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Request/PaymentStatusRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PaymentStatusRequest
protected $transactionId;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Request/RefundRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RefundRequest implements \JsonSerializable
use JsonSerializable;

/**
* Validates with Respect\Validation library and throws an exception for invalid objects
* Validates properties and throws an exception for invalid values
*
* @throws ValidationException
*/
Expand Down Expand Up @@ -96,21 +96,21 @@ public function validate()
/**
* Refund recipient email address.
*
* @var $mail
* @var string|null $email
*/
protected $email;

/**
* Merchant unique identifier for the refund.
*
* @var $refundStamp
* @var string|null $refundStamp
*/
protected $refundStamp;

/**
* Refund reference.
*
* @var $refundReference
* @var string|null $refundReference
*/
protected $refundReference;

Expand Down
Loading