Test Failed
Pull Request — main (#372)
by
unknown
16:58 queued 06:32
created

ArithmeticRange::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=0);
4
5
namespace MartinGeorgiev\Model;
6
7
use MartinGeorgiev\Utils\MathUtils;
8
9
/**
10
 * @implements RangeInterface<float|int>
11
 */
12
class ArithmeticRange extends BaseRange
13
{
14
    public function __construct(
15
        public null|float|int $lower,
16
        public null|float|int $upper,
17
        public bool $lowerInclusive = true,
18
        public bool $upperInclusive = false,
19
    ) {
20
        // Void
21
    }
22
23
    public function __toString(): string
24
    {
25
        if (null !== $this->lower && $this->lower === $this->upper && !$this->lowerInclusive && !$this->upperInclusive) {
26
            return 'empty';
27
        }
28
29
        return \sprintf(
30
            '%s%s,%s%s',
31
            $this->lowerInclusive ? '[' : '(',
32
            $this->lower,
33
            $this->upper,
34
            $this->upperInclusive ? ']' : ')',
35
        );
36
    }
37
38
    public function contains(mixed $target): bool
39
    {
40
        return MathUtils::inRange($target, $this->lower, $this->upper, $this->lowerInclusive, $this->upperInclusive);
41
    }
42
43
    /**
44
     * @see https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-INFINITE
45
     */
46
    public static function createFromString(string $value): self
47
    {
48
        if (!\preg_match('/([\[(])(.*),(.*)([])])/', $value, $matches)) {
49
            throw new \RuntimeException('Unexpected value: '.$value);
50
        }
51
52
        $startParenthesis = $matches[1];
53
        $startsAtString = \trim($matches[2], '"');
54
        $endsAtString = \trim($matches[3], '"');
55
        $endParenthesis = $matches[4];
56
57
        if (\in_array($startsAtString, ['infinity', '-infinity', ''], true)) {
58
            $startsAt = null;
59
        } else {
60
            $startsAt = MathUtils::stringToNumber($startsAtString);
61
        }
62
63
        if (\in_array($endsAtString, ['infinity', '-infinity', ''], true)) {
64
            $endsAt = null;
65
        } else {
66
            $endsAt = MathUtils::stringToNumber($endsAtString);
67
        }
68
69
        $startInclusive = '[' === $startParenthesis;
70
        $endInclusive = ']' === $endParenthesis;
71
72
        return new NumRange($startsAt, $endsAt, $startInclusive, $endInclusive);
0 ignored issues
show
Bug introduced by
The type MartinGeorgiev\Model\NumRange was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
    }
74
}
75