Failed Conditions
Push — master ( 4b6ad7...9fab89 )
by Adrien
06:57
created

Font::writeFont()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7.0119

Importance

Changes 0
Metric Value
cc 7
eloc 37
c 0
b 0
f 0
nc 48
nop 0
dl 0
loc 54
ccs 30
cts 32
cp 0.9375
crap 7.0119
rs 8.3946

How to fix   Long Method   

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\Writer\Xls;
4
5
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
6
7
class Font
8
{
9
    /**
10
     * Color index.
11
     *
12
     * @var int
13
     */
14
    private $colorIndex;
15
16
    /**
17
     * Font.
18
     *
19
     * @var \PhpOffice\PhpSpreadsheet\Style\Font
20
     */
21
    private $font;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param \PhpOffice\PhpSpreadsheet\Style\Font $font
27
     */
28 44
    public function __construct(\PhpOffice\PhpSpreadsheet\Style\Font $font)
29
    {
30 44
        $this->colorIndex = 0x7FFF;
31 44
        $this->font = $font;
32 44
    }
33
34
    /**
35
     * Set the color index.
36
     *
37
     * @param int $colorIndex
38
     */
39 44
    public function setColorIndex($colorIndex)
40
    {
41 44
        $this->colorIndex = $colorIndex;
42 44
    }
43
44
    /**
45
     * Get font record data.
46
     *
47
     * @return string
48
     */
49 44
    public function writeFont()
50
    {
51 44
        $font_outline = 0;
52 44
        $font_shadow = 0;
53
54 44
        $icv = $this->colorIndex; // Index to color palette
55 44
        if ($this->font->getSuperscript()) {
56 2
            $sss = 1;
57 44
        } elseif ($this->font->getSubscript()) {
58 2
            $sss = 2;
59
        } else {
60 44
            $sss = 0;
61
        }
62 44
        $bFamily = 0; // Font family
63 44
        $bCharSet = \PhpOffice\PhpSpreadsheet\Shared\Font::getCharsetFromFontName($this->font->getName()); // Character set
64
65 44
        $record = 0x31; // Record identifier
66 44
        $reserved = 0x00; // Reserved
67 44
        $grbit = 0x00; // Font attributes
68 44
        if ($this->font->getItalic()) {
69 11
            $grbit |= 0x02;
70
        }
71 44
        if ($this->font->getStrikethrough()) {
72 2
            $grbit |= 0x08;
73
        }
74 44
        if ($font_outline) {
75
            $grbit |= 0x10;
76
        }
77 44
        if ($font_shadow) {
78
            $grbit |= 0x20;
79
        }
80
81 44
        $data = pack(
82 44
            'vvvvvCCCC',
83
            // Fontsize (in twips)
84 44
            $this->font->getSize() * 20,
85
            $grbit,
86
            // Colour
87
            $icv,
88
            // Font weight
89 44
            self::mapBold($this->font->getBold()),
90
            // Superscript/Subscript
91
            $sss,
92 44
            self::mapUnderline($this->font->getUnderline()),
93
            $bFamily,
94
            $bCharSet,
95 44
            $reserved
96
        );
97 44
        $data .= StringHelper::UTF8toBIFF8UnicodeShort($this->font->getName());
98
99 44
        $length = strlen($data);
100 44
        $header = pack('vv', $record, $length);
101
102 44
        return $header . $data;
103
    }
104
105
    /**
106
     * Map to BIFF5-BIFF8 codes for bold.
107
     *
108
     * @param bool $bold
109
     *
110
     * @return int
111
     */
112 44
    private static function mapBold($bold)
113
    {
114 44
        if ($bold) {
115 17
            return 0x2BC; //  700 = Bold font weight
116
        }
117
118 44
        return 0x190; //  400 = Normal font weight
119
    }
120
121
    /**
122
     * Map of BIFF2-BIFF8 codes for underline styles.
123
     *
124
     * @var array of int
125
     */
126
    private static $mapUnderline = [
127
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE => 0x00,
128
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE => 0x01,
129
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE => 0x02,
130
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLEACCOUNTING => 0x21,
131
        \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLEACCOUNTING => 0x22,
132
    ];
133
134
    /**
135
     * Map underline.
136
     *
137
     * @param string $underline
138
     *
139
     * @return int
140
     */
141 44
    private static function mapUnderline($underline)
142
    {
143 44
        if (isset(self::$mapUnderline[$underline])) {
144 44
            return self::$mapUnderline[$underline];
145
        }
146
147
        return 0x00;
148
    }
149
}
150