Passed
Pull Request — master (#4378)
by Owen
15:19
created

StyleMerger::mergeFontStyle()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 9.2222
c 0
b 0
f 0
cc 6
nc 32
nop 2
crap 6.0702
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
4
5
use PhpOffice\PhpSpreadsheet\Style\Border;
6
use PhpOffice\PhpSpreadsheet\Style\Borders;
7
use PhpOffice\PhpSpreadsheet\Style\Fill;
8
use PhpOffice\PhpSpreadsheet\Style\Font;
9
use PhpOffice\PhpSpreadsheet\Style\Style;
10
11
class StyleMerger
12
{
13
    protected Style $baseStyle;
14
15 4
    public function __construct(Style $baseStyle)
16
    {
17 4
        $this->baseStyle = $baseStyle;
18
    }
19
20 4
    public function getStyle(): Style
21
    {
22 4
        return $this->baseStyle;
23
    }
24
25 3
    public function mergeStyle(Style $style): void
26
    {
27 3
        if ($style->getNumberFormat()->getFormatCode() !== null) {
1 ignored issue
show
introduced by
The condition $style->getNumberFormat(...etFormatCode() !== null is always true.
Loading history...
28
            $this->baseStyle->getNumberFormat()->setFormatCode($style->getNumberFormat()->getFormatCode());
29
        }
30
        $this->mergeFontStyle($this->baseStyle->getFont(), $style->getFont());
31 3
        $this->mergeFillStyle($this->baseStyle->getFill(), $style->getFill());
32 3
        $this->mergeBordersStyle($this->baseStyle->getBorders(), $style->getBorders());
33
    }
34
35 3
    protected function mergeFontStyle(Font $baseFontStyle, Font $fontStyle): void
36 3
    {
37
        if ($fontStyle->getBold() !== null) {
38
            $baseFontStyle->setBold($fontStyle->getBold());
39 3
        }
40 3
        if ($fontStyle->getItalic() !== null) {
41
            $baseFontStyle->setItalic($fontStyle->getItalic());
42
        }
43
        if ($fontStyle->getStrikethrough() !== null) {
44 3
            $baseFontStyle->setStrikethrough($fontStyle->getStrikethrough());
45
        }
46 3
        if ($fontStyle->getUnderline() !== null) {
47
            $baseFontStyle->setUnderline($fontStyle->getUnderline());
48
        }
49
        if ($fontStyle->getColor()->getARGB() !== null) {
50 3
            $baseFontStyle->setColor($fontStyle->getColor());
51
        }
52
    }
53
54 3
    protected function mergeFillStyle(Fill $baseFillStyle, Fill $fillStyle): void
55
    {
56
        if ($fillStyle->getFillType() !== null) {
57
            $baseFillStyle->setFillType($fillStyle->getFillType());
58 3
        }
59
        $baseFillStyle->setRotation($fillStyle->getRotation());
60
        if ($fillStyle->getStartColor()->getARGB() !== null) {
61
            $baseFillStyle->setStartColor($fillStyle->getStartColor());
62 3
        }
63
        if ($fillStyle->getEndColor()->getARGB() !== null) {
64
            $baseFillStyle->setEndColor($fillStyle->getEndColor());
65
        }
66
    }
67 3
68
    protected function mergeBordersStyle(Borders $baseBordersStyle, Borders $bordersStyle): void
69 3
    {
70 3
        $this->mergeBorderStyle($baseBordersStyle->getTop(), $bordersStyle->getTop());
71
        $this->mergeBorderStyle($baseBordersStyle->getBottom(), $bordersStyle->getBottom());
72
        $this->mergeBorderStyle($baseBordersStyle->getLeft(), $bordersStyle->getLeft());
73
        $this->mergeBorderStyle($baseBordersStyle->getRight(), $bordersStyle->getRight());
74 3
    }
75
76
    protected function mergeBorderStyle(Border $baseBorderStyle, Border $borderStyle): void
77 3
    {
78 3
        $baseBorderStyle->setBorderStyle($borderStyle->getBorderStyle());
79
        if ($borderStyle->getColor()->getARGB() !== null) {
80
            $baseBorderStyle->setColor($borderStyle->getColor());
81 3
        }
82
    }
83
}
84