• 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

98.99
/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 strtolower;
44
use function substr;
45
use function substr_count;
46
use function trim;
47

48
use const PATHINFO_FILENAME;
49

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

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

71
        $moduleScope = $env->getCurrentScope()->getModule($node->module);
7 ✔
72

73
        if (! $moduleScope) {
7 ✔
74
            throw ModuleResolutionException::notFound($node->module);
1 ✔
75
        }
76

77
        if (NameNormalizer::isPrivate($node->name)) {
6 ✔
78
            throw UndefinedSymbolException::variableInModule($node->module, $node->name);
1 ✔
79
        }
80

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

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

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

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

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

100
        if ($state->callDepth >= 100) {
42 ✔
101
            throw new MaxIterationsExceededException('@include');
1 ✔
102
        }
103

104
        $state->callDepth++;
41 ✔
105
    }
106

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

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

116
        if ($state->importEvaluationDepth === 0 && $state->hasSassImport) {
250 ✔
117
            throw ModuleResolutionException::mixedImportUse();
1 ✔
118
        }
119

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

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

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

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

134
            if ($alias === '*') {
211 ✔
135
                return;
1 ✔
136
            }
137

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

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

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

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

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

153
            return;
45 ✔
154
        }
155

156
        $namespace = $node->namespace ?? $this->deriveNamespaceFromUsePath($node->path);
42 ✔
157

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

162
            return;
3 ✔
163
        }
164

165
        if ($namespace !== '*' && $env->getCurrentScope()->hasModuleLocal($namespace)) {
42 ✔
166
            throw ModuleResolutionException::duplicateNamespace($namespace);
1 ✔
167
        }
168

169
        $file = $this->loader->load($node->path);
41 ✔
170

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

173
        $moduleId = $file['path'];
40 ✔
174

175
        if ($node->configuration === [] && isset($state->loadedModulesById[$moduleId])) {
40 ✔
176
            $moduleData = $state->loadedModulesById[$moduleId];
1 ✔
177

178
            $state->loadedModules[$namespace] = $moduleData;
1 ✔
179

180
            $env->getCurrentScope()->addModule($namespace, $moduleData['scope']);
1 ✔
181
            $env->getGlobalScope()->addModule($namespace, $moduleData['scope']);
1 ✔
182

183
            return;
1 ✔
184
        }
185

186
        if (isset($state->loadingFiles[$moduleId])) {
39 ✔
187
            throw ModuleResolutionException::circularDependency($moduleId);
1 ✔
188
        }
189

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

195
        $moduleAst = $this->parser->parse($moduleSource);
38 ✔
196

197
        if ($node->configuration !== []) {
38 ✔
198
            $defaultVars = $this->collectDefaultVariableNames($moduleAst);
4 ✔
199

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

207
        $moduleEnv = new Environment();
37 ✔
208

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

216
        $state->loadingFiles[$moduleId] = true;
37 ✔
217

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

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

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

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

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

241
            return;
3 ✔
242
        }
243

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

250
        $state->loadedModules[$namespace] = $moduleData;
34 ✔
251

252
        if ($node->configuration === []) {
34 ✔
253
            $state->loadedModulesById[$moduleId] = $moduleData;
31 ✔
254
        }
255

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

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

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

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

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

274
            if ($path === '') {
7 ✔
UNCOV
275
                continue;
×
276
            }
277

278
            if ($state->importEvaluationDepth === 0) {
7 ✔
279
                if ($state->hasUseDirective) {
7 ✔
280
                    throw ModuleResolutionException::mixedImportUse();
1 ✔
281
                }
282

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

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

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

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

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

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

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

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

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

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

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

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

338
        if ($raw === '') {
20 ✔
339
            return ['type' => 'css', 'raw' => $raw];
1 ✔
340
        }
341

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

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

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

355
            if ($closingIndex >= $length) {
12 ✔
356
                return ['type' => 'css', 'raw' => $raw];
1 ✔
357
            }
358

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

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

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

369
        if (str_contains($raw, ' ')) {
5 ✔
370
            return ['type' => 'css', 'raw' => $raw];
1 ✔
371
        }
372

373
        if ($this->isCssImportPath($raw)) {
4 ✔
374
            return ['type' => 'css', 'raw' => $raw];
1 ✔
375
        }
376

377
        return ['type' => 'sass', 'path' => $raw];
3 ✔
378
    }
