From f3c5f29dab561ef24ea385e4194634c9081a53df Mon Sep 17 00:00:00 2001 From: s4ddly <110701663+s4ddly@users.noreply.github.com> Date: Wed, 6 Mar 2024 10:21:16 +0100 Subject: [PATCH] Add PHPCS PSR12 standard (#45) --- .dev-tools/.php-cs.xml | 11 ++++++ .dev-tools/composer.json | 9 +++-- .dev-tools/composer.lock | 36 +++++++++++++++++- .github/workflows/sca.yaml | 5 ++- Controller/tpay/Notification.php | 2 +- Model/ResourceModel/Token.php | 2 +- Model/ResourceModel/Token/Collection.php | 2 +- Model/Tokens.php | 2 +- Service/TpayService.php | 48 ++++++++++++++++++------ Service/TpayTokensService.php | 19 ++++++++-- 10 files changed, 112 insertions(+), 24 deletions(-) create mode 100644 .dev-tools/.php-cs.xml diff --git a/.dev-tools/.php-cs.xml b/.dev-tools/.php-cs.xml new file mode 100644 index 0000000..dc30a9a --- /dev/null +++ b/.dev-tools/.php-cs.xml @@ -0,0 +1,11 @@ + + + Tpay PHPCS coding standard + ../ + */vendor/* + */*.js + + + + + diff --git a/.dev-tools/composer.json b/.dev-tools/composer.json index f21153f..b12c12f 100644 --- a/.dev-tools/composer.json +++ b/.dev-tools/composer.json @@ -8,7 +8,8 @@ "tpay-com/coding-standards": "^1.0.1", "tpay-com/tpay-openapi-php": "^1.3", "tpay-com/tpay-php": "^2.3.4", - "vimeo/psalm": "^5.15.0" + "vimeo/psalm": "^5.15.0", + "squizlabs/php_codesniffer": "*" }, "repositories": [ { @@ -19,7 +20,8 @@ "scripts": { "cs:fixer": "./vendor/bin/php-cs-fixer fix -vvv --diff", "cs:phpstan": "./vendor/bin/phpstan", - "cs:psalm": "./vendor/bin/psalm" + "cs:psalm": "./vendor/bin/psalm", + "cs:sniffer": "./vendor/bin/phpcs --standard=.php-cs.xml -n" }, "config": { "allow-plugins": { @@ -27,7 +29,8 @@ "kubawerlos/composer-smaller-lock": true, "magento/composer-dependency-version-audit-plugin": false, "magento/magento-composer-installer": false, - "php-http/discovery": false + "php-http/discovery": false, + "dealerdirect/phpcodesniffer-composer-installer": true } } } diff --git a/.dev-tools/composer.lock b/.dev-tools/composer.lock index da69f3e..286af36 100644 --- a/.dev-tools/composer.lock +++ b/.dev-tools/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e91ecc8244e34cc4efd5dee7a771c5cd", + "content-hash": "8c9b4cab6ef339dc03ce47dae2cb7089", "packages": [ { "name": "amphp/amp", @@ -4602,6 +4602,40 @@ ], "description": "A PHP framework for managing Public Key Infrastructures. It comprises X.509 public key certificates, attribute certificates, certification requests and certification path validation." }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.9.0", + "source": { + "type": "git", + "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", + "reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/d63cee4890a8afaf86a22e51ad4d97c91dd4579b", + "reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "bin": [ + "bin/phpcbf", + "bin/phpcs" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "license": [ + "BSD-3-Clause" + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards." + }, { "name": "symfony/config", "version": "v7.0.0", diff --git a/.github/workflows/sca.yaml b/.github/workflows/sca.yaml index 2161e92..0c8c22b 100644 --- a/.github/workflows/sca.yaml +++ b/.github/workflows/sca.yaml @@ -13,7 +13,7 @@ jobs: - uses: shivammathur/setup-php@v2 with: php-version: 8.2 - extensions: none, bcmath, curl, dom, fileinfo, gd, iconv, intl, mbstring, pdo_mysql, simplexml, soap, sockets, tokenizer, xsl, zip + extensions: none, bcmath, curl, dom, fileinfo, gd, iconv, intl, mbstring, pdo_mysql, simplexml, soap, sockets, tokenizer, xsl, xmlwriter, zip coverage: none - run: composer validate --strict @@ -27,6 +27,9 @@ jobs: - working-directory: .dev-tools run: composer cs:fixer -- --dry-run + - working-directory: .dev-tools + run: composer cs:sniffer + - working-directory: .dev-tools run: composer cs:phpstan diff --git a/Controller/tpay/Notification.php b/Controller/tpay/Notification.php index e5725bb..4f44a71 100644 --- a/Controller/tpay/Notification.php +++ b/Controller/tpay/Notification.php @@ -125,7 +125,7 @@ private function extractNotification(StoreInterface $store): Response } $this->saveCard($notification, $orderId); - $this->tpayService->SetOrderStatus($orderId, $notification, $this->tpay); + $this->tpayService->setOrderStatus($orderId, $notification, $this->tpay); return (new Response())->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE'); } catch (Exception $e) { diff --git a/Model/ResourceModel/Token.php b/Model/ResourceModel/Token.php index cbdca06..a0b2b9f 100644 --- a/Model/ResourceModel/Token.php +++ b/Model/ResourceModel/Token.php @@ -6,7 +6,7 @@ class Token extends AbstractDb { - protected function _construct() + protected function _construct()// phpcs:ignore { $this->_init('tpay_credit_cards', 'id'); } diff --git a/Model/ResourceModel/Token/Collection.php b/Model/ResourceModel/Token/Collection.php index 410cd3d..2e3728f 100644 --- a/Model/ResourceModel/Token/Collection.php +++ b/Model/ResourceModel/Token/Collection.php @@ -6,7 +6,7 @@ class Collection extends AbstractCollection { - protected function _construct() + protected function _construct()// phpcs:ignore { $this->_init('tpaycom\magento2basic\Model\Tokens', 'tpaycom\magento2basic\Model\ResourceModel\Token'); } diff --git a/Model/Tokens.php b/Model/Tokens.php index 5ffef94..0604897 100644 --- a/Model/Tokens.php +++ b/Model/Tokens.php @@ -113,7 +113,7 @@ public function deleteToken($requestToken) return $this; } - protected function _construct() + protected function _construct() // phpcs:ignore { $this->_init('tpaycom\magento2basic\Model\ResourceModel\Token'); } diff --git a/Service/TpayService.php b/Service/TpayService.php index 327d219..d217709 100644 --- a/Service/TpayService.php +++ b/Service/TpayService.php @@ -11,7 +11,6 @@ use Magento\Sales\Api\Data\OrderPaymentInterface; use Magento\Sales\Model\Order; use Magento\Sales\Model\Order\Invoice; -use Magento\Sales\Model\Order\Payment; use Magento\Sales\Model\Order\Payment\Operations\RegisterCaptureNotificationOperation; use Magento\Sales\Model\Order\Payment\State\CommandInterface; use Magento\Sales\Model\Order\Payment\Transaction; @@ -93,7 +92,7 @@ public function getPayment(string $orderId): OrderPaymentInterface * * @return bool|OrderInterface */ - public function SetOrderStatus(string $orderId, array $validParams, TpayInterface $tpayModel) + public function setOrderStatus(string $orderId, array $validParams, TpayInterface $tpayModel) { $order = $this->getOrderById($orderId); @@ -158,8 +157,9 @@ public function setCardOrderStatus($orderId, array $validParams, $tpayModel) $emailNotify = false; $order = $this->updateTransactionId($order, $validParams); + $amountCheck = (float) number_format((float) $validParams['amount'], 2, '.', '') !== $orderAmount; - if (!isset($validParams['status']) || 'correct' !== $validParams['status'] || ((float) number_format((float) $validParams['amount'], 2, '.', '') !== $orderAmount)) { + if (!isset($validParams['status']) || 'correct' !== $validParams['status'] || $amountCheck) { $comment = __('Payment has been declined. ').'
'.$transactionDesc; $this->addCommentToHistory($orderId, $comment); } else { @@ -222,7 +222,9 @@ protected function getCardTransactionDesc($validParams) } $transactionDesc = (is_null($error)) ? ' ' : ' Reason: '.$error.''; - $transactionDesc .= (isset($validParams['test_mode'])) && 1 === (int) $validParams['test_mode'] ? ' TEST ' : ' '; + $transactionDesc .= (isset($validParams['test_mode'])) && 1 === (int) $validParams['test_mode'] + ? ' TEST ' + : ' '; return $transactionDesc; } @@ -233,10 +235,20 @@ protected function getCardTransactionDesc($validParams) * @param float|string $amount * @param bool|int $skipFraudDetection */ - private function registerCaptureNotificationTpay(OrderPaymentInterface $payment, $amount, array $validParams, $skipFraudDetection = false) - { + private function registerCaptureNotificationTpay( + OrderPaymentInterface $payment, + $amount, + array $validParams, + $skipFraudDetection = false + ) { // @var $payment Payment - $payment->setTransactionId($this->transactionManager->generateTransactionId($payment, Transaction::TYPE_CAPTURE, $payment->getAuthorizationTransaction())); + $payment->setTransactionId( + $this->transactionManager->generateTransactionId( + $payment, + Transaction::TYPE_CAPTURE, + $payment->getAuthorizationTransaction() + ) + ); $order = $payment->getOrder(); $amount = (float) $amount; @@ -271,10 +283,20 @@ private function registerCaptureNotificationTpay(OrderPaymentInterface $payment, $payment->addTransactionCommentsToOrder($transaction, $message); } - private function registerCardCaptureNotificationTpay(OrderPaymentInterface $payment, $amount, $validParams, $skipFraudDetection = false) - { + private function registerCardCaptureNotificationTpay( + OrderPaymentInterface $payment, + $amount, + $validParams, + $skipFraudDetection = false + ) { // @var $payment Payment - $payment->setTransactionId($this->transactionManager->generateTransactionId($payment, Transaction::TYPE_CAPTURE, $payment->getAuthorizationTransaction())); + $payment->setTransactionId( + $this->transactionManager->generateTransactionId( + $payment, + Transaction::TYPE_CAPTURE, + $payment->getAuthorizationTransaction() + ) + ); $order = $payment->getOrder(); $amount = (float) $amount; @@ -285,7 +307,11 @@ private function registerCardCaptureNotificationTpay(OrderPaymentInterface $paym } $orderCurrency = array_search($orderCurrencyCode, CurrencyCodesDictionary::CODES); // register new capture - if (!$invoice && $payment->isCaptureFinal($amount) && ($orderCurrency === (int) $validParams['currency'] || $orderCurrencyCode === $validParams['currency'])) { + if ( + !$invoice + && $payment->isCaptureFinal($amount) + && ($orderCurrency === (int) $validParams['currency'] || $orderCurrencyCode === $validParams['currency']) + ) { $invoice = $order->prepareInvoice()->register(); $invoice->setOrder($order); $order->addRelatedObject($invoice); diff --git a/Service/TpayTokensService.php b/Service/TpayTokensService.php index bba6463..8605152 100644 --- a/Service/TpayTokensService.php +++ b/Service/TpayTokensService.php @@ -14,14 +14,25 @@ class TpayTokensService extends Tokens /** @var ResourceConnection */ private $resourceConnection; - public function __construct(Context $context, Registry $registry, ResourceConnection $resourceConnection, $resource = null, ?AbstractDb $resourceCollection = null, array $data = []) - { + public function __construct( + Context $context, + Registry $registry, + ResourceConnection $resourceConnection, + $resource = null, + ?AbstractDb $resourceCollection = null, + array $data = [] + ) { $this->resourceConnection = $resourceConnection; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } - public function setCustomerToken(string $customerId, ?string $token, string $shortCode, string $vendor, ?string $crc = null) - { + public function setCustomerToken( + string $customerId, + ?string $token, + string $shortCode, + string $vendor, + ?string $crc = null + ) { $tokenEntity = $this->load($token, 'cli_auth'); if (!$tokenEntity->getId()) {