Skip to content

Commit e220d35

Browse files
committed
fixed PHPStan errors WIP
1 parent 793d9a6 commit e220d35

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
@@ -146,7 +146,7 @@ protected function normalizeNewLines(string $s): string
146146
/** @return list<string> */
147147
protected function getSignedHeaders(Message $message): array
148148
{
149-
return array_filter($this->signHeaders, fn($name) => $message->getHeader($name) !== null);
149+
return array_values(array_filter($this->signHeaders, fn($name) => $message->getHeader($name) !== null));
150150
}
151151

152152

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
use const FILEINFO_MIME_TYPE;
1414

1515

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

8283

@@ -105,7 +106,8 @@ public function setSubject(string $subject): static
105106
*/
106107
public function getSubject(): ?string
107108
{
108-
return $this->getHeader('Subject');
109+
$value = $this->getHeader('Subject');
110+
return is_string($value) ? $value : null;
109111
}
110112

111113

@@ -173,7 +175,8 @@ public function setReturnPath(string $email): static
173175
*/
174176
public function getReturnPath(): ?string
175177
{
176-
return $this->getHeader('Return-Path');
178+
$value = $this->getHeader('Return-Path');
179+
return is_string($value) ? $value : null;
177180
}
178181

179182

@@ -219,7 +222,8 @@ public function setHtmlBody(string $html, ?string $basePath = null): static
219222
foreach (array_reverse($matches) as $m) {
220223
$file = rtrim($basePath, '/\\') . '/' . (isset($m[4]) ? $m[4][0] : urldecode($m[3][0]));
221224
if (!isset($cids[$file])) {
222-
$cids[$file] = substr($this->addEmbeddedFile($file)->getHeader('Content-ID'), 1, -1);
225+
$contentId = $this->addEmbeddedFile($file)->getHeader('Content-ID');
226+
$cids[$file] = is_string($contentId) ? substr($contentId, 1, -1) : '';
223227
}
224228

225229
$html = substr_replace(
@@ -312,7 +316,9 @@ private function createAttachment(
312316
}
313317

314318
if (!$contentType) {
315-
$contentType = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
319+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
320+
$contentType = $finfo ? finfo_buffer($finfo, $content) : false;
321+
$contentType = $contentType ?: 'application/octet-stream';
316322
}
317323

318324
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 a header.
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
}
@@ -179,7 +179,8 @@ public function setEncoding(string $encoding): static
179179
*/
180180
public function getEncoding(): string
181181
{
182-
return $this->getHeader('Content-Transfer-Encoding');
182+
$encoding = $this->getHeader('Content-Transfer-Encoding');
183+
return is_string($encoding) ? $encoding : '';
183184
}
184185

185186

@@ -304,7 +305,7 @@ private static function encodeSequence(string $s, int &$offset = 0, ?int $type =
304305
'scheme' => 'B', // Q is broken
305306
'input-charset' => 'UTF-8',
306307
'output-charset' => 'UTF-8',
307-
]);
308+
]) ?: '';
308309

309310
$offset = strlen($s) - strrpos($s, "\n");
310311
$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
use const STREAM_CLIENT_CONNECT, STREAM_CRYPTO_METHOD_TLS_CLIENT, STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
1212

1313

@@ -85,10 +85,12 @@ public function send(Message $mail): void
8585
$this->connect();
8686
}
8787

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

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

140-
stream_set_timeout($this->connection, $this->timeout, 0);
142+
$this->connection = $connection;
143+
144+
stream_set_timeout($connection, $this->timeout, 0);
141145
$this->read(); // greeting
142146

143147
if ($this->encryption === self::EncryptionTLS) {
144148
$this->write("EHLO $this->clientHost", 250);
145149
$this->write('STARTTLS', 220);
146150
if (!stream_socket_enable_crypto(
147-
$this->connection,
151+
$connection,
148152
true,
149153
STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
150154
)) {
@@ -189,8 +193,10 @@ protected function connect(): void
189193
*/
190194
protected function disconnect(): void
191195
{
192-
fclose($this->connection);
193-
$this->connection = null;
196+
if ($this->connection) {
197+
fclose($this->connection);
198+
$this->connection = null;
199+
}
194200
}
195201

196202

@@ -200,7 +206,7 @@ protected function disconnect(): void
200206
*/
201207
protected function write(string $line, int|array|null $expectedCode = null, ?string $message = null): void
202208
{
203-
fwrite($this->connection, $line . Message::EOL);
209+
fwrite($this->connection ?? throw new SmtpException('Not connected to SMTP server.'), $line . Message::EOL);
204210
if ($expectedCode) {
205211
$response = $this->read();
206212
if (!in_array((int) $response, (array) $expectedCode, strict: true)) {
@@ -227,6 +233,8 @@ protected function read(): string
227233
} elseif ($info['eof']) {
228234
throw new SmtpException('Connection has been closed unexpectedly.');
229235
}
236+
237+
continue;
230238
}
231239

232240
$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)