From ac0bdd712d2da3fde1bc784a6c2dd6a4be547053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Wed, 18 Dec 2024 06:21:41 -0300 Subject: [PATCH 1/2] PHPAY-24-phpstan-9: feat: adding phpstan level 9 --- src/Contracts/GatewayInterface.php | 20 ++++ src/Gateways/Asaas/AsaasGateway.php | 6 +- .../Asaas/Enums/InvoiceStatusEnum.php | 14 +-- .../Asaas/Interface/AsaasGatewayInterface.php | 20 ++++ .../Asaas/Requests/AsaasChargeRequest.php | 8 +- .../Asaas/Requests/AsaasCustomerRequest.php | 6 +- .../Asaas/Requests/AsaasPixRequest.php | 14 ++- .../Asaas/Resources/Charge/Charge.php | 35 ++++--- .../Charge/Interface/ChargeInterface.php | 92 ++++++++++++++++++- .../Asaas/Resources/Customer/Customer.php | 20 ++-- .../Customer/Interface/CustomerInterface.php | 53 +++++++++++ .../Webhook/Interface/WebhookInterface.php | 34 +++++++ .../Asaas/Resources/Webhook/Webhook.php | 17 ++-- src/Gateways/Asaas/Traits/HasAsaasClient.php | 19 ++-- src/PHPay.php | 6 +- tests/Pest.php | 7 +- 16 files changed, 301 insertions(+), 70 deletions(-) diff --git a/src/Contracts/GatewayInterface.php b/src/Contracts/GatewayInterface.php index 2bbed84..03396e9 100644 --- a/src/Contracts/GatewayInterface.php +++ b/src/Contracts/GatewayInterface.php @@ -4,7 +4,27 @@ interface GatewayInterface { + /** + * get resource customer from gateway. + * + * @param array $customer + * @return object + */ public function customer(array $customer = []): object; + + /** + * get resource charge from gateway. + * + * @param array $charge + * @return object + */ public function charge(array $charge = []): object; + + /** + * get resource webhook from gateway. + * + * @param array $webhook + * @return object + */ public function webhook(array $webhook = []): object; } diff --git a/src/Gateways/Asaas/AsaasGateway.php b/src/Gateways/Asaas/AsaasGateway.php index 34604c4..f1237ad 100644 --- a/src/Gateways/Asaas/AsaasGateway.php +++ b/src/Gateways/Asaas/AsaasGateway.php @@ -37,7 +37,7 @@ public function __construct( /** * customer * - * @param array $customer + * @param array $customer * @return Customer */ public function customer(array $customer = []): Customer @@ -50,7 +50,7 @@ public function customer(array $customer = []): Customer /** * charge * - * @param array $charge + * @param array $charge * @return Charge */ public function charge(array $charge = []): Charge @@ -61,7 +61,7 @@ public function charge(array $charge = []): Charge /** * webhook * - * @param array $webhook + * @param array $webhook * @return Webhook */ public function webhook(array $webhook = []): Webhook diff --git a/src/Gateways/Asaas/Enums/InvoiceStatusEnum.php b/src/Gateways/Asaas/Enums/InvoiceStatusEnum.php index da52a21..f1148fc 100644 --- a/src/Gateways/Asaas/Enums/InvoiceStatusEnum.php +++ b/src/Gateways/Asaas/Enums/InvoiceStatusEnum.php @@ -10,11 +10,11 @@ enum InvoiceStatusEnum: string case OVERDUE = 'OVERDUE'; case REFUNDED = 'REFUNDED'; case REFUND_REQUESTED = 'REFUNDED_REQUESTED'; - case REFUND_IN_PROGRESS = 'REFUNDED_REQUESTED'; - case CHARGEBACK_REQUESTED = 'REFUNDED_REQUESTED'; - case CHARGEBACK_DISPUT = 'REFUNDED_REQUESTED'; - case AWAITING_CHARGEBACK_REVERSAL = 'REFUNDED_REQUESTED'; - case DUNNING_REQUESTED = 'DUNING'; - case DUNNING_RECEIVED = 'DUNING'; - case AWAITING_RISK_ANALYSIS = 'REFUNDED_REQUESTED'; + case REFUND_IN_PROGRESS = 'REFUND_IN_PROGRESS'; + case CHARGEBACK_REQUESTED = 'CHARGEBACK_REQUESTED'; + case CHARGEBACK_DISPUT = 'CHARGEBACK_DISPUT'; + case AWAITING_CHARGEBACK_REVERSAL = 'AWAITING_CHARGEBACK_REVERSAL'; + case DUNNING_REQUESTED = 'DUNNING_REQUESTED'; + case DUNNING_RECEIVED = 'DUNNING_RECEIVED'; + case AWAITING_RISK_ANALYSIS = 'AWAITING_RISK_ANALYSIS'; } diff --git a/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php b/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php index b5e0b2f..72fcb95 100644 --- a/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php +++ b/src/Gateways/Asaas/Interface/AsaasGatewayInterface.php @@ -9,7 +9,27 @@ interface AsaasGatewayInterface extends GatewayInterface { + /** + * get resource customer from gateway. + * + * @param array $customer + * @return Customer + */ public function customer(array $customer = []): Customer; + + /** + * get resource charge from gateway. + * + * @param array $charge + * @return Charge + */ public function charge(array $charge = []): Charge; + + /** + * get resource webhook from gateway. + * + * @param array $webhook + * @return Webhook + */ public function webhook(array $webhook = []): Webhook; } diff --git a/src/Gateways/Asaas/Requests/AsaasChargeRequest.php b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php index 74393f4..1b4d6fe 100644 --- a/src/Gateways/Asaas/Requests/AsaasChargeRequest.php +++ b/src/Gateways/Asaas/Requests/AsaasChargeRequest.php @@ -9,29 +9,33 @@ class AsaasChargeRequest /** * validate customer and charge data * - * @param array $customer - * @param array $charge + * @param array $charge * @return void */ public static function validate(array $charge): void { if (!isset($charge['customer']) && !is_string($charge['customer'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->charge->customer, 400); } if (!isset($charge['billingType'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->charge->billingType, 400); } if (!BillingTypeEnum::tryFrom($charge['billingType'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->charge->billingType, 400); } if (!isset($charge['value']) && !is_numeric($charge['value'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->charge->value, 400); } if (!isset($charge['dueDate']) && !is_string($charge['dueDate'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->charge->dueDate, 400); } } diff --git a/src/Gateways/Asaas/Requests/AsaasCustomerRequest.php b/src/Gateways/Asaas/Requests/AsaasCustomerRequest.php index 4b485b4..3247c75 100644 --- a/src/Gateways/Asaas/Requests/AsaasCustomerRequest.php +++ b/src/Gateways/Asaas/Requests/AsaasCustomerRequest.php @@ -7,16 +7,20 @@ class AsaasCustomerRequest /** * validate customer * - * @param array $customer + * @param array $customer + * @property-read string $name + * @property-read string $cpfCnpj * @return void */ public static function validate(array $customer): void { if (!isset($customer['name'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->name, 400); } if (!isset($customer['cpfCnpj'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->cpfCnpj, 400); } } diff --git a/src/Gateways/Asaas/Requests/AsaasPixRequest.php b/src/Gateways/Asaas/Requests/AsaasPixRequest.php index 2f68455..5af61b8 100644 --- a/src/Gateways/Asaas/Requests/AsaasPixRequest.php +++ b/src/Gateways/Asaas/Requests/AsaasPixRequest.php @@ -2,30 +2,38 @@ namespace PHPay\Gateways\Asaas\Requests; -use stdClass; - class AsaasPixRequest { + /** + * validate pix + * + * @param array $pix + * @return void + */ public static function validate(array $pix): void { if (!isset($pix['description'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->description); } if (!isset($pix['amount'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->amount); } if (!isset($pix['due_date'])) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->due_date->required); } if ($pix['due_date'] < date('Y-m-d')) { + /** @phpstan-ignore property.notFound */ throw new \InvalidArgumentException(self::messages()->due_date->date); } } - public static function messages(): stdClass + public static function messages(): object { return (object) [ 'description' => 'Asaas: Descrição do pagamento é obrigatório para o Asaas', diff --git a/src/Gateways/Asaas/Resources/Charge/Charge.php b/src/Gateways/Asaas/Resources/Charge/Charge.php index 62cbda7..16bc5bd 100644 --- a/src/Gateways/Asaas/Resources/Charge/Charge.php +++ b/src/Gateways/Asaas/Resources/Charge/Charge.php @@ -20,14 +20,14 @@ class Charge implements ChargeInterface private Client $client; /** - * @var array + * @var array */ private array $filter = []; /** * construct * - * @param array $charge + * @param array $charge */ public function __construct( private string $token, @@ -39,14 +39,12 @@ public function __construct( if (!empty($charge)) { $this->charge = $charge; } - - return $this; } /** * set filters * - * @param array $filters + * @param array $filters * @return ChargeInterface */ public function setFilters(array $filters): ChargeInterface @@ -72,7 +70,7 @@ public function setCustomer(string $customerId): ChargeInterface /** * find charges by id * - * @return array + * @return array */ public function find(string $id): array { @@ -82,7 +80,7 @@ public function find(string $id): array /** * get all charges * - * @return array + * @return array */ public function getAll(): array { @@ -95,6 +93,7 @@ public function getAll(): array * create charge * * @return string + * @return array * @see fields available in https://docs.asaas.com/reference/criar-nova-cobranca */ public function create(): array @@ -108,8 +107,8 @@ public function create(): array * update charge * * @param string $id - * @param array $data - * @return array + * @param array $data + * @return array */ public function update(string $id, array $data): array { @@ -120,7 +119,7 @@ public function update(string $id, array $data): array * destroy charge * * @param string $id - * @return array + * @return bool */ public function destroy(string $id): bool { @@ -131,7 +130,7 @@ public function destroy(string $id): bool * restore charge * * @param string $id - * @return array + * @return array */ public function restore(string $id): array { @@ -142,7 +141,7 @@ public function restore(string $id): array * get status charge * * @param string $id - * @return array + * @return array */ public function getStatus(string $id): array { @@ -153,9 +152,9 @@ public function getStatus(string $id): array * get digitable line * * @param string $id - * @return array + * @return mixed */ - public function getDigitableLine(string $id): string + public function getDigitableLine(string $id): mixed { return $this->get("payments/{$id}/identificationField")['identificationField']; } @@ -164,7 +163,7 @@ public function getDigitableLine(string $id): string * get qrcode pix * * @param string $id - * @return array + * @return array */ public function getQrCodePix(string $id): array { @@ -175,8 +174,8 @@ public function getQrCodePix(string $id): array * confirm receipt * * @param string $id - * @param array $data - * @return array + * @param array $data + * @return array */ public function confirmReceipt(string $id, array $data): array { @@ -187,7 +186,7 @@ public function confirmReceipt(string $id, array $data): array * undo confirm receipt * * @param string $id - * @return array + * @return array */ public function undoConfirmReceipt(string $id): array { diff --git a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php index 25eb0b9..9e1f4b5 100644 --- a/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php +++ b/src/Gateways/Asaas/Resources/Charge/Interface/ChargeInterface.php @@ -4,17 +4,107 @@ interface ChargeInterface { + /** + * get all charges + * + * @return array + */ public function getAll(): array; + + /** + * find charge by id + * + * @param string $id + * @return array + */ public function find(string $id): array; + + /** + * create charge + * + * @return array + */ public function create(): array; + + /** + * update charge by id + * + * @param string $id + * @param array $data + * @return array + */ public function update(string $id, array $data): array; + + /** + * delete charge by id + * + * @param string $id + * @return bool + */ public function destroy(string $id): bool; + + /** + * restore charge by id + * + * @param string $id + * @return array + */ public function restore(string $id): array; + + /** + * get charge notifications by customer + * + * @param string $customer + * @return ChargeInterface + */ public function setCustomer(string $customer): ChargeInterface; + + /** + * set filter + * + * @param array $filters + * @return ChargeInterface + */ public function setFilters(array $filters): ChargeInterface; + + /** + * get status charge by id + * + * @param string $id + * @return array + */ public function getStatus(string $id): array; - public function getDigitableLine(string $id): string; + + /** + * get charge digit line by id + * + * @param string $id + * @return mixed + */ + public function getDigitableLine(string $id): mixed; + + /** + * get charge qr code pix by id + * + * @param string $id + * @return array + */ public function getQrCodePix(string $id): array; + + /** + * confirm receipt charge by id + * + * @param string $id + * @param array $data + * @return array + */ public function confirmReceipt(string $id, array $data): array; + + /** + * undo confirm receipt charge by id + * + * @param string $id + * @return array + */ public function undoConfirmReceipt(string $id): array; } diff --git a/src/Gateways/Asaas/Resources/Customer/Customer.php b/src/Gateways/Asaas/Resources/Customer/Customer.php index 2e75c94..5c2ba88 100644 --- a/src/Gateways/Asaas/Resources/Customer/Customer.php +++ b/src/Gateways/Asaas/Resources/Customer/Customer.php @@ -20,14 +20,14 @@ class Customer implements CustomerInterface private Client $client; /** - * @var array + * @var array */ private array $filter = []; /** * construct * - * @param array $customer + * @param array $customer */ public function __construct( private string $token, @@ -39,14 +39,12 @@ public function __construct( if (!empty($customer)) { $this->customer = $customer; } - - return $this; } /** * get all customers * - * @return array + * @return array */ public function getAll(): array { @@ -59,7 +57,7 @@ public function getAll(): array * get customer by id * * @param string $id - * @return array + * @return array */ public function find(string $id): array { @@ -69,7 +67,7 @@ public function find(string $id): array /** * create customer * - * @return array + * @return array * @see available fields in https://docs.asaas.com/reference/criar-novo-cliente */ public function create(): array @@ -83,7 +81,7 @@ public function create(): array * update customer * * @param string $id - * @return array + * @return array * @see available fields in https://docs.asaas.com/reference/criar-novo-cliente */ public function update(string $id): array @@ -106,7 +104,7 @@ public function destroy(string $id): bool * restore customer deleted * * @param string $id - * @return array + * @return array */ public function restore(string $id): array { @@ -117,7 +115,7 @@ public function restore(string $id): array * get notifications * * @param string $id - * @return array + * @return array */ public function getNotifications(string $id): array { @@ -127,7 +125,7 @@ public function getNotifications(string $id): array /** * set filter * - * @param array $filter + * @param array $filter * @return CustomerInterface */ public function setFilter(array $filter = []): CustomerInterface diff --git a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php index 7882408..1e7a41c 100644 --- a/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php +++ b/src/Gateways/Asaas/Resources/Customer/Interface/CustomerInterface.php @@ -4,12 +4,65 @@ interface CustomerInterface { + /** + * find customer by id + * + * @param string $id + * @return array + */ public function find(string $id): array; + + /** + * get all customers + * + * @return array + */ public function getAll(): array; + + /** + * create customer + * + * @return array + */ public function create(): array; + + /** + * update customer by id + * + * @param string $id + * @return array + */ public function update(string $id): array; + + /** + * delete customer by id + * + * @param string $id + * @return bool + */ public function destroy(string $id): bool; + + /** + * restore customer by id + * + * @param string $id + * @return array + */ public function restore(string $id): array; + + /** + * get customer notifications by id + * + * @param string $id + * @return array + */ public function getNotifications(string $id): array; + + /** + * set filter + * + * @param array $filter + * @return self + */ public function setFilter(array $filter = []): self; } diff --git a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php index d966caa..63a9f34 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php +++ b/src/Gateways/Asaas/Resources/Webhook/Interface/WebhookInterface.php @@ -4,9 +4,43 @@ interface WebhookInterface { + /** + * create webhook + * + * @param array $webhook + * @return array + */ public function create(array $webhook = []): array; + + /** + * get all webhooks + * + * @return array + */ public function getAll(): array; + + /** + * find webhook by id + * + * @param string $id + * @return array + */ public function find(string $id): array; + + /** + * update webhook by id + * + * @param string $id + * @param array $webhook + * @return array + */ public function update(string $id, array $webhook = []): array; + + /** + * delete webhook by id + * + * @param string $id + * @return bool + */ public function destroy(string $id): bool; } diff --git a/src/Gateways/Asaas/Resources/Webhook/Webhook.php b/src/Gateways/Asaas/Resources/Webhook/Webhook.php index f799080..fe62505 100644 --- a/src/Gateways/Asaas/Resources/Webhook/Webhook.php +++ b/src/Gateways/Asaas/Resources/Webhook/Webhook.php @@ -21,7 +21,7 @@ class Webhook implements WebhookInterface /** * construct * - * @param array $webhook + * @param array $webhook */ public function __construct( private string $token, @@ -33,14 +33,13 @@ public function __construct( if (!empty($webhook)) { $this->webhook = $webhook; } - - return $this; } /** * construct * - * @param array $webhook + * @param array $webhook + * @return array */ public function create(array $webhook = []): array { @@ -50,7 +49,7 @@ public function create(array $webhook = []): array /** * get all webhooks * - * @return array + * @return array */ public function getAll(): array { @@ -61,7 +60,7 @@ public function getAll(): array * get webhook by id * * @param string $id - * @return array + * @return array */ public function find(string $id): array { @@ -72,8 +71,8 @@ public function find(string $id): array * update webhook by id * * @param string $id - * @param array $webhook - * @return array + * @param array $webhook + * @return array */ public function update(string $id, array $webhook = []): array { @@ -84,7 +83,7 @@ public function update(string $id, array $webhook = []): array * delete webhook by id * * @param string $id - * @return array + * @return bool */ public function destroy(string $id): bool { diff --git a/src/Gateways/Asaas/Traits/HasAsaasClient.php b/src/Gateways/Asaas/Traits/HasAsaasClient.php index 0dca1aa..593f992 100644 --- a/src/Gateways/Asaas/Traits/HasAsaasClient.php +++ b/src/Gateways/Asaas/Traits/HasAsaasClient.php @@ -8,6 +8,8 @@ trait HasAsaasClient { /** * boot client + * + * @return Client */ protected function clientAsaasBoot(): Client { @@ -29,7 +31,8 @@ protected function clientAsaasBoot(): Client * get data * * @param string $endpoint - * @return array + * @param array $filters + * @return array */ protected function get(string $endpoint, array $filters = []): array { @@ -42,7 +45,7 @@ protected function get(string $endpoint, array $filters = []): array ->getBody() ->getContents(); - return json_decode($content, true); + return (array) json_decode($content, true); } catch (\Exception $e) { return [ 'error' => $e->getCode(), @@ -55,8 +58,8 @@ protected function get(string $endpoint, array $filters = []): array * post data * * @param string $endpoint - * @param array $data - * @return array + * @param array $data + * @return array */ protected function post(string $endpoint, array $data = []): array { @@ -69,7 +72,7 @@ protected function post(string $endpoint, array $data = []): array ->getBody() ->getContents(); - return json_decode($content, true); + return (array) json_decode($content, true); } catch (\Exception $e) { return [ 'error' => $e->getCode(), @@ -82,8 +85,8 @@ protected function post(string $endpoint, array $data = []): array * put data * * @param string $endpoint - * @param array $data - * @return array + * @param array $data + * @return array */ protected function put(string $endpoint, array $data): array { @@ -96,7 +99,7 @@ protected function put(string $endpoint, array $data): array ->getBody() ->getContents(); - return json_decode($content, true); + return (array) json_decode($content, true); } catch (\Exception $e) { return [ 'error' => $e->getCode(), diff --git a/src/PHPay.php b/src/PHPay.php index a3af370..b2cb48c 100644 --- a/src/PHPay.php +++ b/src/PHPay.php @@ -20,7 +20,7 @@ public function __construct( /** * get resource customer from gateway. * - * @param array $customer + * @param array $customer * @return object */ public function customer(array $customer = []): object @@ -31,7 +31,7 @@ public function customer(array $customer = []): object /** * get resource charge from gateway. * - * @param array $charge + * @param array $charge * @return object */ public function charge(array $charge = []): object @@ -42,7 +42,7 @@ public function charge(array $charge = []): object /** * get resource webhook from gateway. * - * @param array $webhook + * @param array $webhook * @return object */ public function webhook(array $webhook = []): object diff --git a/tests/Pest.php b/tests/Pest.php index 883a813..fc10e77 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -39,7 +39,6 @@ | */ -function something() -{ - // .. -} +// function something() +// { +// } From 097365c501bb1e0a8701888427dcec11b382b14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Lucas?= Date: Wed, 18 Dec 2024 06:22:38 -0300 Subject: [PATCH 2/2] PHPAY-24-phpstan-9: feat: adding phpstan level 9 --- .husky/pre-push | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.husky/pre-push b/.husky/pre-push index e349335..42deb54 100755 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -19,20 +19,20 @@ echo "🟢 Pint Ok!" echo "\n" -# echo "=============================================" -# echo "Running PHPStan tests 🧪" -# echo "=============================================" +echo "=============================================" +echo "Running PHPStan tests 🧪" +echo "=============================================" -# pint_output=$(./vendor/bin/phpstan analyse -l 9 src tests 2>&1) +pint_output=$(./vendor/bin/phpstan analyse -l 9 src tests 2>&1) -# if [ $? -ne 0 ]; then -# echo "PHPStan tests failed. Please fix the issues before pushing." -# exit 1 -# fi +if [ $? -ne 0 ]; then + echo "PHPStan tests failed. Please fix the issues before pushing." + exit 1 +fi -# echo "🟢 PHPStan Ok!" +echo "🟢 PHPStan Ok!" -# echo "\n" +echo "\n" # echo "=============================================" # echo "Running pest tests 🧪"