Failed Conditions
Pull Request — develop_3.0 (#434)
by Adrien
03:05
created

Cell::isFormula()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Box\Spout\Writer\Common\Entity;
4
5
use Box\Spout\Writer\Common\Entity\Style\Style;
6
use Box\Spout\Writer\Common\Helper\CellHelper;
7
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
8
9
/**
10
 * Class Cell
11
 */
12
class Cell
13
{
14
    /**
15
     * Numeric cell type (whole numbers, fractional numbers, dates)
16
     */
17
    const TYPE_NUMERIC = 0;
18
19
    /**
20
     * String (text) cell type
21
     */
22
    const TYPE_STRING = 1;
23
24
    /**
25
     * Formula cell type
26
     * Not used at the moment
27
     */
28
    const TYPE_FORMULA = 2;
29
30
    /**
31
     * Empty cell type
32
     */
33
    const TYPE_EMPTY = 3;
34
35
    /**
36
     * Boolean cell type
37
     */
38
    const TYPE_BOOLEAN = 4;
39
40
    /**
41
     * Error cell type
42
     */
43
    const TYPE_ERROR = 5;
44
45
    /**
46
     * The value of this cell
47
     * @var mixed|null
48
     */
49
    protected $value;
50
51
    /**
52
     * The cell type
53
     * @var int|null
54
     */
55
    protected $type;
56
57
    /**
58
     * The cell style
59
     * @var Style|null
60
     */
61
    protected $style;
62
63
    /**
64
     * @var StyleMerger
65
     */
66
    protected $styleMerger;
67
68
    /**
69
     * @param $value mixed
70
     * @param Style|null $style
71
     */
72 97
    public function __construct($value, Style $style = null)
73
    {
74 97
        $this->setValue($value);
75 97
        $this->setStyle($style);
76 97
        $this->styleMerger = new StyleMerger();
77 97
    }
78
79
    /**
80
     * @param mixed|null $value
81
     */
82 97
    public function setValue($value)
83
    {
84 97
        $this->value = $value;
85 97
        $this->type = $this->detectType($value);
86 97
    }
87
88
    /**
89
     * @return mixed|null
90
     */
91 94
    public function getValue()
92
    {
93 94
        return $this->value;
94
    }
95
96
    /**
97
     * @param Style|null $style
98
     */
99 97
    public function setStyle($style)
100
    {
101 97
        $this->style = $style ?: new Style();
102 97
    }
103
104
    /**
105
     * @return Style
106
     */
107 64
    public function getStyle()
108
    {
109 64
        return $this->style;
110
    }
111
112
    /**
113
     * @return int|null
114
     */
115
    public function getType()
116
    {
117
        return $this->type;
118
    }
119
120
    /**
121
     * Get the current value type
122
     *
123
     * @param mixed|null $value
124
     * @return int
125
     */
126 97
    protected function detectType($value)
127
    {
128 97
        if (CellHelper::isBoolean($value)) {
129 6
            return self::TYPE_BOOLEAN;
130
        }
131 95
        if (CellHelper::isEmpty($value)) {
132 8
            return self::TYPE_EMPTY;
133
        }
134 92
        if (CellHelper::isNumeric($this->getValue())) {
135 7
            return self::TYPE_NUMERIC;
136
        }
137 90
        if (CellHelper::isNonEmptyString($value)) {
138 87
            return self::TYPE_STRING;
139
        }
140
141 4
        return self::TYPE_ERROR;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 11
    public function isBoolean()
148
    {
149 11
        return $this->type === self::TYPE_BOOLEAN;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155 23
    public function isEmpty()
156
    {
157 23
        return $this->type === self::TYPE_EMPTY;
158
    }
159
160
    /**
161
     * Not used at the moment
162
     *
163
     * @return bool
164
     */
165
    public function isFormula()
166
    {
167
        return $this->type === self::TYPE_FORMULA;
168
    }
169
170
    /**
171
     * @return bool
172
     */
173 10
    public function isNumeric()
174
    {
175 10
        return $this->type === self::TYPE_NUMERIC;
176
    }
177
178
    /**
179
     * @return bool
180
     */
181 65
    public function isString()
182
    {
183 65
        return $this->type === self::TYPE_STRING;
184
    }
185
186
    /**
187
     * @return bool
188
     */
189 1
    public function isError()
190
    {
191 1
        return $this->type === self::TYPE_ERROR;
192
    }
193
194
    /**
195
     * @return string
196
     */
197 8
    public function __toString()
198
    {
199 8
        return (string) $this->value;
200
    }
201
202
    /**
203
     * @param Style|null $style
204
     * @return Cell
205
     */
206 62
    public function applyStyle($style)
207
    {
208 62
        if ($style === null) {
209
            return $this;
210
        }
211
212 62
        $mergedStyle = $this->styleMerger->merge($this->style, $style);
0 ignored issues
show
Bug introduced by
It seems like $this->style can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
213 62
        $this->setStyle($mergedStyle);
214
215 62
        return $this;
216
    }
217
}
218