Skip to content

Commit 71720fa

Browse files
committed
Misc code cleanup
1 parent cca055c commit 71720fa

File tree

5 files changed

+74
-35
lines changed

5 files changed

+74
-35
lines changed

src/Block.php

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Block
6262
* @var array
6363
*/
6464
public $children;
65+
6566
/**
6667
* @var \Leafo\ScssPhp\Block
6768
*/

src/Compiler.php

+55-12
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ protected function matchExtends($selector, &$out, $from = 0, $initial = true)
466466

467467
for ($l = count($tempReplacement) - 1; $l >= 0; $l--) {
468468
$slice = [];
469+
469470
foreach ($tempReplacement[$l] as $chunk) {
470471
if (!in_array($chunk, $slice)) {
471472
$slice[] = $chunk;
@@ -610,7 +611,6 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin)
610611
return $found;
611612
}
612613

613-
614614
/**
615615
* Extract a relationship from the fragment.
616616
*
@@ -620,6 +620,7 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin)
620620
* the rest.
621621
*
622622
* @param array $fragment The selector fragment maybe ending with a direction relationship combinator.
623+
*
623624
* @return array The selector without the relationship fragment if any, the relationship fragment.
624625
*/
625626
protected function extractRelationshipFromFragment(array $fragment)
@@ -804,6 +805,7 @@ protected function compileAtRoot(Block $block)
804805
}
805806

806807
$selfParent = $block->selfParent;
808+
807809
if (! $block->selfParent->selectors && isset($block->parent) && $block->parent && isset($block->parent->selectors) && $block->parent->selectors) {
808810
$selfParent = $block->parent;
809811
}
@@ -825,8 +827,10 @@ protected function compileAtRoot(Block $block)
825827

826828
/**
827829
* Filter at-root scope depending of with/without option
828-
* @param $scope
829-
* @param $without
830+
*
831+
* @param \Leafo\ScssPhp\Formatter\OutputBlock $scope
832+
* @param mixed $without
833+
*
830834
* @return mixed
831835
*/
832836
protected function filterScopeWithout($scope, $without)
@@ -846,14 +850,17 @@ protected function filterScopeWithout($scope, $without)
846850
if (! $scope) {
847851
break;
848852
}
853+
849854
if (! $this->isWithout($without, $scope)) {
850855
$s = clone $scope;
851856
$s->children = [];
852857
$s->lines = [];
853858
$s->parent = null;
859+
854860
if ($s->type !== Type::T_MEDIA && $s->type !== Type::T_DIRECTIVE) {
855861
$s->selectors = [];
856862
}
863+
857864
$filteredScopes[] = $s;
858865
}
859866

@@ -863,15 +870,18 @@ protected function filterScopeWithout($scope, $without)
863870
$scope = null;
864871
}
865872
}
873+
866874
if (!count($filteredScopes)) {
867875
return $this->rootBlock;
868876
}
869877

870878
$newScope = array_shift($filteredScopes);
871879
$newScope->parent = $this->rootBlock;
880+
872881
$this->rootBlock->children[] = $newScope;
873882

