• 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

88.41
/src/Services/Module.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Bugo\SCSS\Services;
6

7
use Bugo\SCSS\CompilerContext;
8
use Bugo\SCSS\Exceptions\CannotModifyBuiltInVariableException;
9
use Bugo\SCSS\Exceptions\MaxIterationsExceededException;
10
use Bugo\SCSS\Exceptions\ModuleResolutionException;
11
use Bugo\SCSS\Exceptions\UndefinedSymbolException;
12
use Bugo\SCSS\LoaderInterface;
13
use Bugo\SCSS\NodeDispatcherInterface;
14
use Bugo\SCSS\Nodes\AstNode;
15
use Bugo\SCSS\Nodes\ForwardNode;
16
use Bugo\SCSS\Nodes\ImportNode;
17
use Bugo\SCSS\Nodes\ModuleVarDeclarationNode;
18
use Bugo\SCSS\Nodes\RootNode;
19
use Bugo\SCSS\Nodes\UseNode;
20
use Bugo\SCSS\Nodes\VariableDeclarationNode;
21
use Bugo\SCSS\ParserInterface;
22
use Bugo\SCSS\Runtime\Environment;
23
use Bugo\SCSS\Runtime\Scope;
24
use Bugo\SCSS\States\ModuleState;
25
use Bugo\SCSS\Syntax;
26
use Bugo\SCSS\Utils\NameNormalizer;
27

28
use function array_key_exists;
29
use function array_keys;
30
use function array_map;
31
use function array_unique;
32
use function dirname;
33
use function explode;
34
use function implode;
35
use function in_array;
36
use function ksort;
37
use function ltrim;
38
use function pathinfo;
39
use function serialize;
40
use function str_ends_with;
41
use function str_starts_with;
42
use function strlen;
43
use function strpos;
44
use function strtolower;
45
use function substr;
46
use function substr_count;
47
use function trim;
48

49
use const PATHINFO_FILENAME;
50

