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

dragomano / scss-php / 23953478554

03 Apr 2026 04:26PM UTC coverage: 99.596% (+7.0%) from 92.642%
23953478554

push

github

dragomano
Adopt @PER-CS ruleset and reformat codebase

349 of 349 new or added lines in 51 files covered. (100.0%)

31 existing lines in 13 files now uncovered.

12325 of 12375 relevant lines covered (99.6%)

95.88 hits per line

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

99.05
/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
    ) {}
1,072 ✔
50

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

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

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

65
            return $this->parseFunctionFromName($identifier);
446 ✔
66
        }
67

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

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

78
        $moduleVariableSeparator = strpos($identifier, '.$');
393 ✔
79

80
        if ($moduleVariableSeparator !== false) {
393 ✔
81
            $moduleName   = substr($identifier, 0, $moduleVariableSeparator);
1 ✔
82
            $variableName = substr($identifier, $moduleVariableSeparator + 2);
1 ✔
83

84
            if ($moduleName !== '' && $variableName !== '') {
1 ✔
85
                return new VariableReferenceNode($moduleName . '.' . $variableName);
1 ✔
86
            }
87
        }
88

89
        $normalizedIdentifier = strtolower($identifier);
392 ✔
90

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

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

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

103
        return new StringNode($identifier, false, $startToken->line, $startToken->column);
386 ✔
104
    }
105

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

110
        $arguments = [];
7 ✔
111

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

114
        $name = $this->parseSingleValueNode();
7 ✔
115

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

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

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

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

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

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

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

139
        $argument = '';
7 ✔
140
        $depth    = 1;
7 ✔
141

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

145
            if ($token->type === TokenType::LPAREN) {
7 ✔
146
                $depth++;
1 ✔
147

148
                $argument .= '(';
1 ✔
149

150
                $this->stream->advance();
1 ✔
151

152
                continue;
1 ✔
153
            }
154

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

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

161
                    break;
7 ✔
162
                }
163

164
                $argument .= ')';
1 ✔
165

166
                $this->stream->advance();
1 ✔
167

168
                continue;
1 ✔
169
            }
170

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

174
                continue;
1 ✔
175
            }
176

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

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

182
                continue;
1 ✔
183
            }
184

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

193
            $this->stream->advance();
6 ✔
194
        }
195

196
        $argument = trim($argument);
7 ✔
197

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

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

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

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

213
        $this->stream->advance();
447 ✔
214

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

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

223
        $arguments = [];
447 ✔
224
        $loopCount = 0;
447 ✔
225

226
        while (! $this->stream->isEof()) {
447 ✔
227
            $loopCount++;
446 ✔
228

229
            if ($loopCount > 100) {
446 ✔
UNCOV
230
                break;
×
231
            }
232

233
            $this->stream->skipWhitespace();
446 ✔
234

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

239
            if ($this->stream->is(TokenType::COMMA)) {
433 ✔
240
                $this->stream->advance();
229 ✔
241
            }
242

243
            $this->stream->skipWhitespace();
433 ✔
244

245
            $savedPos     = $this->stream->getPosition();
433 ✔
246
            $potentialArg = $this->parseSingleValueNode();
433 ✔
247

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

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

256
                    $rightSide = $this->parseSingleValueNode();
2 ✔
257

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

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

265
                        continue;
2 ✔
266
                    }
267
                }
268

269
                if ($this->stream->is(TokenType::COLON)) {
432 ✔
270
                    $this->stream->setPosition($savedPos);
40 ✔
271

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

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

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

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

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

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

296
                    $arg = $this->parseFunctionArgument();
431 ✔
297

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

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

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

310
                    continue;
1 ✔
311
                }
312

UNCOV
313
                break;
×
314
            }
315
        }
316

317
        return new FunctionNode($name, $arguments, $line);
447 ✔
318
    }
319

320
    private function parseSingleValueNode(): ?AstNode
321
    {
322
        return ($this->parseSingleValue)();
439 ✔
323
    }
324

325
    private function parseFunctionArgument(): ?AstNode
326
    {
327
        $argument = ($this->parseCommaSeparatedValue)();
432 ✔
328

329
        if ($argument !== null && StreamUtils::consumeEllipsis($this->stream)) {
432 ✔
330
            return new SpreadArgumentNode($argument);
6 ✔
331
        }
332

333
        return $argument;
427 ✔
334
    }
335

336
    private function quoteStringForReparse(string $value): string
