• 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

83.73
/src/Parser/FunctionCallParser.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\BooleanNode;
11
use Bugo\SCSS\Nodes\ColorNode;
12
use Bugo\SCSS\Nodes\FunctionNode;
13
use Bugo\SCSS\Nodes\ListNode;
14
use Bugo\SCSS\Nodes\NamedArgumentNode;
15
use Bugo\SCSS\Nodes\NullNode;
16
use Bugo\SCSS\Nodes\NumberNode;
17
use Bugo\SCSS\Nodes\SpreadArgumentNode;
18
use Bugo\SCSS\Nodes\StringNode;
19
use Bugo\SCSS\Nodes\VariableReferenceNode;
20
use Closure;
21

22
use function implode;
23
use function in_array;
24
use function str_contains;
25
use function strlen;
26
use function strpbrk;
27
use function strpos;
28
use function strtolower;
29
use function substr;
30
use function trim;
31

32
final readonly class FunctionCallParser
33
{
34
    /**
35
     * @param TokenStream $stream
36
     * @param Closure(string): AstNode $parseInlineValue
37
     * @param Closure(): ?AstNode $parseSingleValue
38
     * @param Closure(array<int, TokenType>): ?AstNode $parseValueUntil
39
     * @param Closure(): VariableReferenceNode $parseVariableReference
40
     * @param Closure(): ?AstNode $parseCommaSeparatedValue
41
     */
42
    public function __construct(
43
        private TokenStream $stream,
44
        private Closure $parseInlineValue,
45
        private Closure $parseSingleValue,
46
        private Closure $parseValueUntil,
47
        private Closure $parseVariableReference,
48
        private Closure $parseCommaSeparatedValue
49
    ) {}
816 ✔
50

51
    public function parseIdentifierOrFunction(): AstNode
52
    {
53
        $identifier = StreamUtils::parseQualifiedIdentifier($this->stream);
623 ✔
54

55
        if ($this->stream->is(TokenType::LPAREN)) {
623 ✔
56
            if ($identifier === 'url') {
448 ✔
57
                return $this->parseUrlFunctionFromName();
6 ✔
58
            }
59

60
            if ($identifier === 'var') {
444 ✔
61
                return $this->parseVarFunction();
7 ✔
62
            }
63

64
            return $this->parseFunctionFromName($identifier);
438 ✔
65
        }
66

67
        if (
68
            in_array(strtolower($identifier), ['and', 'or'], true)
353 ✔
69
            && $this->stream->is(TokenType::WHITESPACE)
353 ✔
70
            && $this->stream->peek()->type === TokenType::LPAREN
353 ✔
71
        ) {
72
            $this->stream->skipWhitespace();
1 ✔
73

74
            return $this->parseFunctionFromName($identifier);
1 ✔
75
        }
76

77
        $moduleVariableSeparator = strpos($identifier, '.$');
353 ✔
78

79
        if ($moduleVariableSeparator !== false) {
353 ✔
NEW
80
            $moduleName   = substr($identifier, 0, $moduleVariableSeparator);
×
NEW
81
            $variableName = substr($identifier, $moduleVariableSeparator + 2);
×
82

NEW
83
            if ($moduleName !== '' && $variableName !== '') {
×
NEW
84
                return new VariableReferenceNode($moduleName . '.' . $variableName);
×
85
            }
86
        }
87

88
        $normalizedIdentifier = strtolower($identifier);
353 ✔
89

90
        if ($normalizedIdentifier === 'true') {
353 ✔
91
            return new BooleanNode(true);
13 ✔
92
        }
93

94
        if ($normalizedIdentifier === 'false') {
352 ✔
95
            return new BooleanNode(false);
11 ✔
96
        }
97

98
        if ($normalizedIdentifier === 'null') {
350 ✔
99
            return new NullNode();
5 ✔
100
        }
101

102
        return new StringNode($identifier);
347 ✔
103
    }
104

105
    public function parseVarFunction(): FunctionNode
106
    {
107
        $this->stream->advance();
7 ✔
108

109
        $arguments = [];
7 ✔
110

111
        $this->stream->skipWhitespace();
7 ✔
112

113
        $name = ($this->parseSingleValue)();
7 ✔
114

115
        if ($name !== null) {
7 ✔
116
            $arguments[] = $name;
7 ✔
117
        }
118

119
        $this->stream->skipWhitespace();
7 ✔
120

121
        if ($this->stream->consume(TokenType::COMMA)) {
7 ✔
122
            $fallback = ($this->parseValueUntil)([TokenType::RPAREN]);
3 ✔
123

124
            if ($fallback !== null) {
3 ✔
125
                $arguments[] = $fallback;
3 ✔
126
            }
127
        }
128

129
        $this->stream->consume(TokenType::RPAREN);
7 ✔
130

131
        return new FunctionNode('var', $arguments);
7 ✔
132
    }
133

134
    public function parseUrlFunctionFromName(): FunctionNode
135
    {
136
        $this->stream->advance();
6 ✔
137

138
        $argument = '';
6 ✔
139
        $depth    = 1;
6 ✔
140

141
        while (! $this->stream->isEof()) {
6 ✔
142
            $token = $this->stream->current();
6 ✔
143

144
            if ($token->type === TokenType::LPAREN) {
6 ✔
NEW
145
                $depth++;
×
146

NEW
147
                $argument .= '(';
×
148

NEW
149
                $this->stream->advance();
×
150

NEW
151
                continue;
×
152
            }
153

154
            if ($token->type === TokenType::RPAREN) {
6 ✔
155
                $depth--;
6 ✔
156

157
                if ($depth === 0) {
6 ✔
158
                    $this->stream->advance();
6 ✔
159

160
                    break;
6 ✔
161
                }
162

NEW
163
                $argument .= ')';
×
164

NEW
165
                $this->stream->advance();
×
166

NEW
167
                continue;
×
168
            }
169

170
            if ($token->type === TokenType::WHITESPACE) {
5 ✔
171
                $this->stream->advance();
1 ✔
172

173
                continue;
1 ✔
174
            }
175

176
            if ($token->type === TokenType::HASH && $this->stream->peek()->type === TokenType::LBRACE) {
5 ✔
177
                $argument .= '#{';
1 ✔
178

179
                $this->stream->advance(2);
1 ✔
180

181
                continue;
1 ✔
182
            }
183

184
            if ($token->type === TokenType::HASH) {
5 ✔
NEW
185
                $argument .= '#' . $token->value;
×
186
            } elseif ($token->type === TokenType::STRING) {
5 ✔
187
                $argument .= $this->quoteStringForReparse($token->value);
3 ✔
188
            } else {
189
                $argument .= StreamUtils::tokenToRawString($token->type, $token->value);
3 ✔
190
            }
191

192
            $this->stream->advance();
5 ✔
193
        }
194

195
        $argument = trim($argument);
6 ✔
196

197
        if ($argument === '') {
6 ✔
198
            return new FunctionNode('url', []);
1 ✔
199
        }
200

201
        if ($this->isValidUnquotedUrl($argument)) {
5 ✔
202
            return new FunctionNode('url', [new StringNode($argument)]);
3 ✔
203
        }
204

205
        return new FunctionNode('url', [($this->parseInlineValue)($argument)]);
3 ✔
206
    }
207

208
    public function parseFunctionFromName(string $name): FunctionNode
209
    {
210
        $line = $this->stream->current()->line;
438 ✔
211

212
        $this->stream->advance();
438 ✔
213

214
        if ($name === 'if') {
438 ✔
215
            $inlineIfArguments = $this->tryParseInlineIfExpressionArguments();
11 ✔
216

217
            if ($inlineIfArguments !== null) {
11 ✔
218
                return new FunctionNode($name, $inlineIfArguments, $line, modernSyntax: true);
9 ✔
219
            }
220
        }
221

222
        $arguments = [];
438 ✔
223
        $loopCount = 0;
438 ✔
224

225
        while (! $this->stream->isEof()) {
438 ✔
226
            $loopCount++;
438 ✔
227

228
            if ($loopCount > 100) {
438 ✔
NEW
229
                break;
×
230
            }
231

232
            $this->stream->skipWhitespace();
438 ✔
233

234
            if ($this->stream->consume(TokenType::RPAREN)) {
438 ✔
235
                break;
438 ✔
236
            }
237

238
            if ($this->stream->is(TokenType::COMMA)) {
425 ✔
239
                $this->stream->advance();
227 ✔
240
            }
241

242
            $this->stream->skipWhitespace();
425 ✔
243

244
            $savedPos     = $this->stream->getPosition();
425 ✔
245
            $potentialArg = ($this->parseSingleValue)();
425 ✔
246

247
            if ($potentialArg !== null) {
425 ✔
248
                $this->stream->skipWhitespace();
425 ✔
249

250
                // Legacy = operator for IE compatibility (creates unquoted string)
251
                if ($this->stream->is(TokenType::ASSIGN)) {
425 ✔
252
                    $this->stream->advance();
2 ✔
253
                    $this->stream->skipWhitespace();
2 ✔
254

255
                    $rightSide = ($this->parseSingleValue)();
2 ✔
256

257
                    if ($rightSide !== null) {
2 ✔
258
                        // Convert both sides to strings and combine with =
259
                        $leftStr  = $this->nodeToString($potentialArg);
2 ✔
260
                        $rightStr = $this->nodeToString($rightSide);
2 ✔
261

262
                        $arguments[] = new StringNode($leftStr . '=' . $rightStr, false);
2 ✔
263

264
                        continue;
2 ✔
265
                    }
266
                }
267

268
                if ($this->stream->is(TokenType::COLON)) {
424 ✔
269
                    $this->stream->setPosition($savedPos);
39 ✔
270

271
                    if ($this->stream->is(TokenType::DOLLAR)) {
39 ✔
272
                        $varRef = ($this->parseVariableReference)();
39 ✔
273

274
                        $this->stream->skipWhitespace();
39 ✔
275

276
                        if ($this->stream->consume(TokenType::COLON)) {
39 ✔
277
                            $this->stream->skipWhitespace();
39 ✔
278

279
                            $value = ($this->parseCommaSeparatedValue)();
39 ✔
280

281
                            if ($value !== null) {
39 ✔
282
                                $arguments[] = new NamedArgumentNode($varRef->name, $value);
39 ✔
283
                            }
284
                        }
285
                    } else {
NEW
286
                        $arg = $this->parseFunctionArgument();
×
287

NEW
288
                        if ($arg !== null) {
×
NEW
289
                            $arguments[] = $arg;
×
290
                        }
291
                    }
292
                } else {
293
                    $this->stream->setPosition($savedPos);
424 ✔
294

295
                    $arg = $this->parseFunctionArgument();
424 ✔
296

297
                    if ($arg !== null) {
424 ✔
298
                        $arguments[] = $arg;
424 ✔
299
                    }
300
                }
301
            } else {
302
                $this->stream->setPosition($savedPos);
1 ✔
303

304
                $arg = $this->parseFunctionArgument();
1 ✔
305

306
                if ($arg !== null) {
1 ✔
307
                    $arguments[] = $arg;
1 ✔
308

309
                    continue;
1 ✔
310
                }
311

NEW
312
                break;
×
313
            }
314
        }
315

316
        return new FunctionNode($name, $arguments, $line);
438 ✔
317
    }
318

319
    private function parseFunctionArgument(): ?AstNode
320
    {
321
        $argument = ($this->parseCommaSeparatedValue)();
424 ✔
322

323
        if ($argument !== null && StreamUtils::consumeEllipsis($this->stream)) {
424 ✔
324
            return new SpreadArgumentNode($argument);
6 ✔
325
        }
326

327
        return $argument;
419 ✔
328
    }
329

330
    private function quoteStringForReparse(string $value): string
331
    {
332
        $quote = str_contains($value, '"') && ! str_contains($value, "'")
3 ✔
333
            ? "'"
1 ✔
334
            : '"';
3 ✔
335

336
        $escaped = '';
3 ✔
337
        $length  = strlen($value);
3 ✔
338

339
        for ($index = 0; $index < $length; $index++) {
3 ✔
340
            $char = $value[$index];
3 ✔
341

342
            if ($char === '\\' || $char === $quote) {
3 ✔
NEW
343
                $escaped .= '\\';
×
344
            }
345

346
            $escaped .= $char;
3 ✔
347
        }
348

349
        return $quote . $escaped . $quote;
3 ✔
350
    }
351

352
    /**
353
     * @return array<int, AstNode>|null
354
     */
355
    private function tryParseInlineIfExpressionArguments(): ?array
356
    {
357
        $savedPosition = $this->stream->getPosition();
11 ✔
358

359
        $arguments = [];
11 ✔
360
        $condition = ($this->parseValueUntil)([TokenType::COLON, TokenType::COMMA, TokenType::RPAREN]);
11 ✔
361

362
        if ($condition === null) {
11 ✔
NEW
363
            $this->stream->setPosition($savedPosition);
×
364

NEW
365
            return null;
×
366
        }
367

368
        if (! $this->stream->is(TokenType::COLON)) {
11 ✔
369
            $this->stream->setPosition($savedPosition);
2 ✔
370

371
            return null;
2 ✔
372
        }
373

374
        $arguments[] = $condition;
9 ✔
375

376
        $this->stream->advance();
9 ✔
377
        $this->stream->skipWhitespace();
9 ✔
378

379
        $truthy = ($this->parseValueUntil)([TokenType::SEMICOLON, TokenType::RPAREN, TokenType::COMMA]);
9 ✔
380

381
        if ($truthy !== null) {
9 ✔
382
            $arguments[] = $truthy;
9 ✔
383
        }
384

385
        $this->stream->skipWhitespace();
9 ✔
386

387
        if ($this->stream->consume(TokenType::SEMICOLON)) {
9 ✔
388
            $this->stream->skipWhitespace();
6 ✔
389

390
            if (
391
                $this->stream->is(TokenType::IDENTIFIER)
6 ✔
392
                && strtolower($this->stream->current()->value) === 'else'
6 ✔
393
            ) {
394
                $this->stream->advance();
6 ✔
395
                $this->stream->skipWhitespace();
6 ✔
396
                $this->stream->consume(TokenType::COLON);
6 ✔
397
                $this->stream->skipWhitespace();
6 ✔
398
            }
399

400
            $falsy = ($this->parseValueUntil)([TokenType::RPAREN]);
6 ✔
401

402
            if ($falsy !== null) {
6 ✔
403
                $arguments[] = $falsy;
6 ✔
404
            }
405
        }
406

407
        $this->stream->skipWhitespace();
9 ✔
408
        $this->stream->consume(TokenType::RPAREN);
9 ✔
409

410
        return $arguments;
9 ✔
411
    }
412

413
    private function isValidUnquotedUrl(string $argument): bool
414
    {
415
        if ($argument === '') {
5 ✔
NEW
416
            return false;
×
417
        }
418

419
        if (str_contains($argument, '$') || str_contains($argument, '+')) {
5 ✔
420
            return false;
3 ✔
421
        }
422

423
        $withoutInterpolation = '';
5 ✔
424

425
        $length = strlen($argument);
5 ✔
426
        $index  = 0;
5 ✔
427

428
        while ($index < $length) {
5 ✔
429
            if ($argument[$index] === '#' && $index + 1 < $length && $argument[$index + 1] === '{') {
5 ✔
NEW
430
                $closingBrace = strpos($argument, '}', $index + 2);
×
431

NEW
432
                if ($closingBrace !== false) {
×
NEW
433
                    $index = $closingBrace + 1;
×
434

NEW
435
                    continue;
×
436
                }
437
            }
438

439
            $withoutInterpolation .= $argument[$index];
5 ✔
440

441
            $index++;
5 ✔
442
        }
443

444
        if ($withoutInterpolation === '') {
5 ✔
NEW
445
            return false;
×
446
        }
447

448
        return strpbrk($withoutInterpolation, " \t\n\r\0\x0B\"'()") === false;
5 ✔
449
    }
450

451
    private function nodeToString(AstNode $node): string
452
    {
453
        if ($node instanceof StringNode) {
2 ✔
454
            return $node->value;
2 ✔
455
        }
456

457
        if ($node instanceof NumberNode) {
2 ✔
458
            return "$node->value$node->unit";
1 ✔
459
        }
460

461
        if ($node instanceof ColorNode) {
1 ✔
462
            return $node->value;
1 ✔
463
        }
464

NEW
465
        if ($node instanceof VariableReferenceNode) {
×
NEW
466
            return '$' . $node->name;
×
467
        }
468

NEW
469
        if ($node instanceof ListNode) {
×
NEW
470
            $parts = [];
×
NEW
471
            foreach ($node->items as $item) {
×
NEW
472
                $parts[] = $this->nodeToString($item);
×
473
            }
474

NEW
475
            return implode($node->separator === ',' ? ', ' : ' ', $parts);
×
476
        }
477

NEW
478
        return '';
×
479
    }
480
}
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