Completed
Pull Request — master (#316)
by Adrien
03:14
created

Style   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 385
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 96.58%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 48
lcom 2
cbo 0
dl 0
loc 385
ccs 113
cts 117
cp 0.9658
rs 8.4864
c 2
b 0
f 0

28 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A setId() 0 5 1
A getBorder() 0 4 1
A setBorder() 0 6 1
A shouldApplyBorder() 0 4 1
A isFontBold() 0 4 1
A setFontBold() 0 7 1
A isFontItalic() 0 4 1
A setFontItalic() 0 7 1
A isFontUnderline() 0 4 1
A setFontUnderline() 0 7 1
A isFontStrikethrough() 0 4 1
A setFontStrikethrough() 0 7 1
A getFontSize() 0 4 1
A setFontSize() 0 7 1
A getFontColor() 0 4 1
A setFontColor() 0 7 1
A getFontName() 0 4 1
A setFontName() 0 7 1
A shouldWrapText() 0 4 1
A shouldApplyFont() 0 4 1
A getBackgroundColor() 0 4 1
A setShouldWrapText() 0 6 1
A hasSetWrapText() 0 4 1
A setBackgroundColor() 0 6 1
A shouldApplyBackgroundColor() 0 4 1
A serialize() 0 12 1
F mergeWith() 0 37 21

How to fix   Complexity   

Complex Class

Complex classes like Style often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Style, and based on these observations, apply Extract Interface, too.

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
     * @var Border
66
     */
67
    protected $border = null;
68
69
    /**
70
     * @var bool Whether border properties should be applied
71
     */
72
    protected $shouldApplyBorder = false;
73
74
    /** @var string Background color */
75
    protected $backgroundColor = null;
76
77
    /** @var bool */
78
    protected $hasSetBackgroundColor = false;
79
80
81
    /**
82
     * @return int|null
83
     */
84 228
    public function getId()
85
    {
86 228
        return $this->id;
87
    }
88
89
    /**
90
     * @param int $id
91
     * @return Style
92
     */
93 270
    public function setId($id)
94
    {
95 270
        $this->id = $id;
96 270
        return $this;
97
    }
98
99
    /**
100
     * @return Border
101
     */
102 96
    public function getBorder()
103
    {
104 96
        return $this->border;
105
    }
106
107
    /**
108
     * @param Border $border
109
     */
110 24
    public function setBorder(Border $border)
111
    {
112 24
        $this->shouldApplyBorder = true;
113 24
        $this->border = $border;
114 24
        return $this;
115
    }
116
117
    /**
118
     * @return bool
119
     */
120 258
    public function shouldApplyBorder()
121
    {
122 258
        return $this->shouldApplyBorder;
123
    }
124
125
    /**
126
     * @return bool
127
     */
128 150
    public function isFontBold()
129
    {
130 150
        return $this->fontBold;
131
    }
132
133
    /**
134
     * @return Style
135
     */
136 48
    public function setFontBold()
137
    {
138 48
        $this->fontBold = true;
139 48
        $this->hasSetFontBold = true;
140 48
        $this->shouldApplyFont = true;
141 48
        return $this;
142
    }
143
144
    /**
145
     * @return bool
146
     */
147 150
    public function isFontItalic()
148
    {
149 150
        return $this->fontItalic;
150
    }
151
152
    /**
153
     * @return Style
154
     */
155 12
    public function setFontItalic()
156
    {
157 12
        $this->fontItalic = true;
158 12
        $this->hasSetFontItalic = true;
159 12
        $this->shouldApplyFont = true;
160 12
        return $this;
161
    }
162
163
    /**
164
     * @return bool
165
     */
166 150
    public function isFontUnderline()
167
    {
168 150
        return $this->fontUnderline;
169
    }
170
171
    /**
172
     * @return Style
173
     */
174 18
    public function setFontUnderline()
175
    {
176 18
        $this->fontUnderline = true;
177 18
        $this->hasSetFontUnderline = true;
178 18
        $this->shouldApplyFont = true;
179 18
        return $this;
180
    }
181
182
    /**
183
     * @return bool
184
     */
185 150
    public function isFontStrikethrough()
186
    {
187 150
        return $this->fontStrikethrough;
188
    }
189
190
    /**
191
     * @return Style
192
     */
193 12
    public function setFontStrikethrough()
194
    {
195 12
        $this->fontStrikethrough = true;
196 12
        $this->hasSetFontStrikethrough = true;
197 12
        $this->shouldApplyFont = true;
198 12
        return $this;
199
    }
200
201
    /**
202
     * @return int
203
     */
204 207
    public function getFontSize()
205
    {
206 207
        return $this->fontSize;
207
    }
208
209
    /**
210
     * @param int $fontSize Font size, in pixels
211
     * @return Style
212
     */
213 153
    public function setFontSize($fontSize)
214
    {
215 153
        $this->fontSize = $fontSize;
216 153
        $this->hasSetFontSize = true;
217 153
        $this->shouldApplyFont = true;
218 153
        return $this;
219
    }
220
221
    /**
222
     * @return string
223
     */
224 207
    public function getFontColor()
225
    {
226 207
        return $this->fontColor;
227
    }
228
229
    /**
230
     * Sets the font color.
231
     *
232
     * @param string $fontColor ARGB color (@see Color)
233
     * @return Style
234
     */
235 6
    public function setFontColor($fontColor)
236
    {
237 6
        $this->fontColor = $fontColor;
238 6
        $this->hasSetFontColor = true;
239 6
        $this->shouldApplyFont = true;
240 6
        return $this;
241
    }
242
243
    /**
244
     * @return string
245
     */
246 249
    public function getFontName()
247
    {
248 249
        return $this->fontName;
249
    }
250
251
    /**
252
     * @param string $fontName Name of the font to use
253
     * @return Style
254
     */
255 144
    public function setFontName($fontName)
256
    {
257 144
        $this->fontName = $fontName;
258 144
        $this->hasSetFontName = true;
259 144
        $this->shouldApplyFont = true;
260 144
        return $this;
261
    }
262
263
    /**
264
     * @return bool
265
     */
266 225
    public function shouldWrapText()
267
    {
268 225
        return $this->shouldWrapText;
269
    }
270
271
    /**
272
     * @param bool|void $shouldWrap Should the text be wrapped
273
     * @return Style
274 30
     */
275
    public function setShouldWrapText($shouldWrap = true)
276 30
    {
277 30
        $this->shouldWrapText = $shouldWrap;
278 30
        $this->hasSetWrapText = true;
279
        return $this;
280
    }
281
282
    /**
283
     * @return bool
284 171
     */
285
    public function hasSetWrapText()
286 171
    {
287
        return $this->hasSetWrapText;
288
    }
289
290
    /**
291
     * @return bool Whether specific font properties should be applied
292
     */
293
    public function shouldApplyFont()
294 18
    {
295
        return $this->shouldApplyFont;
296 18
    }
297 18
298 18
    /**
299
     * Sets the background color
300
     * @param string $color ARGB color (@see Color)
301
     * @return Style
302
     */
303
    public function setBackgroundColor($color)
304 141
    {
305
        $this->hasSetBackgroundColor = true;
306 141
        $this->backgroundColor = $color;
307
        return $this;
308
    }
309
310
    /**
311
     * @return string
312
     */
313 147
    public function getBackgroundColor()
314
    {
315 147
        return $this->backgroundColor;
316
    }
317
318
    /**
319
     *
320
     * @return bool Whether the background color should be applied
321
     */
322
    public function shouldApplyBackgroundColor()
323
    {
324
        return $this->hasSetBackgroundColor;
325 270
    }
326
327
    /**
328 270
     * Serializes the style for future comparison with other styles.
329 270
     * The ID is excluded from the comparison, as we only care about
330
     * actual style properties.
331 270
     *
332
     * @return string The serialized style
333 270
     */
334
    public function serialize()
335 270
    {
336
        // In order to be able to properly compare style, set static ID value
337
        $currentId = $this->id;
338
        $this->setId(0);
339
340
        $serializedStyle = serialize($this);
341
342
        $this->setId($currentId);
343
344
        return $serializedStyle;
345
    }
346
347
    /**
348
     * Merges the current style with the given style, using the given style as a base. This means that:
349 93
     *   - if current style and base style both have property A set, use current style property's value
350
     *   - if current style has property A set but base style does not, use current style property's value
351 93
     *   - if base style has property A set but current style does not, use base style property's value
352
     *
353 93
     * @NOTE: This function returns a new style.
354 6
     *
355 6
     * @param Style $baseStyle
356 93
     * @return Style New style corresponding to the merge of the 2 styles
357 3
     */
358 3
    public function mergeWith($baseStyle)
359 93
    {
360 3
        $mergedStyle = clone $this;
361 3
362 93
        if (!$this->hasSetFontBold && $baseStyle->isFontBold()) {
363 3
            $mergedStyle->setFontBold();
364 3
        }
365 93
        if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) {
366 42
            $mergedStyle->setFontItalic();
367 42
        }
368 93
        if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) {
369
            $mergedStyle->setFontUnderline();
370
        }
