Skip to content

Commit 13cb13d

Browse files
committed
fix: remove PHPStan error suppressions for TLS methods
Configure the crypto method bitmask through the SSL context before enabling TLS, using the context fallback supported since before PHP 7.1. This keeps combined protocol flags working without relying on PHPStan's incomplete third-argument signature. Remove ignoreErrors and reportUnmatchedIgnoredErrors overrides, and cover both single and combined TLS methods for client and server connections.
1 parent 21b3a71 commit 13cb13d

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

‎phpstan.neon.dist‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,3 @@ parameters:
55
phpVersion: 70100
66
# Retain runtime guards for older supported Promise implementations.
77
treatPhpDocTypesAsCertain: false
8-
# The crypto bitmask signature is already correct in older PHPStan releases.
9-
reportUnmatchedIgnoredErrors: false
10-
ignoreErrors:
11-
# PHP accepts combined crypto flags; some PHPStan versions only list individual constants.
12-
-
13-
message: '#^Parameter \#3 \$crypto_method of function stream_socket_enable_crypto expects .+, int given\.$#'
14-
path: src/StreamEncryption.php

‎src/StreamEncryption.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ public function toggleCrypto($socket, Deferred $deferred, bool $toggle, int $met
125125
return true;
126126
});
127127

128-
$result = \stream_socket_enable_crypto($socket, $toggle, $method);
128+
if ($toggle) {
129+
// Configure the full crypto bitmask through the SSL context, including combined protocol flags.
130+
\stream_context_set_option($socket, 'ssl', 'crypto_method', $method);
131+
}
132+
$result = \stream_socket_enable_crypto($socket, $toggle);
129133

130134
\restore_error_handler();
131135

‎tests/FunctionalSecureServerTest.php‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL()
8181
$server->close();
8282
}
8383

84-
public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClient()
84+
/** @dataProvider provideClientCryptoMethods */
85+
public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClient($method)
8586
{
8687
$server = new TcpServer(0);
8788
$server = new SecureServer($server, null, [
@@ -90,7 +91,7 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien
9091

9192
$connector = new SecureConnector(new TcpConnector(), null, [
9293
'verify_peer' => false,
93-
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
94+
'crypto_method' => $method
9495
]);
9596
$promise = $connector->connect($server->getAddress());
9697

@@ -108,12 +109,21 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien
108109
$server->close();
109110
}
110111

111-
public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServer()
112+
public function provideClientCryptoMethods()
113+
{
114+
return [
115+
'single method' => [STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT],
116+
'combined methods' => [STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT]
117+
];
118+
}
119+
120+
/** @dataProvider provideServerCryptoMethods */
121+
public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServer($method)
112122
{
113123
$server = new TcpServer(0);
114124
$server = new SecureServer($server, null, [
115125
'local_cert' => __DIR__ . '/../examples/localhost.pem',
116-
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER
126+
'crypto_method' => $method
117127
]);
118128

119129
$connector = new SecureConnector(new TcpConnector(), null, [
@@ -135,6 +145,14 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe
135145
$server->close();
136146
}
137147

148+
public function provideServerCryptoMethods()
149+
{
150+
return [
151+
'single method' => [STREAM_CRYPTO_METHOD_TLSv1_2_SERVER],
152+
'combined methods' => [STREAM_CRYPTO_METHOD_TLSv1_1_SERVER | STREAM_CRYPTO_METHOD_TLSv1_2_SERVER]
153+
];
154+
}
155+
138156
public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClient()
139157
{
140158
$server = new TcpServer(0);

0 commit comments

Comments
 (0)