Skip to content

Commit b30cb0f

Browse files
committed
fixed PHPStan errors WIP
1 parent 4d65eb5 commit b30cb0f

7 files changed

Lines changed: 66 additions & 44 deletions

File tree

phpstan.neon

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
parameters:
2-
level: 5
2+
level: 8
33

44
paths:
55
- src
6+
7+
ignoreErrors:
8+
-
9+
identifier: if.alwaysFalse
10+
path: src/Mail/Mailer.php

src/Bridges/MailDI/MailExtension.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,23 @@ public function getConfigSchema(): Nette\Schema\Schema
3939
'passPhrase' => Expect::string()->dynamic(),
4040
])->castTo('array'),
4141
),
42-
])->castTo('array');
42+
]);
4343
}
4444

4545

4646
public function loadConfiguration(): void
4747
{
48+
$config = $this->config;
49+
\assert($config instanceof \stdClass);
4850
$builder = $this->getContainerBuilder();
4951

5052
$mailer = $builder->addDefinition($this->prefix('mailer'))
5153
->setType(Nette\Mail\Mailer::class);
5254

53-
if ($this->config['dkim']) {
54-
$dkim = $this->config['dkim'];
55+
if ($config->dkim) {
56+
$dkim = $config->dkim;
5557
$dkim['privateKey'] = Nette\Utils\FileSystem::read($dkim['privateKey']);
56-
unset($this->config['dkim']);
58+
unset($config->dkim);
5759

5860
$signer = $builder->addDefinition($this->prefix('signer'))
5961
->setType(Nette\Mail\Signer::class)
@@ -62,17 +64,17 @@ public function loadConfiguration(): void
6264
$mailer->addSetup('setSigner', [$signer]);
6365
}
6466

65-
if ($this->config['smtp']) {
67+
if ($config->smtp) {
6668
$mailer->setFactory(Nette\Mail\SmtpMailer::class, [
67-
'host' => $this->config['host'] ?? ini_get('SMTP'),
68-
'port' => isset($this->config['host']) ? $this->config['port'] : (int) ini_get('smtp_port'),
69-
'username' => $this->config['username'],
70-
'password' => $this->config['password'],
71-
'encryption' => $this->config['encryption'] ?? $this->config['secure'],
72-
'persistent' => $this->config['persistent'],
73-
'timeout' => $this->config['timeout'],
74-
'clientHost' => $this->config['clientHost'],
75-
'streamOptions' => $this->config['context'] ?: null,
69+
'host' => $config->host ?? ini_get('SMTP'),
70+
'port' => isset($config->host) ? $config->port : (int) ini_get('smtp_port'),
71+
'username' => $config->username,
72+
'password' => $config->password,
73+
'encryption' => $config->encryption ?? $config->secure,
74+
'persistent' => $config->persistent,
75+
'timeout' => $config->timeout,
76+
'clientHost' => $config->clientHost,
77+
'streamOptions' => $config->context ?: null,
7678
]);
7779

7880
} else {

src/Mail/DkimSigner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected function normalizeNewLines(string $s): string
167167
/** @return list<string> */
168168
protected function getSignedHeaders(Message $message): array
169169
{
170-
return array_filter($this->signHeaders, fn($name) => $message->getHeader($name) !== null);
170+
return array_values(array_filter($this->signHeaders, fn($name) => $message->getHeader($name) !== null));
171171
}
172172

173173

src/Mail/Message.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Nette;
1111
use Nette\Utils\Strings;
12-
use function addcslashes, array_map, array_reverse, basename, date, explode, finfo_buffer, finfo_open, implode, is_numeric, ltrim, php_uname, preg_match, preg_replace, rtrim, str_replace, strcasecmp, stripslashes, strlen, substr, substr_replace, trim, urldecode;
12+
use function addcslashes, array_map, array_reverse, basename, date, explode, finfo_buffer, finfo_open, implode, is_array, is_numeric, is_string, ltrim, php_uname, preg_match, preg_replace, rtrim, str_replace, strcasecmp, stripslashes, strlen, substr, substr_replace, trim, urldecode;
1313

1414

1515
/**
@@ -75,7 +75,8 @@ public function setFrom(string $email, ?string $name = null): static
7575
*/
7676
public function getFrom(): ?array
7777
{
78-
return $this->getHeader('From');
78+
$value = $this->getHeader('From');
79+
return is_array($value) ? $value : null;
7980
}
8081

8182

@@ -98,7 +99,8 @@ public function setSubject(string $subject): static
9899

99100
public function getSubject(): ?string
100101
{
101-
return $this->getHeader('Subject');
102+
$value = $this->getHeader('Subject');
103+
return is_string($value) ? $value : null;
102104
}
103105

104106

@@ -160,7 +162,8 @@ public function setReturnPath(string $email): static
160162

161163
public function getReturnPath(): ?string
162164
{
163-
return $this->getHeader('Return-Path');
165+
$value = $this->getHeader('Return-Path');
166+
return is_string($value) ? $value : null;
164167
}
165168

166169

@@ -202,7 +205,8 @@ public function setHtmlBody(string $html, ?string $basePath = null): static
202205
foreach (array_reverse($matches) as $m) {
203206
$file = rtrim($basePath, '/\\') . '/' . (isset($m[4]) ? $m[4][0] : urldecode($m[3][0]));
204207
if (!isset($cids[$file])) {
205-
$cids[$file] = substr($this->addEmbeddedFile($file)->getHeader('Content-ID'), 1, -1);
208+
$contentId = $this->addEmbeddedFile($file)->getHeader('Content-ID');
209+
$cids[$file] = is_string($contentId) ? substr($contentId, 1, -1) : '';
206210
}
207211

208212
$html = substr_replace(
@@ -293,7 +297,9 @@ private function createAttachment(
293297
}
294298

295299
if (!$contentType) {
296-
$contentType = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
300+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
301+
$contentType = $finfo ? finfo_buffer($finfo, $content) : false;
302+
$contentType = $contentType ?: 'application/octet-stream';
297303
}
298304

299305
if (!strcasecmp($contentType, 'message/rfc822')) { // not allowed for attached files

src/Mail/MimePart.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Nette;
1111
use Nette\Utils\Strings;
12-
use function addcslashes, base64_encode, chunk_split, iconv_mime_encode, is_array, ltrim, preg_match, preg_replace, quoted_printable_encode, rtrim, str_ends_with, str_repeat, str_replace, stripslashes, strlen, strrpos, strspn, substr;
12+
use function addcslashes, base64_encode, chunk_split, iconv_mime_encode, is_array, is_string, ltrim, preg_match, preg_replace, quoted_printable_encode, rtrim, str_ends_with, str_repeat, str_replace, stripslashes, strlen, strrpos, strspn, substr;
1313

1414

1515
/**
@@ -95,7 +95,7 @@ public function setHeader(string $name, string|array|null $value, bool $append =
9595
* Returns the header value, or null if not set.
9696
* @return string|array<string, ?string>|null
9797
*/
98-
public function getHeader(string $name): mixed
98+
public function getHeader(string $name): string|array|null
9999
{
100100
return $this->headers[$name] ?? null;
101101
}
@@ -167,7 +167,8 @@ public function setEncoding(string $encoding): static
167167

168168
public function getEncoding(): string
169169
{
170-
return $this->getHeader('Content-Transfer-Encoding');
170+
$encoding = $this->getHeader('Content-Transfer-Encoding');
171+
return is_string($encoding) ? $encoding : '';
171172
}
172173

173174

@@ -286,7 +287,7 @@ private static function encodeSequence(string $s, int &$offset = 0, ?int $type =
286287
'scheme' => 'B', // Q is broken
287288
'input-charset' => 'UTF-8',
288289
'output-charset' => 'UTF-8',
289-
]);
290+
]) ?: '';
290291

291292
$offset = strlen($s) - strrpos($s, "\n");
292293
$s = substr($s, $old + 2); // adds ': '

src/Mail/SmtpMailer.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Nette\Mail;
99

10-
use function in_array, is_resource;
10+
use function in_array, is_resource, is_string;
1111

1212

1313
/**
@@ -84,10 +84,12 @@ public function send(Message $mail): void
8484
$this->connect();
8585
}
8686

87-
if (
88-
($from = $mail->getHeader('Return-Path'))
89-
|| ($from = array_keys((array) $mail->getHeader('From'))[0] ?? null)
90-
) {
87+
$from = $mail->getHeader('Return-Path');
88+
if (!is_string($from)) {
89+
$from = array_keys((array) $mail->getHeader('From'))[0] ?? null;
90+
}
91+
92+
if (is_string($from)) {
9193
$this->write("MAIL FROM:<$from>", 250);
9294
}
9395

@@ -124,26 +126,28 @@ public function send(Message $mail): void
124126
protected function connect(): void
125127
{
126128
$port = $this->port ?? ($this->encryption === self::EncryptionSSL ? 465 : 25);
127-
$this->connection = @stream_socket_client(// @ is escalated to exception
129+
$connection = @stream_socket_client(// @ is escalated to exception
128130
($this->encryption === self::EncryptionSSL ? 'ssl://' : '') . $this->host . ':' . $port,
129131
$errno,
130132
$error,
131133
$this->timeout,
132134
STREAM_CLIENT_CONNECT,
133135
$this->context,
134136
);
135-
if (!$this->connection) {
136-
throw new SmtpException($error ?: error_get_last()['message'], $errno);
137+
if (!$connection) {
138+
throw new SmtpException($error ?: error_get_last()['message'] ?? 'Unknown error', (int) $errno);
137139
}
138140

139-
stream_set_timeout($this->connection, $this->timeout, 0);
141+
$this->connection = $connection;
142+
143+
stream_set_timeout($connection, $this->timeout, 0);
140144
$this->read(); // greeting
141145

142146
if ($this->encryption === self::EncryptionTLS) {
143147
$this->write("EHLO $this->clientHost", 250);
144148
$this->write('STARTTLS', 220);
145149
if (!stream_socket_enable_crypto(
146-
$this->connection,
150+
$connection,
147151
true,
148152
STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
149153
)) {
@@ -185,8 +189,10 @@ protected function connect(): void
185189

186190
protected function disconnect(): void
187191
{
188-
fclose($this->connection);
189-
$this->connection = null;
192+
if ($this->connection) {
193+
fclose($this->connection);
194+
$this->connection = null;
195+
}
190196
}
191197

192198

@@ -196,7 +202,7 @@ protected function disconnect(): void
196202
*/
197203
protected function write(string $line, int|array|null $expectedCode = null, ?string $message = null): void
198204
{
199-
fwrite($this->connection, $line . Message::EOL);
205+
fwrite($this->connection ?? throw new SmtpException('Not connected to SMTP server.'), $line . Message::EOL);
200206
if ($expectedCode) {
201207
$response = $this->read();
202208
if (!in_array((int) $response, (array) $expectedCode, strict: true)) {
@@ -223,6 +229,8 @@ protected function read(): string
223229
} elseif ($info['eof']) {
224230
throw new SmtpException('Connection has been closed unexpectedly.');
225231
}
232+
233+
continue;
226234
}
227235

228236
$data .= $line;

tests/Mail/Mail.dkim.headers.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ $mail->setSubject('Hello Jane!');
2929
$mail->setBody('Příliš žluťoučký kůň');
3030

3131
Assert::equal([
32-
0 => 'From',
33-
1 => 'To',
34-
2 => 'Date',
35-
3 => 'Subject',
36-
5 => 'X-Mailer',
32+
'From',
33+
'To',
34+
'Date',
35+
'Subject',
36+
'X-Mailer',
3737
], $signer->getSignedHeaders($mail));

0 commit comments

Comments
 (0)