Skip to content

Commit

Permalink
RATESWSX-311: increase phpstan level
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Sep 12, 2024
1 parent 6733ab0 commit 17dff46
Show file tree
Hide file tree
Showing 30 changed files with 87 additions and 47 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 6
level: 7
paths:
- src
- tests
Expand Down
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
SwitchNegatedTernaryRector::class,
DisallowedEmptyRuleFixerRector::class,
\Rector\Php81\Rector\Array_\FirstClassCallableRector::class,
ExplicitBoolCompareRector::class,
]);

$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
Expand Down
5 changes: 0 additions & 5 deletions src/Bootstrap/AbstractBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,4 @@ final public function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}

final protected function getPluginPath(): string
{
return $this->container->getParameter('kernel.root_dir') . DIRECTORY_SEPARATOR . $this->plugin->getPath();
}
}
1 change: 1 addition & 0 deletions src/Bootstrap/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Database extends AbstractBootstrap

public function injectServices(): void
{
/* @phpstan-ignore-next-line */
$this->connection = $this->container->get(Connection::class);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Bootstrap/PaymentMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ class PaymentMethods extends AbstractBootstrap

public function injectServices(): void
{
/* @phpstan-ignore-next-line */
$this->paymentRepository = $this->container->get('payment_method.repository');
/* @phpstan-ignore-next-line */
$this->paymentMethodDefinition = $this->container->get(PaymentMethodDefinition::class);
/* @phpstan-ignore-next-line */
$this->connection = $this->container->get(Connection::class);
}

Expand Down
1 change: 1 addition & 0 deletions src/Bootstrap/PluginConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PluginConfiguration extends AbstractBootstrap

public function injectServices(): void
{
/* @phpstan-ignore-next-line */
$this->configService = $this->container->get(SystemConfigService::class);
}

Expand Down
1 change: 1 addition & 0 deletions src/Components/AdminOrders/Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function login(Request $request): Response
],
], $context);

/** @var array{scheme: string, host: string, path: ?string} $urlInfo */
$urlInfo = parse_url((string) $saleChannelDomain->getUrl());
$routerContext = $this->router->getContext();
$routerContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public function load(Request $request, SalesChannelContext $context): HandlePaym

$paymentHandlerIdentifier = null;
if ($request->request->getBoolean('updatePayment')) {
/** @var string $orderId */
$orderId = $request->request->get('orderId');

/** @var OrderEntity|null $order */
$order = $this->orderRepository->search(CriteriaHelper::getCriteriaForOrder($orderId), $context->getContext())->first();
if ($order instanceof OrderEntity && ($transaction = $order->getTransactions()->last()) instanceof OrderTransactionEntity) {
$paymentHandlerIdentifier = $transaction->getPaymentMethod()->getHandlerIdentifier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ public static function getSubscribedEvents(): array
];
}

