Failed Conditions
Pull Request — master (#4141)
by Owen
13:32
created

StringValueBinder::bindValue()   D

Complexity

Conditions 18
Paths 22

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 18

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 32
rs 4.8666
ccs 21
cts 21
cp 1
cc 18
nc 22
nop 2
crap 18

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Cell;
4
5
use DateTimeInterface;
6
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
7
use PhpOffice\PhpSpreadsheet\RichText\RichText;
8
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9
use Stringable;
10
11
class StringValueBinder extends DefaultValueBinder implements IValueBinder
12
{
13
    protected bool $convertNull = true;
14
15
    protected bool $convertBoolean = true;
16
17
    protected bool $convertNumeric = true;
18
19
    protected bool $convertFormula = true;
20
21 3
    protected bool $setIgnoredErrors = false;
22
23 3
    public function setSetIgnoredErrors(bool $setIgnoredErrors = false): self
24
    {
25 3
        $this->setIgnoredErrors = $setIgnoredErrors;
26
27
        return $this;
28 8
    }
29
30 8
    public function setNullConversion(bool $suppressConversion = false): self
31
    {
32 8
        $this->convertNull = $suppressConversion;
33
34
        return $this;
35 6
    }
36
37 6
    public function setBooleanConversion(bool $suppressConversion = false): self
38
    {
39
        $this->convertBoolean = $suppressConversion;
40 11
41
        return $this;
42 11
    }
43
44 11
    public function getBooleanConversion(): bool
45
    {
46
        return $this->convertBoolean;
47 2
    }
48
49 2
    public function setNumericConversion(bool $suppressConversion = false): self
50
    {
51 2
        $this->convertNumeric = $suppressConversion;
52
53
        return $this;
54 22
    }
55
56 22
    public function setFormulaConversion(bool $suppressConversion = false): self
57 22
    {
58 22
        $this->convertFormula = $suppressConversion;
59 22
60
        return $this;
61 22
    }
62
63
    public function setConversionForAllValueTypes(bool $suppressConversion = false): self
64
    {
65
        $this->convertNull = $suppressConversion;
66
        $this->convertBoolean = $suppressConversion;
67
        $this->convertNumeric = $suppressConversion;
68
        $this->convertFormula = $suppressConversion;
69
70 72
        return $this;
71
    }
72 72
73 3
    /**
74
     * Bind value to a cell.
75 70
     *
76 1
     * @param Cell $cell Cell to bind value to
77
     * @param mixed $value Value to bind in cell
78
     */
79
    public function bindValue(Cell $cell, mixed $value): bool
80 69
    {
81 29
        if (is_object($value)) {
82
            return $this->bindObjectValue($cell, $value);
83
        }
84 69
        if ($value !== null && !is_scalar($value)) {
85 2
            throw new SpreadsheetException('Unable to bind unstringable ' . gettype($value));
86 67
        }
87 6
88 63
        // sanitize UTF-8 strings
89 16
        if (is_string($value)) {
90 47
            $value = StringHelper::sanitizeUTF8($value);
91 2
        }
92
93 45
        $ignoredErrors = false;
94
        if ($value === null && $this->convertNull === false) {
95
            $cell->setValueExplicit($value, DataType::TYPE_NULL);
96 69
        } elseif (is_bool($value) && $this->convertBoolean === false) {
97
            $cell->setValueExplicit($value, DataType::TYPE_BOOL);
98
        } elseif ((is_int($value) || is_float($value)) && $this->convertNumeric === false) {
99 3
            $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
100
        } elseif (is_string($value) && strlen($value) > 1 && $value[0] === '=' && $this->convertFormula === false && parent::dataTypeForValue($value) === DataType::TYPE_FORMULA) {
101
            $cell->setValueExplicit($value, DataType::TYPE_FORMULA);
102 3
        } else {
103 1
            $ignoredErrors = is_numeric($value);
104 1
            $cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
105 2
        }
106 1
        if ($this->setIgnoredErrors) {
107 1
            $cell->getIgnoredErrors()->setNumberStoredAsText($ignoredErrors);
108 1
        }
109
110 1
        return true;
111
    }
112
113 3
    protected function bindObjectValue(Cell $cell, object $value): bool
114
    {
115
        // Handle any objects that might be injected
116
        $ignoredErrors = false;
117
        if ($value instanceof DateTimeInterface) {
118
            $value = $value->format('Y-m-d H:i:s');
119
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
120
        } elseif ($value instanceof RichText) {
121
            $cell->setValueExplicit($value, DataType::TYPE_INLINE);
122
            $ignoredErrors = is_numeric($value->getPlainText());
123
        } elseif ($value instanceof Stringable) {
124
            $cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
125
            $ignoredErrors = is_numeric((string) $value);
126
        } else {
127
            throw new SpreadsheetException('Unable to bind unstringable object of type ' . get_class($value));
128
        }
129
        if ($this->setIgnoredErrors) {
130
            $cell->getIgnoredErrors()->setNumberStoredAsText($ignoredErrors);
131
        }
132
133
        return true;
134
    }
135
}
136