Passed
Pull Request — master (#4158)
by Owen
13:53
created

Formula::text()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

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