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

Cell::getType()   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
 * @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|null Style
74
     */
75 97
    public function __construct($value, Style $style = null)
76
    {
77 97
        $this->setValue($value);
78 97
        $this->setStyle($style);
0 ignored issues
show
Bug introduced by
It seems like $style defined by parameter $style on line 75 can be null; however, Box\Spout\Writer\Common\Entity\Cell::setStyle() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
79 1
        $this->styleMerger = new StyleMerger();
80 1
    }
81
82
    /**
83
     * @param $value mixed|null
84
     */
85 97
    public function setValue($value)
86
    {
87 97
        $this->value = $value;
88 97
        $this->type = $this->detectType($value);
89 97
    }
90
91
    /**
92
     * @return mixed|null
93
     */
94 91
    public function getValue()
95
    {
96 91
        return $this->value;
97
    }
98
99
    /**
100
     * @param Style $style
101
     */
102 1
    public function setStyle(Style $style)
103
    {
104 1
        $this->style = $style;
105 1
    }
106
107
    /**
108
     * @return Style|null
109
     */
110 1
    public function getStyle()
111
    {
112 1
        if (!isset($this->style)) {
113
            $this->setStyle(new Style());
114
        }
115 1
        return $this->style;
116
    }
117
118
    /**
119
     * @return int|null
120
     */
121
    public function getType()
122
    {
123
        return $this->type;
124
    }
125
126
    /**
127
     * Get the current value type
128
     * @param mixed|null $value
129
     * @return int
130
     */
131 97
    protected function detectType($value)
132
    {
133 97
        if (CellHelper::isBoolean($value)) {
134 2
            return self::TYPE_BOOLEAN;
135 95
        } elseif (CellHelper::isEmpty($value)) {
136 4
            return self::TYPE_EMPTY;
137 91
        } elseif (CellHelper::isNumeric($this->getValue())) {
138 2
            return self::TYPE_NUMERIC;
139 89
        } elseif (CellHelper::isNonEmptyString($value)) {
140 86
            return self::TYPE_STRING;
141
        } else {
142 3
            return self::TYPE_ERROR;
143
        }
144
    }
145
146
    /**
147
     * @return bool
148
     */
149
    public function isBoolean()
150
    {
151
        return $this->type === self::TYPE_BOOLEAN;
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    public function isEmpty()
158
    {
159
        return $this->type === self::TYPE_EMPTY;
160
    }
161
162
    /**
163
     * Not used at the moment
164
     * @return bool
165
     */
166
    public function isFormula()
167
    {
168
        return $this->type === self::TYPE_FORMULA;
169
    }
170
171
    /**
172
     * @return bool
173
     */
174
    public function isNumeric()
175
    {
176
        return $this->type === self::TYPE_NUMERIC;
177
    }
178
179
    /**
180
     * @return bool
181
     */
182 1
    public function isString()
183
    {
184 1
        return $this->type === self::TYPE_STRING;
185
    }
186
187
    /**
188
     * @return bool
189
     */
190
    public function isError()
191
    {
192
        return $this->type === self::TYPE_ERROR;
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function __toString()
199
    {
200
        return (string)$this->value;
201
    }
202
203
    /**
204
     * @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...
205
     * @return Cell
206
     */
207
    public function applyStyle(Style $style = null)
208
    {
209
        if ($style === null) {
210
            return $this;
211
        }
212
        $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...
213
        $this->setStyle($mergedStyle);
214
        return $this;
215
    }
216
}
217