51
final readonly class Module
52
{
53
    public function __construct(
54
        private CompilerContext $ctx,
55
        private LoaderInterface $loader,
56
        private ParserInterface $parser,
57
        private AstEvaluator $ast,
58
        private Evaluator $evaluation,
59
        private Selector $selector,
60
        private NodeDispatcherInterface $dispatcher
61
    ) {}
614 ✔
62

63
    public function assignModuleVariable(
64
        ModuleVarDeclarationNode $node,
65
        Environment $env,
66
        bool $evaluateValue = true
67
    ): void {
68
        if ($this->ctx->functionRegistry->isBuiltinAlias($node->module)) {
3 ✔
69
            throw new CannotModifyBuiltInVariableException();
1 ✔
70
        }
71

72
        $moduleScope = $env->getCurrentScope()->getModule($node->module);
2 ✔
73

74
        if (! $moduleScope) {
2 ✔
NEW
75
            throw ModuleResolutionException::notFound($node->module);
×
76
        }
77

78
        if (NameNormalizer::isPrivate($node->name)) {
2 ✔
NEW
79
            throw UndefinedSymbolException::variableInModule($node->module, $node->name);
×
80
        }
81

82
        $value = $evaluateValue ? $this->evaluation->evaluateValue($node->value, $env) : $node->value;
2 ✔
83

84
        $moduleScope->setVariableLocal($node->name, $value, $node->default);
2 ✔
85
    }
86

87
    public function state(): ModuleState
88
    {
89
        return $this->ctx->moduleState;
266 ✔
90
    }
91

92
    public function moduleState(): ModuleState
93
    {
94
        return $this->state();
26 ✔
95
    }
96

97
    public function incrementCallDepth(): void
98
    {
99
        $state = $this->state();
37 ✔
100

101
        if ($state->callDepth >= 100) {
37 ✔
NEW
102
            throw new MaxIterationsExceededException('@include');
×
103
        }
104

105
        $state->callDepth++;
37 ✔
106
    }
107

108
    public function decrementCallDepth(): void
109
    {
110
        $this->state()->callDepth--;
37 ✔
111
    }
112

113
    public function handleUse(UseNode $node, Environment $env): void
114
    {
115
        $state = $this->state();
236 ✔
116

117
        if ($state->importEvaluationDepth === 0 && $state->hasSassImport) {
236 ✔
NEW
118
            throw ModuleResolutionException::mixedImportUse();
×
119
        }
120

121
        if ($state->importEvaluationDepth === 0) {
236 ✔
122
            $state->hasUseDirective = true;
236 ✔
123
        }
124

125
        if (str_starts_with($node->path, 'sass:')) {
236 ✔
126
            if ($node->configuration !== []) {
211 ✔
127
                throw ModuleResolutionException::builtInModuleConfiguration($node->path);
1 ✔
128
            }
129

130
            $this->ctx->functionRegistry->registerUse($node->path, $node->namespace);
210 ✔
131

132
            $moduleName = substr($node->path, 5);
210 ✔
133
            $alias      = $node->namespace ?? $moduleName;
210 ✔
134

135
            if ($alias === '*') {
210 ✔
NEW
136
                return;
×
137
            }
138

139
            $variables = $this->ctx->functionRegistry->moduleVariablesByAlias($alias) ?? [];
210 ✔
140

141
            if ($variables === []) {
210 ✔
142
                return;
170 ✔
143
            }
144

145
            $moduleScope = new Scope();
45 ✔
146

147
            foreach ($variables as $name => $value) {
45 ✔
148
                $moduleScope->setVariable($name, $value);
45 ✔
149
            }
150

151
            $env->getCurrentScope()->addModule($alias, $moduleScope);
45 ✔
152
            $env->getGlobalScope()->addModule($alias, $moduleScope);
45 ✔
153

154
            return;
45 ✔
155
        }
156

157
        $namespace = $node->namespace ?? $this->deriveNamespaceFromUsePath($node->path);
30 ✔
158

159
        if ($node->configuration === [] && isset($state->loadedModules[$namespace])) {
30 ✔
160
            $env->getCurrentScope()->addModule($namespace, $state->loadedModules[$namespace]['scope']);
3 ✔
161
            $env->getGlobalScope()->addModule($namespace, $state->loadedModules[$namespace]['scope']);
3 ✔
162

163
            return;
3 ✔
164
        }
165

166
        if ($namespace !== '*' && $env->getCurrentScope()->hasModuleLocal($namespace)) {
30 ✔
NEW
167
            throw ModuleResolutionException::duplicateNamespace($namespace);
×
168
        }
169

170
        $file = $this->loader->load($node->path);
30 ✔
171

172
        $this->loader->addPath(dirname($file['path']));
29 ✔
173

174
        $moduleId = $file['path'];
29 ✔
175

176
        if ($node->configuration === [] && isset($state->loadedModulesById[$moduleId])) {
29 ✔
NEW
177
            $moduleData = $state->loadedModulesById[$moduleId];
×
178

NEW
179
            $state->loadedModules[$namespace] = $moduleData;
×
180

NEW
181
            $env->getCurrentScope()->addModule($namespace, $moduleData['scope']);
×
NEW
182
            $env->getGlobalScope()->addModule($namespace, $moduleData['scope']);
×
183

NEW
184
            return;
×
185
        }
186

187
        if (isset($state->loadingFiles[$moduleId])) {
29 ✔
NEW
188
            throw ModuleResolutionException::circularDependency($moduleId);
×
189
        }
190

191
        $moduleSource = $this->ctx->normalizerPipeline->process(
29 ✔
192
            $file['content'],
29 ✔
193
            Syntax::fromPath($file['path'], $file['content'])
29 ✔
194
        );
29 ✔
195

196
        $moduleAst = $this->parser->parse($moduleSource);
29 ✔
197

198
        if ($node->configuration !== []) {
29 ✔
199
            $defaultVars = $this->collectDefaultVariableNames($moduleAst);
3 ✔
200

201
            foreach (array_keys($node->configuration) as $configName) {
3 ✔
202
                if (! isset($defaultVars[NameNormalizer::normalize($configName)])) {
3 ✔
NEW
203
                    throw ModuleResolutionException::nonConfigurableVariable($configName, $moduleId);
×
204
                }
205
            }
206
        }
207

208
        $moduleEnv = new Environment();
29 ✔
209

210
        foreach ($node->configuration as $name => $valueNode) {
29 ✔
211
            $moduleEnv->getCurrentScope()->setVariable(
3 ✔
212
                $name,
3 ✔
213
                $this->evaluation->evaluateValue($valueNode, $env)
3 ✔
214
            );
3 ✔
215
        }
216

217
        $state->loadingFiles[$moduleId] = true;
29 ✔
218

219
        try {
220
            $this->ast->evaluate($moduleAst, $moduleEnv);
29 ✔
221
        } finally {
222
            unset($state->loadingFiles[$moduleId]);
29 ✔
223
        }
224

225
        if ($namespace === '*') {
29 ✔
226
            foreach (array_keys($moduleEnv->getCurrentScope()->getVariables()) as $name) {
3 ✔
227
                if (NameNormalizer::isPrivate($name)) {
3 ✔
228
                    continue;
1 ✔
229
                }
230

231
                $env->getCurrentScope()->setVariableLocal($name, $moduleEnv->getCurrentScope()->getVariable($name));
3 ✔
232
            }
233

234
            foreach ($moduleEnv->getCurrentScope()->getMixins() as $name => $mixin) {
3 ✔
235
                $env->getCurrentScope()->setMixin($name, $mixin);
2 ✔
236
            }
237

238
            foreach ($moduleEnv->getCurrentScope()->getFunctions() as $name => $function) {
3 ✔
239
                $env->getCurrentScope()->setFunction($name, $function);
1 ✔
240
            }
241

242
            return;
3 ✔
243
        }
244

245
        $moduleData = [
26 ✔
246
            'id'    => $moduleId,
26 ✔
247
            'scope' => $moduleEnv->getCurrentScope(),
26 ✔
248
            'css'   => $this->dispatcher->compile($moduleAst, $moduleEnv),
26 ✔
249
        ];
26 ✔
250

251
        $state->loadedModules[$namespace] = $moduleData;
26 ✔
252

253
        if ($node->configuration === []) {
26 ✔
254
            $state->loadedModulesById[$moduleId] = $moduleData;
23 ✔
255
        }
256

257
        $env->getCurrentScope()->addModule($namespace, $moduleEnv->getCurrentScope());
26 ✔
258
        $env->getGlobalScope()->addModule($namespace, $moduleEnv->getCurrentScope());
26 ✔
259
    }
260

261
    public function handleImport(ImportNode $node, Environment $env): void
262
    {
263
        $state = $this->state();
7 ✔
264

265
        foreach ($node->imports as $import) {
7 ✔
266
            $resolvedImport = $this->resolveImport($import);
7 ✔
267

268
            if ($resolvedImport['type'] !== 'sass') {
7 ✔
269
                continue;
1 ✔
270
            }
271

272
            /** @var array{type: 'sass', path: string} $resolvedImport */
273
            $path = $resolvedImport['path'];
6 ✔
274

275
            if ($path === '') {
6 ✔
NEW
276
                continue;
×
277
            }
278

279
            if ($state->importEvaluationDepth === 0) {
6 ✔
280
                if ($state->hasUseDirective) {
6 ✔
NEW
281
                    throw ModuleResolutionException::mixedImportUse();
×
282
                }
283

284
                $state->hasSassImport = true;
6 ✔
285
            }
286

287
            $data = $this->loadAndEvaluateModule(
6 ✔
288
                $path,
6 ✔
289
                [],
6 ✔
290
                true,
6 ✔
291
                false,
6 ✔
292
                $this->extractAstVariables($env->getCurrentScope()->getVariables())
6 ✔
293
            );
6 ✔
294

295
            $this->mergeScopeExports($data['scope'], $env->getCurrentScope());
6 ✔
296
        }
297
    }
298

299
    public function handleForward(ForwardNode $node, Environment $env): void
300
    {
301
        $path = $node->path;
6 ✔
302

303
        $resolvedConfiguration = $this->resolveForwardConfiguration($node, $env);
6 ✔
304

305
        if ($this->importEvaluationDepth() > 0) {
6 ✔
306
            $resolvedConfiguration = $this->resolveImportForwardConfiguration($node, $env, $resolvedConfiguration);
1 ✔
307
        }
308

309
        $forwardKey = $this->forwardCacheKey($path, $resolvedConfiguration, $env);
6 ✔
310
        $state      = $this->state();
6 ✔
311

312
        if (! isset($state->forwardedModules[$forwardKey])) {
6 ✔
313
            $state->forwardedModules[$forwardKey] = $this->loadAndEvaluateModule($path, $resolvedConfiguration);
6 ✔
314
        }
315

316
        $moduleData = $state->forwardedModules[$forwardKey];
6 ✔
317

318
        $this->mergeScopeExports(
6 ✔
319
            $moduleData['scope'],
6 ✔
320
            $env->getCurrentScope(),
6 ✔
321
            $node->prefix,
6 ✔
322
            $node->visibility,
6 ✔
323
            $node->members
6 ✔
324
        );
6 ✔
325
    }
326

327
    public function deriveNamespaceFromUsePath(string $path): string
328
    {
329
        return ltrim(pathinfo($path, PATHINFO_FILENAME), '_');
18 ✔
330
    }
331

332
    /**
333
     * @return array{type: 'css', raw: string}|array{type: 'sass', path: string}
334
     */
335
    public function resolveImport(string $import): array
336
    {
337
        $raw = trim($import);
9 ✔
338

339
        if ($raw === '') {
9 ✔
NEW
340
            return ['type' => 'css', 'raw' => $raw];
×
341
        }
342

343
        if ($this->isCssImportRaw($raw)) {
9 ✔
344
            return ['type' => 'css', 'raw' => $raw];
2 ✔
345
        }
346

347
        if ($raw[0] === '"' || $raw[0] === "'") {
8 ✔
348
            $quote        = $raw[0];
8 ✔
349
            $length       = strlen($raw);
8 ✔
350
            $closingIndex = 1;
8 ✔
351

352
            while ($closingIndex < $length && $raw[$closingIndex] !== $quote) {
8 ✔
353
                $closingIndex++;
8 ✔
354
            }
355

356
            if ($closingIndex >= $length) {
8 ✔
NEW
357
                return ['type' => 'css', 'raw' => $raw];
×
358
            }
359

360
            $path  = substr($raw, 1, $closingIndex - 1);
8 ✔
361
            $media = trim(substr($raw, $closingIndex + 1));
8 ✔
362

363
            if ($media !== '' || $this->isCssImportPath($path)) {
8 ✔
364
                return ['type' => 'css', 'raw' => '"' . $path . '"' . ($media !== '' ? ' ' . $media : '')];
1 ✔
365
            }
366

367
            return ['type' => 'sass', 'path' => $path];
7 ✔
368
        }
369

NEW
370
        $spacePosition = strpos($raw, ' ');
×
371

NEW
372
        if ($spacePosition !== false) {
×
NEW
373
            $path  = trim(substr($raw, 0, $spacePosition));
×
NEW
374
            $media = trim(substr($raw, $spacePosition + 1));
×
375

NEW
376
            if ($media !== '' || $this->isCssImportPath($path)) {
×
NEW
377
                return ['type' => 'css', 'raw' => $raw];
×
378
            }
379

NEW
380
            return ['type' => 'sass', 'path' => $path];
×
381
        }
382

NEW
383
        if ($this->isCssImportPath($raw)) {
×
NEW
384
            return ['type' => 'css', 'raw' => $raw];
×
385
        }
386

NEW
387
        return ['type' => 'sass', 'path' => $raw];
×
388
    }
389

390
    /**
391
     * @param array<string, AstNode> $configuration
392
     * @param array<string, AstNode> $initialVariables
393
     * @return array{scope: Scope, css: string}
394
     */
395
    public function loadAndEvaluateModule(
396
        string $path,
397
        array $configuration = [],
398
        bool $fromImport = false,
399
        bool $compileCss = true,
400
        array $initialVariables = []
401
    ): array {
402
        $file = $this->loader->load($path, $fromImport);
13 ✔
403

404
        $this->loader->addPath(dirname($file['path']));
13 ✔
405

406
        $moduleSource = $this->ctx->normalizerPipeline->process(
13 ✔
407
            $file['content'],
13 ✔
408
            Syntax::fromPath($file['path'], $file['content'])
13 ✔
409
        );
13 ✔
410

411
        $moduleAst = $this->parser->parse($moduleSource);
13 ✔
412
        $moduleEnv = new Environment();
13 ✔
413

414
        foreach ($initialVariables as $name => $value) {
13 ✔
415
            $moduleEnv->getCurrentScope()->setVariableLocal($name, $value);
4 ✔
416
        }
417

418
        foreach ($configuration as $name => $value) {
13 ✔
419
            $moduleEnv->getCurrentScope()->setVariable($name, $value);
3 ✔
420
        }
421

422
        if ($fromImport) {
13 ✔
423
            $resolvedPath = $file['path'];
7 ✔
424

425
            if (isset($this->ctx->moduleState->loadingFiles[$resolvedPath])) {
7 ✔
NEW
426
                throw ModuleResolutionException::circularDependency($resolvedPath);
×
427
            }
428

429
            $this->ctx->moduleState->loadingFiles[$resolvedPath] = true;
7 ✔
430
            $this->ctx->moduleState->importEvaluationDepth++;
7 ✔
431
        }
432

433
        try {
434
            $this->ast->evaluate($moduleAst, $moduleEnv);
13 ✔
435

436
            $css = $compileCss ? $this->dispatcher->compile($moduleAst, $moduleEnv) : '';
13 ✔
437
        } finally {
438
            if ($fromImport) {
13 ✔
439
                $this->ctx->moduleState->importEvaluationDepth--;
7 ✔
440
                unset($this->ctx->moduleState->loadingFiles[$file['path']]);
13 ✔
441
            }
442
        }
443

444
        return [
13 ✔
445
            'scope' => $moduleEnv->getCurrentScope(),
13 ✔
446
            'css'   => $css,
13 ✔
447
        ];
13 ✔
448
    }
449

450
    /**
451
     * @param array<string, mixed> $variables
452
     * @return array<string, AstNode>
453
     */
454
    public function extractAstVariables(array $variables): array
455
    {
456
        return array_filter($variables, static fn(mixed $value): bool => $value instanceof AstNode);
7 ✔
457
    }
458

459
    public function qualifyImportedCssWithParentSelector(string $css, string $parentSelector): string
460
    {
461
        $lines  = explode("\n", $css);
2 ✔
462
        $result = [];
2 ✔
463
        $depth  = 0;
2 ✔
464

465
        foreach ($lines as $line) {
2 ✔
466
            $trimmed = trim($line);
2 ✔
467

468
            if (
469
                $depth === 0
2 ✔
470
                && $trimmed !== ''
2 ✔
471
                && str_ends_with($trimmed, '{')
2 ✔
472
                && ! str_starts_with($trimmed, '@')
2 ✔
473
            ) {
474
                $selector = trim(substr($trimmed, 0, -1));
2 ✔
475

476
                if ($selector !== '') {
2 ✔
477
                    $leadingSpaces = strlen($line) - strlen(ltrim($line, ' '));
2 ✔
478
                    $combined      = $this->selector->combineNestedSelectorWithParent($selector, $parentSelector);
2 ✔
479
                    $line          = str_repeat(' ', $leadingSpaces) . $combined . ' {';
2 ✔
480
                }
481
            }
482

483
            $result[] = $line;
2 ✔
484

485
            $depth += substr_count($line, '{') - substr_count($line, '}');
2 ✔
486
        }
487

488
        return implode("\n", $result);
2 ✔
489
    }
490

491
    /**
492
     * @return array<string, AstNode>
493
     */
494
    public function resolveForwardConfiguration(ForwardNode $node, Environment $env): array
495
    {
496
        if ($node->configuration === []) {
7 ✔
497
            return [];
6 ✔
498
        }
499

500
        $resolved = [];
1 ✔
501

502
        foreach ($node->configuration as $name => $entry) {
1 ✔
503
            $valueNode    = $entry['value'];
1 ✔
504
            $isDefault    = $entry['default'];
1 ✔
505
            $currentValue = $isDefault ? $env->getCurrentScope()->getAstVariable($name) : null;
1 ✔
506

507
            if ($currentValue !== null) {
1 ✔
508
                $resolved[$name] = $currentValue;
1 ✔
509

510
                continue;
1 ✔
511
            }
512

513
            $resolved[$name] = $this->evaluation->evaluateValue($valueNode, $env);
1 ✔
514
        }
515

516
        return $resolved;
1 ✔
517
    }
518

519
    /**
520
     * @param array<string, AstNode> $resolvedConfiguration
521
     * @return array<string, AstNode>
522
     */
523
    public function resolveImportForwardConfiguration(
524
        ForwardNode $node,
525
        Environment $env,
526
        array $resolvedConfiguration
527
    ): array {
528
        $prefix = $node->prefix ?? '';
1 ✔
529

530
        if ($prefix === '') {
1 ✔
NEW
531
            return $resolvedConfiguration;
×
532
        }
533

534
        foreach (array_keys($env->getCurrentScope()->getVariables()) as $name) {
1 ✔
535
            if (! str_starts_with($name, $prefix)) {
1 ✔
NEW
536
                continue;
×
537
            }
538

539
            $forwardedName = substr($name, strlen($prefix));
1 ✔
540

541
            if ($forwardedName === '' || array_key_exists($forwardedName, $resolvedConfiguration)) {
1 ✔
NEW
542
                continue;
×
543
            }
544

545
            $value = $env->getCurrentScope()->getAstVariable($name);
1 ✔
546

547
            if ($value !== null) {
1 ✔
548
                $resolvedConfiguration[$forwardedName] = $value;
1 ✔
549
            }
550
        }
551

552
        return $resolvedConfiguration;
1 ✔
553
    }
554

555
    /**
556
     * @param array<string, AstNode> $configuration
557
     */
558
    public function forwardCacheKey(string $path, array $configuration, Environment $env): string
559
    {
560
        if ($configuration === []) {
7 ✔
561
            return $path;
5 ✔
562
        }
563

564
        $normalized = array_map(
2 ✔
565
            fn(AstNode $value): string => $this->evaluation->format($value, $env),
2 ✔
566
            $configuration
2 ✔
567
        );
2 ✔
568

569
        ksort($normalized);
2 ✔
570

571
        return $path . "\0" . serialize($normalized);
2 ✔
572
    }
573

574
    public function importEvaluationDepth(): int
575
    {
576
        return $this->ctx->moduleState->importEvaluationDepth;
7 ✔
577
    }
578

579
    /**
580
     * @param array<int, string> $members
581
     */
582
    private function mergeScopeExports(
583
        Scope $from,
584
        Scope $to,
585
        ?string $prefix = null,
586
        ?string $visibility = null,
587
        array $members = []
588
    ): void {
589
        $normalizedMembers = $this->normalizeForwardMembers($members);
11 ✔
590

591
        foreach (array_keys($from->getVariables()) as $name) {
11 ✔
592
            if (! $this->shouldForwardMember($name, true, $visibility, $normalizedMembers)) {
8 ✔
593
                continue;
1 ✔
594
            }
595

596
            $to->setVariableLocal($this->prefixExportName($name, $prefix), $from->getVariable($name));
7 ✔
597
        }
598

599
        foreach ($from->getMixins() as $name => $mixin) {
11 ✔
600
            if (! $this->shouldForwardMember($name, false, $visibility, $normalizedMembers)) {
6 ✔
601
                continue;
1 ✔
602
            }
603

604
            $to->setMixin(
5 ✔
605
                $this->prefixExportName($name, $prefix),
5 ✔
606
                $mixin
5 ✔
607
            );
5 ✔
608
        }
609

610
        foreach ($from->getFunctions() as $name => $function) {
11 ✔
611
            if (! $this->shouldForwardMember($name, false, $visibility, $normalizedMembers)) {
5 ✔
NEW
612
                continue;
×
613
            }
614

615
            $to->setFunction(
5 ✔
616
                $this->prefixExportName($name, $prefix),
5 ✔
617
                $function
5 ✔
618
            );
5 ✔
619
        }
620
    }
621

622
    private function prefixExportName(string $name, ?string $prefix): string
623
    {
624
        if ($prefix === null || $prefix === '') {
8 ✔
625
            return $name;
7 ✔
626
        }
627

628
        return $prefix . $name;
2 ✔
629
    }
630

631
    /**
632
     * @param array<int, string> $members
633
     * @return array{variables: array<int, string>, others: array<int, string>}
634
     */
635
    private function normalizeForwardMembers(array $members): array
636
    {
637
        $variables = [];
11 ✔
638
        $others    = [];
11 ✔
639

640
        foreach ($members as $member) {
11 ✔
641
            if ($member === '') {
2 ✔
NEW
642
                continue;
×
643
            }
644

645
            if ($member[0] === '$') {
2 ✔
646
                $variables[] = NameNormalizer::normalize(substr($member, 1));
2 ✔
647

648
                continue;
2 ✔
649
            }
650

651
            $others[] = NameNormalizer::normalize($member);
1 ✔
652
        }
653

654
        return [
11 ✔
655
            'variables' => array_unique($variables),
11 ✔
656
            'others'    => array_unique($others),
11 ✔
657
        ];
11 ✔
658
    }
659

660
    /**
661
     * @param array{variables: array<int, string>, others: array<int, string>} $members
662
     */
663
    private function shouldForwardMember(
664
        string $name,
665
        bool $isVariable,
666
        ?string $visibility,
667
        array $members
668
    ): bool {
669
        if ($visibility !== 'show' && $visibility !== 'hide') {
8 ✔
670
            return true;
6 ✔
671
        }
672

673
        $normalizedName = NameNormalizer::normalize($name);
2 ✔
674
        $set            = $isVariable ? $members['variables'] : $members['others'];
2 ✔
675
        $contains       = in_array($normalizedName, $set, true);
2 ✔
676

677
        return $visibility === 'show' ? $contains : ! $contains;
2 ✔
678
    }
679

680
    private function isCssImportRaw(string $raw): bool
681
    {
682
        $lower = strtolower($raw);
9 ✔
683

684
        if (str_starts_with($lower, 'url(')) {
9 ✔
685
            return true;
2 ✔
686
        }
687

688
        if (str_starts_with($lower, 'http:' . '//') || str_starts_with($lower, 'https://')) {
8 ✔
NEW
689
            return true;
×
690
        }
691

692
        return false;
8 ✔
693
    }
694

695
    private function isCssImportPath(string $path): bool
696
    {
697
        $lower = strtolower(trim($path));
8 ✔
698

699
        if ($lower === '') {
8 ✔
NEW
700
            return false;
×
701
        }
702

703
        if (str_starts_with($lower, 'http:' . '//') || str_starts_with($lower, 'https://')) {
8 ✔
704
            return true;
1 ✔
705
        }
706

707
        return str_ends_with($lower, '.css');
8 ✔
708
    }
709

710
    /** @return array<string, true> */
711
    private function collectDefaultVariableNames(RootNode $ast): array
712
    {
713
        $defaults = [];
3 ✔
714

715
        foreach ($ast->children as $node) {
3 ✔
716
            if ($node instanceof VariableDeclarationNode && $node->default) {
3 ✔
717
                $defaults[NameNormalizer::normalize($node->name)] = true;
2 ✔
718
            }
719

720
            if ($node instanceof ForwardNode) {
3 ✔
721
                foreach ($node->configuration as $name => $entry) {
1 ✔
722
                    if ($entry['default']) {
1 ✔
723
                        $defaults[NameNormalizer::normalize($name)] = true;
1 ✔
724
                    }
725
                }
726
            }
727
        }
728

729
        return $defaults;
3 ✔
730
    }
731
}
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