Skip to content

Commit e5578f0

Browse files
committed
fixed PHPStan errors WIP
1 parent dbb86f9 commit e5578f0

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
@@ -41,21 +41,23 @@ public function getConfigSchema(): Nette\Schema\Schema
4141
'passPhrase' => Expect::string()->dynamic(),
4242
])->castTo('array'),
4343
),
44-
])->castTo('array');
44+
]);
4545
}
4646

4747

4848
public function loadConfiguration(): void
4949
{
50+
$config = $this->config;
51+
\assert($config instanceof \stdClass);
5052
$builder = $this->getContainerBuilder();
5153

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

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

6062
$signer = $builder->addDefinition($this->prefix('signer'))
6163
->setType(Nette\Mail\Signer::class)
@@ -64,17 +66,17 @@ public function loadConfiguration(): void
6466
$mailer->addSetup('setSigner', [$signer]);
6567
}
6668

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

8082
} else {

src/Mail/DkimSigner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function normalizeNewLines(string $s): string
148148
/** @return list<string> */
149149
protected function getSignedHeaders(Message $message): array
150150
{
151-
return array_filter($this->signHeaders, fn($name) => $message->getHeader($name) !== null);
151+
return array_values(array_filter($this->signHeaders, fn($name) => $message->getHeader($name) !== null));
152152
}
153153

154154

src/Mail/Message.php

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

1212
use Nette;
1313
use Nette\Utils\Strings;
14-
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;
14+
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;
1515
use const FILEINFO_MIME_TYPE;
1616

1717

@@ -78,7 +78,8 @@ public function setFrom(string $email, ?string $name = null): static
7878
*/
7979
public function getFrom(): ?array
8080
{
81-
return $this->getHeader('From');
81+
$value = $this->getHeader('From');
82+
return is_array($value) ? $value : null;
8283
}
8384

8485

@@ -107,7 +108,8 @@ public function setSubject(string $subject): static
107108
*/
108109
public function getSubject(): ?string
109110
{
110-
return $this->getHeader('Subject');
111+
$value = $this->getHeader('Subject');
112+
return is_string($value) ? $value : null;
111113
}
112114

113115

@@ -175,7 +177,8 @@ public function setReturnPath(string $email): static
175177
*/
176178
public function getReturnPath(): ?string
177179
{
178-
return $this->getHeader('Return-Path');
180+
$value = $this->getHeader('Return-Path');
181+
return is_string($value) ? $value : null;
179182
}
180183

181184

@@ -221,7 +224,8 @@ public function setHtmlBody(string $html, ?string $basePath = null): static
221224
foreach (array_reverse($matches) as $m) {
222225
$file = rtrim($basePath, '/\\') . '/' . (isset($m[4]) ? $m[4][0] : urldecode($m[3][0]));
223226
if (!isset($cids[$file])) {
224-
$cids[$file] = substr($this->addEmbeddedFile($file)->getHeader('Content-ID'), 1, -1);
227+
$contentId = $this->addEmbeddedFile($file)->getHeader('Content-ID');
228+
$cids[$file] = is_string($contentId) ? substr($contentId, 1, -1) : '';
225229
}
226230

227231
$html = substr_replace(
@@ -314,7 +318,9 @@ private function createAttachment(
314318
}
315319

316320
if (!$contentType) {
317-
$contentType = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
321+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
322+
$contentType = $finfo ? finfo_buffer($finfo, $content) : false;
323+
$contentType = $contentType ?: 'application/octet-stream';
318324
}
319325

320326
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
@@ -11,7 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Utils\Strings;
14-
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;
14+
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;
1515

1616

1717
/**
@@ -97,7 +97,7 @@ public function setHeader(string $name, string|array|null $value, bool $append =
9797
* Returns a header.
9898
* @return string|array<string, ?string>|null
9999
*/
100-
public function getHeader(string $name): mixed
100+
public function getHeader(string $name): string|array|null
101101
{
102102
return $this->headers[$name] ?? null;
103103
}
@@ -181,7 +181,8 @@ public function setEncoding(string $encoding): static
181181
*/
182182
public function getEncoding(): string
183183
{
184-
return $this->getHeader('Content-Transfer-Encoding');
184+
$encoding = $this->getHeader('Content-Transfer-Encoding');
185+
return is_string($encoding) ? $encoding : '';
185186
}
186187

187188

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

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

src/Mail/SmtpMailer.php

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

1010
namespace Nette\Mail;
1111

12-
use function in_array, is_resource;
12+
use function in_array, is_resource, is_string;
1313
use const STREAM_CLIENT_CONNECT, STREAM_CRYPTO_METHOD_TLS_CLIENT, STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
1414

1515

@@ -87,10 +87,12 @@ public function send(Message $mail): void
8787
$this->connect();
8888
}
8989

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

@@ -127,26 +129,28 @@ public function send(Message $mail): void
127129
protected function connect(): void
128130
{
129131
$port = $this->port ?? ($this->encryption === self::EncryptionSSL ? 465 : 25);
130-
$this->connection = @stream_socket_client(// @ is escalated to exception
132+
$connection = @stream_socket_client(// @ is escalated to exception
131133
($this->encryption === self::EncryptionSSL ? 'ssl://' : '') . $this->host . ':' . $port,
132134
$errno,
133135
$error,
134136
$this->timeout,
135137
STREAM_CLIENT_CONNECT,
136138
$this->context,
137139
);
138-
if (!$this->connection) {
139-
throw new SmtpException($error ?: error_get_last()['message'], $errno);
140+
if (!$connection) {
141+
throw new SmtpException($error ?: error_get_last()['message'] ?? 'Unknown error', (int) $errno);
140142
}
141143

142-
stream_set_timeout($this->connection, $this->timeout, 0);
144+
$this->connection = $connection;
145+
146+
stream_set_timeout($connection, $this->timeout, 0);
143147
$this->read(); // greeting
144148

145149
if ($this->encryption === self::EncryptionTLS) {
146150
$this->write("EHLO $this->clientHost", 250);
147151
$this->write('STARTTLS', 220);
148152
if (!stream_socket_enable_crypto(
149-
$this->connection,
153+
$connection,
150154
true,
151155
STREAM_CRYPTO_METHOD_TLS_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
152156
)) {
@@ -191,8 +195,10 @@ protected function connect(): void
191195
*/
192196
protected function disconnect(): void
193197
{
194-
fclose($this->connection);
195-
$this->connection = null;
198+
if ($this->connection) {
199+
fclose($this->connection);
200+
$this->connection = null;
201+
}
196202
}
197203

198204

@@ -202,7 +208,7 @@ protected function disconnect(): void
202208
*/
203209
protected function write(string $line, int|array|null $expectedCode = null, ?string $message = null): void
204210
{
205-
fwrite($this->connection, $line . Message::EOL);
211+
fwrite($this->connection ?? throw new SmtpException('Not connected to SMTP server.'), $line . Message::EOL);
206212
if ($expectedCode) {
207213
$response = $this->read();
208214
if (!in_array((int) $response, (array) $expectedCode, strict: true)) {
@@ -229,6 +235,8 @@ protected function read(): string
229235
} elseif ($info['eof']) {
230236
throw new SmtpException('Connection has been closed unexpectedly.');
231237
}
238+
239+
continue;
232240
}
233241

234242
$data .= $line;

tests/Mail/Mail.dkim.headers.phpt

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

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

0 commit comments

Comments
 (0)