Skip to content

Commit d23aecc

Browse files
committed
code quality improved & doc blocks added
1 parent 0d61de5 commit d23aecc

12 files changed

+58
-36
lines changed

examples/custom_attachment_mask.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ public function custom_save(): bool {
3333

3434
}
3535

36-
/** @var \Webklex\PHPIMAP\Client $client */
3736
$cm = new \Webklex\PHPIMAP\ClientManager('path/to/config/imap.php');
37+
38+
/** @var \Webklex\PHPIMAP\Client $client */
3839
$client = $cm->account('default');
3940
$client->connect();
4041
$client->setDefaultAttachmentMask(CustomAttachmentMask::class);

examples/custom_message_mask.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public function getAttachmentCount(): int {
3030

3131
}
3232

33-
/** @var \Webklex\PHPIMAP\Client $client */
3433
$cm = new \Webklex\PHPIMAP\ClientManager('path/to/config/imap.php');
34+
35+
/** @var \Webklex\PHPIMAP\Client $client */
3536
$client = $cm->account('default');
3637
$client->connect();
3738

@@ -44,7 +45,7 @@ public function getAttachmentCount(): int {
4445
/** @var CustomMessageMask $masked_message */
4546
$masked_message = $message->mask(CustomMessageMask::class);
4647

47-
echo 'Token for uid ['.$masked_message->uid.']: '.$masked_message->token().' @atms:'.$masked_message->getAttachmentCount();
48+
echo 'Token for uid [' . $masked_message->uid . ']: ' . $masked_message->token() . ' @atms:' . $masked_message->getAttachmentCount();
4849

4950
$masked_message->setFlag('seen');
5051

src/Connection/Protocols/ImapProtocol.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ public function connect(string $host, int $port = null): bool {
9898
* Check if the current session is connected
9999
*
100100
* @return bool
101+
* @throws ImapBadRequestException
101102
*/
102103
public function connected(): bool {
103104
if ((bool)$this->stream) {
104105
try {
105106
$this->requestAndResponse('NOOP');
106107
return true;
107-
}
108-
catch (ImapServerErrorException|RuntimeException) {
108+
} catch (ImapServerErrorException|RuntimeException) {
109109
return false;
110110
}
111111
}
@@ -120,7 +120,7 @@ public function connected(): bool {
120120
* @throws ImapServerErrorException
121121
* @throws RuntimeException
122122
*/
123-
protected function enableStartTls() {
123+
protected function enableStartTls(): void {
124124
$response = $this->requestAndResponse('STARTTLS');
125125
$result = $response->successful() && stream_socket_enable_crypto($this->stream, true, $this->getCryptoMethod());
126126
if (!$result) {
@@ -324,14 +324,33 @@ public function readResponse(Response $response, string $tag, bool $dontParse =
324324

325325
$original = is_array($original) ? $original : [$original];
326326

327+
327328
// last line has response code
328329
if ($tokens[0] == 'OK') {
329330
return $lines ?: [true];
330331
} elseif ($tokens[0] == 'NO' || $tokens[0] == 'BAD' || $tokens[0] == 'BYE') {
331-
throw new ImapServerErrorException(implode("\n", $original));
332+
throw new ImapServerErrorException($this->stringifyArray($original));
332333
}
333334

334-
throw new ImapBadRequestException(implode("\n", $original));
335+
throw new ImapBadRequestException($this->stringifyArray($original));
336+
}
337+
338+
/**
339+
* Convert an array to a string
340+
* @param array $arr array to stringify
341+
*
342+
* @return string stringified array
343+
*/
344+
private function stringifyArray(array $arr): string {
345+
$string = "";
346+
foreach ($arr as $value) {
347+
if (is_array($value)) {
348+
$string .= "(" . $this->stringifyArray($value) . ")";
349+
} else {
350+
$string .= $value . " ";
351+
}
352+
}
353+
return $string;
335354
}
336355

337356
/**
@@ -523,7 +542,7 @@ public function logout(): Response {
523542
try {
524543
$result = $this->requestAndResponse('LOGOUT', [], true);
525544
fclose($this->stream);
526-
} catch (\Throwable) {
545+
} catch (Throwable) {
527546
}
528547

529548
$this->reset();
@@ -572,7 +591,7 @@ public function examineOrSelect(string $command = 'EXAMINE', string $folder = 'I
572591

573592
$result = [];
574593
$tokens = []; // define $tokens variable before first use
575-
while (!$this->readLine($response, $tokens, $tag, false)) {
594+
while (!$this->readLine($response, $tokens, $tag)) {
576595
if ($tokens[0] == 'FLAGS') {
577596
array_shift($tokens);
578597
$result['flags'] = $tokens;
@@ -645,7 +664,7 @@ public function examineFolder(string $folder = 'INBOX'): Response {
645664
* @throws RuntimeException
646665
*/
647666
public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY']): Response {
648-
$response = $this->requestAndResponse('STATUS', [$this->escapeString($folder), $this->escapeList($arguments)], false);
667+
$response = $this->requestAndResponse('STATUS', [$this->escapeString($folder), $this->escapeList($arguments)]);
649668
$data = $response->validatedData();
650669

651670
if (!isset($data[0]) || !isset($data[0][2])) {
@@ -654,7 +673,7 @@ public function folderStatus(string $folder = 'INBOX', $arguments = ['MESSAGES',
654673

655674
$result = [];
656675
$key = null;
657-
foreach($data[0][2] as $value) {
676+
foreach ($data[0][2] as $value) {
658677
if ($key === null) {
659678
$key = $value;
660679
} else {
@@ -1245,7 +1264,7 @@ public function getQuotaRoot(string $quota_root = 'INBOX'): Response {
12451264
*
12461265
* @throws RuntimeException
12471266
*/
1248-
public function idle() {
1267+
public function idle(): void {
12491268
$response = $this->sendRequest("IDLE");
12501269
if (!$this->assumedNextLine($response, '+ ')) {
12511270
throw new RuntimeException('idle failed');

src/Connection/Protocols/LegacyProtocol.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ public function login(string $user, string $password): Response {
125125
* @param string $token access token
126126
*
127127
* @return Response
128-
* @throws AuthFailedException
129-
* @throws RuntimeException
130128
*/
131129
public function authenticate(string $user, string $token): Response {
132130
return $this->login($user, $token);
@@ -392,7 +390,7 @@ public function getUid(int $id = null): Response {
392390
}
393391

394392
/**
395-
* Get a message number for a uid
393+
* Get the message number of a given uid
396394
* @param string $id uid
397395
*
398396
* @return Response message number

src/Connection/Protocols/Protocol.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function buildUIDCommand(string $command, int|string $uid): string {
274274
*
275275
* @param array|null $uids
276276
*/
277-
public function setUidCache(?array $uids) {
277+
public function setUidCache(?array $uids): void {
278278
if (is_null($uids)) {
279279
$this->uid_cache = [];
280280
return;
@@ -336,7 +336,7 @@ public function connected(): bool {
336336
}
337337

338338
/**
339-
* Retrieves header/meta data from the resource stream
339+
* Retrieves header/metadata from the resource stream
340340
*
341341
* @return array
342342
*/

src/Folder.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function getChildren(): FolderCollection {
252252
protected function decodeName($name): string|array|bool|null {
253253
$parts = [];
254254
foreach(explode($this->delimiter, $name) as $item) {
255-
$parts[] = EncodingAliases::convert($item, "UTF7-IMAP", "UTF-8");
255+
$parts[] = EncodingAliases::convert($item, "UTF7-IMAP");
256256
}
257257

258258
return implode($this->delimiter, $parts);
@@ -402,7 +402,7 @@ public function rename(string $new_name, bool $expunge = true): array {
402402
public function delete(bool $expunge = true): array {
403403
$status = $this->client->getConnection()->deleteFolder($this->path)->validatedData();
404404
if($this->client->getActiveFolder() == $this->path){
405-
$this->client->setActiveFolder(null);
405+
$this->client->setActiveFolder();
406406
}
407407

408408
if($expunge) $this->client->expunge();
@@ -527,7 +527,7 @@ public function idle(callable $callback, int $timeout = 300): void {
527527
* @throws ResponseException
528528
*/
529529
public function status(): array {
530-
return $this->client->getConnection()->folderStatus($this->path, ['MESSAGES', 'UNSEEN', 'RECENT', 'UIDNEXT', 'UIDVALIDITY'])->validatedData();
530+
return $this->client->getConnection()->folderStatus($this->path)->validatedData();
531531
}
532532

533533
/**

src/Header.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected function parse(): void {
205205
$this->set("subject", $this->decode($header->subject));
206206
}
207207
if (property_exists($header, 'references')) {
208-
$this->set("references", array_map(function ($item) {
208+
$this->set("references", array_map(function($item) {
209209
return str_replace(['<', '>'], '', $item);
210210
}, explode(" ", $header->references)));
211211
}
@@ -440,14 +440,14 @@ public function decode(mixed $value): mixed {
440440
$value = $tempValue;
441441
} else if (extension_loaded('imap')) {
442442
$value = \imap_utf8($value);
443-
}else if (function_exists('iconv_mime_decode')){
443+
} else if (function_exists('iconv_mime_decode')) {
444444
$value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
445-
}else{
445+
} else {
446446
$value = mb_decode_mimeheader($value);
447447
}
448-
}elseif ($decoder === 'iconv') {
448+
} elseif ($decoder === 'iconv') {
449449
$value = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, "UTF-8");
450-
}else if ($this->is_uft8($value)) {
450+
} else if ($this->is_uft8($value)) {
451451
$value = mb_decode_mimeheader($value);
452452
}
453453

@@ -771,10 +771,10 @@ private function parseDate(object $header): void {
771771
try {
772772
$parsed_date = Carbon::parse($date);
773773
} catch (\Exception $_e) {
774-
if (!isset($this->config["fallback_date"])) {
774+
if (!isset($this->options["fallback_date"])) {
775775
throw new InvalidMessageDateException("Invalid message date. ID:" . $this->get("message_id") . " Date:" . $header->date . "/" . $date, 1100, $e);
776776
} else {
777-
$parsed_date = Carbon::parse($this->config["fallback_date"]);
777+
$parsed_date = Carbon::parse($this->options["fallback_date"]);
778778
}
779779
}
780780
}

src/Message.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Webklex\PHPIMAP;
1414

15+
use Exception;
1516
use ReflectionClass;
1617
use ReflectionException;
1718
use Webklex\PHPIMAP\Exceptions\AuthFailedException;
@@ -87,7 +88,7 @@ class Message {
8788
*
8889
* @var ?Client
8990
*/
90-
private ?Client $client = null;
91+
private ?Client $client;
9192

9293
/**
9394
* Default mask
@@ -565,7 +566,7 @@ public function parseRawHeader(string $raw_header): void {
565566
* @param array $raw_flags
566567
*/
567568
public function parseRawFlags(array $raw_flags): void {
568-
$this->flags = FlagCollection::make([]);
569+
$this->flags = FlagCollection::make();
569570

570571
foreach ($raw_flags as $flag) {
571572
if (str_starts_with($flag, "\\")) {
@@ -592,7 +593,7 @@ public function parseRawFlags(array $raw_flags): void {
592593
*/
593594
private function parseFlags(): void {
594595
$this->client->openFolder($this->folder_path);
595-
$this->flags = FlagCollection::make([]);
596+
$this->flags = FlagCollection::make();
596597

597598
$sequence_id = $this->getSequenceId();
598599
try {
@@ -628,7 +629,7 @@ public function parseBody(): Message {
628629
try {
629630
$contents = $this->client->getConnection()->content([$sequence_id], "RFC822", $this->sequence)->validatedData();
630631
} catch (Exceptions\RuntimeException $e) {
631-
throw new MessageContentFetchingException("failed to fetch content", 0);
632+
throw new MessageContentFetchingException("failed to fetch content", 0, $e);
632633
}
633634
if (!isset($contents[$sequence_id])) {
634635
throw new MessageContentFetchingException("no content found", 0);
@@ -919,7 +920,7 @@ public function convertEncoding($str, string $from = "ISO-8859-2", string $to =
919920
if (function_exists('iconv') && !EncodingAliases::isUtf7($from) && !EncodingAliases::isUtf7($to)) {
920921
try {
921922
return iconv($from, $to.'//IGNORE', $str);
922-
} catch (\Exception $e) {
923+
} catch (Exception) {
923924
return @iconv($from, $to, $str);
924925
}
925926
} else {
@@ -985,7 +986,7 @@ public function getFolder(): ?Folder {
985986
* @throws ResponseException
986987
*/
987988
public function thread(Folder $sent_folder = null, MessageCollection &$thread = null, Folder $folder = null): MessageCollection {
988-
$thread = $thread ?: MessageCollection::make([]);
989+
$thread = $thread ?: MessageCollection::make();
989990
$folder = $folder ?: $this->getFolder();
990991
$sent_folder = $sent_folder ?: $this->client->getFolderByPath($this->config->get("options.common_folders.sent", "INBOX/Sent"));
991992

src/Query/Query.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public function curate_messages(Collection $available_messages): MessageCollecti
315315
if ($available_messages->count() > 0) {
316316
return $this->populate($available_messages);
317317
}
318-
return MessageCollection::make([]);
318+
return MessageCollection::make();
319319
} catch (Exception $e) {
320320
throw new GetMessagesFailedException($e->getMessage(), 0, $e);
321321
}

src/Query/WhereQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function where(mixed $criteria, mixed $value = null): static {
155155
*
156156
* @throws InvalidWhereQueryCriteriaException
157157
*/
158-
protected function push_search_criteria(string $criteria, mixed $value){
158+
protected function push_search_criteria(string $criteria, mixed $value): void {
159159
$criteria = $this->validate_criteria($criteria);
160160
$value = $this->parse_value($value);
161161

src/Support/Masks/AttachmentMask.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Class AttachmentMask
1919
*
2020
* @package Webklex\PHPIMAP\Support\Masks
21+
* @mixin Attachment
2122
*/
2223
class AttachmentMask extends Mask {
2324

src/Support/Masks/MessageMask.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Class MessageMask
2020
*
2121
* @package Webklex\PHPIMAP\Support\Masks
22+
* @mixin Message
2223
*/
2324
class MessageMask extends Mask {
2425

0 commit comments

Comments
 (0)