@@ -458,10 +458,33 @@ public function compile($code, $path = null)
458
458
}
459
459
460
460
/**
461
- * Compile scss
461
+ * Compiles the provided scss file into CSS.
462
+ *
463
+ * @param string $path
464
+ *
465
+ * @return CompilationResult
466
+ *
467
+ * @throws SassException when the source fails to compile
468
+ */
469
+ public function compileFile ($ path )
470
+ {
471
+ $ source = file_get_contents ($ path );
472
+
473
+ if ($ source === false ) {
474
+ throw new \RuntimeException ('Could not read the file content ' );
475
+ }
476
+
477
+ return $ this ->compileString ($ source , $ path );
478
+ }
479
+
480
+ /**
481
+ * Compiles the provided scss source code into CSS.
482
+ *
483
+ * If provided, the path is considered to be the path from which the source code comes
484
+ * from, which will be used to resolve relative imports.
462
485
*
463
486
* @param string $source
464
- * @param string|null $path
487
+ * @param string|null $path The path for the source, used to resolve relative imports
465
488
*
466
489
* @return CompilationResult
467
490
*
@@ -548,7 +571,7 @@ public function compileString($source, $path = null)
548
571
549
572
$ sourceMap = null ;
550
573
551
- if (! empty ($ out ) && $ this ->sourceMap && $ this -> sourceMap !== self ::SOURCE_MAP_NONE ) {
574
+ if (! empty ($ out ) && $ this ->sourceMap !== self ::SOURCE_MAP_NONE && $ this -> sourceMap ) {
552
575
assert ($ sourceMapGenerator !== null );
553
576
$ sourceMap = $ sourceMapGenerator ->generateJson ($ prefix );
554
577
$ sourceMapUrl = null ;
@@ -1508,6 +1531,7 @@ protected function filterScopeWithWithout($scope, $with, $without)
1508
1531
// start from the root
1509
1532
while ($ scope ->parent && $ scope ->parent ->type !== Type::T_ROOT ) {
1510
1533
array_unshift ($ childStash , $ scope );
1534
+ \assert ($ scope ->parent !== null );
1511
1535
$ scope = $ scope ->parent ;
1512
1536
}
1513
1537
@@ -2090,6 +2114,11 @@ protected function collapseSelectors($selectors)
2090
2114
foreach ($ selector as $ node ) {
2091
2115
$ compound = '' ;
2092
2116
2117
+ if (!is_array ($ node )) {
2118
+ $ output [] = $ node ;
2119
+ continue ;
2120
+ }
2121
+
2093
2122
array_walk_recursive (
2094
2123
$ node ,
2095
2124
function ($ value , $ key ) use (&$ compound ) {
@@ -2124,12 +2153,16 @@ private function collapseSelectorsAsList($selectors)
2124
2153
foreach ($ selector as $ node ) {
2125
2154
$ compound = '' ;
2126
2155
2127
- array_walk_recursive (
2128
- $ node ,
2129
- function ($ value , $ key ) use (&$ compound ) {
2130
- $ compound .= $ value ;
2131
- }
2132
- );
2156
+ if (!is_array ($ node )) {
2157
+ $ compound .= $ node ;
2158
+ } else {
2159
+ array_walk_recursive (
2160
+ $ node ,
2161
+ function ($ value , $ key ) use (&$ compound ) {
2162
+ $ compound .= $ value ;
2163
+ }
2164
+ );
2165
+ }
2133
2166
2134
2167
if ($ this ->isImmediateRelationshipCombinator ($ compound )) {
2135
2168
if (\count ($ output )) {
@@ -2885,7 +2918,7 @@ protected function compileChild($child, OutputBlock $out)
2885
2918
{
2886
2919
if (isset ($ child [Parser::SOURCE_LINE ])) {
2887
2920
$ this ->sourceIndex = isset ($ child [Parser::SOURCE_INDEX ]) ? $ child [Parser::SOURCE_INDEX ] : null ;
2888
- $ this ->sourceLine = isset ( $ child [Parser::SOURCE_LINE ]) ? $ child [Parser:: SOURCE_LINE ] : - 1 ;
2921
+ $ this ->sourceLine = $ child [Parser::SOURCE_LINE ];
2889
2922
$ this ->sourceColumn = isset ($ child [Parser::SOURCE_COLUMN ]) ? $ child [Parser::SOURCE_COLUMN ] : -1 ;
2890
2923
} elseif (\is_array ($ child ) && isset ($ child [1 ]->sourceLine ) && $ child [1 ] instanceof Block) {
2891
2924
$ this ->sourceIndex = $ child [1 ]->sourceIndex ;
@@ -4529,8 +4562,10 @@ public function compileValue($value, $quote = true)
4529
4562
return $ colorName ;
4530
4563
}
4531
4564
4532
- if (is_numeric ($ alpha )) {
4565
+ if (\is_int ( $ alpha ) || \is_float ($ alpha )) {
4533
4566
$ a = new Number ($ alpha , '' );
4567
+ } elseif (is_numeric ($ alpha )) {
4568
+ $ a = new Number ((float ) $ alpha , '' );
4534
4569
} else {
4535
4570
$ a = $ alpha ;
4536
4571
}
@@ -5806,13 +5841,13 @@ public function findImport($url, $currentDir = null)
5806
5841
5807
5842
if (! \is_null ($ file )) {
5808
5843
if (\is_array ($ dir )) {
5809
- $ callableDescription = (\is_object ($ dir [0 ]) ? \get_class ($ dir [0 ]) : $ dir [0 ]). ':: ' . $ dir [1 ];
5844
+ $ callableDescription = (\is_object ($ dir [0 ]) ? \get_class ($ dir [0 ]) : $ dir [0 ]) . ':: ' . $ dir [1 ];
5810
5845
} elseif ($ dir instanceof \Closure) {
5811
5846
$ r = new \ReflectionFunction ($ dir );
5812
5847
if (false !== strpos ($ r ->name , '{closure} ' )) {
5813
5848
$ callableDescription = sprintf ('closure{%s:%s} ' , $ r ->getFileName (), $ r ->getStartLine ());
5814
5849
} elseif ($ class = $ r ->getClosureScopeClass ()) {
5815
- $ callableDescription = $ class ->name . ':: ' . $ r ->name ;
5850
+ $ callableDescription = $ class ->name . ':: ' . $ r ->name ;
5816
5851
} else {
5817
5852
$ callableDescription = $ r ->name ;
5818
5853
}
@@ -5925,15 +5960,15 @@ private function checkImportPathConflicts(array $paths)
5925
5960
private function tryImportPathWithExtensions ($ path )
5926
5961
{
5927
5962
$ result = array_merge (
5928
- $ this ->tryImportPath ($ path. '.sass ' ),
5929
- $ this ->tryImportPath ($ path. '.scss ' )
5963
+ $ this ->tryImportPath ($ path . '.sass ' ),
5964
+ $ this ->tryImportPath ($ path . '.scss ' )
5930
5965
);
5931
5966
5932
5967
if ($ result ) {
5933
5968
return $ result ;
5934
5969
}
5935
5970
5936
- return $ this ->tryImportPath ($ path. '.css ' );
5971
+ return $ this ->tryImportPath ($ path . '.css ' );
5937
5972
}
5938
5973
5939
5974
/**
@@ -5943,7 +5978,7 @@ private function tryImportPathWithExtensions($path)
5943
5978
*/
5944
5979
private function tryImportPath ($ path )
5945
5980
{
5946
- $ partial = dirname ($ path ). '/_ ' . basename ($ path );
5981
+ $ partial = dirname ($ path ) . '/_ ' . basename ($ path );
5947
5982
5948
5983
$ candidates = [];
5949
5984
@@ -5969,7 +6004,7 @@ private function tryImportPathAsDirectory($path)
5969
6004
return null ;
5970
6005
}
5971
6006
5972
- return $ this ->checkImportPathConflicts ($ this ->tryImportPathWithExtensions ($ path. '/index ' ));
6007
+ return $ this ->checkImportPathConflicts ($ this ->tryImportPathWithExtensions ($ path . '/index ' ));
5973
6008
}
5974
6009
5975
6010
/**
@@ -5984,7 +6019,7 @@ private function getPrettyPath($path)
5984
6019
}
5985
6020
5986
6021
$ normalizedPath = $ path ;
5987
- $ normalizedRootDirectory = $ this ->rootDirectory . '/ ' ;
6022
+ $ normalizedRootDirectory = $ this ->rootDirectory . '/ ' ;
5988
6023
5989
6024
if (\DIRECTORY_SEPARATOR === '\\' ) {
5990
6025
$ normalizedRootDirectory = str_replace ('\\' , '/ ' , $ normalizedRootDirectory );
@@ -6371,8 +6406,6 @@ public static function isNativeFunction($name)
6371
6406
*/
6372
6407
protected function sortNativeFunctionArgs ($ functionName , $ prototypes , $ args )
6373
6408
{
6374
- static $ parser = null ;
6375
-
6376
6409
if (! isset ($ prototypes )) {
6377
6410
$ keyArgs = [];
6378
6411
$ posArgs = [];
@@ -6526,7 +6559,7 @@ private function parseFunctionPrototype(array $prototype)
6526
6559
*
6527
6560
* @return array
6528
6561
*
6529
- * @phpstan-param non-empty-list <array{arguments: list<array{0: string, 1: string, 2: array|Number|null}>, rest_argument: string|null}> $prototypes
6562
+ * @phpstan-param non-empty-array <array{arguments: list<array{0: string, 1: string, 2: array|Number|null}>, rest_argument: string|null}> $prototypes
6530
6563
* @phpstan-return array{arguments: list<array{0: string, 1: string, 2: array|Number|null}>, rest_argument: string|null}
6531
6564
*/
6532
6565
private function selectFunctionPrototype (array $ prototypes , $ positional , array $ names )
@@ -6984,10 +7017,14 @@ protected function coerceValue($value)
6984
7017
return static ::$ null ;
6985
7018
}
6986
7019
6987
- if (is_numeric ($ value )) {
7020
+ if (\is_int ( $ value ) || \is_float ($ value )) {
6988
7021
return new Number ($ value , '' );
6989
7022
}
6990
7023
7024
+ if (is_numeric ($ value )) {
7025
+ return new Number ((float ) $ value , '' );
7026
+ }
7027
+
6991
7028
if ($ value === '' ) {
6992
7029
return static ::$ emptyString ;
6993
7030
}
@@ -7675,9 +7712,9 @@ private function HWBtoRGB($hue, $whiteness, $blackness)
7675
7712
$ b = min (1.0 - $ w , $ b );
7676
7713
7677
7714
$ rgb = $ this ->toRGB ($ hue , 100 , 50 );
7678
- for ($ i = 1 ; $ i < 4 ; $ i ++) {
7679
- $ rgb [$ i ] *= (1.0 - $ w - $ b );
7680
- $ rgb [$ i ] = round ($ rgb [$ i ] + 255 * $ w + 0.0001 );
7715
+ for ($ i = 1 ; $ i < 4 ; $ i ++) {
7716
+ $ rgb [$ i ] *= (1.0 - $ w - $ b );
7717
+ $ rgb [$ i ] = round ($ rgb [$ i ] + 255 * $ w + 0.0001 );
7681
7718
}
7682
7719
7683
7720
return $ rgb ;
@@ -7704,7 +7741,6 @@ private function RGBtoHWB($red, $green, $blue)
7704
7741
if ((int ) $ d === 0 ) {
7705
7742
$ h = 0 ;
7706
7743
} else {
7707
-
7708
7744
if ($ red == $ max ) {
7709
7745
$ h = 60 * ($ green - $ blue ) / $ d ;
7710
7746
} elseif ($ green == $ max ) {
@@ -7714,7 +7750,7 @@ private function RGBtoHWB($red, $green, $blue)
7714
7750
}
7715
7751
}
7716
7752
7717
- return [Type::T_HWB , fmod ($ h , 360 ), $ min / 255 * 100 , 100 - $ max / 255 *100 ];
7753
+ return [Type::T_HWB , fmod ($ h , 360 ), $ min / 255 * 100 , 100 - $ max / 255 * 100 ];
7718
7754
}
7719
7755
7720
7756
@@ -7923,7 +7959,13 @@ protected function alterColor(array $args, $operation, $fn)
7923
7959
$ scale = $ operation === 'scale ' ;
7924
7960
$ change = $ operation === 'change ' ;
7925
7961
7926
- /** @phpstan-var callable(string, float|int, bool=, bool=): (float|int|null) $getParam */
7962
+ /**
7963
+ * @param string $name
7964
+ * @param float|int $max
7965
+ * @param bool $checkPercent
7966
+ * @param bool $assertPercent
7967
+ * @return float|int|null
7968
+ */
7927
7969
$ getParam = function ($ name , $ max , $ checkPercent = false , $ assertPercent = false ) use (&$ kwargs , $ scale , $ change ) {
7928
7970
if (!isset ($ kwargs [$ name ])) {
7929
7971
return null ;
@@ -8065,7 +8107,7 @@ protected function libAdjustColor($args)
8065
8107
protected static $ libChangeColor = ['color ' , 'kwargs... ' ];
8066
8108
protected function libChangeColor ($ args )
8067
8109
{
8068
- return $ this ->alterColor ($ args ,'change ' , function ($ base , $ alter , $ max ) {
8110
+ return $ this ->alterColor ($ args , 'change ' , function ($ base , $ alter , $ max ) {
8069
8111
if ($ alter === null ) {
8070
8112
return $ base ;
8071
8113
}
0 commit comments