Skip to content

Commit

Permalink
Merge pull request #711 from stof/implement_functions
Browse files Browse the repository at this point in the history
Implement global functions for the new evaluation system
  • Loading branch information
stof committed Apr 6, 2024
2 parents 030845a + 5159920 commit 227c612
Show file tree
Hide file tree
Showing 16 changed files with 2,397 additions and 1 deletion.
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Expand Up @@ -1520,6 +1520,16 @@ parameters:
count: 1
path: src/Formatter/OutputBlock.php

-
message: "#^Parameter \\#2 \\$overloads of static method ScssPhp\\\\ScssPhp\\\\SassCallable\\\\BuiltInCallable\\:\\:overloadedFunction\\(\\) expects array\\<string, callable\\(list\\<ScssPhp\\\\ScssPhp\\\\Value\\\\Value\\>\\)\\: ScssPhp\\\\ScssPhp\\\\Value\\\\Value\\>, 'sass\\:color'\\|'sass\\:list'\\|'sass\\:map'\\|'sass\\:math'\\|'sass\\:meta'\\|'sass\\:selector'\\|'sass\\:string'\\|non\\-empty\\-array\\<literal\\-string, array\\{'ScssPhp\\\\\\\\ScssPhp\\\\\\\\Function\\\\\\\\ColorFunctions', 'adjust'\\|'adjustHue'\\|'alpha'\\|'alphaMicrosoft'\\|'blue'\\|'change'\\|'complement'\\|'darken'\\|'desaturate'\\|'grayscale'\\|'green'\\|'hsl'\\|'hsla'\\|'hslaOneArgs'\\|'hslaTwoArgs'\\|'hslOneArgs'\\|'hslTwoArgs'\\|'hue'\\|'ieHexStr'\\|'invert'\\|'lighten'\\|'lightness'\\|'mix'\\|'opacify'\\|'opacity'\\|'red'\\|'rgb'\\|'rgba'\\|'rgbaOneArgs'\\|'rgbaTwoArgs'\\|'rgbOneArgs'\\|'rgbTwoArgs'\\|'saturate'\\|'saturateCss'\\|'saturation'\\|'scale'\\|'transparentize'\\}\\|array\\{'ScssPhp\\\\\\\\ScssPhp\\\\\\\\Function\\\\\\\\ListFunctions'\\|'ScssPhp\\\\\\\\ScssPhp\\\\\\\\Function\\\\\\\\SelectorFunctions'\\|'ScssPhp\\\\\\\\ScssPhp\\\\\\\\Function\\\\\\\\StringFunctions', 'append'\\|'extend'\\|'index'\\|'insert'\\|'isBracketed'\\|'isSuperselector'\\|'join'\\|'length'\\|'nest'\\|'nth'\\|'parse'\\|'quote'\\|'replace'\\|'separator'\\|'setNth'\\|'simpleSelectors'\\|'slice'\\|'toLowerCase'\\|'toUpperCase'\\|'unify'\\|'uniqueId'\\|'unquote'\\|'zip'\\}\\|array\\{'ScssPhp\\\\\\\\ScssPhp\\\\\\\\Function\\\\\\\\MapFunctions', 'get'\\|'hasKey'\\|'keys'\\|'mergeTwoArgs'\\|'mergeVariadic'\\|'remove'\\|'removeNoKeys'\\|'values'\\}\\|array\\{'ScssPhp\\\\\\\\ScssPhp\\\\\\\\Function\\\\\\\\MathFunctions', 'abs'\\|'ceil'\\|'compatible'\\|'floor'\\|'isUnitless'\\|'max'\\|'min'\\|'percentage'\\|'random'\\|'round'\\|'unit'\\}\\|array\\{'ScssPhp\\\\\\\\ScssPhp\\\\\\\\Function\\\\\\\\MetaFunctions', 'featureExists'\\|'inspect'\\|'typeof'\\}\\> given\\.$#"
count: 1
path: src/Function/FunctionRegistry.php

-
message: "#^Parameter \\#3 \\$url of static method ScssPhp\\\\ScssPhp\\\\SassCallable\\\\BuiltInCallable\\:\\:overloadedFunction\\(\\) expects string\\|null, array\\<string, array\\<int, string\\>\\>\\|string given\\.$#"
count: 1
path: src/Function/FunctionRegistry.php

-
message: "#^Method ScssPhp\\\\ScssPhp\\\\Importer\\\\FilesystemImporter\\:\\:load\\(\\) never returns null so it can be removed from the return type\\.$#"
count: 1
Expand Down
33 changes: 33 additions & 0 deletions src/Collection/Map.php
Expand Up @@ -102,6 +102,11 @@ public function get(Value $key)
return null;
}

public function containsKey(Value $key): bool
{
return $this->get($key) !== null;
}

/**
* Associates the key with the given value.
*
Expand Down Expand Up @@ -151,6 +156,34 @@ public function remove(Value $key)
return null;
}

/**
* @return list<Value>
*/
public function keys(): array
{
$keys = [];

foreach ($this->pairs as $pair) {
$keys[] = $pair[0];
}

return $keys;
}

/**
* @return list<T>
*/
public function values(): array
{
$values = [];

foreach ($this->pairs as $pair) {
$values[] = $pair[1];
}

return $values;
}

private function assertModifiable(): void
{
if (!$this->modifiable) {
Expand Down
80 changes: 80 additions & 0 deletions src/Evaluation/EvaluationContext.php
@@ -0,0 +1,80 @@
<?php

/**
* SCSSPHP
*
* @copyright 2012-2020 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://scssphp.github.io/scssphp
*/

namespace ScssPhp\ScssPhp\Evaluation;

use ScssPhp\ScssPhp\Deprecation;
use ScssPhp\ScssPhp\SourceSpan\FileSpan;

/**
* @internal
*/
abstract class EvaluationContext
{
private static ?EvaluationContext $evaluationContext = null;

/**
* The current evaluation context.
*
* @throws \LogicException if there isn't a Sass stylesheet currently being
* evaluated.
*/
public static function getCurrent(): EvaluationContext
{
if (self::$evaluationContext !== null) {
return self::$evaluationContext;
}

throw new \LogicException('No Sass stylesheet is currently being evaluated.');
}

/**
* Runs $callback with $context as {@see EvaluationContext::getCurrent()}.
*
* @template T
*
* @param callable(): T $callback
*
* @return T
*/
public static function withEvaluationContext(EvaluationContext $context, callable $callback)
{
$oldContext = self::$evaluationContext;
self::$evaluationContext = $context;

try {
return $callback();
} finally {
self::$evaluationContext = $oldContext;
}
}

/**
* Returns the span for the currently executing callable.
*
* For normal exception reporting, this should be avoided in favor of
* throwing {@see SassScriptException}s. It should only be used when calling APIs
* that require spans.
*
* @throws \LogicException if there isn't a callable being invoked.
*/
abstract public function getCurrentCallableSpan(): FileSpan;

/**
* Prints a warning message associated with the current `@import` or function
* call.
*
* If $deprecation is non-null`, the warning is emitted as a deprecation
* warning of that type.
*/
abstract public function warn(string $message, ?Deprecation $deprecation = null): void;
}

0 comments on commit 227c612

Please sign in to comment.