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

Formula   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 32
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B text() 0 24 7
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