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

basis-company / nats.php / 30461032612

29 Jul 2026 02:28PM UTC coverage: 94.816% (-0.06%) from 94.876%
30461032612

push

github

nekufa
Handle invalid stream resource with processException

1 of 1 new or added line in 1 file covered. (100.0%)

5 existing lines in 2 files now uncovered.

1573 of 1659 relevant lines covered (94.82%)

84.88 hits per line

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

95.65
/src/Configuration.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Basis\Nats;
6

7
use InvalidArgumentException;
8

9
class Configuration
10
{
11
    public const DELAY_CONSTANT = 'constant';
12
    public const DELAY_LINEAR = 'linear';
13
    public const DELAY_EXPONENTIAL = 'exponential';
14

15
    protected float $delay;
16
    protected string $delayMode;
17

18
    /**
19
     * @param array<string, string|int|bool|null> $options
20
     */
21
    public function __construct(
340✔
22
        array $options = [], // deprecated
23
        array $options2 = [], // deprecated multi option array support
24
        array $options3 = [], // deprecated multi option array support
25
        public string $host = 'localhost',
26
        public int $port = 4222,
27
        public ?string $user = null,
28
        public ?string $jwt = null,
29
        public ?string $pass = null,
30
        public ?string $token = null,
31
        public ?string $nkey = null,
32
        public ?string $tlsKeyFile = null,
33
        public ?string $tlsCertFile = null,
34
        public ?string $tlsCaFile = null,
35
        public bool $tlsHandshakeFirst = false,
36
        public bool $pedantic = false,
37
        public bool $reconnect = true,
38
        public bool $verbose = false,
39
        public float $timeout = 1,
40
        public int $pingInterval = 2,
41
        float $delay = 0.001,
42
        string $delayMode = self::DELAY_CONSTANT,
43
        public string $lang = 'php',
44
        public string $version = 'dev',
45
        public string $inboxPrefix = '_INBOX',
46
        public int $maxReconnectAttempts = -1,
47
    ) {
48

49
        $this->setDelay($delay, $delayMode);
340✔
50

51
        foreach (array_merge($options, $options2, $options3) as $k => $v) {
340✔
52
            if (!property_exists($this, $k)) {
84✔
53
                throw new InvalidArgumentException("Invalid config option $k");
4✔
54
            }
55
            if ($k == 'delayMode') {
80✔
56
                $this->setDelay($this->delay, $v);
4✔
57
            } elseif ($k == 'delay') {
76✔
58
                $this->setDelay($v, $this->delayMode);
4✔
59
            } else {
60
                $this->$k = $v;
76✔
61
            }
62
        }
63
    }
64

65
    public function getOptions(): array
312✔
66
    {
67
        $options = [
312✔
68
            'lang' => $this->lang,
312✔
69
            'pedantic' => $this->pedantic,
312✔
70
            'verbose' => $this->verbose,
312✔
71
            'version' => $this->version,
312✔
72
            'headers' => true,
312✔
73
        ];
312✔
74

75
        if ($this->user !== null) {
312✔
76
            $options['user'] = $this->user;
4✔
77
            $options['pass'] = $this->pass;
4✔
78
        } elseif ($this->token !== null) {
308✔
79
            $options['auth_token'] = $this->token;
4✔
80
        } elseif ($this->jwt !== null) {
304✔
81
            $options['jwt'] = $this->jwt;
12✔
82
        }
83

84
        return $options;
312✔
85
    }
86

87
    public function delay(int $iteration)
5✔
88
    {
89
        $milliseconds = intval($this->delay * 1_000);
5✔
90

91
        switch ($this->delayMode) {
5✔
92
            case self::DELAY_EXPONENTIAL:
93
                $milliseconds = $milliseconds ** $iteration;
4✔
94
                break;
4✔
95

96
            case self::DELAY_LINEAR:
UNCOV
97
                $milliseconds = $milliseconds * $iteration;
×
UNCOV
98
                break;
×
99
        }
100

101
        $microseconds = $milliseconds * 1_000;
5✔
102
        // Avoid type errors from integers overflowing to floats
103
        $microseconds = is_float($microseconds) ? PHP_INT_MAX : $microseconds;
5✔
104
        usleep($microseconds);
5✔
105
    }
106

107
    public function setDelay(float $delay, string $mode = self::DELAY_CONSTANT): self
340✔
108
    {
109
        if (!in_array($mode, [self::DELAY_CONSTANT, self::DELAY_EXPONENTIAL, self::DELAY_LINEAR])) {
340✔
110
            throw new InvalidArgumentException("Invalid mode: $mode");
4✔
111
        }
112

113
        $this->delay = $delay;
340✔
114
        $this->delayMode = $mode;
340✔
115

116
        return $this;
340✔
117
    }
118

119
    public function getDelay(): float
4✔
120
    {
121
        return $this->delay;
4✔
122
    }
123

124
    public function getDelayMode(): string
4✔
125
    {
126
        return $this->delayMode;
4✔
127
    }
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc