Code quality and documentation

This commit is contained in:
Eric Lippmann 2021-12-06 14:59:36 +01:00
parent a775ae5f66
commit 5f46493148
3 changed files with 29 additions and 45 deletions

View File

@ -6,16 +6,21 @@ use Less_Tree_Call;
use Less_Tree_Color; use Less_Tree_Color;
use Less_Tree_Keyword; use Less_Tree_Keyword;
/**
* ColorProp renders Less colors as CSS var() function calls
*
* It extends {@link Less_Tree_Color} so that Less functions that take a Less_Tree_Color as an argument do not fail.
*/
class ColorProp extends Less_Tree_Color class ColorProp extends Less_Tree_Color
{ {
/** @var Less_Tree_Color */ /** @var Less_Tree_Color Color with which we created the ColorProp */
protected $color; protected $color;
/** @var int */ /** @var int */
protected $index; protected $index;
/** @var string */ /** @var string Color variable name */
protected $origin; protected $name;
public function __construct() public function __construct()
{ {
@ -24,11 +29,11 @@ class ColorProp extends Less_Tree_Color
/** /**
* @param Less_Tree_Color $color * @param Less_Tree_Color $color
* *
* @return self * @return static
*/ */
public static function fromColor(Less_Tree_Color $color) public static function fromColor(Less_Tree_Color $color)
{ {
$self = new self(); $self = new static();
$self->color = $color; $self->color = $color;
foreach ($color as $k => $v) { foreach ($color as $k => $v) {
@ -61,34 +66,30 @@ class ColorProp extends Less_Tree_Color
/** /**
* @return string * @return string
*/ */
public function getOrigin() public function getName()
{ {
return $this->origin; return $this->name;
} }
/** /**
* @param string $origin * @param string $name
* *
* @return $this * @return $this
*/ */
public function setOrigin($origin) public function setName($name)
{ {
$this->origin = $origin; $this->name = $name;
return $this; return $this;
} }
public function compile()
{
return $this;
}
public function genCSS($output) public function genCSS($output)
{ {
$css = (new Less_Tree_Call( $css = (new Less_Tree_Call(
'var', 'var',
[ [
new Less_Tree_Keyword('--' . $this->getOrigin()), new Less_Tree_Keyword('--' . $this->getName()),
// Use the Less_Tree_Color with which we created the ColorProp so that we don't get into genCSS() loops.
$this->color $this->color
], ],
$this->getIndex() $this->getIndex()

View File

@ -6,6 +6,9 @@ use Less_Tree;
use Less_Tree_Color; use Less_Tree_Color;
use Less_Tree_Variable; use Less_Tree_Variable;
/**
* Compile a Less variable to {@link ColorProp} if it is a color
*/
class ColorPropOrVariable extends Less_Tree class ColorPropOrVariable extends Less_Tree
{ {
public $type = 'Variable'; public $type = 'Variable';
@ -38,9 +41,9 @@ class ColorPropOrVariable extends Less_Tree
$v = $this->getVariable(); $v = $this->getVariable();
if ($v->name[1] === '@') { if ($v->name[1] === '@') {
// Evaluate variable variable as in Less_Tree_Variable:28 // Evaluate variable variable as in Less_Tree_Variable:28.
$vv = new Less_Tree_Variable(substr($v->name, 1), $v->index + 1, $v->currentFileInfo); $vv = new Less_Tree_Variable(substr($v->name, 1), $v->index + 1, $v->currentFileInfo);
// Overwrite the name so that the variable variable is not evaluated again // Overwrite the name so that the variable variable is not evaluated again.
$v->name = '@' . $vv->compile($env)->value; $v->name = '@' . $vv->compile($env)->value;
} }
@ -54,7 +57,7 @@ class ColorPropOrVariable extends Less_Tree
if ($compiled instanceof Less_Tree_Color) { if ($compiled instanceof Less_Tree_Color) {
return ColorProp::fromColor($compiled) return ColorProp::fromColor($compiled)
->setIndex($v->index) ->setIndex($v->index)
->setOrigin(substr($v->name, 1)); ->setName(substr($v->name, 1));
} }
return $compiled; return $compiled;

View File

@ -7,6 +7,9 @@ use LogicException;
/** /**
* Replace compiled Less colors with CSS var() function calls * Replace compiled Less colors with CSS var() function calls
*
* This basically works by replacing every visited Less variable with {@link ColorPropOrVariable},
* which is later compiled to {@link ColorProp} if it is a color.
*/ */
class Visitor extends Less_VisitorReplacing class Visitor extends Less_VisitorReplacing
{ {
@ -21,16 +24,6 @@ class Visitor extends Less_VisitorReplacing
*/ */
protected $callingVar = false; protected $callingVar = false;
/**
* Whether defining a mixin
*
* If that's the case, don't try to replace compiled Less colors with CSS var() function calls.
* This is handled explicitly.
*
* @var bool|string
*/
protected $definingMixin = false;
/** /**
* Whether defining a variable * Whether defining a variable
* *
@ -71,6 +64,7 @@ class Visitor extends Less_VisitorReplacing
public function visitMixinCall($c) public function visitMixinCall($c)
{ {
// Less_Tree_Mixin_Call::accept() does not visit arguments, but we have to replace them if necessary.
foreach ($c->arguments as $a) { foreach ($c->arguments as $a) {
$a['value'] = $this->visitObj($a['value']); $a['value'] = $this->visitObj($a['value']);
} }
@ -80,10 +74,7 @@ class Visitor extends Less_VisitorReplacing
public function visitMixinDefinition($m) public function visitMixinDefinition($m)
{ {
if ($this->definingMixin !== false) { // Less_Tree_Mixin_Definition::accept() does not visit params, but we have to replace them if necessary.
throw new LogicException('Already defining mixin');
}
foreach ($m->params as $p) { foreach ($m->params as $p) {
if (! isset($p['value'])) { if (! isset($p['value'])) {
continue; continue;
@ -92,20 +83,9 @@ class Visitor extends Less_VisitorReplacing
$p['value'] = $this->visitObj($p['value']); $p['value'] = $this->visitObj($p['value']);
} }
$m->rules = $this->visitArray($m->rules);
$this->definingMixin = spl_object_hash($m);
return $m; return $m;
} }
public function visitMixinDefinitionOut($m)
{
if ($this->definingMixin !== false && $this->definingMixin === spl_object_hash($m)) {
$this->definingMixin = false;
}
}
public function visitRule($r) public function visitRule($r)
{ {
if ($r->name[0] === '@' && $r->variable) { if ($r->name[0] === '@' && $r->variable) {
@ -128,7 +108,7 @@ class Visitor extends Less_VisitorReplacing
public function visitVariable($v) public function visitVariable($v)
{ {
if ($this->callingVar !== false || $this->definingMixin !== false || $this->definingVar !== false) { if ($this->callingVar !== false || $this->definingVar !== false) {
return $v; return $v;
} }