Skip to content

Commit ac50aa5

Browse files
committed
refactor: simplified code
1 parent 0e8bad4 commit ac50aa5

File tree

2 files changed

+46
-58
lines changed

2 files changed

+46
-58
lines changed

phpmyfaq/src/phpMyFAQ/Mail.php

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,19 @@ class Mail
184184
*/
185185
public function __construct(Configuration $configuration)
186186
{
187-
// Set default value for public properties
188-
$this->agent = $configuration->get('mail.remoteSMTP') ? 'smtp' : 'built-in';
187+
// Set the default value for public properties
188+
$this->agent = $configuration->get(item: 'mail.remoteSMTP') ? 'smtp' : 'built-in';
189189
$this->boundary = self::createBoundary();
190190
$this->messageId =
191191
'<'
192-
. Request::createFromGlobals()->server->get('REQUEST_TIME')
192+
. Request::createFromGlobals()->server->get(key: 'REQUEST_TIME')
193193
. '.'
194194
. md5(microtime())
195195
. '@'
196196
. self::getServerName()
197197
. '>';
198198

199-
// Set default value for private properties
199+
// Set the default value for private properties
200200
$this->configuration = $configuration;
201201

202202
// Set phpMyFAQ related data
@@ -241,7 +241,7 @@ public static function getServerName(): string
241241
*/
242242
public function setFrom(string $address, ?string $name = null): bool
243243
{
244-
return $this->setEmailTo($this->from, 'From', $address, $name);
244+
return $this->setEmailTo($this->from, targetAlias: 'From', address: $address, name: $name);
245245
}
246246

247247
/**
@@ -260,8 +260,8 @@ private function setEmailTo(array &$target, string $targetAlias, string $address
260260
if (count($target) > 2) {
261261
$keys = array_keys($target);
262262
throw new Exception(
263-
sprintf('<strong>Mail Class</strong>: too many e-mail addresses, %s, ', $keys[0])
264-
. sprintf("have been already added as '%s'!", $targetAlias),
263+
sprintf('Too many e-mail addresses, %s, ', $keys[0])
264+
. sprintf(format: "have been already added as '%s'!", values: $targetAlias),
265265
);
266266
}
267267

@@ -292,20 +292,20 @@ private function addEmailTo(array &$target, string $targetAlias, string $address
292292

293293
if (isset($name)) {
294294
// Remove CR and LF characters to prevent header injection
295-
$name = str_replace(["\n", "\r"], '', $name);
295+
$name = str_replace(search: ["\n", "\r"], replace: '', subject: $name);
296296

297297
// Encode any special characters in the displayed name
298298
$name = iconv_mime_encode($targetAlias, $name);
299299

300300
// Wrap the displayed name in quotes (to fix problems with commas etc.),
301301
// and escape any existing quotes
302-
$name = '"' . str_replace('"', '\"', $name) . '"';
302+
$name = '"' . str_replace(search: '"', replace: '\"', subject: $name) . '"';
303303
}
304304

305305
// Add the email address into the target array
306306
$target[$address] = $name;
307-
// On Windows, when using PHP built-in mail drop any name, just use the e-mail address
308-
if ('WIN' !== strtoupper(substr(PHP_OS, 0, 3))) {
307+
// On Windows, when using PHP built-in mail drops any name, just use the e-mail address
308+
if ('WIN' !== strtoupper(substr(string: PHP_OS, offset: 0, length: 3))) {
309309
return true;
310310
}
311311

@@ -332,7 +332,7 @@ public static function validateEmail(string $address): bool
332332
}
333333

334334
$unsafe = ["\r", "\n"];
335-
if ($address !== str_replace($unsafe, '', $address)) {
335+
if ($address !== str_replace(search: $unsafe, replace: '', subject: $address)) {
336336
return false;
337337
}
338338

@@ -349,7 +349,7 @@ public static function validateEmail(string $address): bool
349349
*/
350350
public function addCc(string $address, ?string $name = null): bool
351351
{
352-
return $this->addEmailTo($this->cc, 'Cc', $address, $name);
352+
return $this->addEmailTo($this->cc, targetAlias: 'Cc', address: $address, name: $name);
353353
}
354354

355355
/**
@@ -362,7 +362,7 @@ public function addCc(string $address, ?string $name = null): bool
362362
*/
363363
public function addTo(string $address, ?string $name = null): bool
364364
{
365-
return $this->addEmailTo($this->to, 'To', $address, $name);
365+
return $this->addEmailTo($this->to, targetAlias: 'To', address: $address, name: $name);
366366
}
367367

368368
/**
@@ -374,9 +374,7 @@ public function send(): int
374374
{
375375
// Check
376376
if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
377-
throw new Exception(
378-
'<strong>Mail Class</strong>: you need at least to set one recipient among TO, CC and BCC!',
379-
);
377+
throw new Exception(message: 'You need at least to set one recipient among TO, CC and BCC!');
380378
}
381379

382380
// Has any alternative message been provided?
@@ -393,7 +391,7 @@ public function send(): int
393391
$hasInlineAttachments = false;
394392
$idx = 0;
395393
while (!$hasInlineAttachments && $idx < count($this->attachments)) {
396-
$hasInlineAttachments = 'inline' == $this->attachments[$idx]['disposition'];
394+
$hasInlineAttachments = 'inline' === $this->attachments[$idx]['disposition'];
397395
++$idx;
398396
}
399397

@@ -410,9 +408,9 @@ public function send(): int
410408
$to[] = (empty($name) ? '' : $name . ' ') . '<' . $address . '>';
411409
}
412410

413-
$recipients = implode(',', $to);
411+
$recipients = implode(separator: ',', array: $to);
414412
// Check for the need of undisclosed recipients outlook-like <TO:>
415-
if (($recipients === '' || $recipients === '0') && 0 == count($this->cc)) {
413+
if (($recipients === '' || $recipients === '0') && 0 === count($this->cc)) {
416414
$recipients = '<Undisclosed-Recipient:;>';
417415
}
418416

@@ -425,13 +423,13 @@ public function send(): int
425423
// Send the email adopting to the given MUA
426424
$mua = self::getMUA($this->agent);
427425

428-
if (method_exists($mua, 'setAuthConfig')) {
426+
if (method_exists($mua, method: 'setAuthConfig')) {
429427
$mua->setAuthConfig(
430-
$this->configuration->get('mail.remoteSMTPServer'),
431-
$this->configuration->get('mail.remoteSMTPUsername'),
432-
$this->configuration->get('mail.remoteSMTPPassword'),
433-
(int) $this->configuration->get('mail.remoteSMTPPort'),
434-
$this->configuration->get('mail.remoteSMTPDisableTLSPeerVerification'),
428+
$this->configuration->get(item: 'mail.remoteSMTPServer'),
429+
$this->configuration->get(item: 'mail.remoteSMTPUsername'),
430+
$this->configuration->get(item: 'mail.remoteSMTPPassword'),
431+
(int) $this->configuration->get(item: 'mail.remoteSMTPPort'),
432+
$this->configuration->get(item: 'mail.remoteSMTPDisableTLSPeerVerification'),
435433
);
436434
}
437435

@@ -450,7 +448,7 @@ private function createHeaders(): void
450448
$this->headers = [];
451449

452450
// Check if the message consists of just a "plain" single item
453-
if (!str_contains($this->contentType, 'multipart')) {
451+
if (!str_contains($this->contentType, needle: 'multipart')) {
454452
// Content-Disposition: inline
455453
$this->headers['Content-Disposition'] = $this->contentDisposition;
456454
// Content-Type
@@ -471,7 +469,7 @@ private function createHeaders(): void
471469
$notifyTos[] = (empty($name) ? '' : $name . ' ') . '<' . $address . '>';
472470
}
473471

474-
$notifyTo = implode(',', $notifyTos);
472+
$notifyTo = implode(separator: ',', array: $notifyTos);
475473
if ($notifyTo !== '' && $notifyTo !== '0') {
476474
$this->headers['Disposition-Notification-To'] = $notifyTo;
477475
}
@@ -519,9 +517,9 @@ private function createHeaders(): void
519517
}
520518

521519
// Subject. Note: it must be RFC 2047 compliant
522-
// TODO: wrap mb_encode_mimeheader() to add other content encodings
520+
// @todo: wrap mb_encode_mimeheader() to add other content encodings
523521
$this->headers['Subject'] = Utils::resolveMarkers(
524-
html_entity_decode($this->subject, ENT_COMPAT, 'UTF-8'),
522+
html_entity_decode($this->subject, ENT_COMPAT, encoding: 'UTF-8'),
525523
$this->configuration,
526524
);
527525

@@ -551,7 +549,7 @@ private function createHeaders(): void
551549
*/
552550
public static function getDate(int $date): string
553551
{
554-
return date('r', $date);
552+
return date(format: 'r', timestamp: $date);
555553
}
556554

557555
/**
@@ -563,7 +561,7 @@ public static function getDate(int $date): string
563561
*/
564562
public static function getTime(): int
565563
{
566-
return Request::createFromGlobals()->server->get('REQUEST_TIME') ?? time();
564+
return Request::createFromGlobals()->server->get(key: 'REQUEST_TIME') ?? time();
567565
}
568566

569567
/**
@@ -578,19 +576,19 @@ private function createBody(): void
578576
$this->body = '';
579577

580578
// Add lines
581-
if (str_contains($this->contentType, 'multipart')) {
579+
if (str_contains($this->contentType, needle: 'multipart')) {
582580
$lines[] = 'This is a multi-part message in MIME format.';
583581
$lines[] = '';
584582
}
585583

586-
if (in_array($this->contentType, ['multipart/mixed', 'multipart/related'])) {
584+
if (in_array($this->contentType, ['multipart/mixed', 'multipart/related'], strict: true)) {
587585
$lines[] = '--' . $mainBoundary;
588586
$this->boundary = '--=alternative=' . self::createBoundary();
589587
$lines[] = 'Content-Type: multipart/alternative; boundary="' . $this->boundary . '"';
590588
$lines[] = '';
591589
}
592590

593-
if (str_contains($this->contentType, 'multipart')) {
591+
if (str_contains($this->contentType, needle: 'multipart')) {
594592
// At least we have messageAlt and message
595593
if ($this->messageAlt !== '' && $this->messageAlt !== '0') {
596594
// 1/2. messageAlt, supposed as plain text
@@ -614,15 +612,15 @@ private function createBody(): void
614612
$lines[] = self::wrapLines($this->message);
615613
}
616614

617-
if (in_array($this->contentType, ['multipart/mixed', 'multipart/related'])) {
615+
if (in_array($this->contentType, ['multipart/mixed', 'multipart/related'], strict: true)) {
618616
// Back to the main boundary
619617
$this->boundary = $mainBoundary;
620618
// Add the attachments
621619
foreach ($this->attachments as $attachment) {
622620
$lines[] = '--' . $this->boundary;
623621
$lines[] = 'Content-Type: ' . $attachment['mimetype'] . '; name="' . $attachment['name'] . '"';
624622
$lines[] = 'Content-Transfer-Encoding: base64';
625-
if ('inline' == $attachment['disposition']) {
623+
if ('inline' === $attachment['disposition']) {
626624
$lines[] = 'Content-ID: <' . $attachment['cid'] . '>';
627625
}
628626

@@ -648,18 +646,17 @@ private function createBody(): void
648646
*
649647
* @param string $message Message.
650648
* @param int $width Column width. Defaults to 72.
651-
* @param bool $cut Cutting a word is allowed. Defaults to false.
652649
* @return string The given message, wrapped as requested.
653650
*/
654-
public function wrapLines(string $message, int $width = 72, bool $cut = false): string
651+
public function wrapLines(string $message, int $width = 72): string
655652
{
656653
$message = $this->fixEOL($message);
657654

658655
$lines = explode($this->eol, $message);
659656
$wrapped = '';
660657
foreach ($lines as $line) {
661658
$wrapped .= $wrapped === '' || $wrapped === '0' ? '' : $this->eol;
662-
$wrapped .= wordwrap($line, $width, $this->eol, $cut);
659+
$wrapped .= wordwrap($line, $width, $this->eol);
663660
}
664661

665662
return $wrapped;
@@ -677,18 +674,18 @@ public function fixEOL(string $text): string
677674
{
678675
// Assure that anything among CRLF, CR will be replaced with just LF
679676
$text = str_replace(
680-
[
677+
search: [
681678
"\r\n",
682679
// CRLF
683680
"\r",
684681
// CR
685682
"\n",
686683
],
687-
"\n", // LF
688-
$text,
684+
replace: "\n", // LF
685+
subject: $text,
689686
);
690687
// Set any LF to the RFC 2822 EOL
691-
return str_replace("\n", $this->eol, $text);
688+
return str_replace(search: "\n", replace: $this->eol, subject: $text);
692689
}
693690

694691
/**
@@ -699,7 +696,7 @@ public function fixEOL(string $text): string
699696
*/
700697
public static function getMUA(string $mua): Builtin|Smtp
701698
{
702-
$className = ucfirst(str_replace('-', '', $mua));
699+
$className = ucfirst(str_replace(search: '-', replace: '', subject: $mua));
703700
$class = 'phpMyFAQ\Mail\\' . $className;
704701

705702
return new $class();
@@ -715,7 +712,7 @@ public static function getMUA(string $mua): Builtin|Smtp
715712
*/
716713
public function setReplyTo(string $address, ?string $name = null): bool
717714
{
718-
return $this->setEmailTo($this->replyTo, 'Reply-To', $address, $name);
715+
return $this->setEmailTo($this->replyTo, targetAlias: 'Reply-To', address: $address, name: $name);
719716
}
720717

721718
/**
@@ -729,7 +726,7 @@ public function setReplyTo(string $address, ?string $name = null): bool
729726
*/
730727
public function safeEmail(string $email): string
731728
{
732-
if ($this->configuration->get('spam.enableSafeEmail')) {
729+
if ($this->configuration->get(item: 'spam.enableSafeEmail')) {
733730
return str_replace(['@', '.'], ['_AT_', '_DOT_'], $email);
734731
}
735732

tests/phpMyFAQ/MailTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testGetTimeWithNoRequestTime(): void
149149
$_SERVER['REQUEST_TIME'] = $requestTimeToRestore;
150150
}
151151

152-
public function testWrapLinesWithDefaultWidthAndNoCut(): void
152+
public function testWrapLinesWithDefaultWidth(): void
153153
{
154154
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum acnunc quis neque tempor varius.";
155155
$result = $this->mail->wrapLines($message);
@@ -158,16 +158,7 @@ public function testWrapLinesWithDefaultWidthAndNoCut(): void
158158
$this->assertSame($expectedResult, $result);
159159
}
160160

161-
public function testWrapLinesWithCustomWidthAndCut(): void
162-
{
163-
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac nunc quis neque tempor varius.";
164-
$result = $this->mail->wrapLines($message, 20, true);
165-
166-
$expectedResult = "Lorem ipsum dolor\r\nsit amet,\r\nconsectetur\r\nadipiscing elit.\r\nVestibulum ac nunc\r\nquis neque tempor\r\nvarius.";
167-
$this->assertSame($expectedResult, $result);
168-
}
169-
170-
public function testWrapLinesWithCustomWidthAndNoCut(): void
161+
public function testWrapLinesWithCustomWidth(): void
171162
{
172163
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac nunc quis neque tempor varius.";
173164
$result = $this->mail->wrapLines($message, 30);

0 commit comments

Comments
 (0)