• 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

86.13
/src/Services/UserFunctionExecutor.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Bugo\SCSS\Services;
6

7
use Bugo\SCSS\Exceptions\FunctionReturnValueException;
8
use Bugo\SCSS\Exceptions\MaxIterationsExceededException;
9
use Bugo\SCSS\Exceptions\MissingFunctionArgumentsException;
10
use Bugo\SCSS\Nodes\ArgumentListNode;
11
use Bugo\SCSS\Nodes\ArgumentNode;
12
use Bugo\SCSS\Nodes\AstNode;
13
use Bugo\SCSS\Nodes\DebugNode;
14
use Bugo\SCSS\Nodes\EachNode;
15
use Bugo\SCSS\Nodes\ErrorNode;
16
use Bugo\SCSS\Nodes\ForNode;
17
use Bugo\SCSS\Nodes\IfNode;
18
use Bugo\SCSS\Nodes\NumberNode;
19
use Bugo\SCSS\Nodes\ReturnNode;
20
use Bugo\SCSS\Nodes\WarnNode;
21
use Bugo\SCSS\Nodes\WhileNode;
22
use Bugo\SCSS\Runtime\CallableDefinition;
23
use Bugo\SCSS\Runtime\Environment;
24
use Bugo\SCSS\Runtime\Scope;
25
use Closure;
26

27
use function array_filter;
28
use function array_slice;
29

30
use const ARRAY_FILTER_USE_KEY;
31