371 93
        if (!$this->hasSetFontStrikethrough && $baseStyle->isFontStrikethrough()) {
372 39
            $mergedStyle->setFontStrikethrough();
373 39
        }
374 93
        if (!$this->hasSetFontSize && $baseStyle->getFontSize() !== self::DEFAULT_FONT_SIZE) {
375 3
            $mergedStyle->setFontSize($baseStyle->getFontSize());
376 3
        }
377 93
        if (!$this->hasSetFontColor && $baseStyle->getFontColor() !== self::DEFAULT_FONT_COLOR) {
378 3
            $mergedStyle->setFontColor($baseStyle->getFontColor());
379 3
        }
380 93
        if (!$this->hasSetFontName && $baseStyle->getFontName() !== self::DEFAULT_FONT_NAME) {
381
            $mergedStyle->setFontName($baseStyle->getFontName());
382
        }
383
        if (!$this->hasSetWrapText && $baseStyle->shouldWrapText()) {
384 93
            $mergedStyle->setShouldWrapText();
385
        }
386
        if (!$this->getBorder() && $baseStyle->shouldApplyBorder()) {
387
            $mergedStyle->setBorder($baseStyle->getBorder());
388
        }
389
        if (!$this->hasSetBackgroundColor && $baseStyle->shouldApplyBackgroundColor()) {
390
            $mergedStyle->setBackgroundColor($baseStyle->getBackgroundColor());
391
        }
392
393
        return $mergedStyle;
394
    }
395
}
396