Passed
Push — master ( b8bba6...d51bf5 )
by
unknown
15:35 queued 01:10
created

Hyperlink::set()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
rs 8.8333
c 0
b 0
f 0
cc 7
nc 20
nop 3
crap 7.0178
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Calculation\LookupRef;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
6
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
7
use PhpOffice\PhpSpreadsheet\Cell\Cell;
8
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9
10
class Hyperlink
11
{
12
    /**
13
     * HYPERLINK.
14
     *
15
     * Excel Function:
16
     *        =HYPERLINK(linkURL, [displayName])
17
     *
18
     * @param mixed $linkURL Expect string. Value to check, is also the value returned when no error
19
     * @param mixed $displayName Expect string. Value to return when testValue is an error condition
20
     * @param ?Cell $cell The cell to set the hyperlink in
21
     *
22
     * @return string The value of $displayName (or $linkURL if $displayName was blank)
23
     */
24 15
    public static function set(mixed $linkURL = '', mixed $displayName = null, ?Cell $cell = null): string
25
    {
26 15
        $linkURL = ($linkURL === null) ? '' : StringHelper::convertToString(Functions::flattenSingleValue($linkURL));
27 15
        $displayName = ($displayName === null) ? '' : Functions::flattenSingleValue($displayName);
28
29 15
        if ((!is_object($cell)) || (trim($linkURL) == '')) {
30 3
            return ExcelError::REF();
31
        }
32
33 12
        if (is_object($displayName)) {
34
            $displayName = $linkURL;
35
        }
36 12
        $displayName = StringHelper::convertToString($displayName);
37 12
        if (trim($displayName) === '') {
38 8
            $displayName = $linkURL;
39
        }
40
41 12
        $cell->getHyperlink()
42 12
            ->setUrl($linkURL);
43 12
        $cell->getHyperlink()->setTooltip($displayName);
44
45 12
        return $displayName;
46
    }
47
}
48