From 2312af010a8cbad2c37183606c2ace818e7f0569 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 10 Jan 2023 23:33:48 +0100 Subject: [PATCH] apply static code analysis changes --- phpstan.neon | 5 ++++- .../Controller/Admin/AdminOrder.php | 16 +++++++++------ .../Controller/Admin/AdminUser.php | 15 +++++++++----- src/Application/Model/Configuration.php | 4 ++-- .../Exceptions/successfullySentException.php | 4 +++- .../Model/MessageTypes/AbstractMessage.php | 8 +++++--- src/Application/Model/MessageTypes/Sms.php | 20 ++++++++++++------- src/Application/Model/OrderRecipients.php | 13 +++++++----- src/Application/Model/RequestFactory.php | 7 ++++--- src/Application/Model/UserRecipients.php | 12 +++++++---- src/Modules/Core/EmailCore.php | 5 +++++ src/Setup/Actions.php | 18 ++++++++++++----- src/Setup/Events.php | 4 ++-- 13 files changed, 87 insertions(+), 44 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 6c8b9a1..f15cc97 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,10 @@ parameters: - ../../oxid-esales/oxideshop-ce/source/oxfunctions.php paths: - src + excludePaths: + - src/tests level: 9 phpVersion: 70100 ignoreErrors: - - '#Psr\\Container\\ContainerExceptionInterface is not subtype of Throwable#' \ No newline at end of file + - '#Psr\\Container\\ContainerExceptionInterface is not subtype of Throwable#' + - '#Cannot cast mixed to string.#' \ No newline at end of file diff --git a/src/Application/Controller/Admin/AdminOrder.php b/src/Application/Controller/Admin/AdminOrder.php index ea0bcf4..047fb11 100644 --- a/src/Application/Controller/Admin/AdminOrder.php +++ b/src/Application/Controller/Admin/AdminOrder.php @@ -44,11 +44,14 @@ class AdminOrder extends AdminController public function __construct() { - $this->item = d3GetOxidDIC()->get('d3ox.linkmobility.'.Order::class); - d3GetOxidDIC()->set(OrderRecipients::class.'.args.order', $this->item); - $this->itemRecipients = d3GetOxidDIC()->get(OrderRecipients::class); - - $this->item->load($this->getEditObjectId()); + /** @var Order $item */ + $item = d3GetOxidDIC()->get('d3ox.linkmobility.'.Order::class); + $this->item = $item; + d3GetOxidDIC()->set(OrderRecipients::class.'.args.order', $item); + /** @var OrderRecipients $itemRecipients */ + $itemRecipients = d3GetOxidDIC()->get(OrderRecipients::class); + $this->itemRecipients = $itemRecipients; + $item->load($this->getEditObjectId()); $this->addTplParam('recipient', $this->getRecipientFromCurrentItem()); @@ -81,6 +84,7 @@ class AdminOrder extends AdminController } catch (noRecipientFoundException $e) { /** @var Language $lang */ $lang = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class); + /** @var string $message */ $message = $lang->translateString($e->getMessage()); /** @var UtilsView $utilsView */ $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); @@ -101,7 +105,7 @@ class AdminOrder extends AdminController try { $utilsView->addErrorToDisplay($this->sendMessage()); } catch (noRecipientFoundException|InvalidArgumentException $e) { - $utilsView->addErrorToDisplay($e); + $utilsView->addErrorToDisplay($e->getMessage()); } } diff --git a/src/Application/Controller/Admin/AdminUser.php b/src/Application/Controller/Admin/AdminUser.php index c23f5c2..cb08393 100644 --- a/src/Application/Controller/Admin/AdminUser.php +++ b/src/Application/Controller/Admin/AdminUser.php @@ -46,10 +46,14 @@ class AdminUser extends AdminController public function __construct() { - $this->item = d3GetOxidDIC()->get('d3ox.linkmobility.'.User::class); - d3GetOxidDIC()->set(UserRecipients::class.".args.user", $this->item); - $this->itemRecipients = d3GetOxidDIC()->get(UserRecipients::class); - $this->item->load($this->getEditObjectId()); + /** @var User $item */ + $item = d3GetOxidDIC()->get('d3ox.linkmobility.'.User::class); + $this->item = $item; + d3GetOxidDIC()->set(UserRecipients::class.".args.user", $item); + /** @var UserRecipients $itemRecipients */ + $itemRecipients = d3GetOxidDIC()->get(UserRecipients::class); + $this->itemRecipients = $itemRecipients; + $item->load($this->getEditObjectId()); $this->addTplParam('recipient', $this->getRecipientFromCurrentItem()); @@ -82,6 +86,7 @@ class AdminUser extends AdminController } catch (noRecipientFoundException $e) { /** @var Language $lang */ $lang = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class); + /** @var string $message */ $message = $lang->translateString($e->getMessage()); /** @var UtilsView $utilsView */ $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); @@ -102,7 +107,7 @@ class AdminUser extends AdminController try { $utilsView->addErrorToDisplay($this->sendMessage()); } catch (noRecipientFoundException|InvalidArgumentException $e) { - $utilsView->addErrorToDisplay($e); + $utilsView->addErrorToDisplay($e->getMessage()); } } diff --git a/src/Application/Model/Configuration.php b/src/Application/Model/Configuration.php index b831d6d..3182533 100644 --- a/src/Application/Model/Configuration.php +++ b/src/Application/Model/Configuration.php @@ -133,8 +133,8 @@ class Configuration } /** - * @param array $customFields - * @return array + * @param string[] $customFields + * @return string[] */ public function sanitizeKeys(array $customFields): array { diff --git a/src/Application/Model/Exceptions/successfullySentException.php b/src/Application/Model/Exceptions/successfullySentException.php index 3843cd5..a54dde8 100644 --- a/src/Application/Model/Exceptions/successfullySentException.php +++ b/src/Application/Model/Exceptions/successfullySentException.php @@ -29,8 +29,10 @@ class successfullySentException extends StandardException */ public function __construct($messageCount = 1, $code = 0, Exception $previous = null) { + /** @var Language $language */ + $language = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class); /** @var string $format */ - $format = d3GetOxidDIC()->get('d3ox.linkmobility.'.Language::class)->translateString('D3LM_EXC_SMS_SUCC_SENT'); + $format = $language->translateString('D3LM_EXC_SMS_SUCC_SENT'); $message = sprintf($format, $messageCount); parent::__construct($message, $code, $previous); diff --git a/src/Application/Model/MessageTypes/AbstractMessage.php b/src/Application/Model/MessageTypes/AbstractMessage.php index 4247d6a..4c496fb 100644 --- a/src/Application/Model/MessageTypes/AbstractMessage.php +++ b/src/Application/Model/MessageTypes/AbstractMessage.php @@ -16,6 +16,7 @@ declare(strict_types=1); namespace D3\Linkmobility4OXID\Application\Model\MessageTypes; use D3\LinkmobilityClient\RecipientsList\RecipientsList; +use D3\LinkmobilityClient\RecipientsList\RecipientsListInterface; use D3\LinkmobilityClient\Response\ResponseInterface; use D3\LinkmobilityClient\ValueObject\Recipient; use Exception; @@ -37,7 +38,7 @@ abstract class AbstractMessage /** @var ResponseInterface */ protected $response; - /** @var RecipientsList */ + /** @var RecipientsListInterface */ protected $recipients; /** @@ -66,6 +67,7 @@ abstract class AbstractMessage */ protected function setRemark(string $userId, string $recipients, string $message): void { + /** @var Remark $remark */ $remark = d3GetOxidDIC()->get('d3ox.linkmobility.'.Remark::class); $remark->assign([ 'oxtype' => self::REMARK_IDENT, @@ -84,10 +86,10 @@ abstract class AbstractMessage } /** - * @param RecipientsList $recipients + * @param RecipientsListInterface $recipients * @return void */ - protected function setRecipients(RecipientsList $recipients) + protected function setRecipients(RecipientsListInterface $recipients) { $this->recipients = $recipients; } diff --git a/src/Application/Model/MessageTypes/Sms.php b/src/Application/Model/MessageTypes/Sms.php index 37ffd71..e5a9d50 100644 --- a/src/Application/Model/MessageTypes/Sms.php +++ b/src/Application/Model/MessageTypes/Sms.php @@ -60,7 +60,9 @@ class Sms extends AbstractMessage return $return; } catch (noRecipientFoundException $e) { $this->getLogger()->warning($e->getMessage()); - d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class)->addErrorToDisplay($e); + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + $utilsView->addErrorToDisplay($e); } return false; @@ -107,7 +109,9 @@ class Sms extends AbstractMessage return $return; } catch (noRecipientFoundException $e) { $this->getLogger()->warning($e->getMessage()); - d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class)->addErrorToDisplay($e); + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + $utilsView->addErrorToDisplay($e); } return false; @@ -146,11 +150,11 @@ class Sms extends AbstractMessage } /** - * @param RecipientsList $recipientsList + * @param RecipientsListInterface $recipientsList * * @return bool */ - public function sendCustomRecipientMessage(RecipientsList $recipientsList): bool + public function sendCustomRecipientMessage(RecipientsListInterface $recipientsList): bool { try { $this->response = $response = $this->submitMessage($recipientsList); @@ -159,7 +163,9 @@ class Sms extends AbstractMessage } catch (abortSendingExceptionInterface|GuzzleException|ApiException|InvalidArgumentException $e) { $this->getLogger()->warning($e->getMessage()); // Oxid does not accept throwable interface only exceptions according to definition - d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class)->addErrorToDisplay($e->getMessage()); + /** @var UtilsView $utilsView */ + $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); + $utilsView->addErrorToDisplay($e->getMessage()); } return false; @@ -192,12 +198,12 @@ class Sms extends AbstractMessage } /** - * @param RecipientsList $recipientsList + * @param RecipientsListInterface $recipientsList * @return ResponseInterface * @throws ApiException * @throws GuzzleException */ - protected function submitMessage(RecipientsList $recipientsList): ResponseInterface + protected function submitMessage(RecipientsListInterface $recipientsList): ResponseInterface { $this->setRecipients($recipientsList); /** @var Configuration $configuration */ diff --git a/src/Application/Model/OrderRecipients.php b/src/Application/Model/OrderRecipients.php index 25a381f..bd9bacc 100644 --- a/src/Application/Model/OrderRecipients.php +++ b/src/Application/Model/OrderRecipients.php @@ -53,19 +53,20 @@ class OrderRecipients } /** - * @param $phoneFieldName - * @param $countryIdFieldName + * @param string $phoneFieldName + * @param string $countryIdFieldName * @return Recipient|null */ - protected function getSmsRecipientByField($phoneFieldName, $countryIdFieldName): ?Recipient + protected function getSmsRecipientByField(string $phoneFieldName, string $countryIdFieldName): ?Recipient { + /** @var Country $country */ + $country = d3GetOxidDIC()->get('d3ox.linkmobility.'.Country::class); try { /** @var string $content */ $content = $this->order->getFieldData($phoneFieldName) ?: ''; $content = trim($content); if (strlen($content)) { - $country = d3GetOxidDIC()->get('d3ox.linkmobility.'.Country::class); /** @var string $countryId */ $countryId = $this->order->getFieldData(trim($countryIdFieldName)); $country->load($countryId); @@ -77,7 +78,9 @@ class OrderRecipients return $recipient; } } catch (NumberParseException|RecipientException $e) { - d3GetOxidDIC()->get(LoggerHandler::class)->getLogger()->info( + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $loggerHandler->getLogger()->info( $e->getMessage(), [$content, $country->getFieldData('oxisoalpha2')] ); diff --git a/src/Application/Model/RequestFactory.php b/src/Application/Model/RequestFactory.php index 5f26836..6d689a2 100644 --- a/src/Application/Model/RequestFactory.php +++ b/src/Application/Model/RequestFactory.php @@ -30,14 +30,15 @@ class RequestFactory extends \D3\LinkmobilityClient\SMS\RequestFactory $configuration = d3GetOxidDIC()->get(Configuration::class); /** parent call */ + /** @var SmsRequestInterface $request */ $request = $this->d3CallMockableFunction([\D3\LinkmobilityClient\SMS\RequestFactory::class, 'getSmsRequest']); d3GetOxidDIC()->setParameter(Sender::class.'.args.number', $configuration->getSmsSenderNumber()); d3GetOxidDIC()->setParameter(Sender::class.'.args.iso2countrycode', $configuration->getSmsSenderCountry()); + /** @var Sender $sender */ + $sender = d3GetOxidDIC()->get(Sender::class); $request->setTestMode($configuration->getTestMode()) - ->setSenderAddress( - d3GetOxidDIC()->get(Sender::class) - ) + ->setSenderAddress($sender) ->setSenderAddressType(RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL); return $request; diff --git a/src/Application/Model/UserRecipients.php b/src/Application/Model/UserRecipients.php index 2e5c3a8..92758a1 100644 --- a/src/Application/Model/UserRecipients.php +++ b/src/Application/Model/UserRecipients.php @@ -53,18 +53,20 @@ class UserRecipients } /** - * @param $fieldName + * @param string $fieldName * @return Recipient|null */ - protected function getSmsRecipientByField($fieldName): ?Recipient + protected function getSmsRecipientByField(string $fieldName): ?Recipient { + /** @var Country $country */ + $country = d3GetOxidDIC()->get('d3ox.linkmobility.'.Country::class); + try { /** @var string $content */ $content = $this->user->getFieldData($fieldName) ?: ''; $content = trim($content); if (strlen($content)) { - $country = d3GetOxidDIC()->get('d3ox.linkmobility.'.Country::class); /** @var string $countryId */ $countryId = $this->user->getFieldData('oxcountryid'); $country->load($countryId); @@ -75,7 +77,9 @@ class UserRecipients return $recipient; } } catch (NumberParseException|RecipientException $e) { - d3GetOxidDIC()->get(LoggerHandler::class)->getLogger()->info( + /** @var LoggerHandler $loggerHandler */ + $loggerHandler = d3GetOxidDIC()->get(LoggerHandler::class); + $loggerHandler->getLogger()->info( $e->getMessage(), [$content, $country->getFieldData('oxisoalpha2')] ); diff --git a/src/Modules/Core/EmailCore.php b/src/Modules/Core/EmailCore.php index a205cce..de58d67 100644 --- a/src/Modules/Core/EmailCore.php +++ b/src/Modules/Core/EmailCore.php @@ -47,6 +47,7 @@ class EmailCore extends EmailCore_parent public function sendOrderEmailToUser($order, $subject = null) { // $ret = parent::sendOrderEmailToUser($order, $subject); + /** @var bool $ret */ $ret = $this->d3CallMockableFunction([EmailCore_parent::class, 'sendOrderEmailToUser'], [$order, $subject]); $this->d3SendOrderFinishedMessageToUser($order); @@ -65,6 +66,7 @@ class EmailCore extends EmailCore_parent public function sendSendedNowMail($order, $subject = null) { // $ret = parent::sendSendedNowMail($order, $subject); + /** @var bool $ret */ $ret = $this->d3CallMockableFunction([EmailCore_parent::class, 'sendSendedNowMail'], [$order, $subject]); $this->d3SendedNowMessage($order); @@ -82,6 +84,7 @@ class EmailCore extends EmailCore_parent */ public function d3SendOrderFinishedMessageToUser(Order $order): void { + /** @var MessageSender $messageSender */ $messageSender = d3GetOxidDIC()->get(MessageSender::class); $messageSender->sendOrderFinishedMessage($order, $this->d3GetOrderFinishedSmsMessageBody($order)); } @@ -111,6 +114,7 @@ class EmailCore extends EmailCore_parent */ public function d3SendedNowMessage(Order $order): void { + /** @var MessageSender $messageSender */ $messageSender = d3GetOxidDIC()->get(MessageSender::class); $messageSender->sendSendedNowMessage($order, $this->d3GetSendedNowSmsMessageBody($order)); } @@ -140,6 +144,7 @@ class EmailCore extends EmailCore_parent */ public function d3SendCancelMessage(Order $order): void { + /** @var MessageSender $messageSender */ $messageSender = d3GetOxidDIC()->get(MessageSender::class); $messageSender->sendCancelOrderMessage($order, $this->d3GetCancelOrderSmsMessageBody($order)); } diff --git a/src/Setup/Actions.php b/src/Setup/Actions.php index 9655c8f..f09792c 100644 --- a/src/Setup/Actions.php +++ b/src/Setup/Actions.php @@ -17,8 +17,10 @@ namespace D3\Linkmobility4OXID\Setup; use D3\Linkmobility4OXID\Application\Model\MessageTypes\AbstractMessage; use Doctrine\DBAL\Driver\Exception as DoctrineDriverException; +use Doctrine\DBAL\Driver\ResultStatement; use Doctrine\DBAL\Exception as DoctrineException; use Doctrine\DBAL\Query\QueryBuilder; +use Doctrine\DBAL\Statement; use Monolog\Logger; use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface; use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database; @@ -30,6 +32,7 @@ use OxidEsales\Eshop\Core\Exception\StandardException; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\UtilsView; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactory; use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -55,7 +58,7 @@ class Actions $logger->error($e->getMessage()); /** @var UtilsView $utilsView */ $utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class); - $utilsView->addErrorToDisplay($e); + $utilsView->addErrorToDisplay($e->getMessage()); } } @@ -64,7 +67,7 @@ class Actions /** * Regenerate views for changed tables */ - public function regenerateViews() + public function regenerateViews(): void { /** @var DbMetaDataHandler $oDbMetaDataHandler */ $oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.linkmobility.'.DbMetaDataHandler::class); @@ -101,8 +104,10 @@ class Actions */ protected function getRemarkTypeFieldType(): string { + /** @var QueryBuilderFactory $queryBuilderFactory */ + $queryBuilderFactory = $this->getContainer()->get(QueryBuilderFactoryInterface::class); /** @var QueryBuilder $qb */ - $qb = $this->getContainer()->get(QueryBuilderFactoryInterface::class)->create(); + $qb = $queryBuilderFactory->create(); $qb->select('column_type') ->from('INFORMATION_SCHEMA.COLUMNS') ->where( @@ -122,7 +127,10 @@ class Actions ) ); - return (string) $qb->execute()->fetchOne(); + /** @var Statement $statement */ + $statement = $qb->execute(); + + return (string) $statement->fetchOne(); } /** @@ -150,7 +158,7 @@ class Actions } /** - * @return array + * @return string[] * @throws DoctrineDriverException * @throws DoctrineException */ diff --git a/src/Setup/Events.php b/src/Setup/Events.php index 7e32872..bdc4bc9 100644 --- a/src/Setup/Events.php +++ b/src/Setup/Events.php @@ -33,7 +33,7 @@ class Events * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public static function onActivate() + public static function onActivate(): void { /** @var Actions $actions */ $actions = d3GetOxidDIC()->get(Actions::class); @@ -41,7 +41,7 @@ class Events $actions->regenerateViews(); } - public static function onDeactivate() + public static function onDeactivate(): void { } }