From 4390a1932d059abf7a7b4722ca0b46b84be0a2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Tue, 12 Jan 2021 12:15:35 +0100 Subject: [PATCH] Remove phpstan errors from the rest of the code [MAILPOET-3235] --- .../WooCommerce/Events/PurchasedProduct.php | 1 + lib/Config/MP2Migrator.php | 1 + lib/Cron/DaemonHttpRunner.php | 5 ++++- lib/Cron/Workers/SendingQueue/SendingQueue.php | 5 ++++- lib/Cron/Workers/StatsNotifications/Worker.php | 13 +++++++++---- lib/DI/ContainerConfigurator.php | 3 +++ lib/Doctrine/TablePrefixMetadataFactory.php | 2 +- lib/Newsletter/Links/Links.php | 4 ++-- lib/Util/DOM.php | 2 ++ 9 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php b/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php index 75fdbfb985..6db6dfd81c 100644 --- a/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php +++ b/lib/AutomaticEmails/WooCommerce/Events/PurchasedProduct.php @@ -91,6 +91,7 @@ class PurchasedProduct { } $woocommerceProducts = array_map(function($product) { + assert($product instanceof \WP_Post); return [ 'id' => $product->ID, 'name' => $product->post_title, // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps diff --git a/lib/Config/MP2Migrator.php b/lib/Config/MP2Migrator.php index c9577169b0..d46544eb50 100644 --- a/lib/Config/MP2Migrator.php +++ b/lib/Config/MP2Migrator.php @@ -968,6 +968,7 @@ class MP2Migrator { $segmentsIds = implode(',', $segments); return '[mailpoet_subscribers_count segments=' . $segmentsIds . ']'; } + return ''; } /** diff --git a/lib/Cron/DaemonHttpRunner.php b/lib/Cron/DaemonHttpRunner.php index 4af3df293b..5f601cff41 100644 --- a/lib/Cron/DaemonHttpRunner.php +++ b/lib/Cron/DaemonHttpRunner.php @@ -12,7 +12,7 @@ class DaemonHttpRunner { public $timer; public $token; - /** @var Daemon */ + /** @var Daemon|null */ private $daemon; /** @var CronHelper */ @@ -69,6 +69,9 @@ class DaemonHttpRunner { if (!empty($error)) { return $this->abortWithError($error); } + if ($this->daemon === null) { + return $this->abortWithError(WPFunctions::get()->__('Daemon does not set correctly.', 'mailpoet')); + } $this->settingsDaemonData['token'] = $this->token; $this->daemon->run($this->settingsDaemonData); // If we're using the WordPress trigger, check the conditions to stop cron if necessary diff --git a/lib/Cron/Workers/SendingQueue/SendingQueue.php b/lib/Cron/Workers/SendingQueue/SendingQueue.php index d59eab62f0..804d58211f 100644 --- a/lib/Cron/Workers/SendingQueue/SendingQueue.php +++ b/lib/Cron/Workers/SendingQueue/SendingQueue.php @@ -8,6 +8,7 @@ use MailPoet\Cron\Workers\SendingQueue\Tasks\Links; use MailPoet\Cron\Workers\SendingQueue\Tasks\Mailer as MailerTask; use MailPoet\Cron\Workers\SendingQueue\Tasks\Newsletter as NewsletterTask; use MailPoet\Cron\Workers\StatsNotifications\Scheduler as StatsNotificationsScheduler; +use MailPoet\Entities\NewsletterEntity; use MailPoet\Logging\LoggerFactory; use MailPoet\Mailer\MailerError; use MailPoet\Mailer\MailerLog; @@ -167,7 +168,9 @@ class SendingQueue { ['newsletter_id' => $newsletter->id, 'task_id' => $queue->taskId] ); $this->newsletterTask->markNewsletterAsSent($newsletter, $queue); - $this->statsNotificationsScheduler->schedule($this->newslettersRepository->findOneById($newsletter->id)); + $newsletter = $this->newslettersRepository->findOneById($newsletter->id); + assert($newsletter instanceof NewsletterEntity); + $this->statsNotificationsScheduler->schedule($newsletter); } $this->enforceSendingAndExecutionLimits($timer); } diff --git a/lib/Cron/Workers/StatsNotifications/Worker.php b/lib/Cron/Workers/StatsNotifications/Worker.php index 88fce88fc1..1c5e253719 100644 --- a/lib/Cron/Workers/StatsNotifications/Worker.php +++ b/lib/Cron/Workers/StatsNotifications/Worker.php @@ -7,6 +7,7 @@ use MailPoet\Cron\CronHelper; use MailPoet\Entities\NewsletterEntity; use MailPoet\Entities\NewsletterLinkEntity; use MailPoet\Entities\ScheduledTaskEntity; +use MailPoet\Entities\SendingQueueEntity; use MailPoet\Entities\StatsNotificationEntity; use MailPoet\Mailer\Mailer; use MailPoet\Mailer\MetaInfo; @@ -117,8 +118,12 @@ class Worker { throw new \RuntimeException('Missing newsletter entity for statistic notification.'); } $link = $this->newsletterLinkRepository->findTopLinkForNewsletter((int)$newsletter->getId()); - $context = $this->prepareContext($newsletter, $link); - $subject = $newsletter->getLatestQueue()->getNewsletterRenderedSubject(); + $sendingQueue = $newsletter->getLatestQueue(); + if (!$sendingQueue instanceof SendingQueueEntity) { + throw new \RuntimeException('Missing sending queue entity for statistic notification.'); + } + $context = $this->prepareContext($newsletter, $sendingQueue, $link); + $subject = $sendingQueue->getNewsletterRenderedSubject(); return [ 'subject' => sprintf(_x('Stats for email %s', 'title of an automatic email containing statistics (newsletter open rate, click rate, etc)', 'mailpoet'), $subject), 'body' => [ @@ -128,12 +133,12 @@ class Worker { ]; } - private function prepareContext(NewsletterEntity $newsletter, NewsletterLinkEntity $link = null) { + private function prepareContext(NewsletterEntity $newsletter, SendingQueueEntity $sendingQueue, NewsletterLinkEntity $link = null) { $statistics = $this->newsletterStatisticsRepository->getStatistics($newsletter); $clicked = ($statistics->getClickCount() * 100) / $statistics->getTotalSentCount(); $opened = ($statistics->getOpenCount() * 100) / $statistics->getTotalSentCount(); $unsubscribed = ($statistics->getUnsubscribeCount() * 100) / $statistics->getTotalSentCount(); - $subject = $newsletter->getLatestQueue()->getNewsletterRenderedSubject(); + $subject = $sendingQueue->getNewsletterRenderedSubject(); $subscribersCount = $this->subscribersRepository->getTotalSubscribers(); $hasValidApiKey = $this->subscribersFeature->hasValidApiKey(); $context = [ diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index e9af75318b..1b0f990f6c 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -373,6 +373,9 @@ class ContainerConfigurator implements IContainerConfigurator { } public static function getPremiumService($id, ContainerInterface $container = null) { + if ($container === null) { + return null; + } if (!$container->has(IContainerConfigurator::PREMIUM_CONTAINER_SERVICE_SLUG)) { return null; } diff --git a/lib/Doctrine/TablePrefixMetadataFactory.php b/lib/Doctrine/TablePrefixMetadataFactory.php index efeab2d30f..265321ef11 100644 --- a/lib/Doctrine/TablePrefixMetadataFactory.php +++ b/lib/Doctrine/TablePrefixMetadataFactory.php @@ -32,7 +32,7 @@ class TablePrefixMetadataFactory extends ClassMetadataFactory { // prefix tables only after they are saved to cache so the prefix does not get included in cache // (getMetadataFor can call itself recursively but it saves to cache only after the recursive calls) - $isCached = $this->getCacheDriver()->contains($classMetadata->getName() . $this->cacheSalt); + $isCached = ($cache = $this->getCacheDriver()) ? $cache->contains($classMetadata->getName() . $this->cacheSalt) : false; if ($classMetadata instanceof ClassMetadata && $isCached) { $this->addPrefix($classMetadata); $this->prefixedMap[$classMetadata->getName()] = true; diff --git a/lib/Newsletter/Links/Links.php b/lib/Newsletter/Links/Links.php index bdfb07ebcd..274a017d50 100644 --- a/lib/Newsletter/Links/Links.php +++ b/lib/Newsletter/Links/Links.php @@ -73,12 +73,12 @@ class Links { $linkToReplace = $processedLink['link']; $replacementLink = $processedLink['processed_link']; if ($processedLink['type'] == self::LINK_TYPE_SHORTCODE) { - $content = str_replace($linkToReplace, $replacementLink, $content); + $content = str_replace($linkToReplace, $replacementLink, (string)$content); } $content = preg_replace( '/\[(.*?)\](\(' . preg_quote($linkToReplace, '/') . '\))/', '[$1](' . $replacementLink . ')', - $content + (string)$content ); } return [ diff --git a/lib/Util/DOM.php b/lib/Util/DOM.php index 46e0d64d47..a978fbfa96 100644 --- a/lib/Util/DOM.php +++ b/lib/Util/DOM.php @@ -12,6 +12,7 @@ class DOM { */ public static function splitOn(DomNode $bound, DomNode $cutElement) { $ignoreTextAndCommentNodes = false; + $grandparent = $cutElement->parent; for ($parent = $cutElement->parent; $bound != $parent; $parent = $grandparent) { // Clone parent node without children, but with attributes $parent->after($parent->toString()); @@ -23,6 +24,7 @@ class DOM { } // Reattach cut_element and right siblings to grandparent + /* @phpstan-ignore-next-line Because there is a wrong annotation in the library tburry/pquery */ $grandparent = $parent->parent; $indexAfterParent = $parent->index() + 1; $right->move($grandparent, $indexAfterParent);