Completed
Push — master ( d37729...8e0761 )
by Remo
01:58
created

Css2Less::getLess()   C

Complexity

Conditions 9
Paths 14

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 9

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 52
ccs 31
cts 31
cp 1
rs 6.5703
cc 9
eloc 28
nc 14
nop 0
crap 9

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 Ortic\Css2Less;
4
5
use Ortic\Css2Less\tokens\LessRuleList;
6
use Ortic\Css2Less\tokens\LessRule;
7
8
class Css2Less
9
{
10
    /**
11
     * @var string $cssContent
12
     */
13
    protected $cssContent;
14
15
    /**
16
     * @var \CssParser $parser
17
     */
18
    protected $parser;
19
20
    /**
21
     * Create a new parser object, use parameter to specify CSS you
22
     * wish to convert into a LESS file
23
     *
24
     * @param string $cssContent
25
     */
26 9
    public function __construct($cssContent)
27
    {
28 9
        $this->cssContent = $cssContent;
29 9
        $this->parser = new \CssParser($this->cssContent);
30 9
    }
31
32
    /**
33
     * Returns a string containing the LESS content matching the CSS input
34
     * @return string
35
     */
36 9
    public function getLess()
37
    {
38 9
        $lessTree = array();
39
40
        // this variable is true, if we're within a ruleset, e.g. p { .. here .. }
41
        // we have to normalize them
42 9
        $withinRulset = false;
43 9
        $ruleSet = null;
44 9
        $ruleSetList = new LessRuleList();
45
46 9
        $tokens = $this->parser->getTokens();
47
48 9
        foreach ($tokens as $token) {
49
            // we have to skip some tokens, their information is redundant
50 9
            if ($token instanceof \CssAtMediaStartToken ||
51
                $token instanceof \CssAtMediaEndToken
52 9
            ) {
53 1
                continue;
54
            }
55
56
            // we have to build a hierarchy with CssRulesetStartToken, CssRulesetEndToken
57 9
            if ($token instanceof \CssRulesetStartToken) {
58 9
                $withinRulset = true;
59 9
                $ruleSet = new LessRule($token->Selectors);
60 9
            } elseif ($token instanceof \CssRulesetEndToken) {
61 9
                $withinRulset = false;
62 9
                if ($ruleSet) {
63 9
                    $ruleSetList->addRule($ruleSet);
64 9
                }
65 9
                $ruleSet = null;
66 9
            } else {
67
                // as long as we're in a ruleset, we're adding all token to a custom array
68
                // this will be lessified once we've found CssRulesetEndToken and then added
69
                // to the actual $lessTree variable
70 9
                if ($withinRulset) {
71 9
                    $ruleSet->addToken($token);
72 9
                } else {
73 1
                    $lessTree[] = $token;
74
                }
75
            }
76 9
        }
77
78 9
        $return = '';
79 9
        foreach ($lessTree as $node) {
80
            // @TODO this format method shouldn't be in this class..
81 1
            $return .= $ruleSetList->formatTokenAsLess($node) . "\n";
82 9
        }
83
84 9
        $return .= $ruleSetList->lessify();
85
86 9
        return $return;
87
    }
88
89
}
0 ignored issues
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
90