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

Cell::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * @var Style|null
60
     */
61
    protected $style = null;
62
63
    /**
64
     * @var StyleMerger
65
     */
66
    protected $styleMerger;
67
68
    /**
69
     * Cell constructor.
70
     * @param $value mixed
71
     * @param $style|null Style
72
     */
73 97
    public function __construct($value, Style $style = null)
74
    {
75 97
        $this->setValue($value);
76 97
        $this->setStyle($style);
77 97
        $this->styleMerger = new StyleMerger();
78 97
    }
79
80
    /**
81
     * @param $value mixed|null
82
     */
83 97
    public function setValue($value)
84
    {
85 97
        $this->value = $value;
86 97
        $this->type = $this->detectType($value);
87 97
    }
88
89
    /**
90
     * @return mixed|null
91
     */
92 94
    public function getValue()
93
    {
94 94
        return $this->value;
95
    }
96
97
    /**
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
     */
100 97
    public function setStyle(Style $style = null)
101
    {
102 97
        $this->style = $style;
103 97
    }
104
105
    /**
106
     * @return Style|null
107
     */
108 63
    public function getStyle()
109
    {
110 63
        if (!isset($this->style)) {
111 62
            $this->setStyle(new Style());
112
        }
113 63
        return $this->style;
114
    }
115
116
    /**
117
     * @return int|null
118
     */
119
    public function getType()
120
    {
121
        return $this->type;
122
    }
123
124
    /**
125
     * Get the current value type
126
     * @param mixed|null $value
127
     * @return int
128
     */
129 97
    protected function detectType($value)
130
    {
131 97
        if (CellHelper::isBoolean($value)) {
132 6
            return self::TYPE_BOOLEAN;
133
        }
134 95
        if (CellHelper::isEmpty($value)) {
135 8
            return self::TYPE_EMPTY;
136
        }
137 92
        if (CellHelper::isNumeric($this->getValue())) {
138 7
            return self::TYPE_NUMERIC;
139
        }
140 90
        if (CellHelper::isNonEmptyString($value)) {
141 87
            return self::TYPE_STRING;
142
        }
143
144 4
        return self::TYPE_ERROR;
145
    }
146
147
    /**
148
     * @return bool
149
     */
150 11
    public function isBoolean()
151
    {
152 11
        return $this->type === self::TYPE_BOOLEAN;
153
    }
154
155
    /**
156
     * @return bool
157
     */
158 30
    public function isEmpty()
159
    {
160 30
        return $this->type === self::TYPE_EMPTY;
161
    }
162
163
    /**
164
     * Not used at the moment
165
     * @return bool
166
     */
167
    public function isFormula()
168
    {
169
        return $this->type === self::TYPE_FORMULA;
170
    }
171
172
    /**
173
     * @return bool
174
     */
175 10
    public function isNumeric()
176
    {
177 10
        return $this->type === self::TYPE_NUMERIC;
178
    }
179
180
    /**
181
     * @return bool
182
     */
183 64
    public function isString()
184
    {
185 64
        return $this->type === self::TYPE_STRING;
186
    }
187
188
    /**
189
     * @return bool
190
     */
191 1
    public function isError()
192
    {
193 1
        return $this->type === self::TYPE_ERROR;
194
    }
195
196
    /**
197
     * @return string
198
     */
199 8
    public function __toString()
200
    {
201 8
        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 61
    public function applyStyle(Style $style = null)
209
    {
210 61
        if ($style === null) {
211
            return $this;
212
        }
213 61
        $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 61
        $this->setStyle($mergedStyle);
215 61
        return $this;
216
    }
217
}
218