Failed Conditions
Pull Request — master (#393)
by Stefan
02:59
created

SizeCalculator::getSingleCharacterArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Helper;
4
5
class SizeCalculator
6
{
7
    /** @var SizeCollection */
8
    private $sizeCollection;
9
10
    /** @var array */
11
    private $characterSizes;
12
13
    /**
14
     * SizeCalculator constructor.
15
     *
16
     * @param SizeCollection $sizeCollection
17
     */
18 138
    public function __construct(SizeCollection $sizeCollection)
19
    {
20 138
        $this->sizeCollection = $sizeCollection;
21 138
    }
22
23
    /**
24
     * Return the estimated width of a cell value.
25
     *
26
     * @param mixed $value
27
     * @param int   $fontSize
28
     * @return float
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
29
     */
30 3
    public function getCellWidth($value, $fontSize)
31
    {
32 3
        $width = 1;
33 3
        foreach ($this->getSingleCharacterArray($value) as $character) {
34 3
            if (isset($this->characterSizes[$character])) {
35
                $width += $this->characterSizes[$character];
36 3
            } elseif (strlen($character)) {
37 3
                $width += 0.1 * $fontSize;
38 3
            }
39 3
        }
40
41 3
        return $width;
42
    }
43
44
    /**
45
     * Set proper font sizes by font.
46
     *
47
     * @param string $fontName
48
     * @param string $fontSize
49
     */
50 3
    public function setFont($fontName, $fontSize)
51
    {
52 3
        $this->characterSizes = $this->sizeCollection->get($fontName, $fontSize);
53 3
    }
54
55
    /**
56
     * Split value into individual characters.
57
     *
58
     * @param mixed $value
59
     * @return array
60
     */
61 3
    private function getSingleCharacterArray($value)
62
    {
63 3
        if (mb_strlen($value) == strlen($value)) {
64 3
            return str_split($value);
65
        }
66
67
        return preg_split('~~u', $value, -1, PREG_SPLIT_NO_EMPTY);
68
    }
69
}