/**
* @param BuildEvent<Head> $buildEvent
*/
public function onPaymentRequest(BuildEvent $buildEvent): void
{
/** @var PaymentRequestData $requestData */
$requestData = $buildEvent->getRequestData();
/** @var Head $head */
$head = $buildEvent->getBuildData();

$ratepayData = RequestHelper::getRatepayData($requestData->getRequestDataBag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ public function __construct(
], methods: ['GET'])]
public function calculateInstallment(Request $request, SalesChannelContext $salesChannelContext, ?string $orderId = null): InstallmentCalculationResponse
{
$type = $request->query->get('type');
$value = (int) $request->query->get('value');
$value = $value ?: 1; // RATESWSX-186: fix that no "0" values can be provided
$type = $request->query->getAlpha('type');
$value = $request->query->getInt('value', 1); // RATESWSX-186: fix that no "0" values can be provided

if ($orderId) {
$order = $this->orderRepository->search(CriteriaHelper::getCriteriaForOrder($orderId), $salesChannelContext->getContext())->first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function calculateInstallmentOffline(InstallmentCalculatorContext $contex
throw new InstallmentCalculationException('Please verify if the payment method is available.');
}

/** @var array{0: InstallmentBuilder, 1: int|float}|array{} $amountBuilders */
/** @var array{0: InstallmentBuilder, 1: int|float}[]|array{} $amountBuilders */
$amountBuilders = [];

foreach ($installmentBuilders as $installmentBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Exception;
use InvalidArgumentException;
use JsonException;
use RatePAY\Model\Request\SubModel\Content\Payment;
use RatePAY\Model\Request\SubModel\Content\Payment\InstallmentDetails;
use RatePAY\Model\Request\SubModel\Content\ShoppingBasket;
Expand Down Expand Up @@ -49,13 +50,16 @@ public static function getSubscribedEvents(): array
];
}

/**
* @param BuildEvent<Payment> $event
* @throws JsonException
*/
public function buildPayment(BuildEvent $event): void
{
/** @var PaymentRequestData $requestData */
$requestData = $event->getRequestData();

if (MethodHelper::isInstallmentMethod($requestData->getTransaction()->getPaymentMethod()->getHandlerIdentifier())) {
/** @var Payment $paymentObject */
$paymentObject = $event->getBuildData();

/** @var DataBag $requestDataBag */ // should be never null, because it is already validated
Expand Down Expand Up @@ -101,6 +105,10 @@ public function buildPayment(BuildEvent $event): void
}
}

/**
* @param BuildEvent<ShoppingBasket> $event
* @return BuildEvent<ShoppingBasket>
*/
public function buildShoppingBasket(BuildEvent $event): BuildEvent
{
/** @var OrderOperationData $requestData */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ public function addRatepayData(ProductPageLoadedEvent $event): void
}

$billingCountyId = $this->getConfig('BillingCountry');
$billingCountry = $this->countryRepository->search(new Criteria([$billingCountyId]), $event->getContext())->first();
$shippingCountyId = $this->getConfig('ShippingCountry');
$paymentMethodId = $this->getConfig('PaymentMethod');

if (!is_string($billingCountyId) || !is_string($shippingCountyId) || !is_string($paymentMethodId)) {
return;
}

$billingCountry = $this->countryRepository->search(new Criteria([$billingCountyId]), $event->getContext())->first();
$shippingCountry = $this->countryRepository->search(new Criteria([$shippingCountyId]), $event->getContext())->first();
if (!$billingCountry instanceof CountryEntity || !$shippingCountry instanceof CountryEntity) {
return;
}

$currency = $event->getSalesChannelContext()->getCurrency();

$paymentMethodId = $this->getConfig('PaymentMethod');

$product = $event->getPage()->getProduct();

// this is a little bit tricky. we need to support the following cases: normal price, configurable price, advanced/tier pricing
Expand Down
2 changes: 1 addition & 1 deletion src/Components/OrderManagement/Service/LineItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function createLineItem(OrderEntity $orderEntity, string $label, float $g
->setPayload([])
->setPriceDefinition(new QuantityPriceDefinition(
$grossAmount,
new TaxRuleCollection([new TaxRule($taxRate, 100)]),
new TaxRuleCollection([new TaxRule((float) $taxRate, 100)]),
1
));
$lineItem->addExtension(OrderConverter::ORIGINAL_ID, new IdStruct($lineItem->getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
#[Route(path: '/reload-config', name: 'ratepay.profile.config.reload', methods: ['POST'])]
public function reloadProfileConfiguration(Request $request): Response
{
if ($id = $request->request->get('id')) {
if ($id = $request->request->getAlnum('id')) {
try {
$configs = $this->profileManagement->refreshProfileConfigs([$id]);
$profileConfig = $configs->get($id);
Expand Down
6 changes: 1 addition & 5 deletions src/Components/RatepayApi/Dto/CheckoutOperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace Ratepay\RpayPayments\Components\RatepayApi\Dto;

use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

Expand All @@ -23,8 +22,5 @@ public function getSalesChannelContext(): SalesChannelContext;

public function getRequestDataBag(): DataBag;

/**
* @return OrderCustomerEntity|CustomerEntity
*/
public function getCustomer();
public function getCustomer(): CustomerEntity;
}
5 changes: 3 additions & 2 deletions src/Components/RatepayApi/Dto/PaymentRequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Ratepay\RpayPayments\Components\RatepayApi\Dto;

use InvalidArgumentException;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
Expand Down Expand Up @@ -74,8 +75,8 @@ public function getPaymentMethodId(): string
return $this->getOrder()->getTransactions()->last()->getPaymentMethodId();
}

public function getCustomer(): ?CustomerEntity
public function getCustomer(): CustomerEntity
{
return $this->getOrder()->getOrderCustomer()->getCustomer();
return $this->getOrder()->getOrderCustomer()?->getCustomer() ?: throw new InvalidArgumentException('customer has not been loaded for order');
}
}
10 changes: 10 additions & 0 deletions src/Components/RatepayApi/Event/BuildEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@

namespace Ratepay\RpayPayments\Components\RatepayApi\Event;

use RatePAY\Model\Request\SubModel\AbstractModel;
use Ratepay\RpayPayments\Components\RatepayApi\Dto\AbstractRequestData;
use Symfony\Contracts\EventDispatcher\Event;

/**
* @template T of AbstractModel
*/
class BuildEvent extends Event
{
/**
* @param T|null $buildData
*/
public function __construct(
private readonly AbstractRequestData $requestData,
private readonly ?object $buildData = null
Expand All @@ -27,6 +34,9 @@ public function getRequestData(): AbstractRequestData
return $this->requestData;
}

/**
* @return T|null
*/
public function getBuildData(): ?object
{
return $this->buildData;
Expand Down
14 changes: 12 additions & 2 deletions src/Components/RatepayApi/Factory/AbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@
namespace Ratepay\RpayPayments\Components\RatepayApi\Factory;

use InvalidArgumentException;
use RatePAY\Model\Request\SubModel\AbstractModel;
use Ratepay\RpayPayments\Components\RatepayApi\Dto\AbstractRequestData;
use Ratepay\RpayPayments\Components\RatepayApi\Event\BuildEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @template ReturnType of AbstractModel|null
*/
abstract class AbstractFactory
{
public function __construct(
protected EventDispatcherInterface $eventDispatcher
) {
}

/**
* @return ReturnType
*/
final public function getData(AbstractRequestData $requestData): ?object
{
if (!$this->isSupported($requestData)) {
Expand All @@ -31,8 +38,8 @@ final public function getData(AbstractRequestData $requestData): ?object

$data = $this->_getData($requestData);
if ($data !== null) {
/** @var BuildEvent $event */
$event = $this->eventDispatcher->dispatch(new BuildEvent($requestData, $data), static::class);
$event = new BuildEvent($requestData, $data);
$this->eventDispatcher->dispatch($event, static::class);
$data = $event->getBuildData();
}

Expand All @@ -41,5 +48,8 @@ final public function getData(AbstractRequestData $requestData): ?object

abstract protected function isSupported(AbstractRequestData $requestData): bool;

/**
* @return ReturnType
*/
abstract protected function _getData(AbstractRequestData $requestData): ?object;
}
4 changes: 2 additions & 2 deletions src/Components/RatepayApi/Factory/CustomerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @method Customer getData(CheckoutOperationInterface $requestData)
* @extends AbstractFactory<Customer>
*/
class CustomerFactory extends AbstractFactory
{
Expand Down Expand Up @@ -150,7 +150,7 @@ protected function _getData(AbstractRequestData $requestData): ?object
}
}

if ($requestData instanceof PaymentRequestData && $requestDataBag->has('bankData')) {
if ($requestDataBag->has('bankData')) {
/** @var RequestDataBag $bankData */
$bankData = $requestDataBag->get('bankData');
$bankAccount = new BankAccount();
Expand Down
2 changes: 1 addition & 1 deletion src/Components/RatepayApi/Factory/ExternalFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;

/**
* @method External getData(AbstractRequestData $requestData)
* @extends AbstractFactory<External>
*/
class ExternalFactory extends AbstractFactory
{
Expand Down
2 changes: 1 addition & 1 deletion src/Components/RatepayApi/Factory/HeadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @method Head getData(AbstractRequestData $requestData)
* @extends AbstractFactory<Head>
*/
class HeadFactory extends AbstractFactory
{
Expand Down
2 changes: 1 addition & 1 deletion src/Components/RatepayApi/Factory/InvoiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Shopware\Core\Checkout\Document\DocumentEntity;

/**
* @method Invoicing getData(AbstractRequestData $requestData)
* @extends AbstractFactory<Invoicing|null>
*/
class InvoiceFactory extends AbstractFactory
{
Expand Down
2 changes: 1 addition & 1 deletion src/Components/RatepayApi/Factory/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Ratepay\RpayPayments\Components\RatepayApi\Dto\PaymentRequestData;

/**
* @method Payment getData(PaymentRequestData $requestData)
* @extends AbstractFactory<Payment>
*/
class PaymentFactory extends AbstractFactory
{
Expand Down
3 changes: 1 addition & 2 deletions src/Components/RatepayApi/Factory/ShoppingBasketFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Ratepay\RpayPayments\Components\RatepayApi\Dto\AbstractRequestData;
use Ratepay\RpayPayments\Components\RatepayApi\Dto\OperationDataWithBasket;
use Ratepay\RpayPayments\Components\RatepayApi\Dto\OrderOperationData;
use Ratepay\RpayPayments\Components\RatepayApi\Dto\PaymentRequestData;
use Ratepay\RpayPayments\Components\RatepayApi\Exception\EmptyBasketException;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
Expand All @@ -30,7 +29,7 @@
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;

/**
* @method ShoppingBasket getData(PaymentRequestData|OrderOperationData $requestData)
* @extends AbstractFactory<ShoppingBasket>
*/
class ShoppingBasketFactory extends AbstractFactory
{
Expand Down
8 changes: 4 additions & 4 deletions src/Components/RatepayApi/Service/Request/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ private function _getRequestHead(AbstractRequestData $requestData): Head
{
$head = $this->getRequestHead($requestData);

/** @var BuildEvent $event */
$event = $this->eventDispatcher->dispatch(new BuildEvent($requestData, $head), static::class . self::EVENT_BUILD_HEAD);
$event = new BuildEvent($requestData, $head);
$this->eventDispatcher->dispatch($event, static::class . self::EVENT_BUILD_HEAD);

return $event->getBuildData();
}
Expand All @@ -196,8 +196,8 @@ private function _getRequestContent(AbstractRequestData $requestData): ?Content
{
$content = $this->getRequestContent($requestData);

/** @var BuildEvent $event */
$event = $this->eventDispatcher->dispatch(new BuildEvent($requestData, $content), static::class . self::EVENT_BUILD_CONTENT);
$event = new BuildEvent($requestData, $content);
$this->eventDispatcher->dispatch($event, static::class . self::EVENT_BUILD_CONTENT);

return $event->getBuildData();
}
Expand Down
Loading

0 comments on commit 17dff46

Please sign in to comment.