Skip to content

Commit a965080

Browse files
committed
Formatting updated
1 parent 4e05e51 commit a965080

File tree

10 files changed

+137
-132
lines changed

10 files changed

+137
-132
lines changed

src/Client.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -305,39 +305,42 @@ protected function setMaskFromConfig($config): void {
305305
throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['message']);
306306
}
307307
}else{
308-
if(class_exists($default_config['message'])) {
309-
$this->default_message_mask = $default_config['message'];
308+
$default_mask = ClientManager::getMask("message");
309+
if($default_mask != ""){
310+
$this->default_message_mask = $default_mask;
310311
}else{
311-
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']);
312+
throw new MaskNotFoundException("Unknown message mask provided");
312313
}
313314
}
314315
if(isset($config['masks']['attachment'])) {
315316
if(class_exists($config['masks']['attachment'])) {
316317
$this->default_attachment_mask = $config['masks']['attachment'];
317318
}else{
318-
throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['attachment']);
319+
throw new MaskNotFoundException("Unknown mask provided: ". $config['masks']['attachment']);
319320
}
320321
}else{
321-
if(class_exists($default_config['attachment'])) {
322-
$this->default_attachment_mask = $default_config['attachment'];
322+
$default_mask = ClientManager::getMask("attachment");
323+
if($default_mask != ""){
324+
$this->default_attachment_mask = $default_mask;
323325
}else{
324-
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']);
326+
throw new MaskNotFoundException("Unknown attachment mask provided");
325327
}
326328
}
327329
}else{
328-
if(class_exists($default_config['message'])) {
329-
$this->default_message_mask = $default_config['message'];
330+
$default_mask = ClientManager::getMask("message");
331+
if($default_mask != ""){
332+
$this->default_message_mask = $default_mask;
330333
}else{
331-
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']);
334+
throw new MaskNotFoundException("Unknown message mask provided");
332335
}
333336

334-
if(class_exists($default_config['attachment'])) {
335-
$this->default_attachment_mask = $default_config['attachment'];
337+
$default_mask = ClientManager::getMask("attachment");
338+
if($default_mask != ""){
339+
$this->default_attachment_mask = $default_mask;
336340
}else{
337-
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']);
341+
throw new MaskNotFoundException("Unknown attachment mask provided");
338342
}
339343
}
340-
341344
}
342345

343346
/**
@@ -375,10 +378,16 @@ public function isConnected(): bool {
375378
* @throws RuntimeException
376379
* @throws ResponseException
377380
*/
378-
public function checkConnection() {
379-
if (!$this->isConnected()) {
381+
public function checkConnection(): bool {
382+
try {
383+
if (!$this->isConnected()) {
384+
$this->connect();
385+
return true;
386+
}
387+
} catch (\Throwable) {
380388
$this->connect();
381389
}
390+
return false;
382391
}
383392

384393
/**

src/ClientManager.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ public function make(array $config): Client {
7676
public static function get(string $key, $default = null): mixed {
7777
$parts = explode('.', $key);
7878
$value = null;
79-
foreach($parts as $part) {
80-
if($value === null) {
81-
if(isset(self::$config[$part])) {
79+
foreach ($parts as $part) {
80+
if ($value === null) {
81+
if (isset(self::$config[$part])) {
8282
$value = self::$config[$part];
83-
}else{
83+
} else {
8484
break;
8585
}
86-
}else{
87-
if(isset($value[$part])) {
86+
} else {
87+
if (isset($value[$part])) {
8888
$value = $value[$part];
89-
}else{
89+
} else {
9090
break;
9191
}
9292
}
@@ -154,8 +154,9 @@ protected function getClientConfig(?string $name): array {
154154
if ($name === null || $name === 'null' || $name === "") {
155155
return ['driver' => 'null'];
156156
}
157+
$account = self::$config["accounts"][$name] ?? [];
157158

158-
return is_array(self::$config["accounts"][$name]) ? self::$config["accounts"][$name] : [];
159+
return is_array($account) ? $account : [];
159160
}
160161

161162
/**
@@ -191,27 +192,27 @@ public function setDefaultAccount(string $name): void {
191192
*/
192193
public function setConfig(array|string $config): ClientManager {
193194

194-
if(is_array($config) === false) {
195+
if (is_array($config) === false) {
195196
$config = require $config;
196197
}
197198

198199
$config_key = 'imap';
199-
$path = __DIR__.'/config/'.$config_key.'.php';
200+
$path = __DIR__ . '/config/' . $config_key . '.php';
200201

201202
$vendor_config = require $path;
202203
$config = $this->array_merge_recursive_distinct($vendor_config, $config);
203204

204-
if(is_array($config)){
205-
if(isset($config['default'])){
206-
if(isset($config['accounts']) && $config['default']){
205+
if (is_array($config)) {
206+
if (isset($config['default'])) {
207+
if (isset($config['accounts']) && $config['default']) {
207208

208209
$default_config = $vendor_config['accounts']['default'];
209-
if(isset($config['accounts'][$config['default']])){
210+
if (isset($config['accounts'][$config['default']])) {
210211
$default_config = array_merge($default_config, $config['accounts'][$config['default']]);
211212
}
212213

213-
if(is_array($config['accounts'])){
214-
foreach($config['accounts'] as $account_key => $account){
214+
if (is_array($config['accounts'])) {
215+
foreach ($config['accounts'] as $account_key => $account) {
215216
$config['accounts'][$account_key] = array_merge($default_config, $account);
216217
}
217218
}
@@ -250,20 +251,20 @@ private function array_merge_recursive_distinct(): mixed {
250251
return array_keys($arr) !== range(0, count($arr) - 1);
251252
};
252253

253-
if(!is_array($base)) $base = empty($base) ? array() : array($base);
254+
if (!is_array($base)) $base = empty($base) ? array() : array($base);
254255

255-
foreach($arrays as $append) {
256+
foreach ($arrays as $append) {
256257

257-
if(!is_array($append)) $append = array($append);
258+
if (!is_array($append)) $append = array($append);
258259

259-
foreach($append as $key => $value) {
260+
foreach ($append as $key => $value) {
260261

261-
if(!array_key_exists($key, $base) and !is_numeric($key)) {
262+
if (!array_key_exists($key, $base) and !is_numeric($key)) {
262263
$base[$key] = $value;
263264
continue;
264265
}
265266

266-
if(
267+
if (
267268
(
268269
is_array($value)
269270
&& $isAssoc($value)
@@ -277,8 +278,8 @@ private function array_merge_recursive_distinct(): mixed {
277278
// else merging $baseConfig['dispositions'] = ['attachment', 'inline'] with $customConfig['dispositions'] = ['attachment']
278279
// results in $resultConfig['dispositions'] = ['attachment', 'inline']
279280
$base[$key] = $this->array_merge_recursive_distinct($base[$key], $value);
280-
} else if(is_numeric($key)) {
281-
if(!in_array($value, $base)) $base[] = $value;
281+
} else if (is_numeric($key)) {
282+
if (!in_array($value, $base)) $base[] = $value;
282283
} else {
283284
$base[$key] = $value;
284285
}

src/Connection/Protocols/ImapProtocol.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ protected function decodeLine(Response $response, string $line): array {
191191
$token = substr($token, 1);
192192
}
193193
if ($token[0] == '"') {
194-
if (preg_match('%^\(*"((.|\\\\|\\")*?)" *%', $line, $matches)) {
194+
if (preg_match('%^\(*"((.|\\\\|\")*?)" *%', $line, $matches)) {
195195
$tokens[] = $matches[1];
196196
$line = substr($line, strlen($matches[0]));
197197
continue;
@@ -203,14 +203,14 @@ protected function decodeLine(Response $response, string $line): array {
203203
if (is_numeric($chars)) {
204204
$token = '';
205205
while (strlen($token) < $chars) {
206-
$token .= $this->nextLine();
206+
$token .= $this->nextLine($response);
207207
}
208208
$line = '';
209209
if (strlen($token) > $chars) {
210210
$line = substr($token, $chars);
211211
$token = substr($token, 0, $chars);
212212
} else {
213-
$line .= $this->nextLine();
213+
$line .= $this->nextLine($response);
214214
}
215215
$tokens[] = $token;
216216
$line = trim($line) . ' ';
@@ -229,7 +229,7 @@ protected function decodeLine(Response $response, string $line): array {
229229
}
230230
$token = $tokens;
231231
$tokens = array_pop($stack);
232-
// special handline if more than one closing brace
232+
// special handling if more than one closing brace
233233
while ($braces-- > 0) {
234234
$tokens[] = $token;
235235
$token = $tokens;
@@ -633,11 +633,11 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, in
633633
if (is_array($from)) {
634634
$set = implode(',', $from);
635635
} elseif ($to === null) {
636-
$set = (int)$from;
637-
} elseif ($to === INF) {
638-
$set = (int)$from . ':*';
636+
$set = $from;
637+
} elseif ($to == INF) {
638+
$set = $from . ':*';
639639
} else {
640-
$set = (int)$from . ':' . (int)$to;
640+
$set = $from . ':' . (int)$to;
641641
}
642642

643643
$items = (array)$items;
@@ -652,6 +652,9 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, in
652652
continue;
653653
}
654654

655+
$uidKey = 0;
656+
$data = [];
657+
655658
// find array key of UID value; try the last elements, or search for it
656659
if ($uid) {
657660
$count = count($tokens[2]);
@@ -682,7 +685,7 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, in
682685
$data = $tokens[2][3];
683686
} else {
684687
$expectedResponse = 0;
685-
// maybe the server send an other field we didn't wanted
688+
// maybe the server send another field we didn't wanted
686689
$count = count($tokens[2]);
687690
// we start with 2, because 0 was already checked
688691
for ($i = 2; $i < $count; $i += 2) {
@@ -698,7 +701,6 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, in
698701
}
699702
}
700703
} else {
701-
$data = [];
702704
while (key($tokens[2]) !== null) {
703705
$data[current($tokens[2])] = next($tokens[2]);
704706
next($tokens[2]);
@@ -713,7 +715,7 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, in
713715
}
714716
if ($uid) {
715717
$result[$tokens[2][$uidKey]] = $data;
716-
}else{
718+
} else {
717719
$result[$tokens[0]] = $data;
718720
}
719721
}
@@ -873,7 +875,7 @@ public function store(
873875
$set = $this->buildSet($from, $to);
874876

875877
$command = $this->buildUIDCommand("STORE", $uid);
876-
$item = ($mode == '-' ? "-" : "+").($item === null ? "FLAGS" : $item).($silent ? '.SILENT' : "");
878+
$item = ($mode == '-' ? "-" : "+") . ($item === null ? "FLAGS" : $item) . ($silent ? '.SILENT' : "");
877879

878880
$response = $this->requestAndResponse($command, [$set, $item, $flags], $silent);
879881

@@ -1029,9 +1031,9 @@ public function ID($ids = null): Response {
10291031
if (is_array($ids) && !empty($ids)) {
10301032
$token = "(";
10311033
foreach ($ids as $id) {
1032-
$token .= '"'.$id.'" ';
1034+
$token .= '"' . $id . '" ';
10331035
}
1034-
$token = rtrim($token).")";
1036+
$token = rtrim($token) . ")";
10351037
}
10361038

10371039
return $this->requestAndResponse("ID", [$token], true);
@@ -1238,7 +1240,7 @@ public function overview(string $sequence, int|string $uid = IMAP::ST_UID): Resp
12381240
$ids = [];
12391241
foreach ($response->data() as $msgn => $v) {
12401242
$id = $uid ? $v : $msgn;
1241-
if ( ($to >= $id && $from <= $id) || ($to === "*" && $from <= $id) ){
1243+
if (($to >= $id && $from <= $id) || ($to === "*" && $from <= $id)) {
12421244
$ids[] = $id;
12431245
}
12441246
}

src/Connection/Protocols/LegacyProtocol.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public function login(string $user, string $password): Response {
9090
throw new AuthFailedException($message);
9191
}
9292

93-
if(!$this->stream) {
94-
$errors = \imap_errors();
95-
$message = implode("; ", (is_array($errors) ? $errors : array()));
96-
throw new AuthFailedException($message);
97-
}
93+
if (!$this->stream) {
94+
$errors = \imap_errors();
95+
$message = implode("; ", (is_array($errors) ? $errors : array()));
96+
throw new AuthFailedException($message);
97+
}
9898

9999
$errors = \imap_errors();
100100
$response->addCommand("imap_errors");
@@ -135,12 +135,12 @@ public function authenticate(string $user, string $token): Response {
135135
* @return string
136136
*/
137137
protected function getAddress(): string {
138-
$address = "{".$this->host.":".$this->port."/".$this->protocol;
138+
$address = "{" . $this->host . ":" . $this->port . "/" . $this->protocol;
139139
if (!$this->cert_validation) {
140140
$address .= '/novalidate-cert';
141141
}
142-
if (in_array($this->encryption,['tls', 'notls', 'ssl'])) {
143-
$address .= '/'.$this->encryption;
142+
if (in_array($this->encryption, ['tls', 'notls', 'ssl'])) {
143+
$address .= '/' . $this->encryption;
144144
} elseif ($this->encryption === "starttls") {
145145
$address .= '/tls';
146146
}
@@ -704,14 +704,14 @@ public function search(array $params, int|string $uid = IMAP::ST_UID): Response
704704
/**
705705
* Enable the debug mode
706706
*/
707-
public function enableDebug(){
707+
public function enableDebug() {
708708
$this->debug = true;
709709
}
710710

711711
/**
712712
* Disable the debug mode
713713
*/
714-
public function disableDebug(){
714+
public function disableDebug() {
715715
$this->debug = false;
716716
}
717717

src/Folder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ public function __construct(Client $client, string $folder_name, string $delimit
135135
$this->events["folder"] = $client->getDefaultEvents("folder");
136136

137137
$this->setDelimiter($delimiter);
138-
$this->path = $folder_name;
139-
$this->full_name = $this->decodeName($folder_name);
140-
$this->name = $this->getSimpleName($this->delimiter, $this->full_name);
138+
$this->path = $folder_name;
139+
$this->full_name = $this->decodeName($folder_name);
140+
$this->name = $this->getSimpleName($this->delimiter, $this->full_name);
141141

142142
$this->parseAttributes($attributes);
143143
}
@@ -245,9 +245,9 @@ protected function getSimpleName($delimiter, $full_name): string|bool {
245245
*/
246246
protected function parseAttributes($attributes): void {
247247
$this->no_inferiors = in_array('\NoInferiors', $attributes);
248-
$this->no_select = in_array('\NoSelect', $attributes);
249-
$this->marked = in_array('\Marked', $attributes);
250-
$this->referral = in_array('\Referral', $attributes);
248+
$this->no_select = in_array('\NoSelect', $attributes);
249+
$this->marked = in_array('\Marked', $attributes);
250+
$this->referral = in_array('\Referral', $attributes);
251251
$this->has_children = in_array('\HasChildren', $attributes);
252252
}
253253

@@ -321,7 +321,7 @@ public function appendMessage(string $message, array $options = null, Carbon|str
321321
* date string that conforms to the rfc2060 specifications for a date_time value or be a Carbon object.
322322
*/
323323

324-
if ($internal_date instanceof Carbon){
324+
if ($internal_date instanceof Carbon) {
325325
$internal_date = $internal_date->format('d-M-Y H:i:s O');
326326
}
327327

src/Header.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ private function parseDate(object $header): void {
712712
break;
713713
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}[\,]\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4})+$/i', $date) > 0:
714714
$date = str_replace(',', '', $date);
715+
break;
715716
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ \+[0-9]{2,4}\ \(\+[0-9]{1,2}\))+$/i', $date) > 0:
716717
case preg_match('/([A-Z]{2,3}[\,|\ \,]\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}.*)+$/i', $date) > 0:
717718
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0:

0 commit comments

Comments
 (0)