Skip to content

[CLEANUP] Add an interface for components of a property value #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Value/CSSFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* A `CSSFunction` represents a special kind of value that also contains a function name and where the values are the
* function’s arguments. It also handles equals-sign-separated argument lists like `filter: alpha(opacity=90);`.
*/
class CSSFunction extends ValueList
class CSSFunction extends ValueList implements Component
{
/**
* @var string
Expand All @@ -18,7 +18,7 @@ class CSSFunction extends ValueList

/**
* @param string $sName
* @param RuleValueList|array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aArguments
* @param RuleValueList|array<int, Component|string> $aArguments
* @param string $sSeparator
* @param int $iLineNo
*/
Expand Down Expand Up @@ -72,7 +72,7 @@ public function setName($sName)
}

/**
* @return array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string>
* @return array<int, Component|string>
*/
public function getArguments()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Value/CSSString.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* `CSSString`s always output with double quotes.
*/
class CSSString extends PrimitiveValue
class CSSString extends PrimitiveValue implements Component
{
/**
* @var string
Expand Down
8 changes: 4 additions & 4 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* `Color's can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are always stored as an array of
* ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form.
*/
class Color extends CSSFunction
class Color extends CSSFunction implements Component
{
/**
* @param array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aColor
* @param array<int, Component|string> $aColor
* @param int $iLineNo
*/
public function __construct(array $aColor, $iLineNo = 0)
Expand Down Expand Up @@ -125,15 +125,15 @@ private static function mapRange($fVal, $fFromMin, $fFromMax, $fToMin, $fToMax)
}

/**
* @return array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string>
* @return array<int, Component|string>
*/
public function getColor()
{
return $this->aComponents;
}

/**
* @param array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aColor
* @param array<int, Component|string> $aColor
*
* @return void
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Value/Component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Sabberworm\CSS\Value;

/**
* To be used by any class that represents a component of a CSS property value.
* Values often comprise 'component operator component' (recursively).
* This interface has no methods to implement;
* its purpose is abstract: to allow a unique type to be specified when a `Component` is an argument of a method.
*/
interface Component {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also should make sure that the name makes sense globally (as Mermaid on GitHub doesn't allow namespaces). Maybe CssPropertyValueComponent oder PropertyValueComponent?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also should make sure that the name makes sense globally (as Mermaid on GitHub doesn't allow namespaces). Maybe CssPropertyValueComponent oder PropertyValueComponent?

I don't think whatever limitations of Mermaid there may be should dictate class naming conventions. Mermaid might in future be enhanced to show the namespaces. We should choose the most apt names for classes, interfaces and namespaces independently of whatever other tools we use.

The FQN is Value\Component. The namespace Value could perhaps be PropertyValue (or Property\Value) for clarity, though any such rename would be beyond the scope of this PR, and probably unnecessary (it would be a breaking change).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should check if there are any common methods that should be part of the interface so the interface is more than just a marker.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should check if there are any common methods that should be part of the interface so the interface is more than just a marker.

Looking again, I don't see any common methods, but do see that all of these classes extend Value, which implements Renderable. A Value can be a simple value, or a complex value comprised of simple values connected with operators.

So perhaps a better approach would be to simply use the Value type whenever any of the various subclasses is expected, and not introduce a new type (or interface) at all.

4 changes: 2 additions & 2 deletions src/Value/LineName.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
use Sabberworm\CSS\Parsing\UnexpectedTokenException;

class LineName extends ValueList
class LineName extends ValueList implements Component
{
/**
* @param array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aComponents
* @param array<int, Component|string> $aComponents
* @param int $iLineNo
*/
public function __construct(array $aComponents = [], $iLineNo = 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Value/RuleValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* (where the value would be a whitespace-separated list of the primitive value `bold`, a slash-separated list
* and a comma-separated list).
*/
class RuleValueList extends ValueList
class RuleValueList extends ValueList implements Component
{
/**
* @param string $sSeparator
Expand Down
2 changes: 1 addition & 1 deletion src/Value/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* A `Size` consists of a numeric `size` value and a unit.
*/
class Size extends PrimitiveValue
class Size extends PrimitiveValue implements Component
{
/**
* vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
Expand Down
2 changes: 1 addition & 1 deletion src/Value/URL.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* This class represents URLs in CSS. `URL`s always output in `URL("")` notation.
*/
class URL extends PrimitiveValue
class URL extends PrimitiveValue implements Component
{
/**
* @var CSSString
Expand Down
4 changes: 2 additions & 2 deletions src/Value/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public function __construct($iLineNo = 0)
/**
* @param array<array-key, string> $aListDelimiters
*
* @return RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string
* @return Component|string
*
* @throws UnexpectedTokenException
* @throws UnexpectedEOFException
*/
public static function parseValue(ParserState $oParserState, array $aListDelimiters = [])
{
/** @var array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aStack */
/** @var array<int, Component|string> $aStack */
$aStack = [];
$oParserState->consumeWhiteSpace();
//Build a list of delimiters and parsed values
Expand Down
8 changes: 4 additions & 4 deletions src/Value/ValueList.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
abstract class ValueList extends Value
{
/**
* @var array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string>
* @var array<int, Component|string>
*/
protected $aComponents;

Expand All @@ -24,7 +24,7 @@ abstract class ValueList extends Value

/**
* phpcs:ignore Generic.Files.LineLength
* @param array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string>|RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string $aComponents
* @param array<int, Component|string>|Component|string $aComponents
* @param string $sSeparator
* @param int $iLineNo
*/
Expand All @@ -49,15 +49,15 @@ public function addListComponent($mComponent)
}

/**
* @return array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string>
* @return array<int, Component|string>
*/
public function getListComponents()
{
return $this->aComponents;
}

/**
* @param array<int, RuleValueList|CSSFunction|CSSString|LineName|Size|URL|string> $aComponents
* @param array<int, Component|string> $aComponents
*
* @return void
*/
Expand Down