379

380
    /**
381
     * @param array<string, AstNode> $configuration
382
     * @param array<string, AstNode> $initialVariables
383
     * @return array{scope: Scope, css: string}
384
     */
385
    public function loadAndEvaluateModule(
386
        string $path,
387
        array $configuration = [],
388
        bool $fromImport = false,
389
        bool $compileCss = true,
390
        array $initialVariables = [],
391
    ): array {
392
        $file = $this->loader->load($path, $fromImport);
21 ✔
393

394
        $this->loader->addPath(dirname($file['path']));
21 ✔
395

396
        $moduleSource = $this->ctx->normalizerPipeline->process(
21 ✔
397
            $file['content'],
21 ✔
398
            Syntax::fromPath($file['path'], $file['content']),
21 ✔
399
        );
21 ✔
400

401
        $moduleAst = $this->parser->parse($moduleSource);
21 ✔
402
        $moduleEnv = new Environment();
21 ✔
403

404
        foreach ($initialVariables as $name => $value) {
21 ✔
405
            $moduleEnv->getCurrentScope()->setVariableLocal($name, $value);
5 ✔
406
        }
407

408
        foreach ($configuration as $name => $value) {
21 ✔
409
            $moduleEnv->getCurrentScope()->setVariable($name, $value);
3 ✔
410
        }
411

412
        if ($fromImport) {
21 ✔
413
            $resolvedPath = $file['path'];
11 ✔
414

415
            if (isset($this->ctx->moduleState->loadingFiles[$resolvedPath])) {
11 ✔
416
                throw ModuleResolutionException::circularDependency($resolvedPath);
1 ✔
417
            }
418

419
            $this->ctx->moduleState->loadingFiles[$resolvedPath] = true;
10 ✔
420
            $this->ctx->moduleState->importEvaluationDepth++;
10 ✔
421
        }
422

423
        try {
424
            $this->ast->evaluate($moduleAst, $moduleEnv);
20 ✔
425

426
            $css = $compileCss ? $this->dispatcher->compile($moduleAst, $moduleEnv) : '';
20 ✔
427
        } finally {
428
            if ($fromImport) {
20 ✔
429
                $this->ctx->moduleState->importEvaluationDepth--;
10 ✔
430
                unset($this->ctx->moduleState->loadingFiles[$file['path']]);
20 ✔
431
            }
432
        }
433

434
        return [
20 ✔
435
            'scope' => $moduleEnv->getCurrentScope(),
20 ✔
436
            'css'   => $css,
20 ✔
437
        ];
20 ✔
438
    }
439

440
    /**
441
     * @param array<string, mixed> $variables
442
     * @return array<string, AstNode>
443
     */
444
    public function extractAstVariables(array $variables): array
445
    {
446
        return array_filter($variables, static fn(mixed $value): bool => $value instanceof AstNode);
10 ✔
447
    }
448

449
    public function qualifyImportedCssWithParentSelector(string $css, string $parentSelector): string
450
    {
451
        $lines  = explode("\n", $css);
4 ✔
452
        $result = [];
4 ✔
453
        $depth  = 0;
4 ✔
454

455
        foreach ($lines as $line) {
4 ✔
456
            $trimmed = trim($line);
4 ✔
457

458
            if (
459
                $depth === 0
4 ✔
460
                && $trimmed !== ''
4 ✔
461
                && str_ends_with($trimmed, '{')
4 ✔
462
                && ! str_starts_with($trimmed, '@')
4 ✔
463
            ) {
464
                $selector = trim(substr($trimmed, 0, -1));
4 ✔
465

466
                if ($selector !== '') {
4 ✔
467
                    $leadingSpaces = strlen($line) - strlen(ltrim($line, ' '));
4 ✔
468
                    $combined      = $this->selector->combineNestedSelectorWithParent($selector, $parentSelector);
4 ✔
469
                    $line          = str_repeat(' ', $leadingSpaces) . $combined . ' {';
4 ✔
470
                }
471
            }
472

473
            $result[] = $line;
4 ✔
474

475
            $depth += substr_count($line, '{') - substr_count($line, '}');
4 ✔
476
        }
477

478
        return implode("\n", $result);
4 ✔
479
    }
