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

Cell::getStyle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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