Completed
Push — master ( da315f...8e06d5 )
by Stefan
03:13
created

WidthCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.45%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 8
c 4
b 1
f 1
lcom 1
cbo 0
dl 0
loc 72
ccs 21
cts 22
cp 0.9545
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A loadWidthsFromCsv() 0 14 4
A get() 0 10 3
1
<?php
2
3
namespace OneSheet\Width;
4
5
/**
6
 * Class WidthCollection
7
 *
8
 * @package OneSheet
9
 */
10
class WidthCollection
11
{
12
    /**
13
     * Array containing character widths for each font & size.
14
     *
15
     * @var array
16
     */
17
    private static $widths = array();
18
19
    /**
20
     * Create character width map for each font.
21
     */
22 6
    public function __construct()
23
    {
24 6
        self::loadWidthsFromCsv(dirname(__FILE__) . '/widths.csv');
25
//        self::loadAdditionalWidths();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26 6
    }
27
28
    /**
29
     * Dirty way to allow developers to load character widths that
30
     * are not yet included.
31
     *
32
     * @param string $csvPath
33
     */
34 6
    public static function loadWidthsFromCsv($csvPath)
35
    {
36 6
        foreach (file($csvPath) as $line) {
37 6
            if ($line[0] == 'f') continue;
38 6
            $widths = explode(',', trim($line));
39 6
            if (count(range(33, 126)) + 3 == count($widths)) {
40 6
                $fontName = array_shift($widths);
41 6
                $fontSize = array_shift($widths);
42 6
                $boldMulti = array_shift($widths);
43 6
                self::$widths[$fontName][$fontSize] = array_combine(array_map('chr', range(33, 126)), $widths);
44 6
                self::$widths[$fontName][$fontSize] += array('bold' => $boldMulti);
45 6
            }
46 6
        }
47 6
    }
48
49
//    Testing additional character widths ... fixed types.
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
//    private static function loadAdditionalWidths()
51
//    {
52
//        $hiragana = array('ぁ','あ','ぃ','い','ぅ','う','ぇ','え','ぉ','お','か','が','き','ぎ','く','ぐ','け','げ','こ','ご','さ','ざ','し','じ','す','ず','せ','ぜ','そ','ぞ','た','だ','ち','ぢ','っ','つ','づ','て','で','と','ど','な','に','ぬ','ね','の','は','ば','ぱ','ひ','び','ぴ','ふ','ぶ','ぷ','へ','べ','ぺ','ほ','ぼ','ぽ','ま','み','む','め','も','ゃ','や','ゅ','ゆ','ょ','よ','ら','り','る','れ','ろ','ゎ','わ','ゐ','ゑ','を','ん','ゔ','ゕ','ゖ');
53
//        foreach (self::$widths as &$width) {
54
//            $width[9] += array_combine($hiragana, array_fill(0, count($hiragana), 1.76));
55
//            $width[10] += array_combine($hiragana, array_fill(0, count($hiragana), 2.06));
56
//            $width[11] += array_combine($hiragana, array_fill(0, count($hiragana), 2.35));
57
//            $width[12] += array_combine($hiragana, array_fill(0, count($hiragana), 2.35));
58
//            $width[13] += array_combine($hiragana, array_fill(0, count($hiragana), 2.65));
59
//            $width[14] += array_combine($hiragana, array_fill(0, count($hiragana), 2.94));
60
//            $width[15] += array_combine($hiragana, array_fill(0, count($hiragana), 2.94));
61
//        }
62
//    }
63
64
    /**
65
     * Return character widths for given font name.
66
     *
67
     * @param string $fontName
68
     * @param int    $fontSize
69
     * @return array
70
     */
71 4
    public function get($fontName, $fontSize)
72
    {
73 4
        if (isset(self::$widths[$fontName][$fontSize])) {
74 4
            return self::$widths[$fontName][$fontSize];
75 1
        } elseif (isset(self::$widths['Calibri'][$fontSize])) {
76 1
            return self::$widths['Calibri'][$fontSize];
77
        }
78
79
        return self::$widths['Calibri'][11];
80
    }
81
}
82