32
final readonly class UserFunctionExecutor
33
{
34
    /**
35
     * @param Closure(AstNode, Environment): AstNode $evaluateValue
36
     * @param Closure(AstNode, Environment): bool $applyVariableDeclaration
37
     * @param Closure(AstNode): array<int, AstNode> $eachIterableItems
38
     * @param Closure(array<int, string>, AstNode, Environment): void $assignEachVariables
39
     * @param Closure(AstNode, Environment): AstNode $evaluateValueWithSlashDivision
40
     * @param Closure(string, AstNode, Environment, AstNode|null): void $handleDiagnosticDirective
41
     */
42
    public function __construct(
43
        private Condition $condition,
44
        private Closure $evaluateValue,
45
        private Closure $applyVariableDeclaration,
46
        private Closure $eachIterableItems,
47
        private Closure $assignEachVariables,
48
        private Closure $evaluateValueWithSlashDivision,
49
        private Closure $handleDiagnosticDirective
50
    ) {}
631 ✔
51

52
    /**
53
     * @param array<int, AstNode> $positional
54
     * @param array<string, AstNode> $named
55
     */
56
    public function executeDefinition(
57
        string $name,
58
        CallableDefinition $function,
59
        array $positional,
60
        array $named,
61
        Environment $env
62
    ): AstNode {
63
        $env->enterScope($function->closureScope);
37 ✔
64

65
        $currentScope = $env->getCurrentScope();
37 ✔
66

67
        try {
68
            $this->bindArguments(
37 ✔
69
                $function->arguments,
37 ✔
70
                $positional,
37 ✔
71
                $named,
37 ✔
72
                $currentScope,
37 ✔
73
                function (string $argName, ?AstNode $defaultValue) use ($currentScope, $env, $name): void {
37 ✔
74
                    if ($defaultValue !== null) {
5 ✔
75
                        $currentScope->setVariableLocal($argName, ($this->evaluateValue)($defaultValue, $env));
4 ✔
76

77
                        return;
4 ✔
78
                    }
79

80
                    throw MissingFunctionArgumentsException::required($name, $argName);
1 ✔
81
                }
37 ✔
82
            );
37 ✔
83

84
            $result = $this->runStatements($function->body, $env);
36 ✔
85

86
            if ($result !== null) {
36 ✔
87
                return $result;
35 ✔
88
            }
89
        } finally {
90
            $env->exitScope();
37 ✔
91
        }
92

93
        throw new FunctionReturnValueException($name);
1 ✔
94
    }
95

96
    /**
97
     * @param array<int, ArgumentNode> $parameters
98
     * @param array<int, AstNode> $resolvedPositional
99
     * @param array<string, AstNode> $resolvedNamed
100
     */
101
    public function bindParametersToCurrentScope(
102
        array $parameters,
103
        array $resolvedPositional,
104
        array $resolvedNamed,
105
        Scope $scope
106
    ): void {
107
        $this->bindArguments(
37 ✔
108
            $parameters,
37 ✔
109
            $resolvedPositional,
37 ✔
110
            $resolvedNamed,
37 ✔
111
            $scope,
37 ✔
112
            static function (string $name, ?AstNode $defaultValue) use ($scope): void {
37 ✔
113
                if ($defaultValue !== null) {
2 ✔
114
                    $scope->setVariableLocal($name, $defaultValue);
2 ✔
115
                }
116
            }
37 ✔
117
        );
37 ✔
118
    }
119

120
    /**
121
     * @param array<int, ArgumentNode> $parameters
122
     * @param array<int, AstNode> $resolvedPositional
123
     * @param array<string, AstNode> $resolvedNamed
124
     */
125
    private function bindArguments(
126
        array $parameters,
127
        array $resolvedPositional,
128
        array $resolvedNamed,
129
        Scope $scope,
130
        callable $resolveDefault
131
    ): void {
132
        $parameterNameSet = null;
67 ✔
133

134
        foreach ($parameters as $index => $parameter) {
67 ✔
135
            $parameterName = $parameter->name;
46 ✔
136

137
            if (! $parameter->rest && isset($resolvedNamed[$parameterName])) {
46 ✔
138
                $scope->setVariableLocal($parameterName, $resolvedNamed[$parameterName]);
5 ✔
139

140
                continue;
5 ✔
141
            }
142

143
            if (! $parameter->rest && isset($resolvedPositional[$index])) {
46 ✔
144
                $scope->setVariableLocal($parameterName, $resolvedPositional[$index]);
40 ✔
145

146
                continue;
40 ✔
147
            }
148

149
            if ($parameter->rest) {
14 ✔
150
                if ($parameterNameSet === null) {
7 ✔
151
                    $parameterNameSet = $this->buildParameterNameSet($parameters);
7 ✔
152
                }
153

154
                $scope->setVariableLocal(
7 ✔
155
                    $parameterName,
7 ✔
156
                    new ArgumentListNode(
7 ✔
157
                        array_slice($resolvedPositional, $index),
7 ✔
158
                        'comma',
7 ✔
159
                        false,
7 ✔
160
                        array_filter(
7 ✔
161
                            $resolvedNamed,
7 ✔
162
                            fn(string $name): bool => ! isset($parameterNameSet[$name]),
7 ✔
163
                            ARRAY_FILTER_USE_KEY
7 ✔
164
                        )
7 ✔
165
                    )
7 ✔
166
                );
7 ✔
167

168
                continue;
7 ✔
169
            }
170

171
            $resolveDefault($parameterName, $parameter->defaultValue);
7 ✔
172
        }
173
    }
174

175
    /**
176
     * @param array<int, AstNode> $statements
177
     */
178
    private function runStatements(array $statements, Environment $env): ?AstNode
179
    {
180
        foreach ($statements as $statement) {
36 ✔
181
            if (($this->applyVariableDeclaration)($statement, $env)) {
36 ✔
182
                continue;
9 ✔
183
            }
184

185
            if ($statement instanceof EachNode) {
35 ✔
186
                $iterableValue = ($this->evaluateValue)($statement->list, $env);
4 ✔
187
                $items         = ($this->eachIterableItems)($iterableValue);
4 ✔
188

189
                foreach ($items as $item) {
4 ✔
190
                    ($this->assignEachVariables)($statement->variables, $item, $env);
4 ✔
191

192
                    $result = $this->runStatements($statement->body, $env);
4 ✔
193

194
                    if ($result !== null) {
4 ✔
NEW
195
                        return $result;
×
196
                    }
197
                }
198

199
                continue;
4 ✔
200
            }
201

202
            if ($statement instanceof ForNode) {
35 ✔
203
                $from = $this->loopBoundary($statement->from, $env);
1 ✔
204
                $to   = $this->loopBoundary($statement->to, $env);
1 ✔
205

206
                if (! $statement->inclusive) {
1 ✔
NEW
207
                    $to += $from <= $to ? -1 : 1;
×
208
                }
209

210
                $step          = $from <= $to ? 1 : -1;
1 ✔
211
                $iterations    = 0;
1 ✔
212
                $maxIterations = 10000;
1 ✔
213

214
                for ($i = $from; $step > 0 ? $i <= $to : $i >= $to; $i += $step) {
1 ✔
215
                    $iterations++;
1 ✔
216

217
                    if ($iterations > $maxIterations) {
1 ✔
NEW
218
                        throw new MaxIterationsExceededException('@for');
×
219
                    }
220

221
                    $env->getCurrentScope()->setVariable($statement->variable, new NumberNode($i));
1 ✔
222

223
                    $result = $this->runStatements($statement->body, $env);
1 ✔
224

225
                    if ($result !== null) {
1 ✔
NEW
226
                        return $result;
×
227
                    }
228
                }
229

230
                continue;
1 ✔
231
            }
232

233
            if ($statement instanceof ReturnNode) {
35 ✔
234
                return ($this->evaluateValueWithSlashDivision)($statement->value, $env);
35 ✔
235
            }
236

237
            if ($statement instanceof WhileNode) {
7 ✔
238
                $iterations    = 0;
1 ✔
239
                $maxIterations = 10000;
1 ✔
240

241
                while ($this->condition->evaluate($statement->condition, $env)) {
1 ✔
242
                    $iterations++;
1 ✔
243

244
                    if ($iterations > $maxIterations) {
1 ✔
NEW
245
                        throw new MaxIterationsExceededException('@while');
×
246
                    }
247

248
                    $result = $this->runStatements($statement->body, $env);
1 ✔
249

250
                    if ($result !== null) {
1 ✔
NEW
251
                        return $result;
×
252
                    }
253
                }
254

255
                continue;
1 ✔
256
            }
257

258
            if ($statement instanceof DebugNode) {
6 ✔
259
                ($this->handleDiagnosticDirective)('debug', $statement->message, $env, $statement);
2 ✔
260

261
                continue;
2 ✔
262
            }
263

264
            if ($statement instanceof WarnNode) {
4 ✔
NEW
265
                ($this->handleDiagnosticDirective)('warn', $statement->message, $env, $statement);
×
266

NEW
267
                continue;
×
268
            }
269

270
            if ($statement instanceof ErrorNode) {
4 ✔
NEW
271
                ($this->handleDiagnosticDirective)('error', $statement->message, $env, $statement);
×
272
            }
273

274
            if ($statement instanceof IfNode) {
4 ✔
275
                if ($this->condition->evaluate($statement->condition, $env)) {
4 ✔
276
                    $result = $this->runStatements($statement->body, $env);
4 ✔
277

278
                    if ($result !== null) {
4 ✔
279
                        return $result;
3 ✔
280
                    }
281

282
                    continue;
1 ✔
283
                }
284

285
                $executedElseIf = false;
3 ✔
286

287
                foreach ($statement->elseIfBranches as $branch) {
3 ✔
NEW
288
                    $condition = $branch->condition;
×
NEW
289
                    $body      = $branch->body;
×
290

NEW
291
                    if ($this->condition->evaluate($condition, $env)) {
×
NEW
292
                        $executedElseIf = true;
×
293

NEW
294
                        $result = $this->runStatements($body, $env);
×
295

NEW
296
                        if ($result !== null) {
×
NEW
297
                            return $result;
×
298
                        }
299

NEW
300
                        break;
×
301
                    }
302
                }
303

304
                if ($executedElseIf) {
3 ✔
NEW
305
                    continue;
×
306
                }
307

308
                $result = $this->runStatements($statement->elseBody, $env);
3 ✔
309

310
                if ($result !== null) {
3 ✔
311
                    return $result;
1 ✔
312
                }
313
            }
314
        }
315

316
        return null;
8 ✔
317
    }
318

319
    /**
320
     * @param array<int, ArgumentNode> $parameters
321
     * @return array<string, true>
322
     */
323
    private function buildParameterNameSet(array $parameters): array
324
    {
325
        $names = [];
7 ✔
326

327
        foreach ($parameters as $parameter) {
7 ✔
328
            $names[$parameter->name] = true;
7 ✔
329
        }
330

331
        return $names;
7 ✔
332
    }
333

334
    private function loopBoundary(AstNode $node, Environment $env): int
335
    {
336
        $resolved = ($this->evaluateValue)($node, $env);
1 ✔
337

338
        if ($resolved instanceof NumberNode) {
1 ✔
339
            return (int) $resolved->value;
1 ✔
340
        }
341

NEW
342
        return 0;
×
343
    }
344
}
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