480

481
    /**
482
     * @return array<string, AstNode>
483
     */
484
    public function resolveForwardConfiguration(ForwardNode $node, Environment $env): array
485
    {
486
        if ($node->configuration === []) {
8 ✔
487
            return [];
7 ✔
488
        }
489

490
        $resolved = [];
1 ✔
491

492
        foreach ($node->configuration as $name => $entry) {
1 ✔
493
            $valueNode    = $entry['value'];
1 ✔
494
            $isDefault    = $entry['default'];
1 ✔
495
            $currentValue = $isDefault ? $env->getCurrentScope()->getAstVariable($name) : null;
1 ✔
496

497
            if ($currentValue !== null) {
1 ✔
498
                $resolved[$name] = $currentValue;
1 ✔
499

500
                continue;
1 ✔
501
            }
502

503
            $resolved[$name] = $this->evaluation->evaluateValue($valueNode, $env);
1 ✔
504
        }
505

506
        return $resolved;
1 ✔
507
    }
508

509
    /**
510
     * @param array<string, AstNode> $resolvedConfiguration
511
     * @return array<string, AstNode>
512
     */
513
    public function resolveImportForwardConfiguration(
514
        ForwardNode $node,
515
        Environment $env,
516
        array $resolvedConfiguration,
517
    ): array {
518
        $prefix = $node->prefix ?? '';
3 ✔
519

520
        if ($prefix === '') {
3 ✔
521
            return $resolvedConfiguration;
1 ✔
522
        }
523

524
        foreach (array_keys($env->getCurrentScope()->getVariables()) as $name) {
2 ✔
525
            if (! str_starts_with($name, $prefix)) {
2 ✔
526
                continue;
1 ✔
527
            }
528

529
            $forwardedName = substr($name, strlen($prefix));
2 ✔
530

531
            if ($forwardedName === '' || array_key_exists($forwardedName, $resolvedConfiguration)) {
2 ✔
532
                continue;
1 ✔
533
            }
534

535
            $value = $env->getCurrentScope()->getAstVariable($name);
2 ✔
536

537
            if ($value !== null) {
2 ✔
538
                $resolvedConfiguration[$forwardedName] = $value;
2 ✔
539
            }
540
        }
541

542
        return $resolvedConfiguration;
2 ✔
543
    }
544

545
    /**
546
     * @param array<string, AstNode> $configuration
547
     */
548
    public function forwardCacheKey(string $path, array $configuration, Environment $env): string
549
    {
550
        if ($configuration === []) {
8 ✔
551
            return $path;
6 ✔
552
        }
553

554
        $normalized = array_map(
2 ✔
555
            fn(AstNode $value): string => $this->evaluation->format($value, $env),
2 ✔
556
            $configuration,
2 ✔
557
        );
2 ✔
558

559
        ksort($normalized);
2 ✔
560

561
        return $path . "\0" . serialize($normalized);
2 ✔
562
    }
563

564
    public function importEvaluationDepth(): int
565
    {
566
        return $this->ctx->moduleState->importEvaluationDepth;
8 ✔
567
    }
568

569
    /**
570
     * @param array<int, string> $members
571
     */
572
    private function mergeScopeExports(
573
        Scope $from,
574
        Scope $to,
575
        ?string $prefix = null,
576
        ?string $visibility = null,
577
        array $members = [],
578
    ): void {
579
        $normalizedMembers = $this->normalizeForwardMembers($members);
11 ✔
580

581
        foreach (array_keys($from->getVariables()) as $name) {
11 ✔
582
            if (! $this->shouldForwardMember($name, true, $visibility, $normalizedMembers)) {
8 ✔
583
                continue;
1 ✔
584
            }
585

586
            $to->setVariableLocal($this->prefixExportName($name, $prefix), $from->getVariable($name));
7 ✔
587
        }
588

589
        foreach ($from->getMixins() as $name => $mixin) {
11 ✔
590
            if (! $this->shouldForwardMember($name, false, $visibility, $normalizedMembers)) {
6 ✔
591
                continue;
1 ✔
592
            }
593

594
            $to->setMixin(
5 ✔
595
                $this->prefixExportName($name, $prefix),
5 ✔
596
                $mixin,
5 ✔
597
            );
5 ✔
598
        }
599

600
        foreach ($from->getFunctions() as $name => $function) {
11 ✔
601
            if (! $this->shouldForwardMember($name, false, $visibility, $normalizedMembers)) {
5 ✔
UNCOV
602
                continue;
×
603
            }
604

605
            $to->setFunction(
5 ✔
606
                $this->prefixExportName($name, $prefix),
5 ✔
607
                $function,
5 ✔
608
            );
5 ✔
609
        }
610
    }
