• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

azjezz / psl / 22519606807

28 Feb 2026 11:11AM UTC coverage: 97.532% (-1.2%) from 98.733%
22519606807

push

github

web-flow
feat(network): rewrite networking stack with TLS, UDP, SOCKS5, CIDR, and IO utilities (#585)

860 of 937 new or added lines in 31 files covered. (91.78%)

15 existing lines in 6 files now uncovered.

7470 of 7659 relevant lines covered (97.53%)

42.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/Psl/TLS/Connector.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\TLS;
6

7
use Override;
8
use Psl\Default\DefaultInterface;
9
use Psl\Network;
10
use Psl\TLS\Exception\HandshakeFailedException;
11

12
use function is_resource;
13
use function stream_context_set_options;
14

15
/**
16
 * Performs TLS client handshakes on existing streams.
17
 *
18
 * Takes a plain stream and upgrades it to a TLS-encrypted stream
19
 * using the provided {@see ClientConfig}.
20
 *
21
 * Usage:
22
 *   $connector = Connector::default();
23
 *   $tls = $connector->connect(TCP\connect('example.com', 443), 'example.com');
24
 */
25
final readonly class Connector implements DefaultInterface
26
{
27
    public function __construct(
28
        private ClientConfig $config = new ClientConfig(),
29
    ) {}
8✔
30

31
    /**
32
     * @pure
33
     */
34
    #[Override]
35
    public static function default(): static
36
    {
NEW
37
        return new self();
×
38
    }
39

40
    /**
41
     * Perform a TLS handshake on the given stream.
42
     *
43
     * @param non-empty-string|null $server_name SNI hostname override. If null, uses the config's peerName.
44
     *
45
     * @throws HandshakeFailedException If the TLS handshake fails.
46
     * @throws Network\Exception\RuntimeException If the stream is not available.
47
     */
48
    public function connect(Network\StreamInterface $stream, null|string $server_name = null): StreamInterface
49
    {
50
        $resource = $stream->getStream();
8✔
51
        if (!is_resource($resource)) {
8✔
52
            throw new Network\Exception\RuntimeException('Stream resource is not available.');
1✔
53
        }
54

55
        $config = $this->config;
7✔
56
        if ($server_name !== null && $config->peerName === null) {
7✔
57
            $config = $config->withPeerName($server_name);
7✔
58
        }
59

60
        $ssl_context = Internal\client_ssl_context($config);
7✔
61
        stream_context_set_options($resource, ['ssl' => $ssl_context]);
7✔
62

63
        $crypto_method = Internal\crypto_method($config->minimumVersion, $config->maximumVersion, server: false);
7✔
64

65
        Internal\enable_crypto($resource, $crypto_method);
7✔
66

67
        $state = Internal\extract_connection_state($resource);
7✔
68

69
        return new Internal\Stream($stream, $state);
7✔
70
    }
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc