Passed
Push — master ( 8a61a1...4323fc )
by
unknown
20:10 queued 40s
created

Formula::text()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 25
ccs 15
cts 16
cp 0.9375
rs 8.8333
c 0
b 0
f 0
cc 7
nc 7
nop 2
crap 7.0119
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
use PhpOffice\PhpSpreadsheet\Cell\Cell;
8
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9
10
class Formula
11
{
12
    /**
13
     * FORMULATEXT.
14
     *
15
     * @param mixed $cellReference The cell to check
16
     * @param ?Cell $cell The current cell (containing this formula)
17
     */
18 11
    public static function text(mixed $cellReference = '', ?Cell $cell = null): string
19
    {
20 11
        if ($cell === null) {
21 1
            return ExcelError::REF();
22
        }
23
24 10
        $worksheet = null;
25 10
        $cellReference = StringHelper::convertToString($cellReference);
26 10
        if (1 === preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches)) {
27 10
            $cellReference = $matches[6] . $matches[7];
28 10
            $worksheetName = trim($matches[3], "'");
29 10
            $worksheet = (!empty($worksheetName))
30 10
                ? $cell->getWorksheet()->getParentOrThrow()->getSheetByName($worksheetName)
31
                : $cell->getWorksheet();
32
        }
33
34
        if (
35 10
            $worksheet === null
36 10
            || !$worksheet->cellExists($cellReference)
37 10
            || !$worksheet->getCell($cellReference)->isFormula()
38
        ) {
39 3
            return ExcelError::NA();
40
        }
41
42 7
        return $worksheet->getCell($cellReference)->getValueString();
43
    }
44
}
45