611

612
    private function prefixExportName(string $name, ?string $prefix): string
613
    {
614
        if ($prefix === null || $prefix === '') {
8 ✔
615
            return $name;
7 ✔
616
        }
617

618
        return $prefix . $name;
2 ✔
619
    }
620

621
    /**
622
     * @param array<int, string> $members
623
     * @return array{variables: array<int, string>, others: array<int, string>}
624
     */
625
    private function normalizeForwardMembers(array $members): array
626
    {
627
        $variables = [];
11 ✔
628
        $others    = [];
11 ✔
629

630
        foreach ($members as $member) {
11 ✔
631
            if ($member === '') {
2 ✔
UNCOV
632
                continue;
×
633
            }
634

635
            if ($member[0] === '$') {
2 ✔
636
                $variables[] = NameNormalizer::normalize(substr($member, 1));
2 ✔
637

638
                continue;
2 ✔
639
            }
640

641
            $others[] = NameNormalizer::normalize($member);
1 ✔
642
        }
643

644
        return [
11 ✔
645
            'variables' => array_unique($variables),
11 ✔
646
            'others'    => array_unique($others),
11 ✔
647
        ];
11 ✔
648
    }
649

650
    /**
651
     * @param array{variables: array<int, string>, others: array<int, string>} $members
652
     */
653
    private function shouldForwardMember(
654
        string $name,
655
        bool $isVariable,
656
        ?string $visibility,
657
        array $members,
658
    ): bool {
659
        if ($visibility !== 'show' && $visibility !== 'hide') {
8 ✔
660
            return true;
6 ✔
661
        }
662

663
        $normalizedName = NameNormalizer::normalize($name);
2 ✔
664
        $set            = $isVariable ? $members['variables'] : $members['others'];
2 ✔
665
        $contains       = in_array($normalizedName, $set, true);
2 ✔
666

667
        return $visibility === 'show' ? $contains : ! $contains;
2 ✔
668
    }
669

670
    private function isCssImportRaw(string $raw): bool
671
    {
672
        $lower = strtolower($raw);
19 ✔
673

674
        if (str_starts_with($lower, 'url(')) {
19 ✔
675
            return true;
2 ✔
676
        }
677

678
        if (str_starts_with($lower, 'http:' . '//') || str_starts_with($lower, 'https://')) {
18 ✔
679
            return true;
1 ✔
680
        }
681

682
        return false;
17 ✔
683
    }
684

685
    private function isCssImportPath(string $path): bool
686
    {
687
        $lower = strtolower(trim($path));
16 ✔
688

689
        if ($lower === '') {
16 ✔
690
            return false;
1 ✔
691
        }
692

693
        if (str_starts_with($lower, 'http:' . '//') || str_starts_with($lower, 'https://')) {
15 ✔
694
            return true;
1 ✔
695
        }
696

697
        return str_ends_with($lower, '.css');
15 ✔
698
    }
699

700
    /** @return array<string, true> */
701
    private function collectDefaultVariableNames(RootNode $ast): array
702
    {
703
        $defaults = [];
4 ✔
704

705
        foreach ($ast->children as $node) {
4 ✔
706
            if ($node instanceof VariableDeclarationNode && $node->default) {
3 ✔
707
                $defaults[NameNormalizer::normalize($node->name)] = true;
2 ✔
708
            }
709

710
            if ($node instanceof ForwardNode) {
3 ✔
711
                foreach ($node->configuration as $name => $entry) {
1 ✔
712
                    if ($entry['default']) {
1 ✔
713
                        $defaults[NameNormalizer::normalize($name)] = true;
1 ✔
714
                    }
715
                }
716
            }
717
        }
718

719
        return $defaults;
4 ✔
720
    }
721
}
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