Completed
Push — develop ( e6c95b...1c5db4 )
by Adrien
32:25
created

Font::applyFromArray()   F

Complexity

Conditions 11
Paths 513

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11.0113

Importance

Changes 0
Metric Value
cc 11
eloc 23
nc 513
nop 1
dl 0
loc 36
rs 3.1764
c 0
b 0
f 0
ccs 21
cts 22
cp 0.9545
crap 11.0113

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 PhpOffice\PhpSpreadsheet\Style;
4
5
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
6
use PhpOffice\PhpSpreadsheet\IComparable;
7
8
/**
9
 * Copyright (c) 2006 - 2016 PhpSpreadsheet.
10
 *
11
 * This library is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU Lesser General Public
13
 * License as published by the Free Software Foundation; either
14
 * version 2.1 of the License, or (at your option) any later version.
15
 *
16
 * This library is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
 * Lesser General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Lesser General Public
22
 * License along with this library; if not, write to the Free Software
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
 *
25
 * @category   PhpSpreadsheet
26
 *
27
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
28
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
29
 */
30
class Font extends Supervisor implements IComparable
31
{
32
    /* Underline types */
33
    const UNDERLINE_NONE = 'none';
34
    const UNDERLINE_DOUBLE = 'double';
35
    const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
36
    const UNDERLINE_SINGLE = 'single';
37
    const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
38
39
    /**
40
     * Font Name.
41
     *
42
     * @var string
43
     */
44
    protected $name = 'Calibri';
45
46
    /**
47
     * Font Size.
48
     *
49
     * @var float
50
     */
51
    protected $size = 11;
52
53
    /**
54
     * Bold.
55
     *
56
     * @var bool
57
     */
58
    protected $bold = false;
59
60
    /**
61
     * Italic.
62
     *
63
     * @var bool
64
     */
65
    protected $italic = false;
66
67
    /**
68
     * Superscript.
69
     *
70
     * @var bool
71
     */
72
    protected $superscript = false;
73
74
    /**
75
     * Subscript.
76
     *
77
     * @var bool
78
     */
79
    protected $subscript = false;
80
81
    /**
82
     * Underline.
83
     *
84
     * @var string
85
     */
86
    protected $underline = self::UNDERLINE_NONE;
87
88
    /**
89
     * Strikethrough.
90
     *
91
     * @var bool
92
     */
93
    protected $strikethrough = false;
94
95
    /**
96
     * Foreground color.
97
     *
98
     * @var Color
99
     */
100
    protected $color;
101
102
    /**
103
     * Create a new Font.
104
     *
105
     * @param bool $isSupervisor Flag indicating if this is a supervisor or not
106
     *                                    Leave this value at default unless you understand exactly what
107
     *                                        its ramifications are
108
     * @param bool $isConditional Flag indicating if this is a conditional style or not
109
     *                                    Leave this value at default unless you understand exactly what
110
     *                                        its ramifications are
111
     */
112 80
    public function __construct($isSupervisor = false, $isConditional = false)
113
    {
114
        // Supervisor?
115 80
        parent::__construct($isSupervisor);
116
117
        // Initialise values
118 80
        if ($isConditional) {
119 2
            $this->name = null;
120 2
            $this->size = null;
121 2
            $this->bold = null;
122 2
            $this->italic = null;
123 2
            $this->superscript = null;
124 2
            $this->subscript = null;
125 2
            $this->underline = null;
126 2
            $this->strikethrough = null;
127 2
            $this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
128
        } else {
129 80
            $this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
130
        }
131
        // bind parent if we are a supervisor
132 80
        if ($isSupervisor) {
133 80
            $this->color->bindParent($this, 'color');
134
        }
135 80
    }
136
137
    /**
138
     * Get the shared style component for the currently active cell in currently active sheet.
139
     * Only used for style supervisor.
140
     *
141
     * @return Font
142
     */
143
    public function getSharedComponent()
144
    {
145
        return $this->parent->getSharedComponent()->getFont();
146
    }
147
148
    /**
149
     * Build style array from subcomponents.
150
     *
151
     * @param array $array
152
     *
153
     * @return array
154
     */
155 19
    public function getStyleArray($array)
156
    {
157 19
        return ['font' => $array];
158
    }
159
160
    /**
161
     * Apply styles from array.
162
     * <code>
163
     * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
164
     *        array(
165
     *            'name'      => 'Arial',
166
     *            'bold'      => TRUE,
167
     *            'italic'    => FALSE,
168
     *            'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,
169
     *            'strikethrough'    => FALSE,
170
     *            'color'     => array(
171
     *                'rgb' => '808080'
172
     *            )
173
     *        )
174
     * );
175
     * </code>.
176
     *
177
     * @param array $pStyles Array containing style information
178
     *
179
     * @throws PhpSpreadsheetException
180
     *
181
     * @return Font
182
     */
183 23
    public function applyFromArray(array $pStyles)
184
    {
185 23
        if ($this->isSupervisor) {
186
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
187
        } else {
188 23
            if (isset($pStyles['name'])) {
189 15
                $this->setName($pStyles['name']);
190
            }
191 23
            if (isset($pStyles['bold'])) {
192 22
                $this->setBold($pStyles['bold']);
193
            }
194 23
            if (isset($pStyles['italic'])) {
195 4
                $this->setItalic($pStyles['italic']);
196
            }
197 23
            if (isset($pStyles['superscript'])) {
198 1
                $this->setSuperscript($pStyles['superscript']);
199
            }
200 23
            if (isset($pStyles['subscript'])) {
201 1
                $this->setSubscript($pStyles['subscript']);
202
            }
203 23
            if (isset($pStyles['underline'])) {
204 15
                $this->setUnderline($pStyles['underline']);
205
            }
206 23
            if (isset($pStyles['strikethrough'])) {
207 1
                $this->setStrikethrough($pStyles['strikethrough']);
208
            }
209 23
            if (isset($pStyles['color'])) {
210 16
                $this->getColor()->applyFromArray($pStyles['color']);
211
            }
212 23
            if (isset($pStyles['size'])) {
213 15
                $this->setSize($pStyles['size']);
214
            }
215
        }
216
217 23
        return $this;
218
    }
219
220
    /**
221
     * Get Name.
222
     *
223
     * @return string
224
     */
225 64
    public function getName()
226
    {
227 64
        if ($this->isSupervisor) {
228
            return $this->getSharedComponent()->getName();
229
        }
230
231 64
        return $this->name;
232
    }
233
234
    /**
235
     * Set Name.
236
     *
237
     * @param string $pValue
238
     *
239
     * @return Font
240
     */
241 26 View Code Duplication
    public function setName($pValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243 26
        if ($pValue == '') {
244
            $pValue = 'Calibri';
245
        }
246 26
        if ($this->isSupervisor) {
247 13
            $styleArray = $this->getStyleArray(['name' => $pValue]);
248 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
249
        } else {
250 26
            $this->name = $pValue;
251
        }
252
253 26
        return $this;
254
    }
255
256
    /**
257
     * Get Size.
258
     *
259
     * @return float
260
     */
261 64
    public function getSize()
262
    {
263 64
        if ($this->isSupervisor) {
264
            return $this->getSharedComponent()->getSize();
265
        }
266
267 64
        return $this->size;
268
    }
269
270
    /**
271
     * Set Size.
272
     *
273
     * @param float $pValue
274
     *
275
     * @return Font
276
     */
277 28 View Code Duplication
    public function setSize($pValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
278
    {
279 28
        if ($pValue == '') {
280
            $pValue = 10;
281
        }
282 28
        if ($this->isSupervisor) {
283 13
            $styleArray = $this->getStyleArray(['size' => $pValue]);
284 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
285
        } else {
286 28
            $this->size = $pValue;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pValue can also be of type integer. However, the property $size is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
287
        }
288
289 28
        return $this;
290
    }
291
292
    /**
293
     * Get Bold.
294
     *
295
     * @return bool
296
     */
297 64
    public function getBold()
298
    {
299 64
        if ($this->isSupervisor) {
300
            return $this->getSharedComponent()->getBold();
301
        }
302
303 64
        return $this->bold;
304
    }
305
306
    /**
307
     * Set Bold.
308
     *
309
     * @param bool $pValue
310
     *
311
     * @return Font
312
     */
313 32
    public function setBold($pValue)
314
    {
315 32
        if ($pValue == '') {
316 7
            $pValue = false;
317
        }
318 32
        if ($this->isSupervisor) {
319 19
            $styleArray = $this->getStyleArray(['bold' => $pValue]);
320 19
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
321
        } else {
322 32
            $this->bold = $pValue;
323
        }
324
325 32
        return $this;
326
    }
327
328
    /**
329
     * Get Italic.
330
     *
331
     * @return bool
332
     */
333 64
    public function getItalic()
334
    {
335 64
        if ($this->isSupervisor) {
336
            return $this->getSharedComponent()->getItalic();
337
        }
338
339 64
        return $this->italic;
340
    }
341
342
    /**
343
     * Set Italic.
344
     *
345
     * @param bool $pValue
346
     *
347
     * @return Font
348
     */
349 26
    public function setItalic($pValue)
350
    {
351 26
        if ($pValue == '') {
352 7
            $pValue = false;
353
        }
354 26
        if ($this->isSupervisor) {
355 1
            $styleArray = $this->getStyleArray(['italic' => $pValue]);
356 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
357
        } else {
358 26
            $this->italic = $pValue;
359
        }
360
361 26
        return $this;
362
    }
363
364
    /**
365
     * Get Superscript.
366
     *
367
     * @return bool
368
     */
369 62
    public function getSuperscript()
370
    {
371 62
        if ($this->isSupervisor) {
372
            return $this->getSharedComponent()->getSuperscript();
373
        }
374
375 62
        return $this->superscript;
376
    }
377
378
    /**
379
     * Set Superscript.
380
     *
381
     * @param bool $pValue
382
     *
383
     * @return Font
384
     */
385 3
    public function setSuperscript($pValue)
386
    {
387 3
        if ($pValue == '') {
388
            $pValue = false;
389
        }
390 3
        if ($this->isSupervisor) {
391
            $styleArray = $this->getStyleArray(['superscript' => $pValue]);
392
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
393
        } else {
394 3
            $this->superscript = $pValue;
395 3
            $this->subscript = !$pValue;
396
        }
397
398 3
        return $this;
399
    }
400
401
    /**
402
     * Get Subscript.
403
     *
404
     * @return bool
405
     */
406 62
    public function getSubscript()
407
    {
408 62
        if ($this->isSupervisor) {
409
            return $this->getSharedComponent()->getSubscript();
410
        }
411
412 62
        return $this->subscript;
413
    }
414
415
    /**
416
     * Set Subscript.
417
     *
418
     * @param bool $pValue
419
     *
420
     * @return Font
421
     */
422 3
    public function setSubscript($pValue)
423
    {
424 3
        if ($pValue == '') {
425
            $pValue = false;
426
        }
427 3
        if ($this->isSupervisor) {
428
            $styleArray = $this->getStyleArray(['subscript' => $pValue]);
429
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
430
        } else {
431 3
            $this->subscript = $pValue;
432 3
            $this->superscript = !$pValue;
433
        }
434
435 3
        return $this;
436
    }
437
438
    /**
439
     * Get Underline.
440
     *
441
     * @return string
442
     */
443 64
    public function getUnderline()
444
    {
445 64
        if ($this->isSupervisor) {
446
            return $this->getSharedComponent()->getUnderline();
447
        }
448
449 64
        return $this->underline;
450
    }
451
452
    /**
453
     * Set Underline.
454
     *
455
     * @param string|bool $pValue \PhpOffice\PhpSpreadsheet\Style\Font underline type
456
     *                                    If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
457
     *                                        false equates to UNDERLINE_NONE
458
     *
459
     * @return Font
460
     */
461 21 View Code Duplication
    public function setUnderline($pValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
462
    {
463 21
        if (is_bool($pValue)) {
464
            $pValue = ($pValue) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
465 21
        } elseif ($pValue == '') {
466
            $pValue = self::UNDERLINE_NONE;
467
        }
468 21
        if ($this->isSupervisor) {
469 13
            $styleArray = $this->getStyleArray(['underline' => $pValue]);
470 13
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
471
        } else {
472 21
            $this->underline = $pValue;
473
        }
474
475 21
        return $this;
476
    }
477
478
    /**
479
     * Get Strikethrough.
480
     *
481
     * @return bool
482
     */
483 62
    public function getStrikethrough()
484
    {
485 62
        if ($this->isSupervisor) {
486
            return $this->getSharedComponent()->getStrikethrough();
487
        }
488
489 62
        return $this->strikethrough;
490
    }
491
492
    /**
493
     * Set Strikethrough.
494
     *
495
     * @param bool $pValue
496
     *
497
     * @return Font
498
     */
499 7
    public function setStrikethrough($pValue)
500
    {
501 7
        if ($pValue == '') {
502 6
            $pValue = false;
503
        }
504 7
        if ($this->isSupervisor) {
505
            $styleArray = $this->getStyleArray(['strike' => $pValue]);
506
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
507
        } else {
508 7
            $this->strikethrough = $pValue;
509
        }
510
511 7
        return $this;
512
    }
513
514
    /**
515
     * Get Color.
516
     *
517
     * @return Color
518
     */
519 64
    public function getColor()
520
    {
521 64
        return $this->color;
522
    }
523
524
    /**
525
     * Set Color.
526
     *
527
     * @param Color $pValue
528
     *
529
     * @throws PhpSpreadsheetException
530
     *
531
     * @return Font
532
     */
533 16 View Code Duplication
    public function setColor(Color $pValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
534
    {
535
        // make sure parameter is a real color and not a supervisor
536 16
        $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
537
538 16
        if ($this->isSupervisor) {
539 1
            $styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
540 1
            $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
541
        } else {
542 15
            $this->color = $color;
543
        }
544
545 16
        return $this;
546
    }
547
548
    /**
549
     * Get hash code.
550
     *
551
     * @return string Hash code
552
     */
553 71
    public function getHashCode()
554
    {
555 71
        if ($this->isSupervisor) {
556
            return $this->getSharedComponent()->getHashCode();
557
        }
558
559 71
        return md5(
560 71
            $this->name .
561 71
            $this->size .
562 71
            ($this->bold ? 't' : 'f') .
563 71
            ($this->italic ? 't' : 'f') .
564 71
            ($this->superscript ? 't' : 'f') .
565 71
            ($this->subscript ? 't' : 'f') .
566 71
            $this->underline .
567 71
            ($this->strikethrough ? 't' : 'f') .
568 71
            $this->color->getHashCode() .
569 71
            __CLASS__
570
        );
571
    }
572
}
573