Skip to content

Commit

Permalink
[CLEANUP] Extract CSSFunction methods parseName and parseArguments
Browse files Browse the repository at this point in the history
Also
- Update the `parse` function signature to use PHP7 type hints;
- Remove subsequently redundant DocBlock entries;
- Ditto for same-named method in subclasses to avoid contravariance.

Unfortunately, there seems to be a bug in PHP 7.3 (and 7.2) whereby it
doesn't recognize a specific class name as a more-specific type than
`object`, even though it does recognize it as more specific than an untyped
return value.  Thus some more-specific type annotations have had to be
downgraded.

This is the refactoring change from #390.
  • Loading branch information
Jake Hotson committed Jun 21, 2024
1 parent 5a4245f commit 1648cc7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
37 changes: 29 additions & 8 deletions src/Value/CSSFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,42 @@ public function __construct($sName, $aArguments, $sSeparator = ',', $iLineNo = 0
}

/**
* @param ParserState $oParserState
* @param bool $bIgnoreCase
*
* @throws SourceException
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public static function parse(ParserState $oParserState, $bIgnoreCase = false): CSSFunction
public static function parse(ParserState $oParserState, bool $bIgnoreCase = false): CSSFunction
{
$mResult = $oParserState->parseIdentifier($bIgnoreCase);
$sName = self::parseName($oParserState, $bIgnoreCase);
$oParserState->consume('(');
$aArguments = Value::parseValue($oParserState, ['=', ' ', ',']);
$mResult = new CSSFunction($mResult, $aArguments, ',', $oParserState->currentLine());
$mArguments = self::parseArguments($oParserState);

$oResult = new CSSFunction($sName, $mArguments, ',', $oParserState->currentLine());
$oParserState->consume(')');
return $mResult;

return $oResult;
}

/**
* @throws SourceException
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
private static function parseName(ParserState $oParserState, bool $bIgnoreCase = false): string
{
return $oParserState->parseIdentifier($bIgnoreCase);
}

/**
* @return Value|string
*
* @throws SourceException
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
private static function parseArguments(ParserState $oParserState)
{
return Value::parseValue($oParserState, ['=', ' ', ',']);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Value/CalcFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ class CalcFunction extends CSSFunction
private const T_OPERATOR = 2;

/**
* @param ParserState $oParserState
* @param bool $bIgnoreCase
* @return CalcFunction
*
* @throws UnexpectedTokenException
* @throws UnexpectedEOFException
*/
public static function parse(ParserState $oParserState, $bIgnoreCase = false): CSSFunction
public static function parse(ParserState $oParserState, bool $bIgnoreCase = false): CSSFunction
{
$aOperators = ['+', '-', '*', '/'];
$sFunction = $oParserState->parseIdentifier();
Expand Down
5 changes: 1 addition & 4 deletions src/Value/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ public function __construct(array $aColor, $iLineNo = 0)
}

/**
* @param ParserState $oParserState
* @param bool $bIgnoreCase
*
* @throws UnexpectedEOFException
* @throws UnexpectedTokenException
*/
public static function parse(ParserState $oParserState, $bIgnoreCase = false): CSSFunction
public static function parse(ParserState $oParserState, bool $bIgnoreCase = false): CSSFunction
{
$aColor = [];
if ($oParserState->comes('#')) {
Expand Down

0 comments on commit 1648cc7

Please sign in to comment.