Completed
Push — master ( e3c06e...d37729 )
by Remo
03:26 queued 01:35
created

LessRuleList::parseTreeNode()   C

Complexity

Conditions 14
Paths 57

Size

Total Lines 59
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 14

Importance

Changes 7
Bugs 4 Features 2
Metric Value
c 7
b 4
f 2
dl 0
loc 59
ccs 30
cts 30
cp 1
rs 6.4055
cc 14
eloc 30
nc 57
nop 3
crap 14

How to fix   Long Method    Complexity   

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\tokens;
4
5
/**
6
 * Class LessRuleList
7
 * @package Ortic\Css2Less\tokens
8
 */
9
class LessRuleList
10
{
11
    private $list = array();
12
13
    /**
14
     * Add a new rule object to our list
15
     * @param LessRule $rule
16
     */
17 9
    public function addRule(LessRule $rule)
18
    {
19 9
        $this->list[] = $rule;
20 9
    }
21
22
    /**
23
     * Build and returns a tree for the CSS input
24
     * @return array
25
     */
26 9
    protected function getTree()
27
    {
28 9
        $output = array();
29
30 9
        foreach ($this->list as $ruleSet) {
31 9
            $selectors = $ruleSet->getSelectors();
32
33 9
            foreach ($ruleSet->getTokens() as $token) {
34 9
                $this->parseTreeNode($output, $selectors, $token);
35
            }
36
        }
37 9
        return $output;
38
    }
39
40
    /**
41
     * Parse CSS input part into a LESS node
42
     * @param $output
43
     * @param $selectors
44
     * @param $token
45
     */
46 9
    protected function parseTreeNode(&$output, $selectors, $token)
47
    {
48 9
        foreach ($token->MediaTypes as $mediaType) {
49
            // make sure we're aware of our media type
50 9
            if (!array_key_exists($mediaType, $output)) {
51 9
                $output[$mediaType] = array();
52
            }
53
54 9
            foreach ($selectors as $selector) {
55
                // add declaration token to output for each selector
56 9
                $currentNode = &$output[$mediaType];
57
58
                // add support for direct descendants operator by aligning the spaces properly.
59
                // the code below supports "html >p" since we split by spaces. A selector "html > p" would cause an
60
                // additional tree level, we therefore normalize them with the two lines below.
61 9
                $selector = str_replace('> ', '>', $selector);
62 9
                $selector = str_replace('>', ' >', $selector);
63
64
                // support for pseudo classes by adding a space before ":" and also "&" to let less know that there
65
                // shouldn't be a space when concatenating the nested selectors to a single css rule. We have to
66
                // ignore every colon if it's wrapped by :not(...) as we don't nest this in LESS.
67 9
                $nestedPseudo = false;
68 9
                $lastCharacterColon = false;
69 9
                $selectorOut = '';
70 9
                for ($i = 0; $i < strlen($selector); $i++) {
71 9
                    $c = $selector{$i};
72 9
                    if ($c === '(' || $c === '[') {
73 1
                        $nestedPseudo = true;
74
                    }
75 9
                    if ($c === ')' || $c === ']') {
76 1
                        $nestedPseudo = false;
77
                    }
78
79 9
                    if ($nestedPseudo === false && $c === ':' && $lastCharacterColon === false) {
80 3
                        $selectorOut .= ' &';
81 3
                        $lastCharacterColon = true;
82
                    }
83
                    else {
84 9
                        $lastCharacterColon = false;
85
                    }
86
87 9
                    $selectorOut .= $c;
88
                }
89 9
                $selector = $selectorOut;
90
91
                // selectors like "html body" must be split into an array so we can
92
                // easily nest them
93 9
                $selectorPath = preg_split('[ ]', $selector, -1, PREG_SPLIT_NO_EMPTY);
94 9
                foreach ($selectorPath as $selectorPathItem) {
95 9
                    if (!array_key_exists($selectorPathItem, $currentNode)) {
96 9
                        $currentNode[$selectorPathItem] = array();
97
                    }
98 9
                    $currentNode = &$currentNode[$selectorPathItem];
99
                }
100
101 9
                $currentNode['@rules'][] = $this->formatTokenAsLess($token);
102
            }
103
        }
104 9
    }
105
106
    /**
107
     * Format LESS nodes in a nicer way with indentation and proper brackets
108
     * @param $token
109
     * @param int $level
110
     * @return string
111
     */
112 9
    public function formatTokenAsLess(\aCssToken $token, $level = 0)
113
    {
114 9
        $indentation = str_repeat("\t", $level);
115
116 9
        if ($token instanceof \CssRulesetDeclarationToken) {
117 9
            return $indentation . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
118
        } elseif ($token instanceof \CssAtKeyframesStartToken) {
119 1
            return $indentation . "@" . $token->AtRuleName . " \"" . $token->Name . "\" {";
120
        } elseif ($token instanceof \CssAtKeyframesRulesetStartToken) {
121 1
            return $indentation . "\t" . implode(",", $token->Selectors) . " {";
122
        } elseif ($token instanceof \CssAtKeyframesRulesetEndToken) {
123 1
            return $indentation . "\t" . "}";
124 View Code Duplication
        } elseif ($token instanceof \CssAtKeyframesRulesetDeclarationToken) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125 1
            return $indentation . "\t\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
126
        } elseif ($token instanceof \CssAtCharsetToken) {
127 1
            return $indentation . "@charset " . $token->Charset . ";";
128
        } elseif ($token instanceof \CssAtFontFaceStartToken) {
129 1
            return "@font-face {";
130 View Code Duplication
        } elseif ($token instanceof \CssAtFontFaceDeclarationToken) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131 1
            return $indentation . "\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
132
        } else {
133 1
            return $indentation . $token;
134
        }
135
    }
136
137 9
    protected function formatAsLess($selector, $level = 0)
138
    {
139 9
        $return = '';
140 9
        $indentation = str_repeat("\t", $level);
141 9
        foreach ($selector as $nodeKey => $node) {
142 9
            $return .= $indentation . "{$nodeKey} {\n";
143
144 9
            foreach ($node as $subNodeKey => $subNodes) {
145 9
                if ($subNodeKey === '@rules') {
146 9
                    foreach ($subNodes as $subNode) {
147 9
                        $return .= $indentation . "\t" . $subNode . "\n";
148
                    }
149
                } else {
150 9
                    $return .= $this->formatAsLess(array($subNodeKey => $subNodes), $level + 1);
151
                }
152
            }
153
154 9
            $return .= $indentation . "}\n";
155
156
        }
157 9
        return $return;
158
    }
159
160 9
    public function lessify()
161
    {
162 9
        $tree = $this->getTree();
163 9
        $return = '';
164
165 9
        foreach ($tree as $mediaType => $node) {
166 9
            if ($mediaType == 'all') {
167 9
                $return .= $this->formatAsLess($node);
168
            } else {
169 1
                $return .= "@media {$mediaType} {\n";
170 1
                $return .= $this->formatAsLess($node, 1);
171 9
                $return .= "}\n";
172
            }
173
        }
174
175 9
        return $return;
176
    }
177
}
178