Test Setup Failed
Pull Request — develop_3.0 (#434)
by Hura
04:56
created

Cell::setStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 65
     * @var Style|null
60
     */
61 65
    protected $style = null;
62 65
63
    /**
64
     * @var StyleMerger
65
     */
66
    protected $styleMerger;
67 65
68
    /**
69 65
     * Cell constructor.
70 65
     * @param $value mixed
71 65
     * @param $style|null Style
72
     */
73
    public function __construct($value, Style $style = null)
74
    {
75
        $this->setValue($value);
76 64
        $this->setStyle($style);
77
        $this->styleMerger = new StyleMerger();
78 64
    }
79
80
    /**
81
     * @param $value mixed|null
82
     */
83
    public function setValue($value)
84
    {
85
        $this->value = $value;
86
        $this->type = $this->detectType($value);
87
    }
88
89
    /**
90
     * @return mixed|null
91
     */
92
    public function getValue()
93
    {
94 65
        return $this->value;
95
    }
96 65
97 5
    /**
98
     * @param Style $style|null
0 ignored issues
show
Bug introduced by
There is no parameter named $style|null. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
99 64
     */
100 4
    public function setStyle(Style $style = null)
101
    {
102 63
        $this->style = $style;
103 6
    }
104
105 62
    /**
106 60
     * @return Style|null
107
     */
108
    public function getStyle()
109 4
    {
110
        if (!isset($this->style)) {
111
            $this->setStyle(new Style());
112
        }
113
        return $this->style;
114
    }
115 12
116
    /**
117 12
     * @return int|null
118
     */
119
    public function getType()
120
    {
121
        return $this->type;
122
    }
123 8
124
    /**
125 8
     * Get the current value type
126
     * @param mixed|null $value
127
     * @return int
128
     */
129
    protected function detectType($value)
130
    {
131
        if (CellHelper::isBoolean($value)) {
132
            return self::TYPE_BOOLEAN;
133
        }
134
        if (CellHelper::isEmpty($value)) {
135
            return self::TYPE_EMPTY;
136
        }
137
        if (CellHelper::isNumeric($this->getValue())) {
138
            return self::TYPE_NUMERIC;
139
        }
140 11
        if (CellHelper::isNonEmptyString($value)) {
141
            return self::TYPE_STRING;
142 11
        }
143
144
        return self::TYPE_ERROR;
145
    }
146
147
    /**
148 64
     * @return bool
149
     */
150 64
    public function isBoolean()
151
    {
152
        return $this->type === self::TYPE_BOOLEAN;
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function isEmpty()
159
    {
160
        return $this->type === self::TYPE_EMPTY;
161
    }
162
163
    /**
164 1
     * Not used at the moment
165
     * @return bool
166 1
     */
167
    public function isFormula()
168
    {
169
        return $this->type === self::TYPE_FORMULA;
170
    }
171
172
    /**
173
     * @return bool
174
     */
175
    public function isNumeric()
176
    {
177
        return $this->type === self::TYPE_NUMERIC;
178
    }
179
180
    /**
181
     * @return bool
182
     */
183
    public function isString()
184
    {
185
        return $this->type === self::TYPE_STRING;
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function isError()
192
    {
193
        return $this->type === self::TYPE_ERROR;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function __toString()
200
    {
201
        return (string) $this->value;
202
    }
203
204
    /**
205
     * @param Style $style|null
0 ignored issues
show
Bug introduced by
There is no parameter named $style|null. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
206
     * @return Cell
207
     */
208
    public function applyStyle(Style $style = null)
209
    {
210
        if ($style === null) {
211
            return $this;
212
        }
213
        $mergedStyle = $this->styleMerger->merge($this->getStyle(), $style);
0 ignored issues
show
Bug introduced by
It seems like $this->getStyle() 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...
214
        $this->setStyle($mergedStyle);
215
        return $this;
216
    }
217
}
218