[ticket/17135] Fix Psalm and other issues

PHPBB3-17135
This commit is contained in:
rxu 2023-06-09 11:56:32 +07:00
parent a7b5369138
commit 83294fed84
No known key found for this signature in database
GPG key ID: 955F0567380E586A
8 changed files with 84 additions and 66 deletions

View file

@ -25,6 +25,7 @@ services:
- '@template.twig.extensions.collection' - '@template.twig.extensions.collection'
- '@template.twig.lexer' - '@template.twig.lexer'
- '%core.template.cache_path%' - '%core.template.cache_path%'
- '%core.php_ext%'
messenger.method.email: messenger.method.email:
class: phpbb\messenger\method\phpbb_email class: phpbb\messenger\method\phpbb_email

View file

@ -245,6 +245,7 @@ class message
foreach ($this->recipients as $recipient) foreach ($this->recipients as $recipient)
{ {
/** @psalm-suppress InvalidTemplateParam */
$messenger_collection_iterator = $messenger->getIterator(); $messenger_collection_iterator = $messenger->getIterator();
while ($messenger_collection_iterator->valid()) while ($messenger_collection_iterator->valid())
{ {

View file

@ -22,7 +22,6 @@ use phpbb\log\log_interface;
use phpbb\path_helper; use phpbb\path_helper;
use phpbb\request\request; use phpbb\request\request;
use phpbb\messenger\queue; use phpbb\messenger\queue;
use phpbb\template\template;
use phpbb\template\twig\lexer; use phpbb\template\twig\lexer;
use phpbb\user; use phpbb\user;
@ -58,13 +57,16 @@ abstract class base
/** @var path_helper */ /** @var path_helper */
protected $path_helper; protected $path_helper;
/** @var string */
protected $root_path;
/** @var request */ /** @var request */
protected $request; protected $request;
/** @var string */ /** @var string */
protected $subject = ''; protected $subject = '';
/** @var template */ /** @var \phpbb\template\template */
protected $template; protected $template;
/** @var string */ /** @var string */
@ -97,6 +99,7 @@ abstract class base
* @param service_collection $twig_extensions_collection * @param service_collection $twig_extensions_collection
* @param lexer $twig_lexer * @param lexer $twig_lexer
* @param string $template_cache_path * @param string $template_cache_path
* @param string $phpbb_root_path
*/ */
function __construct( function __construct(
config $config, config $config,
@ -110,7 +113,8 @@ abstract class base
manager $ext_manager, manager $ext_manager,
service_collection $twig_extensions_collection, service_collection $twig_extensions_collection,
lexer $twig_lexer, lexer $twig_lexer,
$template_cache_path $template_cache_path,
$phpbb_root_path
) )
{ {
$this->config = $config; $this->config = $config;
@ -125,6 +129,7 @@ abstract class base
$this->twig_extensions_collection = $twig_extensions_collection; $this->twig_extensions_collection = $twig_extensions_collection;
$this->twig_lexer = $twig_lexer; $this->twig_lexer = $twig_lexer;
$this->template_cache_path = $template_cache_path; $this->template_cache_path = $template_cache_path;
$this->root_path = $phpbb_root_path;
$this->set_use_queue(); $this->set_use_queue();
} }
@ -135,6 +140,12 @@ abstract class base
*/ */
abstract public function get_id(); abstract public function get_id();
/**
* Check if the messenger method is enabled
* @return bool
*/
abstract public function is_enabled();
/** /**
* Sets the use of messenger queue flag * Sets the use of messenger queue flag
* *
@ -360,8 +371,8 @@ abstract class base
* Event to modify the template before parsing * Event to modify the template before parsing
* *
* @event core.modify_notification_template * @event core.modify_notification_template
* @var string subject The message subject * @var string subject The message subject
* @var template template The (readonly) template object * @var string template The (readonly) template object
* @since 3.2.4-RC1 * @since 3.2.4-RC1
* @changed 4.0.0-a1 Removed vars: method, break. * @changed 4.0.0-a1 Removed vars: method, break.
*/ */
@ -375,8 +386,8 @@ abstract class base
* Event to modify notification message text after parsing * Event to modify notification message text after parsing
* *
* @event core.modify_notification_message * @event core.modify_notification_message
* @var string message The message text * @var string message The message text
* @var string subject The message subject * @var string subject The message subject
* @since 3.1.11-RC1 * @since 3.1.11-RC1
* @changed 4.0.0-a1 Removed vars: method, break. * @changed 4.0.0-a1 Removed vars: method, break.
*/ */
@ -455,7 +466,7 @@ abstract class base
*/ */
protected function setup_template() protected function setup_template()
{ {
if (isset($this->template) && $this->template instanceof template) if (isset($this->template) && $this->template instanceof \phpbb\template\template)
{ {
return; return;
} }
@ -487,8 +498,8 @@ abstract class base
/** /**
* Set template paths to load * Set template paths to load
* *
* @param string $path_name Email template path name * @param string|array $path_name Email template path name
* @param string $paths Email template paths * @param string|array $paths Email template paths
* @return void * @return void
*/ */
protected function set_template_paths($path_name, $paths) protected function set_template_paths($path_name, $paths)

View file

@ -17,7 +17,13 @@ use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\DateHeader;
use Symfony\Component\Mime\Header\Headers; use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Header\IdentificationHeader;
use Symfony\Component\Mime\Header\MailboxHeader;
use Symfony\Component\Mime\Header\MailboxListHeader;
use Symfony\Component\Mime\Header\PathHeader;
use Symfony\Component\Mime\Header\UnstructuredHeader;
/** /**
* Messenger class * Messenger class
@ -55,7 +61,10 @@ class phpbb_email extends base
*/ */
protected $dsn = ''; protected $dsn = '';
/** @var string */ /** @var Email */
protected $email;
/** @var Address */
protected $from; protected $from;
/** @var Headers */ /** @var Headers */
@ -73,13 +82,13 @@ class phpbb_email extends base
*/ */
protected $mail_priority = Email::PRIORITY_NORMAL; protected $mail_priority = Email::PRIORITY_NORMAL;
/** @var queue */ /** @var \phpbb\messenger\queue */
protected $queue; protected $queue;
/** @var string */ /** @var Address */
protected $replyto = ''; protected $replyto;
/** @var Transport */ /** @var \Symfony\Component\Mailer\Transport\AbstractTransport */
protected $transport; protected $transport;
/** /**
@ -99,8 +108,7 @@ class phpbb_email extends base
} }
/** /**
* Check if the messenger method is enabled * {@inheritDoc}
* @return void
*/ */
public function is_enabled() public function is_enabled()
{ {
@ -114,13 +122,12 @@ class phpbb_email extends base
{ {
$this->email = new Email(); $this->email = new Email();
$this->headers = $this->email->getHeaders(); $this->headers = $this->email->getHeaders();
$this->msg = $this->replyto = $this->from = ''; $this->subject = $this->msg = '';
$this->mail_priority = Email::PRIORITY_NORMAL; $this->mail_priority = Email::PRIORITY_NORMAL;
$this->subject = $this->additional_headers = []; $this->additional_headers = [];
$this->msg = '';
$this->use_queue = true; $this->use_queue = true;
unset($this->template); unset($this->template, $this->replyto, $this->from);
} }
/** /**
@ -180,7 +187,7 @@ class phpbb_email extends base
} }
$cc = new Address($address, trim($realname)); $cc = new Address($address, trim($realname));
$this->email->getCc() ? $this->email->addCc($to) : $this->email->cc($to); $this->email->getCc() ? $this->email->addCc($cc) : $this->email->cc($cc);
} }
/** /**
@ -198,7 +205,7 @@ class phpbb_email extends base
} }
$bcc = new Address($address, trim($realname)); $bcc = new Address($address, trim($realname));
$this->email->getBcc() ? $this->email->addBcc($to) : $this->email->bcc($to); $this->email->getBcc() ? $this->email->addBcc($bcc) : $this->email->bcc($bcc);
} }
/** /**
@ -397,7 +404,7 @@ class phpbb_email extends base
/** /**
* Get Symfony Mailer transport DSN * Get Symfony Mailer transport DSN
* *
* @return void * @return string
*/ */
public function get_dsn() public function get_dsn()
{ {
@ -418,7 +425,7 @@ class phpbb_email extends base
$this->transport = Transport::fromDsn($this->dsn); $this->transport = Transport::fromDsn($this->dsn);
if ($this->config['smtp_delivery'] && !in_array($this->dsn, ['null://null', 'sendmail://default'])) if ($this->config['smtp_delivery'] && method_exists($this->transport, 'getStream'))
{ {
// Set ssl context options, see http://php.net/manual/en/context.ssl.php // Set ssl context options, see http://php.net/manual/en/context.ssl.php
$options['ssl'] = [ $options['ssl'] = [
@ -460,8 +467,8 @@ class phpbb_email extends base
* Event to send message via external transport * Event to send message via external transport
* *
* @event core.notification_message_process * @event core.notification_message_process
* @var bool break Flag indicating if the function return after hook * @var string break Flag indicating if the function return after hook
* @var Email email The Symfony Email object * @var string email The Symfony Email object
* @since 3.2.4-RC1 * @since 3.2.4-RC1
* @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, subject, msg. * @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, subject, msg.
*/ */
@ -477,25 +484,25 @@ class phpbb_email extends base
{ {
$mailer->send($email); $mailer->send($email);
} }
catch (TransportExceptionInterface $e) catch (\Symfony\Component\Mailer\Exception\TransportExceptionInterface $e)
{ {
$this->error('EMAIL', $e->getDebug()); $this->error($e->getDebug());
continue; continue;
} }
} }
} }
// No more data for this object? Unset it // No more data for this object? Unset it
if (!count($this->queue_data[$queue_object_name]['data'])) if (!count($queue_data[$queue_object_name]['data']))
{ {
unset($this->queue_data[$queue_object_name]); unset($queue_data[$queue_object_name]);
} }
} }
/** /**
* Get mailer transport object * Get mailer transport object
* *
* @return Transport Symfony Mailer transport object * @return \Symfony\Component\Mailer\Transport\TransportInterface Symfony Mailer transport object
*/ */
public function get_transport() public function get_transport()
{ {
@ -526,7 +533,7 @@ class phpbb_email extends base
* @event core.notification_message_email * @event core.notification_message_email
* @var string subject The message subject * @var string subject The message subject
* @var string msg The message text * @var string msg The message text
* @var Email email The Symfony Email object * @var string email The Symfony Email object
* @since 3.2.4-RC1 * @since 3.2.4-RC1
* @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, break * @changed 4.0.0-a1 Added vars: email. Removed vars: addresses, break
*/ */
@ -567,7 +574,7 @@ class phpbb_email extends base
* Modify data before sending out emails with PHP's mail function * Modify data before sending out emails with PHP's mail function
* *
* @event core.phpbb_mail_before * @event core.phpbb_mail_before
* @var Email email The Symfony Email object * @var string email The Symfony Email object
* @var string subject The message subject * @var string subject The message subject
* @var string msg The message text * @var string msg The message text
* @var string headers The email headers * @var string headers The email headers
@ -591,7 +598,7 @@ class phpbb_email extends base
{ {
$mailer->send($this->email); $mailer->send($this->email);
} }
catch (TransportExceptionInterface $e) catch (\Symfony\Component\Mailer\Exception\TransportExceptionInterface $e)
{ {
$this->error($e->getDebug()); $this->error($e->getDebug());
return false; return false;
@ -601,7 +608,7 @@ class phpbb_email extends base
* Execute code after sending out emails with PHP's mail function * Execute code after sending out emails with PHP's mail function
* *
* @event core.phpbb_mail_after * @event core.phpbb_mail_after
* @var Email email The Symfony Email object * @var string email The Symfony Email object
* @var string subject The message subject * @var string subject The message subject
* @var string msg The message text * @var string msg The message text
* @var string headers The email headers * @var string headers The email headers

View file

@ -29,7 +29,7 @@ class phpbb_jabber extends base
/** @var string */ /** @var string */
protected $connect_server; protected $connect_server;
/** @var resource */ /** @var resource|null */
protected $connection = null; protected $connection = null;
/** @var bool */ /** @var bool */
@ -47,7 +47,7 @@ class phpbb_jabber extends base
/** @var string */ /** @var string */
protected $password; protected $password;
/** @var string */ /** @var int */
protected $port; protected $port;
/** @var string */ /** @var string */
@ -127,8 +127,7 @@ class phpbb_jabber extends base
} }
/** /**
* Check if the messenger method is enabled * {@inheritDoc}
* @return void
*/ */
public function is_enabled() public function is_enabled()
{ {
@ -187,10 +186,10 @@ class phpbb_jabber extends base
* Set port to connect to server * Set port to connect to server
* use_ssl flag should be set first * use_ssl flag should be set first
* *
* @param string $port Port to connect to server * @param int $port Port to connect to server
* @return $this * @return $this
*/ */
public function port($port = '') public function port($port = 5222)
{ {
$this->port = ($port) ? $port : 5222; $this->port = ($port) ? $port : 5222;
@ -334,6 +333,7 @@ class phpbb_jabber extends base
} }
$this->session = []; $this->session = [];
/** @psalm-suppress InvalidPropertyAssignmentValue */
return fclose($this->connection); return fclose($this->connection);
} }
@ -352,7 +352,7 @@ class phpbb_jabber extends base
/** /**
* Initiates login (using data from contructor, after calling connect()) * Initiates login (using data from contructor, after calling connect())
* *
* @return bool * @return bool|void
*/ */
public function login() public function login()
{ {
@ -401,11 +401,8 @@ class phpbb_jabber extends base
*/ */
public function reset() public function reset()
{ {
$this->msg = ''; $this->subject = $this->msg = '';
$this->to = []; $this->additional_headers = $this->to = [];
$this->subject = $this->additional_headers = [];
$this->msg = '';
$this->use_queue = true; $this->use_queue = true;
unset($this->template); unset($this->template);
} }
@ -438,7 +435,6 @@ class phpbb_jabber extends base
$package_size = $queue_data[$queue_object_name]['package_size'] ?? 0; $package_size = $queue_data[$queue_object_name]['package_size'] ?? 0;
$num_items = (!$package_size || $messages_count < $package_size) ? $messages_count : $package_size; $num_items = (!$package_size || $messages_count < $package_size) ? $messages_count : $package_size;
$mailer = new Mailer($this->transport);
for ($i = 0; $i < $num_items; $i++) for ($i = 0; $i < $num_items; $i++)
{ {
@ -447,30 +443,30 @@ class phpbb_jabber extends base
if (!$this->connect()) if (!$this->connect())
{ {
$this->error('JABBER', $this->user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->get_log()); $this->error($this->user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->get_log());
return false; return;
} }
if (!$this->login()) if (!$this->login())
{ {
$this->error('JABBER', $this->user->lang['ERR_JAB_AUTH'] . '<br />' . $this->get_log()); $this->error($this->user->lang['ERR_JAB_AUTH'] . '<br />' . $this->get_log());
return false; return;
} }
foreach ($addresses as $address) foreach ($addresses as $address)
{ {
if ($this->send_message($address, $msg, $subject) === false) if ($this->send_message($address, $msg, $subject) === false)
{ {
$this->error('JABBER', $this->get_log()); $this->error($this->get_log());
continue; continue;
} }
} }
} }
// No more data for this object? Unset it // No more data for this object? Unset it
if (!count($this->queue_data[$queue_object_name]['data'])) if (!count($queue_data[$queue_object_name]['data']))
{ {
unset($this->queue_data[$queue_object_name]); unset($queue_data[$queue_object_name]);
} }
$this->disconnect(); $this->disconnect();
@ -500,13 +496,13 @@ class phpbb_jabber extends base
{ {
if (!$this->connect()) if (!$this->connect())
{ {
$this->error('JABBER', $this->user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->get_log()); $this->error($this->user->lang['ERR_JAB_CONNECT'] . '<br />' . $this->get_log());
return false; return false;
} }
if (!$this->login()) if (!$this->login())
{ {
$this->error('JABBER', $this->user->lang['ERR_JAB_AUTH'] . '<br />' . $this->get_log()); $this->error($this->user->lang['ERR_JAB_AUTH'] . '<br />' . $this->get_log());
return false; return false;
} }
@ -514,7 +510,7 @@ class phpbb_jabber extends base
{ {
if ($this->send_message($address, $this->msg, $this->subject) === false) if ($this->send_message($address, $this->msg, $this->subject) === false)
{ {
$this->error('JABBER', $this->get_log()); $this->error($this->get_log());
continue; continue;
} }
} }
@ -542,7 +538,7 @@ class phpbb_jabber extends base
* *
* @param string $xml * @param string $xml
* *
* @return bool * @return int|bool
*/ */
public function send_xml($xml) public function send_xml($xml)
{ {
@ -655,7 +651,7 @@ class phpbb_jabber extends base
/** /**
* Initiates account registration (based on data used for contructor) * Initiates account registration (based on data used for contructor)
* *
* @return bool * @return bool|void
*/ */
public function register() public function register()
{ {
@ -676,7 +672,7 @@ class phpbb_jabber extends base
* @param string $type dnd, away, chat, xa or nothing * @param string $type dnd, away, chat, xa or nothing
* @param bool $unavailable set this to true if you want to become unavailable * @param bool $unavailable set this to true if you want to become unavailable
* *
* @return bool * @return int|bool
*/ */
function send_presence($message = '', $type = '', $unavailable = false) function send_presence($message = '', $type = '', $unavailable = false)
{ {
@ -702,7 +698,7 @@ class phpbb_jabber extends base
* *
* @param array $xml * @param array $xml
* *
* @return bool * @return bool|void
*/ */
function response($xml) function response($xml)
{ {
@ -1021,7 +1017,7 @@ class phpbb_jabber extends base
* @param string $subject Message subject * @param string $subject Message subject
* @param string $type Message type * @param string $type Message type
* *
* @return string * @return int|bool
*/ */
public function send_message($to, $text, $subject = '', $type = 'normal') public function send_message($to, $text, $subject = '', $type = 'normal')
{ {

View file

@ -35,7 +35,7 @@ class queue
/** @var dispatcher */ /** @var dispatcher */
protected $dispatcher; protected $dispatcher;
/** @var phpbb\filesystem\filesystem_interface */ /** @var \phpbb\filesystem\filesystem_interface */
protected $filesystem; protected $filesystem;
/** @var service_collection */ /** @var service_collection */
@ -117,6 +117,7 @@ class queue
include($this->cache_file); include($this->cache_file);
/** @psalm-suppress InvalidTemplateParam */
$messenger_collection_iterator = $this->messenger_method_collection->getIterator(); $messenger_collection_iterator = $this->messenger_method_collection->getIterator();
while ($messenger_collection_iterator->valid()) while ($messenger_collection_iterator->valid())
{ {

View file

@ -112,6 +112,7 @@ abstract class messenger_base extends \phpbb\notification\method\base
continue; continue;
} }
/** @psalm-suppress InvalidTemplateParam */
$messenger_collection_iterator = $this->messenger->getIterator(); $messenger_collection_iterator = $this->messenger->getIterator();
while ($messenger_collection_iterator->valid()) while ($messenger_collection_iterator->valid())
{ {

View file

@ -126,7 +126,7 @@ class phpbb_email_parsing_test extends phpbb_test_case
$this->email = new \phpbb\messenger\method\phpbb_email( $this->email = new \phpbb\messenger\method\phpbb_email(
$config, $dispatcher, $lang, $log, $request, $user, $messenger_queue, $config, $dispatcher, $lang, $log, $request, $user, $messenger_queue,
$phpbb_path_helper, $extension_manager, $twig_extensions_collection, $twig_lexer, $phpbb_path_helper, $extension_manager, $twig_extensions_collection, $twig_lexer,
$cache_path $cache_path, $phpbb_root_path
); );
$phpbb_container->set('messenger.method.email', $this->email); $phpbb_container->set('messenger.method.email', $this->email);