Completed
Push — master ( d93a30...6a259d )
by Mark
41s queued 30s
created

StructuredReference   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
dl 0
loc 41
ccs 19
cts 20
cp 0.95
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromParser() 0 22 4
A __construct() 0 3 1
A value() 0 3 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine\Operands;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
6
7
final class StructuredReference implements Operand
8
{
9
    public const NAME = 'Structured Reference';
10
11
    private const OPEN_BRACE = '[';
12
    private const CLOSE_BRACE = ']';
13
14
    private string $value;
15
16 9
    public function __construct(string $structuredReference)
17
    {
18 9
        $this->value = $structuredReference;
19
    }
20
21 9
    public static function fromParser(string $formula, int $index, array $matches): self
22
    {
23 9
        $val = $matches[0];
24
25 9
        $srCount = substr_count($val, self::OPEN_BRACE)
26 9
            - substr_count($val, self::CLOSE_BRACE);
27 9
        while ($srCount > 0) {
28 9
            $srIndex = strlen($val);
29 9
            $srStringRemainder = substr($formula, $index + $srIndex);
30 9
            $closingPos = strpos($srStringRemainder, self::CLOSE_BRACE);
31 9
            if ($closingPos === false) {
32
                throw new Exception("Formula Error: No closing ']' to match opening '['");
33
            }
34 9
            $srStringRemainder = substr($srStringRemainder, 0, $closingPos + 1);
35 9
            --$srCount;
36 9
            if (strpos($srStringRemainder, self::OPEN_BRACE) !== false) {
37 7
                ++$srCount;
38
            }
39 9
            $val .= $srStringRemainder;
40
        }
41
42 9
        return new self($val);
43
    }
44
45 9
    public function value(): string
46
    {
47 9
        return $this->value;
48
    }
49
}
50