Completed
Push — master ( d37c8b...c70816 )
by Camilo
02:32
created

GeneralTopicRules::generalRulesCheck()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace unreal4u\MQTT\Internals;
6
7
abstract class GeneralTopicRules
8
{
9
    /**
10
     * @param string $topic
11
     * @return bool
12
     * @throws \OutOfBoundsException
13
     * @throws \InvalidArgumentException
14
     */
15 68
    protected function generalRulesCheck(string $topic): bool
16
    {
17 68
        if ($topic === '') {
18 1
            throw new \InvalidArgumentException('Topics must be at least 1 character long');
19
        }
20
21
        // UTF-8 topic names and filters must not be more than 65535 bytes in size
22 67
        if (\strlen($topic) > 65535) {
23 1
            throw new \OutOfBoundsException('Topics can not exceed 65535 bytes in size');
24
        }
25
26 66
        if (strpos($topic, \chr("\n")) !== false) {
0 ignored issues
show
Bug introduced by
' ' of type string is incompatible with the type integer expected by parameter $ascii of chr(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        if (strpos($topic, \chr(/** @scrutinizer ignore-type */ "\n")) !== false) {
Loading history...
27 1
            throw new \InvalidArgumentException('Topics can not contain the termination character');
28
        }
29
30 65
        return true;
31
    }
32
}
33