• 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.68
/src/Handlers/ModuleNodeHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Bugo\SCSS\Handlers;
6

7
use Bugo\SCSS\Nodes\ForwardNode;
8
use Bugo\SCSS\Nodes\ImportNode;
9
use Bugo\SCSS\Nodes\UseNode;
10
use Bugo\SCSS\Runtime\TraversalContext;
11
use Bugo\SCSS\Services\Evaluator;
12
use Bugo\SCSS\Services\Module;
13
use Bugo\SCSS\Services\Render;
14
use Bugo\SCSS\Services\Selector;
15

16
use function count;
17
use function str_contains;
18
use function str_starts_with;
19
use function strlen;
20

21
final readonly class ModuleNodeHandler
22
{
23
    public function __construct(
24
        private Evaluator $evaluation,
25
        private Module $module,
26
        private Render $render,
27
        private Selector $selector
28
    ) {}
240 ✔
29

30
    public function handleForward(ForwardNode $node, TraversalContext $ctx): string
31
    {
32
        $path                  = $node->path;
7 ✔
33
        $resolvedConfiguration = $this->module->resolveForwardConfiguration($node, $ctx->env);
7 ✔
34
        $moduleState           = $this->module->moduleState();
7 ✔
35

36
        if ($this->module->importEvaluationDepth() > 0) {
7 ✔
37
            $resolvedConfiguration = $this->module->resolveImportForwardConfiguration(
1 ✔
38
                $node,
1 ✔
39
                $ctx->env,
1 ✔
40
                $resolvedConfiguration
1 ✔
41
            );
1 ✔
42
        }
43

44
        $forwardKey = $this->module->forwardCacheKey($path, $resolvedConfiguration, $ctx->env);
7 ✔
45

46
        if (! isset($moduleState->forwardedModules[$forwardKey])) {
7 ✔
47
            $moduleState->forwardedModules[$forwardKey] = $this->module->loadAndEvaluateModule(
1 ✔
48
                $path,
1 ✔
49
                $resolvedConfiguration
1 ✔
50
            );
1 ✔
51
        }
52

53
        if (isset($moduleState->emittedForwardCss[$forwardKey])) {
7 ✔
NEW
54
            return '';
×
55
        }
56

57
        $moduleState->emittedForwardCss[$forwardKey] = true;
7 ✔
58

59
        return $moduleState->forwardedModules[$forwardKey]['css'];
7 ✔
60
    }
61

62
    public function handleImport(ImportNode $node, TraversalContext $ctx): string
63
    {
64
        $output          = '';
9 ✔
65
        $prefix          = $this->render->indentPrefix($ctx->indent);
9 ✔
66
        $endsWithNewline = false;
9 ✔
67
        $outputState     = $this->render->outputState();
9 ✔
68

69
        foreach ($node->imports as $import) {
9 ✔
70
            $resolvedImport = $this->module->resolveImport($import);
9 ✔
71

72
            if ($resolvedImport['type'] === 'css') {
9 ✔
73
                /** @var array{type: 'css', raw: string} $resolvedImport */
74
                $rawImport = $resolvedImport['raw'];
2 ✔
75

76
                if (str_contains($rawImport, '#{')) {
2 ✔
77
                    $rawImport = $this->evaluation->interpolateText($rawImport, $ctx->env);
1 ✔
78
                }
79

80
                $line = '@import ' . $rawImport . ';';
2 ✔
81

82
                if ($output !== '' && ! $endsWithNewline) {
2 ✔
NEW
83
                    $output .= "\n";
×
84
                }
85

86
                $output .= $prefix . $line;
2 ✔
87
                $endsWithNewline = false;
2 ✔
88

89
                continue;
2 ✔
90
            }
91

92
            /** @var array{type: 'sass', path: string} $resolvedImport */
93
            $path = $resolvedImport['path'];
7 ✔
94

95
            if ($path === '') {
7 ✔
NEW
96
                continue;
×
97
            }
98

99
            $data = $this->module->loadAndEvaluateModule(
7 ✔
100
                $path,
7 ✔
101
                [],
7 ✔
102
                true,
7 ✔
103
                true,
7 ✔
104
                $this->module->extractAstVariables($ctx->env->getCurrentScope()->getVariables())
7 ✔
105
            );
7 ✔
106

107
            $css = $data['css'];
7 ✔
108

109
            if ($css === '') {
7 ✔
110
                continue;
1 ✔
111
            }
112

113
            $parentSelector = $this->selector->getCurrentParentSelector($ctx->env);
6 ✔
114

115
            if ($parentSelector !== null && $parentSelector !== '') {
6 ✔
116
                $qualifiedCss = $this->module->qualifyImportedCssWithParentSelector($css, $parentSelector);
1 ✔
117
                $stackIndex   = count($outputState->deferredAtRootStack) - 1;
1 ✔
118

119
                if ($stackIndex >= 0) {
1 ✔
120
                    $outputState->deferredAtRootStack[$stackIndex][] = $this->render->trimTrailingNewlines(
1 ✔
121
                        $qualifiedCss
1 ✔
122
                    );
1 ✔
123

124
                    continue;
1 ✔
125
                }
126

NEW
127
                $css = $qualifiedCss;
×
128
            }
129

130
            if ($output !== '' && ! $endsWithNewline) {
5 ✔
NEW
131
                $output .= "\n";
×
132
            }
133

134
            $indented = $this->render->indentLines($css, $prefix);
5 ✔
135
            $output  .= $indented;
5 ✔
136

137
            $indentedLength  = strlen($indented);
5 ✔
138
            $endsWithNewline = $indentedLength > 0 && $indented[$indentedLength - 1] === "\n";
5 ✔
139
        }
140

141
        return $output;
9 ✔
142
    }
143

144
    public function handleUse(UseNode $node): string
145
    {
146
        if (str_starts_with($node->path, 'sass:')) {
232 ✔
147
            return '';
209 ✔
148
        }
149

150
        $namespace = $node->namespace;
28 ✔
151

152
        if ($namespace === null) {
28 ✔
153
            $namespace = $this->module->deriveNamespaceFromUsePath($node->path);
16 ✔
154
        }
155

156
        if ($namespace === '*') {
28 ✔
157
            return '';
3 ✔
158
        }
159

160
        $moduleState = $this->module->moduleState();
25 ✔
161

162
        if (! isset($moduleState->loadedModules[$namespace])) {
25 ✔
NEW
163
            return '';
×
164
        }
165

166
        $moduleData = $moduleState->loadedModules[$namespace];
25 ✔
167
        $moduleId   = $moduleData['id'];
25 ✔
168

169
        if (isset($moduleState->emittedUseCss[$moduleId])) {
25 ✔
170
            return '';
4 ✔
171
        }
172

173
        $moduleState->emittedUseCss[$moduleId] = true;
25 ✔
174

175
        return $moduleData['css'];
25 ✔
176
    }
177
}
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