874883
$p = &$newScope;
884+
875885
while (count($filteredScopes)) {
876886
$s = array_shift($filteredScopes);
877887
$s->parent = $p;
@@ -885,15 +895,18 @@ protected function filterScopeWithout($scope, $without)
885895
/**
886896
* found missing selector from a at-root compilation in the previous scope
887897
* (if at-root is just enclosing a property, the selector is in the parent tree)
888-
* @param $scope
889-
* @param $previousScope
898+
*
899+
* @param \Leafo\ScssPhp\Formatter\OutputBlock $scope
900+
* @param \Leafo\ScssPhp\Formatter\OutputBlock $previousScope
901+
*
890902
* @return mixed
891903
*/
892904
protected function completeScope($scope, $previousScope)
893905
{
894906
if (! $scope->type && (! $scope->selectors || ! count($scope->selectors)) && count($scope->lines)) {
895907
$scope->selectors = $this->findScopeSelectors($previousScope, $scope->depth);
896908
}
909+
897910
if ($scope->children) {
898911
foreach ($scope->children as $k => $c) {
899912
$scope->children[$k] = $this->completeScope($c, $previousScope);
@@ -905,22 +918,26 @@ protected function completeScope($scope, $previousScope)
905918

906919
/**
907920
* Find a selector by the depth node in the scope
908-
* @param $scope
909-
* @param $depth
921+
*
922+
* @param \Leafo\ScssPhp\Formatter\OutputBlock $scope
923+
* @param integer $depth
924+
*
910925
* @return array
911926
*/
912927
protected function findScopeSelectors($scope, $depth)
913928
{
914929
if ($scope->depth === $depth && $scope->selectors) {
915930
return $scope->selectors;
916931
}
932+
917933
if ($scope->children) {
918934
foreach (array_reverse($scope->children) as $c) {
919935
if ($s = $this->findScopeSelectors($c, $depth)) {
920936
return $s;
921937
}
922938
}
923939
}
940+
924941
return [];
925942
}
926943

@@ -1016,11 +1033,13 @@ protected function isWithout($without, $block)
10161033
if (isset($block->name) && $block->name === 'supports') {
10171034
return ($without & static::WITH_SUPPORTS) ? true : false;
10181035
}
1036+
10191037
if (isset($block->selectors) && strpos(serialize($block->selectors), '@supports') !== false) {
10201038
return ($without & static::WITH_SUPPORTS) ? true : false;
10211039
}
10221040
}
10231041
}
1042+
10241043
if ((($without & static::WITH_RULE) && isset($block->selectors))) {
10251044
return true;
10261045
}
@@ -1147,13 +1166,17 @@ protected function compileBlock(Block $block)
11471166

11481167
if (count($block->children)) {
11491168
$out->selectors = $this->multiplySelectors($env, $block->selfParent);
1169+
11501170
// propagate selfParent to the children where they still can be useful
11511171
$selfParentSelectors = null;
1172+
11521173
if (isset($block->selfParent->selectors)) {
11531174
$selfParentSelectors = $block->selfParent->selectors;
11541175
$block->selfParent->selectors = $out->selectors;
11551176
}
1177+
11561178
$this->compileChildrenNoReturn($block->children, $out, $block->selfParent);
1179+
11571180
// and revert for the following childs of the same block
11581181
if ($selfParentSelectors) {
11591182
$block->selfParent->selectors = $selfParentSelectors;
@@ -1174,6 +1197,7 @@ protected function compileComment($block)
11741197
{
11751198
$out = $this->makeOutputBlock(Type::T_COMMENT);
11761199
$out->lines[] = $block[1];
1200+
11771201
$this->scope->children[] = $out;
11781202
}
11791203

@@ -1406,6 +1430,7 @@ protected function compileChildren($stms, OutputBlock $out)
14061430
return $ret;
14071431
}
14081432
}
1433+
14091434
return null;
14101435
}
14111436

@@ -1420,14 +1445,12 @@ protected function compileChildren($stms, OutputBlock $out)
14201445
*/
14211446
protected function compileChildrenNoReturn($stms, OutputBlock $out, $selfParent = null)
14221447
{
1423-
14241448
foreach ($stms as $stm) {
14251449
if ($selfParent && isset($stm[1]) && is_object($stm[1]) && get_class($stm[1]) == 'Leafo\ScssPhp\Block') {
14261450
$stm[1]->selfParent = $selfParent;
14271451
$ret = $this->compileChild($stm, $out);
14281452
$stm[1]->selfParent = null;
1429-
}
1430-
elseif ($selfParent && $stm[0] === TYPE::T_INCLUDE) {
1453+
} elseif ($selfParent && $stm[0] === TYPE::T_INCLUDE) {
14311454
$stm['selfParent'] = $selfParent;
14321455
$ret = $this->compileChild($stm, $out);
14331456
unset($stm['selfParent']);
@@ -1681,6 +1704,7 @@ protected function compileChild($child, OutputBlock $out)
16811704
} elseif (! empty($out->sourceLine) and ! empty($out->sourceName)) {
16821705
$this->sourceLine = $out->sourceLine;
16831706
$this->sourceIndex = array_search($out->sourceName, $this->sourceNames);
1707+
16841708
if ($this->sourceIndex === false) {
16851709
$this->sourceIndex = null;
16861710
}
@@ -1761,6 +1785,7 @@ protected function compileChild($child, OutputBlock $out)
17611785
}
17621786

17631787
$fontValue=&$value;
1788+
17641789
if ($value[0] === Type::T_LIST && $value[1]==',') {
17651790
// this is the case if more than one font is given: example: "font: 400 1em/1.3 arial,helvetica"
17661791
// we need to handle the first list element
@@ -1992,14 +2017,16 @@ protected function compileChild($child, OutputBlock $out)
19922017
// Find the parent selectors in the env to be able to know what '&' refers to in the mixin
19932018
// and assign this fake parent to childs
19942019
$selfParent = null;
2020+
19952021
if (isset($child['selfParent']) && isset($child['selfParent']->selectors)) {
19962022
$selfParent = $child['selfParent'];
1997-
}
1998-
else {
2023+
} else {
19992024
$parentSelectors = $this->multiplySelectors($this->env);
2025+
20002026
if ($parentSelectors) {
20012027
$parent = new Block();
20022028
$parent->selectors = $parentSelectors;
2029+
20032030
foreach ($mixin->children as $k => $child) {
20042031
if (isset($child[1]) && is_object($child[1]) && get_class($child[1]) == 'Leafo\ScssPhp\Block') {
20052032
$mixin->children[$k][1]->parent = $parent;
@@ -2344,6 +2371,7 @@ protected function reduce($value, $inExp = false)
23442371
case Type::T_SELF:
23452372
$selfSelector = $this->multiplySelectors($this->env);
23462373
$selfSelector = $this->collapseSelectors($selfSelector);
2374+
23472375
return [Type::T_STRING, '', [$selfSelector]];
23482376

23492377
default:
@@ -2532,6 +2560,7 @@ protected function opAdd($left, $right)
25322560

25332561
return $strRight;
25342562
}
2563+
25352564
return null;
25362565
}
25372566

@@ -3049,6 +3078,7 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
30493078
$parentSelectors = [[]];
30503079

30513080
$selfParentSelectors = null;
3081+
30523082
if (!is_null($selfParent) and $selfParent->selectors) {
30533083
$selfParentSelectors = $this->evalSelectors($selfParent->selectors);
30543084
}
@@ -3064,12 +3094,15 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
30643094
foreach ($parentSelectors as $parent) {
30653095
if ($selfParentSelectors) {
30663096
$previous = null;
3097+
30673098
foreach ($selfParentSelectors as $selfParent) {
30683099
// if no '&' in the selector, each call will give same result, only add once
30693100
$s = $this->joinSelectors($parent, $selector, $selfParent);
3101+
30703102
if ($s !== $previous) {
30713103
$selectors[serialize($s)] = $s;
30723104
}
3105+
30733106
$previous = $s;
30743107
}
30753108
} else {
@@ -3083,6 +3116,7 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
30833116
}
30843117

30853118
$selectors = array_values($selectors);
3119+
30863120
return $selectors;
30873121
}
30883122

@@ -3092,6 +3126,7 @@ protected function multiplySelectors(Environment $env, $selfParent = null)
30923126
* @param array $parent
30933127
* @param array $child
30943128
* @param array $selfParentSelectors
3129+
30953130
* @return array
30963131
*/
30973132
protected function joinSelectors($parent, $child, $selfParentSelectors = null)
@@ -3105,9 +3140,11 @@ protected function joinSelectors($parent, $child, $selfParentSelectors = null)
31053140
foreach ($part as $p) {
31063141
if ($p === static::$selfSelector) {
31073142
$setSelf = true;
3143+
31083144
if (is_null($selfParentSelectors)) {
31093145
$selfParentSelectors = $parent;
31103146
}
3147+
31113148
foreach ($selfParentSelectors as $i => $parentPart) {
31123149
if ($i > 0) {
31133150
$out[] = $newPart;
@@ -3305,6 +3342,7 @@ protected function setExisting($name, $value, Environment $env, $valueUnreduced
33053342
}
33063343

33073344
$env->store[$name] = $value;
3345+
33083346
if ($valueUnreduced) {
33093347
$env->storeUnreduced[$name] = $valueUnreduced;
33103348
}
@@ -3321,6 +3359,7 @@ protected function setExisting($name, $value, Environment $env, $valueUnreduced
33213359
protected function setRaw($name, $value, Environment $env, $valueUnreduced = null)
33223360
{
33233361
$env->store[$name] = $value;
3362+
33243363
if ($valueUnreduced) {
33253364
$env->storeUnreduced[$name] = $valueUnreduced;
33263365
}
@@ -3355,6 +3394,7 @@ public function get($name, $shouldThrow = true, Environment $env = null, $unredu
33553394
if ($unreduced && isset($env->storeUnreduced[$normalizedName])) {
33563395
return $env->storeUnreduced[$normalizedName];
33573396
}
3397+
33583398
return $env->store[$normalizedName];
33593399
}
33603400

@@ -3712,6 +3752,7 @@ public function setEncoding($encoding)
37123752
public function setIgnoreErrors($ignoreErrors)
37133753
{
37143754
$this->ignoreErrors = $ignoreErrors;
3755+
37153756
return $this;
37163757
}
37173758

@@ -3737,9 +3778,11 @@ public function throwError($msg)
37373778
$line = $this->sourceLine;
37383779
$loc = isset($this->sourceNames[$this->sourceIndex]) ? $this->sourceNames[$this->sourceIndex] . " on line $line" : "line: $line";
37393780
$msg = "$msg: $loc";
3781+
37403782
if ($this->callStack) {
37413783
$msg .= "\nCall Stack:\n";
37423784
$ncall = 0;
3785+
37433786
foreach (array_reverse($this->callStack) as $call) {
37443787
$msg .= "#" . $ncall++ . " " . $call['n'] . " ";
37453788
$msg .= (isset($this->sourceNames[$call[Parser::SOURCE_INDEX]]) ? $this->sourceNames[$call[Parser::SOURCE_INDEX]] : '(unknown file)');

0 commit comments

Comments
 (0)