Failed Conditions
Push — perf-tests ( 50942d...2fc93e )
by Adrien
14:53
created

Style::mergeWith()   F

Complexity

Conditions 17
Paths 256

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 31
rs 3.6909
cc 17
eloc 19
nc 256
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Box\Spout\Writer\Style;
4
5
/**
6
 * Class Style
7
 * Represents a style to be applied to a cell
8
 *
9
 * @package Box\Spout\Writer\Style
10
 */
11
class Style
12
{
13
    /** Default font values */
14
    const DEFAULT_FONT_SIZE = 11;
15
    const DEFAULT_FONT_COLOR = Color::BLACK;
16
    const DEFAULT_FONT_NAME = 'Arial';
17
18
    /** @var int|null Style ID */
19
    protected $id = null;
20
21
    /** @var bool Whether the font should be bold */
22
    protected $fontBold = false;
23
    /** @var bool Whether the bold property was set */
24
    protected $hasSetFontBold = false;
25
26
    /** @var bool Whether the font should be italic */
27
    protected $fontItalic = false;
28
    /** @var bool Whether the italic property was set */
29
    protected $hasSetFontItalic = false;
30
31
    /** @var bool Whether the font should be underlined */
32
    protected $fontUnderline = false;
33
    /** @var bool Whether the underline property was set */
34
    protected $hasSetFontUnderline = false;
35
36
    /** @var bool Whether the font should be struck through */
37
    protected $fontStrikethrough = false;
38
    /** @var bool Whether the strikethrough property was set */
39
    protected $hasSetFontStrikethrough = false;
40
41
    /** @var int Font size */
42
    protected $fontSize = self::DEFAULT_FONT_SIZE;
43
    /** @var bool Whether the font size property was set */
44
    protected $hasSetFontSize = false;
45
46
    /** @var string Font color */
47
    protected $fontColor = self::DEFAULT_FONT_COLOR;
48
    /** @var bool Whether the font color property was set */
49
    protected $hasSetFontColor = false;
50
51
    /** @var string Font name */
52
    protected $fontName = self::DEFAULT_FONT_NAME;
53
    /** @var bool Whether the font name property was set */
54
    protected $hasSetFontName = false;
55
56
    /** @var bool Whether specific font properties should be applied */
57
    protected $shouldApplyFont = false;
58
59
    /** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */
60
    protected $shouldWrapText = false;
61
    /** @var bool Whether the wrap text property was set */
62
    protected $hasSetWrapText = false;
63
64
    /**
65
     * @return int|null
66
     */
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
72
    /**
73
     * @param int $id
74
     * @return Style
75
     */
76
    public function setId($id)
77
    {
78
        $this->id = $id;
79
        return $this;
80
    }
81
82
    /**
83
     * @return boolean
84
     */
85
    public function isFontBold()
86
    {
87
        return $this->fontBold;
88
    }
89
90
    /**
91
     * @return Style
92
     */
93
    public function setFontBold()
94
    {
95
        $this->fontBold = true;
96
        $this->hasSetFontBold = true;
97
        $this->shouldApplyFont = true;
98
        return $this;
99
    }
100
101
    /**
102
     * @return boolean
103
     */
104
    public function isFontItalic()
105
    {
106
        return $this->fontItalic;
107
    }
108
109
    /**
110
     * @return Style
111
     */
112
    public function setFontItalic()
113
    {
114
        $this->fontItalic = true;
115
        $this->hasSetFontItalic = true;
116
        $this->shouldApplyFont = true;
117
        return $this;
118
    }
119
120
    /**
121
     * @return boolean
122
     */
123
    public function isFontUnderline()
124
    {
125
        return $this->fontUnderline;
126
    }
127
128
    /**
129
     * @return Style
130
     */
131
    public function setFontUnderline()
132
    {
133
        $this->fontUnderline = true;
134
        $this->hasSetFontUnderline = true;
135
        $this->shouldApplyFont = true;
136
        return $this;
137
    }
138
139
    /**
140
     * @return boolean
141
     */
142
    public function isFontStrikethrough()
143
    {
144
        return $this->fontStrikethrough;
145
    }
146
147
    /**
148
     * @return Style
149
     */
150
    public function setFontStrikethrough()
151
    {
152
        $this->fontStrikethrough = true;
153
        $this->hasSetFontStrikethrough = true;
154
        $this->shouldApplyFont = true;
155
        return $this;
156
    }
157
158
    /**
159
     * @return int
160
     */
161
    public function getFontSize()
162
    {
163
        return $this->fontSize;
164
    }
165
166
    /**
167
     * @param int $fontSize Font size, in pixels
168
     * @return Style
169
     */
170
    public function setFontSize($fontSize)
171
    {
172
        $this->fontSize = $fontSize;
173
        $this->hasSetFontSize = true;
174
        $this->shouldApplyFont = true;
175
        return $this;
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getFontColor()
182
    {
183
        return $this->fontColor;
184
    }
185
186
    /**
187
     * Sets the font color.
188
     *
189
     * @param string $fontColor ARGB color (@see Color)
190
     * @return Style
191
     */
192
    public function setFontColor($fontColor)
193
    {
194
        $this->fontColor = $fontColor;
195
        $this->hasSetFontColor = true;
196
        $this->shouldApplyFont = true;
197
        return $this;
198
    }
199
200
    /**
201
     * @return string
202
     */
203
    public function getFontName()
204
    {
205
        return $this->fontName;
206
    }
207
208
    /**
209
     * @param string $fontName Name of the font to use
210
     * @return Style
211
     */
212
    public function setFontName($fontName)
213
    {
214
        $this->fontName = $fontName;
215
        $this->hasSetFontName = true;
216
        $this->shouldApplyFont = true;
217
        return $this;
218
    }
219
220
    /**
221
     * @return boolean
222
     */
223
    public function shouldWrapText()
224
    {
225
        return $this->shouldWrapText;
226
    }
227
228
    /**
229
     * @return Style
230
     */
231
    public function setShouldWrapText()
232
    {
233
        $this->shouldWrapText = true;
234
        $this->hasSetWrapText = true;
235
        return $this;
236
    }
237
238
    /**
239
     * @return bool Whether specific font properties should be applied
240
     */
241
    public function shouldApplyFont()
242
    {
243
        return $this->shouldApplyFont;
244
    }
245
246
    /**
247
     * Serializes the style for future comparison with other styles.
248
     * The ID is excluded from the comparison, as we only care about
249
     * actual style properties.
250
     *
251
     * @return string The serialized style
252
     */
253
    public function serialize()
254
    {
255
        // In order to be able to properly compare style, set static ID value
256
        $currentId = $this->id;
257
        $this->setId(0);
258
259
        $serializedStyle = serialize($this);
260
261
        $this->setId($currentId);
262
263
        return $serializedStyle;
264
    }
265
266
    /**
267
     * Merges the current style with the given style, using the given style as a base. This means that:
268
     *   - if current style and base style both have property A set, use current style property's value
269
     *   - if current style has property A set but base style does not, use current style property's value
270
     *   - if base style has property A set but current style does not, use base style property's value
271
     *
272
     * @NOTE: This function returns a new style.
273
     *
274
     * @param Style $baseStyle
275
     * @return Style New style corresponding to the merge of the 2 styles
276
     */
277
    public function mergeWith($baseStyle)
278
    {
279
        $mergedStyle = clone $this;
280
281
        if (!$this->hasSetFontBold && $baseStyle->isFontBold()) {
282
            $mergedStyle->setFontBold();
283
        }
284
        if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) {
285
            $mergedStyle->setFontItalic();
286
        }
287
        if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) {
288
            $mergedStyle->setFontUnderline();
289
        }
290
        if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) {
291
            $mergedStyle->setFontStrikethrough();
292
        }
293
        if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) {
294
            $mergedStyle->setFontSize($baseStyle->getFontSize());
295
        }
296
        if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) {
297
            $mergedStyle->setFontColor($baseStyle->getFontColor());
298
        }
299
        if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) {
300
            $mergedStyle->setFontName($baseStyle->getFontName());
301
        }
302
        if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) {
303
            $mergedStyle->setShouldWrapText();
304
        }
305
306
        return $mergedStyle;
307
    }
308
}
309