Completed
Push — master ( 799388...1db2c8 )
by Remo
05:41 queued 03:48
created

LessRuleList::lessify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.4286
cc 3
eloc 11
nc 3
nop 0
crap 3
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 10
    public function addRule(LessRule $rule)
18
    {
19 10
        $this->list[] = $rule;
20 10
    }
21
22
    /**
23
     * Build and returns a tree for the CSS input
24
     * @return array
25
     */
26 10
    protected function getTree()
27
    {
28 10
        $output = array();
29
30 10
        foreach ($this->list as $ruleSet) {
31 10
            $selectors = $ruleSet->getSelectors();
32
33 10
            foreach ($ruleSet->getTokens() as $token) {
34 10
                $this->parseTreeNode($output, $selectors, $token);
35 10
            }
36 10
        }
37 10
        return $output;
38
    }
39
40
    /**
41
     * Add support for direct descendants operator by aligning the spaces properly.
42
     * the code below supports "html >p" since we split by spaces. A selector "html > p" would cause an
43
     * additional tree level, we therefore normalize them with the two lines below.
44
     *
45
     * @param string $selector
46
     * @return string
47
     */
48 10
    protected function parseDirectDescendants($selector)
49
    {
50 10
        $selector = str_replace('> ', '>', $selector);
51 10
        $selector = str_replace('>', ' >', $selector);
52 10
        return $selector;
53
    }
54
55
    /**
56
     * Support for pseudo classes by adding a space before ":" and also "&" to let less know that there
57
     * shouldn't be a space when concatenating the nested selectors to a single css rule. We have to
58
     * ignore every colon if it's wrapped by :not(...) as we don't nest this in LESS.
59
     *
60
     * @param string $selector
61
     * @return string
62
     */
63 10
    protected function parsePseudoClasses($selector)
64
    {
65 10
        $nestedPseudo = false;
66 10
        $lastCharacterColon = false;
67 10
        $selectorOut = '';
68 10
        for ($i = 0; $i < strlen($selector); $i++) {
69 10
            $c = $selector{$i};
70
71
            // Don't parse anything between (..) and [..]
72 10
            $nestedPseudo = ($c === '(' || $c === '[') || $nestedPseudo;
73 10
            $nestedPseudo = !($c === ')' || $c === ']') && $nestedPseudo;
74
75 10
            if ($nestedPseudo === false && $c === ':' && $lastCharacterColon === false) {
76 3
                $selectorOut .= ' &';
77 3
                $lastCharacterColon = true;
78 3
            }
79
            else {
80 10
                $lastCharacterColon = false;
81
            }
82
83 10
            $selectorOut .= $c;
84 10
        }
85 10
        return $selectorOut;
86
    }
87
88
    /**
89
     * Parse CSS input part into a LESS node
90
     * @param $output
91
     * @param $selectors
92
     * @param $token
93
     */
94 10
    protected function parseTreeNode(&$output, $selectors, $token)
95
    {
96 10
        foreach ($token->MediaTypes as $mediaType) {
97
            // make sure we're aware of our media type
98 10
            if (!array_key_exists($mediaType, $output)) {
99 10
                $output[$mediaType] = array();
100 10
            }
101
102 10
            foreach ($selectors as $selector) {
103
                // add declaration token to output for each selector
104 10
                $currentNode = &$output[$mediaType];
105
106 10
                $selector = $this->parseDirectDescendants($selector);
107 10
                $selector = $this->parsePseudoClasses($selector);
108
109
                // selectors like "html body" must be split into an array so we can
110
                // easily nest them
111 10
                $selectorPath = preg_split('[ ]', $selector, -1, PREG_SPLIT_NO_EMPTY);
112 10
                foreach ($selectorPath as $selectorPathItem) {
113 10
                    if (!array_key_exists($selectorPathItem, $currentNode)) {
114 10
                        $currentNode[$selectorPathItem] = array();
115 10
                    }
116 10
                    $currentNode = &$currentNode[$selectorPathItem];
117 10
                }
118
119 10
                $currentNode['@rules'][] = $this->formatTokenAsLess($token);
120 10
            }
121 10
        }
122 10
    }
123
124
    /**
125
     * Format LESS nodes in a nicer way with indentation and proper brackets
126
     * @param $token
127
     * @param int $level
128
     * @return string
129
     */
130 10
    public function formatTokenAsLess(\aCssToken $token, $level = 0)
131
    {
132 10
        $indentation = str_repeat("\t", $level);
133
134 10
        if ($token instanceof \CssRulesetDeclarationToken) {
135 10
            return $indentation . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
136 1
        } elseif ($token instanceof \CssAtKeyframesStartToken) {
137 1
            return $indentation . "@" . $token->AtRuleName . " \"" . $token->Name . "\" {";
138 1
        } elseif ($token instanceof \CssAtKeyframesRulesetStartToken) {
139 1
            return $indentation . "\t" . implode(",", $token->Selectors) . " {";
140 1
        } elseif ($token instanceof \CssAtKeyframesRulesetEndToken) {
141 1
            return $indentation . "\t" . "}";
142 1 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...
143 1
            return $indentation . "\t\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
144 1
        } elseif ($token instanceof \CssAtCharsetToken) {
145 1
            return $indentation . "@charset " . $token->Charset . ";";
146 1
        } elseif ($token instanceof \CssAtFontFaceStartToken) {
147 1
            return "@font-face {";
148 1 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...
149 1
            return $indentation . "\t" . $token->Property . ": " . $token->Value . ($token->IsImportant ? " !important" : "") . ($token->IsLast ? "" : ";");
150
        } else {
151 1
            return $indentation . $token;
152
        }
153
    }
154
155 10
    protected function formatAsLess($selector, $level = 0)
156
    {
157 10
        $return = '';
158 10
        $indentation = str_repeat("\t", $level);
159 10
        foreach ($selector as $nodeKey => $node) {
160 10
            $return .= $indentation . "{$nodeKey} {\n";
161
162 10
            foreach ($node as $subNodeKey => $subNodes) {
163 10
                if ($subNodeKey === '@rules') {
164 10
                    foreach ($subNodes as $subNode) {
165 10
                        $return .= $indentation . "\t" . $subNode . "\n";
166 10
                    }
167 10
                } else {
168 8
                    $return .= $this->formatAsLess(array($subNodeKey => $subNodes), $level + 1);
169
                }
170 10
            }
171
172 10
            $return .= $indentation . "}\n";
173
174 10
        }
175 10
        return $return;
176
    }
177
178 10
    public function lessify()
179
    {
180 10
        $tree = $this->getTree();
181 10
        $return = '';
182
183 10
        foreach ($tree as $mediaType => $node) {
184 10
            if ($mediaType == 'all') {
185 10
                $return .= $this->formatAsLess($node);
186 10
            } else {
187 1
                $return .= "@media {$mediaType} {\n";
188 1
                $return .= $this->formatAsLess($node, 1);
189 1
                $return .= "}\n";
190
            }
191 10
        }
192
193 10
        return $return;
194
    }
195
}
196