• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dragomano / scss-php / 23594295754

26 Mar 2026 12:19PM UTC coverage: 84.536%. First build
23594295754

push

github

web-flow
Merge pull request #51 from dragomano/refactor

Rewrite compiler

10140 of 12016 new or added lines in 162 files covered. (84.39%)

10272 of 12151 relevant lines covered (84.54%)

83.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/Parser/ModuleDirectiveParser.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Bugo\SCSS\Parser;
6

7
use Bugo\SCSS\Lexer\TokenStream;
8
use Bugo\SCSS\Lexer\TokenType;
9
use Bugo\SCSS\Nodes\AstNode;
10
use Bugo\SCSS\Nodes\ForwardNode;
11
use Bugo\SCSS\Nodes\ImportNode;
12
use Bugo\SCSS\Nodes\ListNode;
13
use Bugo\SCSS\Nodes\UseNode;
14
use Closure;
15

16
use function array_filter;
17
use function max;
18
use function strtolower;
19
use function trim;
20

21
final readonly class ModuleDirectiveParser
22
{
23
    /**
24
     * @param Closure(): string $parseString
25
     * @param Closure(): string $consumeIdentifier
26
     * @param Closure(array<int, TokenType>): ?AstNode $parseValueUntil
27
     * @param Closure(): array{global: bool, default: bool, important: bool} $parseValueModifiers
28
     */
29
    public function __construct(
30
        private TokenStream $stream,
31
        private Closure $parseString,
32
        private Closure $consumeIdentifier,
33
        private Closure $parseValueUntil,
34
        private Closure $parseValueModifiers
35
    ) {}
813 ✔
36

37
    public function parseUseDirective(): UseNode
38
    {
39
        $this->stream->skipWhitespace();
242 ✔
40

41
        $path = ($this->parseString)();
242 ✔
42

43
        $namespace     = null;
242 ✔
44
        $configuration = [];
242 ✔
45

46
        $this->stream->skipWhitespace();
242 ✔
47

48
        if (StreamUtils::consumeKeyword($this->stream, 'as')) {
242 ✔
49
            if ($this->stream->consume(TokenType::STAR)) {
16 ✔
50
                $namespace = '*';
4 ✔
51
            } else {
52
                $namespace = ($this->consumeIdentifier)();
12 ✔
53
            }
54
        }
55

56
        $this->stream->skipWhitespace();
242 ✔
57

58
        if (StreamUtils::consumeKeyword($this->stream, 'with')) {
242 ✔
59
            $configuration = $this->parseUseConfiguration();
5 ✔
60
        }
61

62
        StreamUtils::consumeSemicolonFromStream($this->stream);
242 ✔
63

64
        return new UseNode($path, $namespace, $configuration);
242 ✔
65
    }
66

67
    public function parseImportDirective(): ImportNode
68
    {
69
        $this->stream->skipWhitespace();
11 ✔
70

71
        $imports = [];
11 ✔
72

73
        while (! $this->stream->isEof() && ! $this->stream->is(TokenType::SEMICOLON)) {
11 ✔
74
            $entry = $this->parseImportEntry();
11 ✔
75

76
            if ($entry !== '') {
11 ✔
77
                $imports[] = $entry;
11 ✔
78
            }
79

80
            if (! StreamUtils::consumeCommaSeparator($this->stream)) {
11 ✔
81
                break;
11 ✔
82
            }
83
        }
84

85
        StreamUtils::consumeSemicolonFromStream($this->stream);
11 ✔
86

87
        return new ImportNode($imports);
11 ✔
88
    }
89

90
    public function parseForwardDirective(): ForwardNode
91
    {
92
        $this->stream->skipWhitespace();
15 ✔
93

94
        $path = ($this->parseString)();
15 ✔
95

96
        $prefix        = null;
15 ✔
97
        $visibility    = null;
15 ✔
98
        $members       = [];
15 ✔
99
        $configuration = [];
15 ✔
100

101
        while (! $this->stream->isEof() && ! $this->stream->is(TokenType::SEMICOLON)) {
15 ✔
102
            $this->stream->skipWhitespace();
12 ✔
103

104
            if (! $this->stream->is(TokenType::IDENTIFIER)) {
12 ✔
NEW
105
                break;
×
106
            }
107

108
            $keyword = strtolower($this->stream->current()->value);
12 ✔
109

110
            if ($keyword === 'as') {
12 ✔
111
                StreamUtils::consumeKeyword($this->stream, $keyword, true);
4 ✔
112

113
                $prefix = ($this->consumeIdentifier)();
4 ✔
114

115
                $this->stream->skipWhitespace();
4 ✔
116
                $this->stream->consume(TokenType::STAR);
4 ✔
117

118
                continue;
4 ✔
119
            }
120

121
            if ($keyword === 'hide' || $keyword === 'show') {
8 ✔
122
                $visibility = $keyword;
6 ✔
123

124
                StreamUtils::consumeKeyword($this->stream, $keyword, true);
6 ✔
125

126
                while (! $this->stream->isEof() && ! $this->stream->is(TokenType::SEMICOLON)) {
6 ✔
127
                    $isVariable = $this->stream->consume(TokenType::DOLLAR) !== null;
6 ✔
128
                    $name       = ($this->consumeIdentifier)();
6 ✔
129

130
                    if ($name !== '') {
6 ✔
131
                        $members[] = $isVariable ? '$' . $name : $name;
6 ✔
132
                    }
133

134
                    if (! StreamUtils::consumeCommaSeparator($this->stream)) {
6 ✔
135
                        break;
6 ✔
136
                    }
137
                }
138

139
                continue;
6 ✔
140
            }
141

142
            if ($keyword === 'with') {
2 ✔
143
                StreamUtils::consumeKeyword($this->stream, $keyword, true);
2 ✔
144

145
                $configuration = $this->parseForwardConfiguration();
2 ✔
146

147
                continue;
2 ✔
148
            }
149

NEW
150
            break;
×
151
        }
152

153
        StreamUtils::consumeSemicolonFromStream($this->stream);
15 ✔
154

155
        return new ForwardNode($path, $prefix, $visibility, $members, $configuration);
15 ✔
156
    }
157

158
    /**
159
     * @return array<string, AstNode>
160
     */
161
    private function parseUseConfiguration(): array
162
    {
163
        $rawConfiguration = $this->parseModuleConfiguration(false);
5 ✔
164

165
        return array_filter(
5 ✔
166
            $rawConfiguration,
5 ✔
167
            fn(AstNode|array $value): bool => $value instanceof AstNode
5 ✔
168
        );
5 ✔
169
    }
170

171
    private function parseImportEntry(): string
172
    {
173
        $entry        = '';
11 ✔
174
        $parenDepth   = 0;
11 ✔
175
        $bracketDepth = 0;
11 ✔
176

177
        while (! $this->stream->isEof()) {
11 ✔
178
            $token = $this->stream->current();
11 ✔
179

180
            if (
181
                $parenDepth === 0
11 ✔
182
                && $bracketDepth === 0
11 ✔
183
                && ($token->type === TokenType::COMMA || $token->type === TokenType::SEMICOLON)
11 ✔
184
            ) {
185
                break;
11 ✔
186
            }
187

188
            if ($token->type === TokenType::LPAREN) {
11 ✔
189
                $parenDepth++;
3 ✔
190
            } elseif ($token->type === TokenType::RPAREN) {
11 ✔
191
                $parenDepth = max(0, $parenDepth - 1);
3 ✔
192
            } elseif ($token->type === TokenType::LBRACKET) {
11 ✔
NEW
193
                $bracketDepth++;
×
194
            } elseif ($token->type === TokenType::RBRACKET) {
11 ✔
NEW
195
                $bracketDepth = max(0, $bracketDepth - 1);
×
196
            }
197

198
            if ($token->type === TokenType::WHITESPACE) {
11 ✔
199
                $entry .= ' ';
2 ✔
200
            } elseif ($token->type === TokenType::STRING) {
11 ✔
201
                $entry .= '"' . $token->value . '"';
11 ✔
202
            } else {
203
                $entry .= StreamUtils::tokenToRawString($token->type, $token->value);
3 ✔
204
            }
205

206
            $this->stream->advance();
11 ✔
207
        }
208

209
        return trim($entry);
11 ✔
210
    }
211

212
    /**
213
     * @return array<string, array{value: AstNode, default: bool}>
214
     */
215
    private function parseForwardConfiguration(): array
216
    {
217
        $rawConfiguration = $this->parseModuleConfiguration(true);
2 ✔
218

219
        $configuration = [];
2 ✔
220

221
        foreach ($rawConfiguration as $name => $entry) {
2 ✔
222
            if ($entry instanceof AstNode) {
2 ✔
NEW
223
                continue;
×
224
            }
225

226
            $configuration[$name] = [
2 ✔
227
                'value'   => $entry['value'],
2 ✔
228
                'default' => $entry['default'],
2 ✔
229
            ];
2 ✔
230
        }
231

232
        return $configuration;
2 ✔
233
    }
234

235
    /**
236
     * @return array<string, AstNode|array{value: AstNode, default: bool}>
237
     */
238
    private function parseModuleConfiguration(bool $withDefaultModifier): array
239
    {
240
        $configuration = [];
6 ✔
241

242
        if (! $this->stream->consume(TokenType::LPAREN)) {
6 ✔
NEW
243
            return $configuration;
×
244
        }
245

246
        while (! $this->stream->isEof()) {
6 ✔
247
            $this->stream->skipWhitespace();
6 ✔
248

249
            if ($this->stream->consume(TokenType::RPAREN)) {
6 ✔
NEW
250
                break;
×
251
            }
252

253
            if (! $this->stream->consume(TokenType::DOLLAR)) {
6 ✔
NEW
254
                break;
×
255
            }
256

257
            $name = ($this->consumeIdentifier)();
6 ✔
258

259
            $this->stream->skipWhitespace();
6 ✔
260

261
            if (! $this->stream->consume(TokenType::COLON)) {
6 ✔
NEW
262
                break;
×
263
            }
264

265
            $value = ($this->parseValueUntil)([TokenType::COMMA, TokenType::RPAREN]) ?? new ListNode([], 'comma');
6 ✔
266

267
            if ($withDefaultModifier) {
6 ✔
268
                $modifiers = ($this->parseValueModifiers)();
2 ✔
269

270
                $configuration[$name] = [
2 ✔
271
                    'value'   => $value,
2 ✔
272
                    'default' => $modifiers['default'],
2 ✔
273
                ];
2 ✔
274
            } else {
275
                $configuration[$name] = $value;
5 ✔
276
            }
277

278
            $this->stream->skipWhitespace();
6 ✔
279

280
            if ($this->stream->consume(TokenType::COMMA)) {
6 ✔
281
                continue;
5 ✔
282
            }
283

284
            if ($this->stream->consume(TokenType::RPAREN)) {
6 ✔
285
                break;
6 ✔
286
            }
287
        }
288

289
        return $configuration;
6 ✔
290
    }
291
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc