StyleMerger::mergeCellProperties()   B
last analyzed

Complexity

Conditions 11
Paths 32

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 11.0699

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
rs 7.3166
c 0
b 0
f 0
cc 11
nc 32
nop 3
crap 11.0699

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\Common\Manager\Style;
4
5
use Box\Spout\Common\Entity\Style\Style;
6
7
/**
8
 * Class StyleMerger
9
 * Takes care of merging styles together
10
 */
11
class StyleMerger
12
{
13
    /**
14
     * Merges the current style with the given style, using the given style as a base. This means that:
15
     *   - if current style and base style both have property A set, use current style property's value
16
     *   - if current style has property A set but base style does not, use current style property's value
17
     *   - if base style has property A set but current style does not, use base style property's value
18
     *
19
     * @NOTE: This function returns a new style.
20
     *
21
     * @param Style $style
22
     * @param Style $baseStyle
23
     * @return Style New style corresponding to the merge of the 2 styles
24
     */
25 79
    public function merge(Style $style, Style $baseStyle)
26
    {
27 79
        $mergedStyle = clone $style;
28
29 79
        $this->mergeFontStyles($mergedStyle, $style, $baseStyle);
30 79
        $this->mergeOtherFontProperties($mergedStyle, $style, $baseStyle);
31 79
        $this->mergeCellProperties($mergedStyle, $style, $baseStyle);
32
33 79
        return $mergedStyle;
34
    }
35
36
    /**
37
     * @param Style $styleToUpdate (passed as reference)
38
     * @param Style $style
39
     * @param Style $baseStyle
40
     * @return void
41
     */
42 79
    private function mergeFontStyles(Style $styleToUpdate, Style $style, Style $baseStyle)
43
    {
44 79
        if (!$style->hasSetFontBold() && $baseStyle->isFontBold()) {
45 3
            $styleToUpdate->setFontBold();
46
        }
47 79
        if (!$style->hasSetFontItalic() && $baseStyle->isFontItalic()) {
48 1
            $styleToUpdate->setFontItalic();
49
        }
50 79
        if (!$style->hasSetFontUnderline() && $baseStyle->isFontUnderline()) {
51 1
            $styleToUpdate->setFontUnderline();
52
        }
53 79
        if (!$style->hasSetFontStrikethrough() && $baseStyle->isFontStrikethrough()) {
54 1
            $styleToUpdate->setFontStrikethrough();
55
        }
56 79
    }
57
58
    /**
59
     * @param Style $styleToUpdate Style to update (passed as reference)
60
     * @param Style $style
61
     * @param Style $baseStyle
62
     * @return void
63
     */
64 79
    private function mergeOtherFontProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
65
    {
66 79
        if (!$style->hasSetFontSize() && $baseStyle->getFontSize() !== Style::DEFAULT_FONT_SIZE) {
67 38
            $styleToUpdate->setFontSize($baseStyle->getFontSize());
68
        }
69 79
        if (!$style->hasSetFontColor() && $baseStyle->getFontColor() !== Style::DEFAULT_FONT_COLOR) {
70 1
            $styleToUpdate->setFontColor($baseStyle->getFontColor());
71
        }
72 79
        if (!$style->hasSetFontName() && $baseStyle->getFontName() !== Style::DEFAULT_FONT_NAME) {
73 35
            $styleToUpdate->setFontName($baseStyle->getFontName());
74
        }
75 79
    }
76
77
    /**
78
     * @param Style $styleToUpdate Style to update (passed as reference)
79
     * @param Style $style
80
     * @param Style $baseStyle
81
     * @return void
82
     */
83 79
    private function mergeCellProperties(Style $styleToUpdate, Style $style, Style $baseStyle)
84
    {
85 79
        if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
86 1
            $styleToUpdate->setShouldWrapText();
87
        }
88 79
        if (!$style->hasSetCellAlignment() && $baseStyle->shouldApplyCellAlignment()) {
89
            $styleToUpdate->setCellAlignment($baseStyle->getCellAlignment());
90
        }
91 79
        if (!$style->getBorder() && $baseStyle->shouldApplyBorder()) {
92 1
            $styleToUpdate->setBorder($baseStyle->getBorder());
93
        }
94 79
        if (!$style->getFormat() && $baseStyle->shouldApplyFormat()) {
95 1
            $styleToUpdate->setFormat($baseStyle->getFormat());
96
        }
97 79
        if (!$style->shouldApplyBackgroundColor() && $baseStyle->shouldApplyBackgroundColor()) {
98 1
            $styleToUpdate->setBackgroundColor($baseStyle->getBackgroundColor());
99
        }
100 79
    }
101
}
102