337
    {
338
        $quote = str_contains($value, '"') && ! str_contains($value, "'")
4 ✔
339
            ? "'"
1 ✔
340
            : '"';
4 ✔
341

342
        $escaped = '';
4 ✔
343
        $length  = strlen($value);
4 ✔
344

345
        for ($index = 0; $index < $length; $index++) {
4 ✔
346
            $char = $value[$index];
4 ✔
347

348
            if ($char === '\\' || $char === $quote) {
4 ✔
349
                $escaped .= '\\';
1 ✔
350
            }
351

352
            $escaped .= $char;
4 ✔
353
        }
354

355
        return $quote . $escaped . $quote;
4 ✔
356
    }
357

358
    /**
359
     * @return array<int, AstNode>|null
360
     */
361
    private function tryParseInlineIfExpressionArguments(): ?array
362
    {
363
        $savedPosition = $this->stream->getPosition();
12 ✔
364

365
        $arguments = [];
12 ✔
366
        $condition = ($this->parseValueUntil)([TokenType::COLON, TokenType::COMMA, TokenType::RPAREN]);
12 ✔
367

368
        if ($condition === null) {
12 ✔
369
            $this->stream->setPosition($savedPosition);
1 ✔
370

371
            return null;
1 ✔
372
        }
373

374
        if (! $this->stream->is(TokenType::COLON)) {
11 ✔
375
            $this->stream->setPosition($savedPosition);
2 ✔
376

377
            return null;
2 ✔
378
        }
379

380
        $arguments[] = $condition;
9 ✔
381

382
        $this->stream->advance();
9 ✔
383
        $this->stream->skipWhitespace();
9 ✔
384

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

387
        if ($truthy !== null) {
9 ✔
388
            $arguments[] = $truthy;
9 ✔
389
        }
390

391
        $this->stream->skipWhitespace();
9 ✔
392

393
        if ($this->stream->consume(TokenType::SEMICOLON)) {
9 ✔
394
            $this->stream->skipWhitespace();
6 ✔
395

396
            if (
397
                $this->stream->is(TokenType::IDENTIFIER)
6 ✔
398
                && strtolower($this->stream->current()->value) === 'else'
6 ✔
399
            ) {
400
                $this->stream->advance();
6 ✔
401
                $this->stream->skipWhitespace();
6 ✔
402
                $this->stream->consume(TokenType::COLON);
6 ✔
403
                $this->stream->skipWhitespace();
6 ✔
404
            }
405

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

408
            if ($falsy !== null) {
6 ✔
409
                $arguments[] = $falsy;
6 ✔
410
            }
411
        }
412

413
        $this->stream->skipWhitespace();
9 ✔
414
        $this->stream->consume(TokenType::RPAREN);
9 ✔
415

416
        return $arguments;
9 ✔
417
    }
418

419
    private function isValidUnquotedUrl(string $argument): bool
420
    {
421
        if ($argument === '') {
7 ✔
422
            return false;
1 ✔
423
        }
424

425
        if (str_contains($argument, '$') || str_contains($argument, '+')) {
7 ✔
426
            return false;
3 ✔
427
        }
428

429
        $withoutInterpolation = '';
7 ✔
430

431
        $length = strlen($argument);
7 ✔
432
        $index  = 0;
7 ✔
433

434
        while ($index < $length) {
7 ✔
435
            if ($argument[$index] === '#' && $index + 1 < $length && $argument[$index + 1] === '{') {
7 ✔
436
                $closingBrace = strpos($argument, '}', $index + 2);
1 ✔
437

438
                if ($closingBrace !== false) {
1 ✔
439
                    $index = $closingBrace + 1;
1 ✔
440

441
                    continue;
1 ✔
442
                }
443
            }
444

445
            $withoutInterpolation .= $argument[$index];
6 ✔
446

447
            $index++;
6 ✔
448
        }
449

450
        if ($withoutInterpolation === '') {
7 ✔
451
            return false;
1 ✔
452
        }
453

454
        return strpbrk($withoutInterpolation, " \t\n\r\0\x0B\"'()") === false;
6 ✔
455
    }
456

457
    private function nodeToString(AstNode $node): string
458
    {
459
        if ($node instanceof StringNode) {
4 ✔
460
            return $node->value;
3 ✔
461
        }
462

463
        if ($node instanceof NumberNode) {
4 ✔
464
            return "$node->value$node->unit";
1 ✔
465
        }
466

467
        if ($node instanceof ColorNode) {
3 ✔
468
            return $node->value;
1 ✔
469
        }
470

471
        if ($node instanceof VariableReferenceNode) {
2 ✔
472
            return '$' . $node->name;
1 ✔
473
        }
474

475
        if ($node instanceof ListNode) {
2 ✔
476
            $parts = [];
1 ✔
477
            foreach ($node->items as $item) {
1 ✔
478
                $parts[] = $this->nodeToString($item);
1 ✔
479
            }
480

481
            return implode($node->separator === ',' ? ', ' : ' ', $parts);
1 ✔
482
        }
483

484
        return '';
1 ✔
485
    }
486
}
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