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

sirn-se / websocket-php / 14221063815

02 Apr 2025 01:37PM UTC coverage: 99.675% (-0.3%) from 100.0%
14221063815

push

github

sirn-se
Compressor: Per-MEssage Deflate

165 of 169 new or added lines in 7 files covered. (97.63%)

1228 of 1232 relevant lines covered (99.68%)

23.01 hits per line

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

96.15
/src/Message/Message.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2025 Textalk and contributors.
5
 * This file is part of Websocket PHP and is free software under the ISC License.
6
 */
7

8
namespace WebSocket\Message;
9

10
use DateTimeImmutable;
11
use DateTimeInterface;
12
use Stringable;
13
use WebSocket\Exception\ConnectionFailureException;
14
use WebSocket\Frame\Frame;
15
use WebSocket\Trait\StringableTrait;
16

17
/**
18
 * WebSocket\Message\Message class.
19
 * Abstract superclass for WebSocket messages.
20
 */
21
abstract class Message implements Stringable
22
{
23
    use StringableTrait;
24

25
    protected string $opcode;
26
    protected string $content;
27
    protected DateTimeInterface $timestamp;
28
    protected bool $compress = false;
29

30
    public function __construct(string $content = '')
31
    {
32
        $this->content = $content;
60✔
33
        $this->timestamp = new DateTimeImmutable();
60✔
34
    }
35

36
    public function getOpcode(): string
37
    {
38
        return $this->opcode;
44✔
39
    }
40

41
    public function getLength(): int
42
    {
43
        return strlen($this->content);
44✔
44
    }
45

46
    public function getTimestamp(): DateTimeInterface
47
    {
48
        return $this->timestamp;
5✔
49
    }
50

51
    public function getContent(): string
52
    {
53
        return $this->content;
29✔
54
    }
55

56
    public function setContent(string $content = ''): void
57
    {
58
        $this->content = $content;
9✔
59
    }
60

61
    public function hasContent(): bool
62
    {
63
        return $this->content != '';
5✔
64
    }
65

66
    public function getPayload(): string
67
    {
68
        return $this->content;
42✔
69
    }
70

71
    public function setPayload(string $payload = ''): void
72
    {
73
        $this->content = $payload;
28✔
74
    }
75

76
    public function isCompressed(): bool
77
    {
78
        return false;
16✔
79
    }
80

81
    public function setCompress(bool $compress): void
82
    {
83
        if ($compress) {
11✔
NEW
84
            throw new ConnectionFailureException('Must not compress control message.');
×
85
        }
86
    }
87

88
    /**
89
     * Split messages into frames
90
     * @param int<1, max> $frameSize
91
     * @return array<Frame>
92
     */
93
    public function getFrames(int $frameSize = 4096): array
94
    {
95
        $frames = [];
46✔
96
        $split = str_split($this->getPayload(), $frameSize);
46✔
97
        if (empty($split)) {
46✔
98
            $split = [''];
8✔
99
        }
100
        foreach ($split as $i => $payload) {
46✔
101
            $frames[] = new Frame(
46✔
102
                $i === 0 ? $this->opcode : 'continuation',
46✔
103
                $payload,
46✔
104
                $i === array_key_last($split)
46✔
105
            );
46✔
106
        }
107
        if ($this->isCompressed()) {
46✔
108
            $frames[0]->setRsv1(true);
5✔
109
        }
110
        return $frames;
46✔
111
    }
112
}
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

© 2025 Coveralls, Inc