Mask   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 36%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 55
ccs 9
cts 25
cp 0.36
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 7 3
A stringBetween() 0 11 2
A shiftLeft() 0 7 3
A query() 0 3 1
A limit() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Dictionary\Features\WordsFinder\Mask;
6
7
use Stringable;
8
9
/**
10
 * @psalm-immutable
11
 */
12
final class Mask implements Stringable
13
{
14
    private const LIMIT = 3;
15
16
    private string $query;
17
    private string $limit;
18
19
    /**
20
     * @throws SearchMaskIsShortException
21
     */
22
    public function __construct(string $mask, int $limit = self::LIMIT)
23
    {
24
        $this->limit = $this->stringBetween($mask, '{', '}');
25
        $this->query = $this->limit ? str_replace($this->limit(), '', $mask) : $mask;
26
27
        if ((int) str_replace('0,', '', $this->limit) <= $limit) {
28
            throw new SearchMaskIsShortException();
29
        }
30
    }
31
32 6
    public function query(): string
33
    {
34 6
        return $this->query;
35
    }
36
37 7
    public function limit(): string
38
    {
39 7
        return sprintf('{%s}', $this->limit);
40
    }
41
42 1
    public function shiftLeft(): self
43
    {
44 1
        $query = $this->query[0] === '.' ? substr($this->query, 1) : $this->query;
45 1
        $query = '*' === $query[0] ? '.' . $query : $query;
46 1
        $limit = explode(',', $this->limit);
47
48 1
        return new self($query . sprintf('{%s,%s}', $limit[0], ((int) $limit[1]) - 1));
49
    }
50
51
    public function __toString(): string
52
    {
53
        return $this->query() . $this->limit();
54
    }
55
56
    private function stringBetween($string, $start, $end): string
57
    {
58
        $string = ' ' . $string;
59
        $ini = strpos($string, $start);
60
        if (!$ini) {
61
            return '0,100';
62
        }
63
        $ini += strlen($start);
64
        $len = strpos($string, $end, $ini) - $ini;
65
66
        return substr($string, $ini, $len);
67
    }
68
}
69