Completed
Push — develop ( 7a734f...e9a151 )
by Stefan
03:10
created

Styler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 81
ccs 28
cts 30
cp 0.9333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addStyle() 0 8 2
A registerStyle() 0 6 1
A getStyleById() 0 4 1
A getStyleSheetXml() 0 15 2
1
<?php
2
3
namespace OneSheet\Style;
4
5
use OneSheet\Xml\StyleXml;
6
7
/**
8
 * Class Styler
9
 *
10
 * @package OneSheet
11
 */
12
class Styler
13
{
14
    /**
15
     * @var Style[]
16
     */
17
    private $styles = array();
18
19
    /**
20
     * @var array
21
     */
22
    private $hashes = array();
23
24
    /**
25
     * Styler constructor.
26
     */
27 2
    public function __construct()
28
    {
29 2
        $this->addStyle(new Style());
30 2
        $grey = new Style();
31 2
        $this->addStyle($grey->fill()->setPattern('grey125')->style());
32 2
    }
33
34
    /**
35
     * Register a new style if it doesnt exists.
36
     *
37
     * @param Style $style
38
     */
39 2
    public function addStyle(Style $style)
40
    {
41 2
        if (!isset($this->hashes[$style->getHash()])) {
42 2
            $this->registerStyle($style, $style->getHash());
43 2
        } else {
44 1
            $style->setId($this->styles[$this->hashes[$style->getHash()]]->getId());
45
        }
46 2
    }
47
48
    /**
49
     * Register a new style.
50
     *
51
     * @param Style  $style
52
     * @param string $styleHash
53
     */
54 2
    private function registerStyle(Style $style, $styleHash)
55
    {
56 2
        $style->setId(count($this->styles))->getId();
0 ignored issues
show
Unused Code introduced by
The call to the method OneSheet\Style\Style::getId() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
57 2
        $this->styles[$style->getId()] = $style;
58 2
        $this->hashes[$styleHash] = $style->getId();
59 2
    }
60
61
    /**
62
     * Return style by its id.
63
     *
64
     * @param int $id
65
     * @return Style
66
     */
67
    public function getStyleById($id)
68
    {
69
        return $this->styles[$id];
70
    }
71
72
    /**
73
     * Return entire xml string for the style sheet.
74
     *
75
     * @return string
76
     */
77 1
    public function getStyleSheetXml()
78
    {
79 1
        $fontsXml = $fillsXml = $cellXfsXml = '';
80 1
        foreach ($this->styles as $style) {
81 1
            $cellXfsXml .= $style->asXml();
82 1
            $fontsXml .= $style->font()->asXml();
83 1
            $fillsXml .= $style->fill()->asXml();
84 1
        }
85
86 1
        return sprintf(StyleXml::STYLE_SHEET_XML,
87 1
            sprintf(StyleXml::FONTS_XML, count($this->styles), $fontsXml),
88 1
            sprintf(StyleXml::FILLS_XML, count($this->styles), $fillsXml),
89 1
            sprintf(StyleXml::CELL_XFS_XML, count($this->styles), $cellXfsXml)
90 1
        );
91
    }
92
}
93