From 36ba54012c96ccae3859b009ac7a79d0360e6e14 Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Fri, 7 May 2021 18:48:56 +0200 Subject: [PATCH] Use PHP 5.6 language features --- lib/Sabberworm/CSS/CSSList/CSSBlockList.php | 2 +- lib/Sabberworm/CSS/CSSList/CSSList.php | 16 +- lib/Sabberworm/CSS/CSSList/Document.php | 8 +- lib/Sabberworm/CSS/OutputFormat.php | 15 +- lib/Sabberworm/CSS/Parsing/ParserState.php | 10 +- lib/Sabberworm/CSS/Property/CSSNamespace.php | 4 +- lib/Sabberworm/CSS/Property/Charset.php | 2 +- lib/Sabberworm/CSS/Property/Import.php | 4 +- lib/Sabberworm/CSS/Rule/Rule.php | 20 +-- .../CSS/RuleSet/DeclarationBlock.php | 120 +++++++-------- lib/Sabberworm/CSS/RuleSet/RuleSet.php | 16 +- lib/Sabberworm/CSS/Value/CalcFunction.php | 2 +- .../CSS/Value/CalcRuleValueList.php | 2 +- lib/Sabberworm/CSS/Value/Color.php | 10 +- lib/Sabberworm/CSS/Value/LineName.php | 4 +- lib/Sabberworm/CSS/Value/RuleValueList.php | 2 +- lib/Sabberworm/CSS/Value/Size.php | 12 +- lib/Sabberworm/CSS/Value/Value.php | 10 +- lib/Sabberworm/CSS/Value/ValueList.php | 4 +- tests/Sabberworm/CSS/CSSList/DocumentTest.php | 2 +- tests/Sabberworm/CSS/OutputFormatTest.php | 2 +- tests/Sabberworm/CSS/ParserTest.php | 66 +++++---- .../CSS/RuleSet/DeclarationBlockTest.php | 140 +++++++++--------- 23 files changed, 239 insertions(+), 234 deletions(-) diff --git a/lib/Sabberworm/CSS/CSSList/CSSBlockList.php b/lib/Sabberworm/CSS/CSSList/CSSBlockList.php index 67b490b5..f8065e8a 100644 --- a/lib/Sabberworm/CSS/CSSList/CSSBlockList.php +++ b/lib/Sabberworm/CSS/CSSList/CSSBlockList.php @@ -68,7 +68,7 @@ protected function allValues($oElement, &$aResult, $sSearchString = null, $bSear protected function allSelectors(&$aResult, $sSpecificitySearch = null) { - $aDeclarationBlocks = array(); + $aDeclarationBlocks = []; $this->allDeclarationBlocks($aDeclarationBlocks); foreach ($aDeclarationBlocks as $oBlock) { foreach ($oBlock->getSelectors() as $oSelector) { diff --git a/lib/Sabberworm/CSS/CSSList/CSSList.php b/lib/Sabberworm/CSS/CSSList/CSSList.php index 100f11cc..7a4ef22c 100644 --- a/lib/Sabberworm/CSS/CSSList/CSSList.php +++ b/lib/Sabberworm/CSS/CSSList/CSSList.php @@ -46,8 +46,8 @@ abstract class CSSList implements Renderable, Commentable */ public function __construct($iLineNo = 0) { - $this->aComments = array(); - $this->aContents = array(); + $this->aComments = []; + $this->aContents = []; $this->iLineNo = $iLineNo; } @@ -130,14 +130,14 @@ private static function parseAtRule(ParserState $oParserState) $oParserState->consumeWhiteSpace(); $sMediaQuery = null; if (!$oParserState->comes(';')) { - $sMediaQuery = trim($oParserState->consumeUntil(array(';', ParserState::EOF))); + $sMediaQuery = trim($oParserState->consumeUntil([';', ParserState::EOF])); } - $oParserState->consumeUntil(array(';', ParserState::EOF), true, true); + $oParserState->consumeUntil([';', ParserState::EOF], true, true); return new Import($oLocation, $sMediaQuery ?: null, $iIdentifierLineNum); } elseif ($sIdentifier === 'charset') { $sCharset = CSSString::parse($oParserState); $oParserState->consumeWhiteSpace(); - $oParserState->consumeUntil(array(';', ParserState::EOF), true, true); + $oParserState->consumeUntil([';', ParserState::EOF], true, true); return new Charset($sCharset, $iIdentifierLineNum); } elseif (self::identifierIs($sIdentifier, 'keyframes')) { $oResult = new KeyFrame($iIdentifierLineNum); @@ -155,7 +155,7 @@ private static function parseAtRule(ParserState $oParserState) $sPrefix = $mUrl; $mUrl = Value::parsePrimitiveValue($oParserState); } - $oParserState->consumeUntil(array(';', ParserState::EOF), true, true); + $oParserState->consumeUntil([';', ParserState::EOF], true, true); if ($sPrefix !== null && !is_string($sPrefix)) { throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom', $iIdentifierLineNum); } @@ -271,7 +271,7 @@ public function replace($oOldItem, $mNewItem) if (is_array($mNewItem)) { array_splice($this->aContents, $iKey, 1, $mNewItem); } else { - array_splice($this->aContents, $iKey, 1, array($mNewItem)); + array_splice($this->aContents, $iKey, 1, [$mNewItem]); } return true; } @@ -284,7 +284,7 @@ public function replace($oOldItem, $mNewItem) */ public function setContents(array $aContents) { - $this->aContents = array(); + $this->aContents = []; foreach ($aContents as $content) { $this->append($content); } diff --git a/lib/Sabberworm/CSS/CSSList/Document.php b/lib/Sabberworm/CSS/CSSList/Document.php index 55932f19..0b07f1ff 100644 --- a/lib/Sabberworm/CSS/CSSList/Document.php +++ b/lib/Sabberworm/CSS/CSSList/Document.php @@ -37,7 +37,7 @@ public static function parse(ParserState $oParserState) */ public function getAllDeclarationBlocks() { - $aResult = array(); + $aResult = []; $this->allDeclarationBlocks($aResult); return $aResult; } @@ -55,7 +55,7 @@ public function getAllSelectors() */ public function getAllRuleSets() { - $aResult = array(); + $aResult = []; $this->allRuleSets($aResult); return $aResult; } @@ -74,7 +74,7 @@ public function getAllValues($mElement = null, $bSearchInFunctionArguments = fal $sSearchString = $mElement; $mElement = $this; } - $aResult = array(); + $aResult = []; $this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments); return $aResult; } @@ -87,7 +87,7 @@ public function getAllValues($mElement = null, $bSearchInFunctionArguments = fal */ public function getSelectorsBySpecificity($sSpecificitySearch = null) { - $aResult = array(); + $aResult = []; $this->allSelectors($aResult, $sSpecificitySearch); return $aResult; } diff --git a/lib/Sabberworm/CSS/OutputFormat.php b/lib/Sabberworm/CSS/OutputFormat.php index e643ddc2..a8d05992 100644 --- a/lib/Sabberworm/CSS/OutputFormat.php +++ b/lib/Sabberworm/CSS/OutputFormat.php @@ -3,6 +3,7 @@ namespace Sabberworm\CSS; use Sabberworm\CSS\Parsing\OutputException; +use Sabberworm\CSS\OutputFormatter; /** * Class OutputFormat @@ -81,7 +82,7 @@ public function __construct() public function get($sName) { - $aVarPrefixes = array('a', 's', 'm', 'b', 'f', 'o', 'c', 'i'); + $aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i']; foreach ($aVarPrefixes as $sPrefix) { $sFieldName = $sPrefix . ucfirst($sName); if (isset($this->$sFieldName)) { @@ -93,11 +94,11 @@ public function get($sName) public function set($aNames, $mValue) { - $aVarPrefixes = array('a', 's', 'm', 'b', 'f', 'o', 'c', 'i'); + $aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i']; if (is_string($aNames) && strpos($aNames, '*') !== false) { - $aNames = array(str_replace('*', 'Before', $aNames), str_replace('*', 'Between', $aNames), str_replace('*', 'After', $aNames)); + $aNames = [str_replace('*', 'Before', $aNames), str_replace('*', 'Between', $aNames), str_replace('*', 'After', $aNames)]; } elseif (!is_array($aNames)) { - $aNames = array($aNames); + $aNames = [$aNames]; } foreach ($aVarPrefixes as $sPrefix) { $bDidReplace = false; @@ -122,8 +123,8 @@ public function __call($sMethodName, $aArguments) return $this->set(substr($sMethodName, 3), $aArguments[0]); } elseif (strpos($sMethodName, 'get') === 0) { return $this->get(substr($sMethodName, 3)); - } elseif (method_exists('\\Sabberworm\\CSS\\OutputFormatter', $sMethodName)) { - return call_user_func_array(array($this->getFormatter(), $sMethodName), $aArguments); + } elseif (method_exists(OutputFormatter::class, $sMethodName)) { + return call_user_func_array([$this->getFormatter(), $sMethodName], $aArguments); } else { throw new \Exception('Unknown OutputFormat method called: ' . $sMethodName); } @@ -197,7 +198,7 @@ public static function createCompact() public static function createPretty() { $format = self::create(); - $format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', array('default' => '', ',' => ' ')); + $format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' ']); return $format; } } diff --git a/lib/Sabberworm/CSS/Parsing/ParserState.php b/lib/Sabberworm/CSS/Parsing/ParserState.php index d68ebabf..60d0b908 100644 --- a/lib/Sabberworm/CSS/Parsing/ParserState.php +++ b/lib/Sabberworm/CSS/Parsing/ParserState.php @@ -134,7 +134,7 @@ public function parseCharacter($bIsForIdentifier) public function consumeWhiteSpace() { - $comments = array(); + $comments = []; do { while (preg_match('/\\s/isSu', $this->peek()) === 1) { $this->consume(1); @@ -238,9 +238,9 @@ public function isEnd() return $this->iCurrentPosition >= $this->iLength; } - public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = array()) + public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = []) { - $aEnd = is_array($aEnd) ? $aEnd : array($aEnd); + $aEnd = is_array($aEnd) ? $aEnd : [$aEnd]; $out = ''; $start = $this->iCurrentPosition; @@ -329,7 +329,7 @@ private function strsplit($sString) return preg_split('//u', $sString, null, PREG_SPLIT_NO_EMPTY); } else { $iLength = mb_strlen($sString, $this->sCharset); - $aResult = array(); + $aResult = []; for ($i = 0; $i < $iLength; ++$i) { $aResult[] = mb_substr($sString, $i, 1, $this->sCharset); } @@ -337,7 +337,7 @@ private function strsplit($sString) } } else { if ($sString === '') { - return array(); + return []; } else { return str_split($sString); } diff --git a/lib/Sabberworm/CSS/Property/CSSNamespace.php b/lib/Sabberworm/CSS/Property/CSSNamespace.php index 6d179d6a..8340ae7d 100644 --- a/lib/Sabberworm/CSS/Property/CSSNamespace.php +++ b/lib/Sabberworm/CSS/Property/CSSNamespace.php @@ -17,7 +17,7 @@ public function __construct($mUrl, $sPrefix = null, $iLineNo = 0) $this->mUrl = $mUrl; $this->sPrefix = $sPrefix; $this->iLineNo = $iLineNo; - $this->aComments = array(); + $this->aComments = []; } /** @@ -76,7 +76,7 @@ public function atRuleName() */ public function atRuleArgs() { - $aResult = array($this->mUrl); + $aResult = [$this->mUrl]; if ($this->sPrefix) { array_unshift($aResult, $this->sPrefix); } diff --git a/lib/Sabberworm/CSS/Property/Charset.php b/lib/Sabberworm/CSS/Property/Charset.php index f90d8374..37a81077 100644 --- a/lib/Sabberworm/CSS/Property/Charset.php +++ b/lib/Sabberworm/CSS/Property/Charset.php @@ -34,7 +34,7 @@ public function __construct($sCharset, $iLineNo = 0) { $this->sCharset = $sCharset; $this->iLineNo = $iLineNo; - $this->aComments = array(); + $this->aComments = []; } /** diff --git a/lib/Sabberworm/CSS/Property/Import.php b/lib/Sabberworm/CSS/Property/Import.php index ff168040..4cd5a10b 100644 --- a/lib/Sabberworm/CSS/Property/Import.php +++ b/lib/Sabberworm/CSS/Property/Import.php @@ -39,7 +39,7 @@ public function __construct(URL $oLocation, $sMediaQuery, $iLineNo = 0) $this->oLocation = $oLocation; $this->sMediaQuery = $sMediaQuery; $this->iLineNo = $iLineNo; - $this->aComments = array(); + $this->aComments = []; } /** @@ -88,7 +88,7 @@ public function atRuleName() */ public function atRuleArgs() { - $aResult = array($this->oLocation); + $aResult = [$this->oLocation]; if ($this->sMediaQuery) { array_push($aResult, $this->sMediaQuery); } diff --git a/lib/Sabberworm/CSS/Rule/Rule.php b/lib/Sabberworm/CSS/Rule/Rule.php index 130b5d71..f3d97fcc 100644 --- a/lib/Sabberworm/CSS/Rule/Rule.php +++ b/lib/Sabberworm/CSS/Rule/Rule.php @@ -28,10 +28,10 @@ public function __construct($sRule, $iLineNo = 0, $iColNo = 0) $this->sRule = $sRule; $this->mValue = null; $this->bIsImportant = false; - $this->aIeHack = array(); + $this->aIeHack = []; $this->iLineNo = $iLineNo; $this->iColNo = $iColNo; - $this->aComments = array(); + $this->aComments = []; } public static function parse(ParserState $oParserState) @@ -69,9 +69,9 @@ public static function parse(ParserState $oParserState) private static function listDelimiterForRule($sRule) { if (preg_match('/^font($|-)/', $sRule)) { - return array(',', '/', ' '); + return [',', '/', ' ']; } - return array(',', ' ', '/'); + return [',', ' ', '/']; } /** @@ -158,19 +158,19 @@ public function setValues($aSpaceSeparatedValues) public function getValues() { if (!$this->mValue instanceof RuleValueList) { - return array(array($this->mValue)); + return [[$this->mValue]]; } if ($this->mValue->getListSeparator() === ',') { - return array($this->mValue->getListComponents()); + return [$this->mValue->getListComponents()]; } - $aResult = array(); + $aResult = []; foreach ($this->mValue->getListComponents() as $mValue) { if (!$mValue instanceof RuleValueList || $mValue->getListSeparator() !== ',') { - $aResult[] = array($mValue); + $aResult[] = [$mValue]; continue; } if ($this->mValue->getListSeparator() === ' ' || count($aResult) === 0) { - $aResult[] = array(); + $aResult[] = []; } foreach ($mValue->getListComponents() as $mValue) { $aResult[count($aResult) - 1][] = $mValue; @@ -185,7 +185,7 @@ public function getValues() public function addValue($mValue, $sType = ' ') { if (!is_array($mValue)) { - $mValue = array($mValue); + $mValue = [$mValue]; } if (!$this->mValue instanceof RuleValueList || $this->mValue->getListSeparator() !== $sType) { $mCurrentValue = $this->mValue; diff --git a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php index 8b82dc43..9d395c9e 100644 --- a/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php +++ b/lib/Sabberworm/CSS/RuleSet/DeclarationBlock.php @@ -29,26 +29,26 @@ class DeclarationBlock extends RuleSet public function __construct($iLineNo = 0) { parent::__construct($iLineNo); - $this->aSelectors = array(); + $this->aSelectors = []; } public static function parse(ParserState $oParserState, $oList = null) { - $aComments = array(); + $aComments = []; $oResult = new DeclarationBlock($oParserState->currentLine()); try { - $aSelectorParts = array(); + $aSelectorParts = []; $sStringWrapperChar = false; do { - $aSelectorParts[] = $oParserState->consume(1) . $oParserState->consumeUntil(array('{', '}', '\'', '"'), false, false, $aComments); - if (in_array($oParserState->peek(), array('\'', '"')) && substr(end($aSelectorParts), -1) != "\\") { + $aSelectorParts[] = $oParserState->consume(1) . $oParserState->consumeUntil(['{', '}', '\'', '"'], false, false, $aComments); + if (in_array($oParserState->peek(), ['\'', '"']) && substr(end($aSelectorParts), -1) != "\\") { if ($sStringWrapperChar === false) { $sStringWrapperChar = $oParserState->peek(); } elseif ($sStringWrapperChar == $oParserState->peek()) { $sStringWrapperChar = false; } } - } while (!in_array($oParserState->peek(), array('{', '}')) || $sStringWrapperChar !== false); + } while (!in_array($oParserState->peek(), ['{', '}']) || $sStringWrapperChar !== false); $oResult->setSelector(implode('', $aSelectorParts), $oList); if ($oParserState->comes('{')) { $oParserState->consume(1); @@ -167,12 +167,12 @@ public function createShorthands() * */ public function expandBorderShorthand() { - $aBorderRules = array( + $aBorderRules = [ 'border', 'border-left', 'border-right', 'border-top', 'border-bottom' - ); - $aBorderSizes = array( + ]; + $aBorderSizes = [ 'thin', 'medium', 'thick' - ); + ]; $aRules = $this->getRulesAssoc(); foreach ($aBorderRules as $sBorderRule) { if (!isset($aRules[$sBorderRule])) { @@ -180,7 +180,7 @@ public function expandBorderShorthand() } $oRule = $aRules[$sBorderRule]; $mRuleValue = $oRule->getValue(); - $aValues = array(); + $aValues = []; if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { @@ -205,7 +205,7 @@ public function expandBorderShorthand() } $oNewRule = new Rule($sNewRuleName, $oRule->getLineNo(), $oRule->getColNo()); $oNewRule->setIsImportant($oRule->getIsImportant()); - $oNewRule->addValue(array($mNewValue)); + $oNewRule->addValue([$mNewValue]); $this->addRule($oNewRule); } $this->removeRule($sBorderRule); @@ -219,13 +219,13 @@ public function expandBorderShorthand() * */ public function expandDimensionsShorthand() { - $aExpansions = array( + $aExpansions = [ 'margin' => 'margin-%s', 'padding' => 'padding-%s', 'border-color' => 'border-%s-color', 'border-style' => 'border-%s-style', 'border-width' => 'border-%s-width' - ); + ]; $aRules = $this->getRulesAssoc(); foreach ($aExpansions as $sProperty => $sExpanded) { if (!isset($aRules[$sProperty])) { @@ -233,7 +233,7 @@ public function expandDimensionsShorthand() } $oRule = $aRules[$sProperty]; $mRuleValue = $oRule->getValue(); - $aValues = array(); + $aValues = []; if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { @@ -260,7 +260,7 @@ public function expandDimensionsShorthand() $left = $aValues[3]; break; } - foreach (array('top', 'right', 'bottom', 'left') as $sPosition) { + foreach (['top', 'right', 'bottom', 'left'] as $sPosition) { $oNewRule = new Rule(sprintf($sExpanded, $sPosition), $oRule->getLineNo(), $oRule->getColNo()); $oNewRule->setIsImportant($oRule->getIsImportant()); $oNewRule->addValue(${$sPosition}); @@ -283,15 +283,15 @@ public function expandFontShorthand() } $oRule = $aRules['font']; // reset properties to 'normal' per http://www.w3.org/TR/21/fonts.html#font-shorthand - $aFontProperties = array( + $aFontProperties = [ 'font-style' => 'normal', 'font-variant' => 'normal', 'font-weight' => 'normal', 'font-size' => 'normal', 'line-height' => 'normal' - ); + ]; $mRuleValue = $oRule->getValue(); - $aValues = array(); + $aValues = []; if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { @@ -301,18 +301,18 @@ public function expandFontShorthand() if (!$mValue instanceof Value) { $mValue = mb_strtolower($mValue); } - if (in_array($mValue, array('normal', 'inherit'))) { - foreach (array('font-style', 'font-weight', 'font-variant') as $sProperty) { + if (in_array($mValue, ['normal', 'inherit'])) { + foreach (['font-style', 'font-weight', 'font-variant'] as $sProperty) { if (!isset($aFontProperties[$sProperty])) { $aFontProperties[$sProperty] = $mValue; } } - } elseif (in_array($mValue, array('italic', 'oblique'))) { + } elseif (in_array($mValue, ['italic', 'oblique'])) { $aFontProperties['font-style'] = $mValue; } elseif ($mValue == 'small-caps') { $aFontProperties['font-variant'] = $mValue; } elseif ( - in_array($mValue, array('bold', 'bolder', 'lighter')) + in_array($mValue, ['bold', 'bolder', 'lighter']) || ($mValue instanceof Size && in_array($mValue->getSize(), range(100, 900, 100))) ) { @@ -350,13 +350,13 @@ public function expandBackgroundShorthand() return; } $oRule = $aRules['background']; - $aBgProperties = array( - 'background-color' => array('transparent'), 'background-image' => array('none'), - 'background-repeat' => array('repeat'), 'background-attachment' => array('scroll'), - 'background-position' => array(new Size(0, '%', null, false, $this->iLineNo), new Size(0, '%', null, false, $this->iLineNo)) - ); + $aBgProperties = [ + 'background-color' => ['transparent'], 'background-image' => ['none'], + 'background-repeat' => ['repeat'], 'background-attachment' => ['scroll'], + 'background-position' => [new Size(0, '%', null, false, $this->iLineNo), new Size(0, '%', null, false, $this->iLineNo)] + ]; $mRuleValue = $oRule->getValue(); - $aValues = array(); + $aValues = []; if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { @@ -381,12 +381,12 @@ public function expandBackgroundShorthand() $aBgProperties['background-image'] = $mValue; } elseif ($mValue instanceof Color) { $aBgProperties['background-color'] = $mValue; - } elseif (in_array($mValue, array('scroll', 'fixed'))) { + } elseif (in_array($mValue, ['scroll', 'fixed'])) { $aBgProperties['background-attachment'] = $mValue; - } elseif (in_array($mValue, array('repeat', 'no-repeat', 'repeat-x', 'repeat-y'))) { + } elseif (in_array($mValue, ['repeat', 'no-repeat', 'repeat-x', 'repeat-y'])) { $aBgProperties['background-repeat'] = $mValue; } elseif ( - in_array($mValue, array('left', 'center', 'right', 'top', 'bottom')) + in_array($mValue, ['left', 'center', 'right', 'top', 'bottom']) || $mValue instanceof Size ) { if ($iNumBgPos == 0) { @@ -409,27 +409,27 @@ public function expandBackgroundShorthand() public function expandListStyleShorthand() { - $aListProperties = array( + $aListProperties = [ 'list-style-type' => 'disc', 'list-style-position' => 'outside', 'list-style-image' => 'none' - ); - $aListStyleTypes = array( + ]; + $aListStyleTypes = [ 'none', 'disc', 'circle', 'square', 'decimal-leading-zero', 'decimal', 'lower-roman', 'upper-roman', 'lower-greek', 'lower-alpha', 'lower-latin', 'upper-alpha', 'upper-latin', 'hebrew', 'armenian', 'georgian', 'cjk-ideographic', 'hiragana', 'hira-gana-iroha', 'katakana-iroha', 'katakana' - ); - $aListStylePositions = array( + ]; + $aListStylePositions = [ 'inside', 'outside' - ); + ]; $aRules = $this->getRulesAssoc(); if (!isset($aRules['list-style'])) { return; } $oRule = $aRules['list-style']; $mRuleValue = $oRule->getValue(); - $aValues = array(); + $aValues = []; if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { @@ -469,7 +469,7 @@ public function expandListStyleShorthand() public function createShorthandProperties(array $aProperties, $sShorthand) { $aRules = $this->getRulesAssoc(); - $aNewValues = array(); + $aNewValues = []; foreach ($aProperties as $sProperty) { if (!isset($aRules[$sProperty])) { continue; @@ -477,7 +477,7 @@ public function createShorthandProperties(array $aProperties, $sShorthand) $oRule = $aRules[$sProperty]; if (!$oRule->getIsImportant()) { $mRuleValue = $oRule->getValue(); - $aValues = array(); + $aValues = []; if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { @@ -500,18 +500,18 @@ public function createShorthandProperties(array $aProperties, $sShorthand) public function createBackgroundShorthand() { - $aProperties = array( + $aProperties = [ 'background-color', 'background-image', 'background-repeat', 'background-position', 'background-attachment' - ); + ]; $this->createShorthandProperties($aProperties, 'background'); } public function createListStyleShorthand() { - $aProperties = array( + $aProperties = [ 'list-style-type', 'list-style-position', 'list-style-image' - ); + ]; $this->createShorthandProperties($aProperties, 'list-style'); } @@ -521,9 +521,9 @@ public function createListStyleShorthand() * */ public function createBorderShorthand() { - $aProperties = array( + $aProperties = [ 'border-width', 'border-style', 'border-color' - ); + ]; $this->createShorthandProperties($aProperties, 'border'); } @@ -535,17 +535,17 @@ public function createBorderShorthand() public function createDimensionsShorthand() { - $aPositions = array('top', 'right', 'bottom', 'left'); - $aExpansions = array( + $aPositions = ['top', 'right', 'bottom', 'left']; + $aExpansions = [ 'margin' => 'margin-%s', 'padding' => 'padding-%s', 'border-color' => 'border-%s-color', 'border-style' => 'border-%s-style', 'border-width' => 'border-%s-width' - ); + ]; $aRules = $this->getRulesAssoc(); foreach ($aExpansions as $sProperty => $sExpanded) { - $aFoldable = array(); + $aFoldable = []; foreach ($aRules as $sRuleName => $oRule) { foreach ($aPositions as $sPosition) { if ($sRuleName == sprintf($sExpanded, $sPosition)) { @@ -555,11 +555,11 @@ public function createDimensionsShorthand() } // All four dimensions must be present if (count($aFoldable) == 4) { - $aValues = array(); + $aValues = []; foreach ($aPositions as $sPosition) { $oRule = $aRules[sprintf($sExpanded, $sPosition)]; $mRuleValue = $oRule->getValue(); - $aRuleValues = array(); + $aRuleValues = []; if (!$mRuleValue instanceof RuleValueList) { $aRuleValues[] = $mRuleValue; } else { @@ -606,9 +606,9 @@ public function createDimensionsShorthand() * */ public function createFontShorthand() { - $aFontProperties = array( + $aFontProperties = [ 'font-style', 'font-variant', 'font-weight', 'font-size', 'line-height', 'font-family' - ); + ]; $aRules = $this->getRulesAssoc(); if (!isset($aRules['font-size']) || !isset($aRules['font-family'])) { return; @@ -616,11 +616,11 @@ public function createFontShorthand() $oOldRule = isset($aRules['font-size']) ? $aRules['font-size'] : $aRules['font-family']; $oNewRule = new Rule('font', $oOldRule->getLineNo(), $oOldRule->getColNo()); unset($oOldRule); - foreach (array('font-style', 'font-variant', 'font-weight') as $sProperty) { + foreach (['font-style', 'font-variant', 'font-weight'] as $sProperty) { if (isset($aRules[$sProperty])) { $oRule = $aRules[$sProperty]; $mRuleValue = $oRule->getValue(); - $aValues = array(); + $aValues = []; if (!$mRuleValue instanceof RuleValueList) { $aValues[] = $mRuleValue; } else { @@ -634,7 +634,7 @@ public function createFontShorthand() // Get the font-size value $oRule = $aRules['font-size']; $mRuleValue = $oRule->getValue(); - $aFSValues = array(); + $aFSValues = []; if (!$mRuleValue instanceof RuleValueList) { $aFSValues[] = $mRuleValue; } else { @@ -644,7 +644,7 @@ public function createFontShorthand() if (isset($aRules['line-height'])) { $oRule = $aRules['line-height']; $mRuleValue = $oRule->getValue(); - $aLHValues = array(); + $aLHValues = []; if (!$mRuleValue instanceof RuleValueList) { $aLHValues[] = $mRuleValue; } else { @@ -661,7 +661,7 @@ public function createFontShorthand() } $oRule = $aRules['font-family']; $mRuleValue = $oRule->getValue(); - $aFFValues = array(); + $aFFValues = []; if (!$mRuleValue instanceof RuleValueList) { $aFFValues[] = $mRuleValue; } else { diff --git a/lib/Sabberworm/CSS/RuleSet/RuleSet.php b/lib/Sabberworm/CSS/RuleSet/RuleSet.php index a0f413da..8cec47b2 100644 --- a/lib/Sabberworm/CSS/RuleSet/RuleSet.php +++ b/lib/Sabberworm/CSS/RuleSet/RuleSet.php @@ -21,9 +21,9 @@ abstract class RuleSet implements Renderable, Commentable public function __construct($iLineNo = 0) { - $this->aRules = array(); + $this->aRules = []; $this->iLineNo = $iLineNo; - $this->aComments = array(); + $this->aComments = []; } public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet) @@ -38,7 +38,7 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet $oRule = Rule::parse($oParserState); } catch (UnexpectedTokenException $e) { try { - $sConsume = $oParserState->consumeUntil(array("\n", ";", '}'), true); + $sConsume = $oParserState->consumeUntil(["\n", ";", '}'], true); // We need to “unfind” the matches to the end of the ruleSet as this will be matched later if ($oParserState->streql(substr($sConsume, -1), '}')) { $oParserState->backtrack(1); @@ -74,7 +74,7 @@ public function addRule(Rule $oRule, Rule $oSibling = null) { $sRule = $oRule->getRule(); if (!isset($this->aRules[$sRule])) { - $this->aRules[$sRule] = array(); + $this->aRules[$sRule] = []; } $iPosition = count($this->aRules[$sRule]); @@ -96,7 +96,7 @@ public function addRule(Rule $oRule, Rule $oSibling = null) } } - array_splice($this->aRules[$sRule], $iPosition, 0, array($oRule)); + array_splice($this->aRules[$sRule], $iPosition, 0, [$oRule]); } /** @@ -114,7 +114,7 @@ public function getRules($mRule = null) if ($mRule instanceof Rule) { $mRule = $mRule->getRule(); } - $aResult = array(); + $aResult = []; foreach ($this->aRules as $sName => $aRules) { // Either no search rule is given or the search rule matches the found rule exactly or the search rule ends in “-” and the found rule starts with the search rule. if (!$mRule || $sName === $mRule || (strrpos($mRule, '-') === strlen($mRule) - strlen('-') && (strpos($sName, $mRule) === 0 || $sName === substr($mRule, 0, -1)))) { @@ -136,7 +136,7 @@ public function getRules($mRule = null) */ public function setRules(array $aRules) { - $this->aRules = array(); + $this->aRules = []; foreach ($aRules as $rule) { $this->addRule($rule); } @@ -150,7 +150,7 @@ public function setRules(array $aRules) */ public function getRulesAssoc($mRule = null) { - $aResult = array(); + $aResult = []; foreach ($this->getRules($mRule) as $oRule) { $aResult[$oRule->getRule()] = $oRule; } diff --git a/lib/Sabberworm/CSS/Value/CalcFunction.php b/lib/Sabberworm/CSS/Value/CalcFunction.php index 21427516..e03fd45f 100644 --- a/lib/Sabberworm/CSS/Value/CalcFunction.php +++ b/lib/Sabberworm/CSS/Value/CalcFunction.php @@ -12,7 +12,7 @@ class CalcFunction extends CSSFunction public static function parse(ParserState $oParserState) { - $aOperators = array('+', '-', '*', '/'); + $aOperators = ['+', '-', '*', '/']; $sFunction = trim($oParserState->consumeUntil('(', false, true)); $oCalcList = new CalcRuleValueList($oParserState->currentLine()); $oList = new RuleValueList(',', $oParserState->currentLine()); diff --git a/lib/Sabberworm/CSS/Value/CalcRuleValueList.php b/lib/Sabberworm/CSS/Value/CalcRuleValueList.php index 1e8d3458..79e0cbcf 100644 --- a/lib/Sabberworm/CSS/Value/CalcRuleValueList.php +++ b/lib/Sabberworm/CSS/Value/CalcRuleValueList.php @@ -6,7 +6,7 @@ class CalcRuleValueList extends RuleValueList { public function __construct($iLineNo = 0) { - parent::__construct(array(), ',', $iLineNo); + parent::__construct([], ',', $iLineNo); } /** diff --git a/lib/Sabberworm/CSS/Value/Color.php b/lib/Sabberworm/CSS/Value/Color.php index c134b5a6..60637c13 100644 --- a/lib/Sabberworm/CSS/Value/Color.php +++ b/lib/Sabberworm/CSS/Value/Color.php @@ -14,7 +14,7 @@ public function __construct($aColor, $iLineNo = 0) public static function parse(ParserState $oParserState) { - $aColor = array(); + $aColor = []; if ($oParserState->comes('#')) { $oParserState->consume('#'); $sValue = $oParserState->parseIdentifier(false); @@ -25,18 +25,18 @@ public static function parse(ParserState $oParserState) } if ($oParserState->strlen($sValue) === 8) { - $aColor = array( + $aColor = [ 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()), 'a' => new Size(round(self::mapRange(intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2), null, true, $oParserState->currentLine()) - ); + ]; } else { - $aColor = array( + $aColor = [ 'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()), 'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()), 'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()) - ); + ]; } } else { $sColorMode = $oParserState->parseIdentifier(true); diff --git a/lib/Sabberworm/CSS/Value/LineName.php b/lib/Sabberworm/CSS/Value/LineName.php index 7d3a8b91..06af7367 100644 --- a/lib/Sabberworm/CSS/Value/LineName.php +++ b/lib/Sabberworm/CSS/Value/LineName.php @@ -7,7 +7,7 @@ class LineName extends ValueList { - public function __construct($aComponents = array(), $iLineNo = 0) + public function __construct($aComponents = [], $iLineNo = 0) { parent::__construct($aComponents, ' ', $iLineNo); } @@ -16,7 +16,7 @@ public static function parse(ParserState $oParserState) { $oParserState->consume('['); $oParserState->consumeWhiteSpace(); - $aNames = array(); + $aNames = []; do { if ($oParserState->getSettings()->bLenientParsing) { try { diff --git a/lib/Sabberworm/CSS/Value/RuleValueList.php b/lib/Sabberworm/CSS/Value/RuleValueList.php index 3474d7b1..7e9f0101 100644 --- a/lib/Sabberworm/CSS/Value/RuleValueList.php +++ b/lib/Sabberworm/CSS/Value/RuleValueList.php @@ -6,6 +6,6 @@ class RuleValueList extends ValueList { public function __construct($sSeparator = ',', $iLineNo = 0) { - parent::__construct(array(), $sSeparator, $iLineNo); + parent::__construct([], $sSeparator, $iLineNo); } } diff --git a/lib/Sabberworm/CSS/Value/Size.php b/lib/Sabberworm/CSS/Value/Size.php index 41aa1bc6..55a08354 100644 --- a/lib/Sabberworm/CSS/Value/Size.php +++ b/lib/Sabberworm/CSS/Value/Size.php @@ -20,7 +20,7 @@ class Size extends PrimitiveValue public function __construct($fSize, $sUnit = null, $bIsColorComponent = false, $iLineNo = 0) { parent::__construct($iLineNo); - $this->fSize = floatval($fSize); + $this->fSize = (float)$fSize; $this->sUnit = $sUnit; $this->bIsColorComponent = $bIsColorComponent; } @@ -50,17 +50,17 @@ public static function parse(ParserState $oParserState, $bIsColorComponent = fal } } } - return new Size(floatval($sSize), $sUnit, $bIsColorComponent, $oParserState->currentLine()); + return new Size((float)$sSize, $sUnit, $bIsColorComponent, $oParserState->currentLine()); } private static function getSizeUnits() { if (self::$SIZE_UNITS === null) { - self::$SIZE_UNITS = array(); + self::$SIZE_UNITS = []; foreach (explode('/', Size::ABSOLUTE_SIZE_UNITS . '/' . Size::RELATIVE_SIZE_UNITS . '/' . Size::NON_SIZE_UNITS) as $val) { $iSize = strlen($val); if (!isset(self::$SIZE_UNITS[$iSize])) { - self::$SIZE_UNITS[$iSize] = array(); + self::$SIZE_UNITS[$iSize] = []; } self::$SIZE_UNITS[$iSize][strtolower($val)] = $val; } @@ -83,7 +83,7 @@ public function getUnit() public function setSize($fSize) { - $this->fSize = floatval($fSize); + $this->fSize = (float)$fSize; } public function getSize() @@ -134,6 +134,6 @@ public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) $l = localeconv(); $sPoint = preg_quote($l['decimal_point'], '/'); $sSize = preg_match("/[\d\.]+e[+-]?\d+/i", (string)$this->fSize) ? preg_replace("/$sPoint?0+$/", "", sprintf("%f", $this->fSize)) : $this->fSize; - return preg_replace(array("/$sPoint/", "/^(-?)0\./"), array('.', '$1.'), $sSize) . ($this->sUnit === null ? '' : $this->sUnit); + return preg_replace(["/$sPoint/", "/^(-?)0\./"], ['.', '$1.'], $sSize) . ($this->sUnit === null ? '' : $this->sUnit); } } diff --git a/lib/Sabberworm/CSS/Value/Value.php b/lib/Sabberworm/CSS/Value/Value.php index 31f66dcd..ec339bfe 100644 --- a/lib/Sabberworm/CSS/Value/Value.php +++ b/lib/Sabberworm/CSS/Value/Value.php @@ -15,9 +15,9 @@ public function __construct($iLineNo = 0) $this->iLineNo = $iLineNo; } - public static function parseValue(ParserState $oParserState, $aListDelimiters = array()) + public static function parseValue(ParserState $oParserState, $aListDelimiters = []) { - $aStack = array(); + $aStack = []; $oParserState->consumeWhiteSpace(); //Build a list of delimiters and parsed values while (!($oParserState->comes('}') || $oParserState->comes(';') || $oParserState->comes('!') || $oParserState->comes(')') || $oParserState->comes('\\'))) { @@ -56,7 +56,7 @@ public static function parseValue(ParserState $oParserState, $aListDelimiters = for ($i = $iStartPosition - 1; $i - $iStartPosition + 1 < $iLength * 2; $i += 2) { $oList->addListComponent($aStack[$i]); } - array_splice($aStack, $iStartPosition - 1, $iLength * 2 - 1, array($oList)); + array_splice($aStack, $iStartPosition - 1, $iLength * 2 - 1, [$oList]); } } if (!isset($aStack[0])) { @@ -71,7 +71,7 @@ public static function parseIdentifierOrFunction(ParserState $oParserState, $bIg if ($oParserState->comes('(')) { $oParserState->consume('('); - $aArguments = Value::parseValue($oParserState, array('=', ' ', ',')); + $aArguments = Value::parseValue($oParserState, ['=', ' ', ',']); $sResult = new CSSFunction($sResult, $aArguments, ',', $oParserState->currentLine()); $oParserState->consume(')'); } @@ -109,7 +109,7 @@ public static function parsePrimitiveValue(ParserState $oParserState) private static function parseMicrosoftFilter(ParserState $oParserState) { $sFunction = $oParserState->consumeUntil('(', false, true); - $aArguments = Value::parseValue($oParserState, array(',', '=')); + $aArguments = Value::parseValue($oParserState, [',', '=']); return new CSSFunction($sFunction, $aArguments, ',', $oParserState->currentLine()); } diff --git a/lib/Sabberworm/CSS/Value/ValueList.php b/lib/Sabberworm/CSS/Value/ValueList.php index c567b61e..6eec4e11 100644 --- a/lib/Sabberworm/CSS/Value/ValueList.php +++ b/lib/Sabberworm/CSS/Value/ValueList.php @@ -8,11 +8,11 @@ abstract class ValueList extends Value protected $aComponents; protected $sSeparator; - public function __construct($aComponents = array(), $sSeparator = ',', $iLineNo = 0) + public function __construct($aComponents = [], $sSeparator = ',', $iLineNo = 0) { parent::__construct($iLineNo); if (!is_array($aComponents)) { - $aComponents = array($aComponents); + $aComponents = [$aComponents]; } $this->aComponents = $aComponents; $this->sSeparator = $sSeparator; diff --git a/tests/Sabberworm/CSS/CSSList/DocumentTest.php b/tests/Sabberworm/CSS/CSSList/DocumentTest.php index 7f47bb4f..647b662a 100644 --- a/tests/Sabberworm/CSS/CSSList/DocumentTest.php +++ b/tests/Sabberworm/CSS/CSSList/DocumentTest.php @@ -20,7 +20,7 @@ public function testOverrideContents() $oDoc2 = $oParser2->parse(); $aContents2 = $oDoc2->getContents(); - $oDoc->setContents(array($aContents[0], $aContents2[0])); + $oDoc->setContents([$aContents[0], $aContents2[0]]); $aFinalContents = $oDoc->getContents(); $this->assertCount(2, $aFinalContents); } diff --git a/tests/Sabberworm/CSS/OutputFormatTest.php b/tests/Sabberworm/CSS/OutputFormatTest.php index 819830da..ef88a319 100644 --- a/tests/Sabberworm/CSS/OutputFormatTest.php +++ b/tests/Sabberworm/CSS/OutputFormatTest.php @@ -62,7 +62,7 @@ public function testSpaceAfterListArgumentSeparator() public function testSpaceAfterListArgumentSeparatorComplex() { $this->assertSame('.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;} -@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(array('default' => ' ', ',' => "\t", '/' => '', ' ' => '')))); +@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}', $this->oDocument->render(OutputFormat::create()->setSpaceAfterListArgumentSeparator(['default' => ' ', ',' => "\t", '/' => '', ' ' => '']))); } public function testSpaceAfterSelectorSeparator() diff --git a/tests/Sabberworm/CSS/ParserTest.php b/tests/Sabberworm/CSS/ParserTest.php index 4094a8f6..a91bb245 100644 --- a/tests/Sabberworm/CSS/ParserTest.php +++ b/tests/Sabberworm/CSS/ParserTest.php @@ -9,6 +9,10 @@ use Sabberworm\CSS\Property\AtRule; use Sabberworm\CSS\Value\URL; use Sabberworm\CSS\Parsing\UnexpectedTokenException; +use Sabberworm\CSS\Property\Import; +use Sabberworm\CSS\RuleSet\AtRuleSet; +use Sabberworm\CSS\Property\CSSNamespace; +use Sabberworm\CSS\Property\Charset; class ParserTest extends \PHPunit\Framework\TestCase { @@ -58,21 +62,21 @@ function testColorParsing() $this->assertSame('red', $oColor); $aColorRule = $oRuleSet->getRules('background-'); $oColor = $aColorRule[0]->getValue(); - $this->assertEquals(array('r' => new Size(35.0, null, true, $oColor->getLineNo()), 'g' => new Size(35.0, null, true, $oColor->getLineNo()), 'b' => new Size(35.0, null, true, $oColor->getLineNo())), $oColor->getColor()); + $this->assertEquals(['r' => new Size(35.0, null, true, $oColor->getLineNo()), 'g' => new Size(35.0, null, true, $oColor->getLineNo()), 'b' => new Size(35.0, null, true, $oColor->getLineNo())], $oColor->getColor()); $aColorRule = $oRuleSet->getRules('border-color'); $oColor = $aColorRule[0]->getValue(); - $this->assertEquals(array('r' => new Size(10.0, null, true, $oColor->getLineNo()), 'g' => new Size(100.0, null, true, $oColor->getLineNo()), 'b' => new Size(230.0, null, true, $oColor->getLineNo())), $oColor->getColor()); + $this->assertEquals(['r' => new Size(10.0, null, true, $oColor->getLineNo()), 'g' => new Size(100.0, null, true, $oColor->getLineNo()), 'b' => new Size(230.0, null, true, $oColor->getLineNo())], $oColor->getColor()); $oColor = $aColorRule[1]->getValue(); - $this->assertEquals(array('r' => new Size(10.0, null, true, $oColor->getLineNo()), 'g' => new Size(100.0, null, true, $oColor->getLineNo()), 'b' => new Size(231.0, null, true, $oColor->getLineNo()), 'a' => new Size("0000.3", null, true, $oColor->getLineNo())), $oColor->getColor()); + $this->assertEquals(['r' => new Size(10.0, null, true, $oColor->getLineNo()), 'g' => new Size(100.0, null, true, $oColor->getLineNo()), 'b' => new Size(231.0, null, true, $oColor->getLineNo()), 'a' => new Size("0000.3", null, true, $oColor->getLineNo())], $oColor->getColor()); $aColorRule = $oRuleSet->getRules('outline-color'); $oColor = $aColorRule[0]->getValue(); - $this->assertEquals(array('r' => new Size(34.0, null, true, $oColor->getLineNo()), 'g' => new Size(34.0, null, true, $oColor->getLineNo()), 'b' => new Size(34.0, null, true, $oColor->getLineNo())), $oColor->getColor()); + $this->assertEquals(['r' => new Size(34.0, null, true, $oColor->getLineNo()), 'g' => new Size(34.0, null, true, $oColor->getLineNo()), 'b' => new Size(34.0, null, true, $oColor->getLineNo())], $oColor->getColor()); } elseif ($sSelector === '#yours') { $aColorRule = $oRuleSet->getRules('background-color'); $oColor = $aColorRule[0]->getValue(); - $this->assertEquals(array('h' => new Size(220.0, null, true, $oColor->getLineNo()), 's' => new Size(10.0, '%', true, $oColor->getLineNo()), 'l' => new Size(220.0, '%', true, $oColor->getLineNo())), $oColor->getColor()); + $this->assertEquals(['h' => new Size(220.0, null, true, $oColor->getLineNo()), 's' => new Size(10.0, '%', true, $oColor->getLineNo()), 'l' => new Size(220.0, '%', true, $oColor->getLineNo())], $oColor->getColor()); $oColor = $aColorRule[1]->getValue(); - $this->assertEquals(array('h' => new Size(220.0, null, true, $oColor->getLineNo()), 's' => new Size(10.0, '%', true, $oColor->getLineNo()), 'l' => new Size(220.0, '%', true, $oColor->getLineNo()), 'a' => new Size(0000.3, null, true, $oColor->getLineNo())), $oColor->getColor()); + $this->assertEquals(['h' => new Size(220.0, null, true, $oColor->getLineNo()), 's' => new Size(10.0, '%', true, $oColor->getLineNo()), 'l' => new Size(220.0, '%', true, $oColor->getLineNo()), 'a' => new Size(0000.3, null, true, $oColor->getLineNo())], $oColor->getColor()); } } foreach ($oDoc->getAllValues('color') as $sColor) { @@ -166,14 +170,14 @@ function testSpecificity() $this->fail("specificity: untested selector " . $oSelector->getSelector()); } } - $this->assertEquals(array(new Selector('#test .help', true)), $oDoc->getSelectorsBySpecificity('> 100')); - $this->assertEquals(array(new Selector('#test .help', true), new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('>= 100')); - $this->assertEquals(array(new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('=== 100')); - $this->assertEquals(array(new Selector('#file', true)), $oDoc->getSelectorsBySpecificity('== 100')); - $this->assertEquals(array(new Selector('#file', true), new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity('<= 100')); - $this->assertEquals(array(new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity('< 100')); - $this->assertEquals(array(new Selector('li.green', true)), $oDoc->getSelectorsBySpecificity('11')); - $this->assertEquals(array(new Selector('ol li::before', true)), $oDoc->getSelectorsBySpecificity(3)); + $this->assertEquals([new Selector('#test .help', true)], $oDoc->getSelectorsBySpecificity('> 100')); + $this->assertEquals([new Selector('#test .help', true), new Selector('#file', true)], $oDoc->getSelectorsBySpecificity('>= 100')); + $this->assertEquals([new Selector('#file', true)], $oDoc->getSelectorsBySpecificity('=== 100')); + $this->assertEquals([new Selector('#file', true)], $oDoc->getSelectorsBySpecificity('== 100')); + $this->assertEquals([new Selector('#file', true), new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)], $oDoc->getSelectorsBySpecificity('<= 100')); + $this->assertEquals([new Selector('.help:hover', true), new Selector('li.green', true), new Selector('ol li::before', true)], $oDoc->getSelectorsBySpecificity('< 100')); + $this->assertEquals([new Selector('li.green', true)], $oDoc->getSelectorsBySpecificity('11')); + $this->assertEquals([new Selector('ol li::before', true)], $oDoc->getSelectorsBySpecificity(3)); } function testManipulation() @@ -298,7 +302,7 @@ function testFunctionSyntax() $mValue->setSize($mValue->getSize() * 3); } } - $sExpected = str_replace(array('1.2em', '.2em', '60%'), array('3.6em', '.6em', '180%'), $sExpected); + $sExpected = str_replace(['1.2em', '.2em', '60%'], ['3.6em', '.6em', '180%'], $sExpected); $this->assertSame($sExpected, $oDoc->render()); foreach ($oDoc->getAllValues(null, true) as $mValue) { @@ -306,7 +310,7 @@ function testFunctionSyntax() $mValue->setSize($mValue->getSize() * 2); } } - $sExpected = str_replace(array('.2s', '.3s', '90deg'), array('.4s', '.6s', '180deg'), $sExpected); + $sExpected = str_replace(['.2s', '.3s', '90deg'], ['.4s', '.6s', '180deg'], $sExpected); $this->assertSame($sExpected, $oDoc->render()); } @@ -640,7 +644,7 @@ function testMissingPropertyValueLenient() $this->assertCount(1, $rulesets); $block = $rulesets[0]; $this->assertTrue($block instanceof DeclarationBlock); - $this->assertEquals(array( 'div' ), $block->getSelectors()); + $this->assertEquals(['div'], $block->getSelectors()); $rules = $block->getRules(); $this->assertCount(1, $rules); $rule = $rules[0]; @@ -670,20 +674,20 @@ function testLineNumbersParsing() { $oDoc = $this->parsedStructureForFile('line-numbers'); // array key is the expected line number - $aExpected = array( - 1 => array('Sabberworm\CSS\Property\Charset'), - 3 => array('Sabberworm\CSS\Property\CSSNamespace'), - 5 => array('Sabberworm\CSS\RuleSet\AtRuleSet'), - 11 => array('Sabberworm\CSS\RuleSet\DeclarationBlock'), + $aExpected = [ + 1 => [Charset::class], + 3 => [CSSNamespace::class], + 5 => [AtRuleSet::class], + 11 => [DeclarationBlock::class], // Line Numbers of the inner declaration blocks - 17 => array('Sabberworm\CSS\CSSList\KeyFrame', 18, 20), - 23 => array('Sabberworm\CSS\Property\Import'), - 25 => array('Sabberworm\CSS\RuleSet\DeclarationBlock') - ); + 17 => [KeyFrame::class, 18, 20], + 23 => [Import::class], + 25 => [DeclarationBlock::class] + ]; - $aActual = array(); + $aActual = []; foreach ($oDoc->getContents() as $oContent) { - $aActual[$oContent->getLineNo()] = array(get_class($oContent)); + $aActual[$oContent->getLineNo()] = [get_class($oContent)]; if ($oContent instanceof KeyFrame) { foreach ($oContent->getContents() as $block) { $aActual[$oContent->getLineNo()][] = $block->getLineNo(); @@ -691,8 +695,8 @@ function testLineNumbersParsing() } } - $aUrlExpected = array(7, 26); // expected line numbers - $aUrlActual = array(); + $aUrlExpected = [7, 26]; // expected line numbers + $aUrlActual = []; foreach ($oDoc->getAllValues() as $oValue) { if ($oValue instanceof URL) { $aUrlActual[] = $oValue->getLineNo(); @@ -700,7 +704,7 @@ function testLineNumbersParsing() } // Checking for the multiline color rule lines 27-31 - $aExpectedColorLines = array(28, 29, 30); + $aExpectedColorLines = [28, 29, 30]; $aDeclBlocks = $oDoc->getAllDeclarationBlocks(); // Choose the 2nd one $oDeclBlock = $aDeclBlocks[1]; diff --git a/tests/Sabberworm/CSS/RuleSet/DeclarationBlockTest.php b/tests/Sabberworm/CSS/RuleSet/DeclarationBlockTest.php index c088f17c..f36ea456 100644 --- a/tests/Sabberworm/CSS/RuleSet/DeclarationBlockTest.php +++ b/tests/Sabberworm/CSS/RuleSet/DeclarationBlockTest.php @@ -24,14 +24,14 @@ public function testExpandBorderShorthand($sCss, $sExpected) public function expandBorderShorthandProvider() { - return array( - array('body{ border: 2px solid #000 }', 'body {border-width: 2px;border-style: solid;border-color: #000;}'), - array('body{ border: none }', 'body {border-style: none;}'), - array('body{ border: 2px }', 'body {border-width: 2px;}'), - array('body{ border: #f00 }', 'body {border-color: #f00;}'), - array('body{ border: 1em solid }', 'body {border-width: 1em;border-style: solid;}'), - array('body{ margin: 1em; }', 'body {margin: 1em;}') - ); + return [ + ['body{ border: 2px solid #000 }', 'body {border-width: 2px;border-style: solid;border-color: #000;}'], + ['body{ border: none }', 'body {border-style: none;}'], + ['body{ border: 2px }', 'body {border-width: 2px;}'], + ['body{ border: #f00 }', 'body {border-color: #f00;}'], + ['body{ border: 1em solid }', 'body {border-width: 1em;border-style: solid;}'], + ['body{ margin: 1em; }', 'body {margin: 1em;}'] + ]; } /** @@ -49,32 +49,32 @@ public function testExpandFontShorthand($sCss, $sExpected) public function expandFontShorthandProvider() { - return array( - array( + return [ + [ 'body{ margin: 1em; }', 'body {margin: 1em;}' - ), - array( + ], + [ 'body {font: 12px serif;}', 'body {font-style: normal;font-variant: normal;font-weight: normal;font-size: 12px;line-height: normal;font-family: serif;}' - ), - array( + ], + [ 'body {font: italic 12px serif;}', 'body {font-style: italic;font-variant: normal;font-weight: normal;font-size: 12px;line-height: normal;font-family: serif;}' - ), - array( + ], + [ 'body {font: italic bold 12px serif;}', 'body {font-style: italic;font-variant: normal;font-weight: bold;font-size: 12px;line-height: normal;font-family: serif;}' - ), - array( + ], + [ 'body {font: italic bold 12px/1.6 serif;}', 'body {font-style: italic;font-variant: normal;font-weight: bold;font-size: 12px;line-height: 1.6;font-family: serif;}' - ), - array( + ], + [ 'body {font: italic small-caps bold 12px/1.6 serif;}', 'body {font-style: italic;font-variant: small-caps;font-weight: bold;font-size: 12px;line-height: 1.6;font-family: serif;}' - ), - ); + ], + ]; } /** @@ -92,14 +92,14 @@ public function testExpandBackgroundShorthand($sCss, $sExpected) public function expandBackgroundShorthandProvider() { - return array( - array('body {border: 1px;}', 'body {border: 1px;}'), - array('body {background: #f00;}', 'body {background-color: #f00;background-image: none;background-repeat: repeat;background-attachment: scroll;background-position: 0% 0%;}'), - array('body {background: #f00 url("foobar.png");}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: repeat;background-attachment: scroll;background-position: 0% 0%;}'), - array('body {background: #f00 url("foobar.png") no-repeat;}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: 0% 0%;}'), - array('body {background: #f00 url("foobar.png") no-repeat center;}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: center center;}'), - array('body {background: #f00 url("foobar.png") no-repeat top left;}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: top left;}'), - ); + return [ + ['body {border: 1px;}', 'body {border: 1px;}'], + ['body {background: #f00;}', 'body {background-color: #f00;background-image: none;background-repeat: repeat;background-attachment: scroll;background-position: 0% 0%;}'], + ['body {background: #f00 url("foobar.png");}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: repeat;background-attachment: scroll;background-position: 0% 0%;}'], + ['body {background: #f00 url("foobar.png") no-repeat;}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: 0% 0%;}'], + ['body {background: #f00 url("foobar.png") no-repeat center;}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: center center;}'], + ['body {background: #f00 url("foobar.png") no-repeat top left;}', 'body {background-color: #f00;background-image: url("foobar.png");background-repeat: no-repeat;background-attachment: scroll;background-position: top left;}'], + ]; } /** @@ -117,13 +117,13 @@ public function testExpandDimensionsShorthand($sCss, $sExpected) public function expandDimensionsShorthandProvider() { - return array( - array('body {border: 1px;}', 'body {border: 1px;}'), - array('body {margin-top: 1px;}', 'body {margin-top: 1px;}'), - array('body {margin: 1em;}', 'body {margin-top: 1em;margin-right: 1em;margin-bottom: 1em;margin-left: 1em;}'), - array('body {margin: 1em 2em;}', 'body {margin-top: 1em;margin-right: 2em;margin-bottom: 1em;margin-left: 2em;}'), - array('body {margin: 1em 2em 3em;}', 'body {margin-top: 1em;margin-right: 2em;margin-bottom: 3em;margin-left: 2em;}'), - ); + return [ + ['body {border: 1px;}', 'body {border: 1px;}'], + ['body {margin-top: 1px;}', 'body {margin-top: 1px;}'], + ['body {margin: 1em;}', 'body {margin-top: 1em;margin-right: 1em;margin-bottom: 1em;margin-left: 1em;}'], + ['body {margin: 1em 2em;}', 'body {margin-top: 1em;margin-right: 2em;margin-bottom: 1em;margin-left: 2em;}'], + ['body {margin: 1em 2em 3em;}', 'body {margin-top: 1em;margin-right: 2em;margin-bottom: 3em;margin-left: 2em;}'], + ]; } /** @@ -141,12 +141,12 @@ public function testCreateBorderShorthand($sCss, $sExpected) public function createBorderShorthandProvider() { - return array( - array('body {border-width: 2px;border-style: solid;border-color: #000;}', 'body {border: 2px solid #000;}'), - array('body {border-style: none;}', 'body {border: none;}'), - array('body {border-width: 1em;border-style: solid;}', 'body {border: 1em solid;}'), - array('body {margin: 1em;}', 'body {margin: 1em;}') - ); + return [ + ['body {border-width: 2px;border-style: solid;border-color: #000;}', 'body {border: 2px solid #000;}'], + ['body {border-style: none;}', 'body {border: none;}'], + ['body {border-width: 1em;border-style: solid;}', 'body {border: 1em solid;}'], + ['body {margin: 1em;}', 'body {margin: 1em;}'] + ]; } /** @@ -164,14 +164,14 @@ public function testCreateFontShorthand($sCss, $sExpected) public function createFontShorthandProvider() { - return array( - array('body {font-size: 12px; font-family: serif}', 'body {font: 12px serif;}'), - array('body {font-size: 12px; font-family: serif; font-style: italic;}', 'body {font: italic 12px serif;}'), - array('body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold;}', 'body {font: italic bold 12px serif;}'), - array('body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; line-height: 1.6;}', 'body {font: italic bold 12px/1.6 serif;}'), - array('body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; line-height: 1.6; font-variant: small-caps;}', 'body {font: italic small-caps bold 12px/1.6 serif;}'), - array('body {margin: 1em;}', 'body {margin: 1em;}') - ); + return [ + ['body {font-size: 12px; font-family: serif}', 'body {font: 12px serif;}'], + ['body {font-size: 12px; font-family: serif; font-style: italic;}', 'body {font: italic 12px serif;}'], + ['body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold;}', 'body {font: italic bold 12px serif;}'], + ['body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; line-height: 1.6;}', 'body {font: italic bold 12px/1.6 serif;}'], + ['body {font-size: 12px; font-family: serif; font-style: italic; font-weight: bold; line-height: 1.6; font-variant: small-caps;}', 'body {font: italic small-caps bold 12px/1.6 serif;}'], + ['body {margin: 1em;}', 'body {margin: 1em;}'] + ]; } /** @@ -189,13 +189,13 @@ public function testCreateDimensionsShorthand($sCss, $sExpected) public function createDimensionsShorthandProvider() { - return array( - array('body {border: 1px;}', 'body {border: 1px;}'), - array('body {margin-top: 1px;}', 'body {margin-top: 1px;}'), - array('body {margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;}', 'body {margin: 1em;}'), - array('body {margin-top: 1em; margin-right: 2em; margin-bottom: 1em; margin-left: 2em;}', 'body {margin: 1em 2em;}'), - array('body {margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em;}', 'body {margin: 1em 2em 3em;}'), - ); + return [ + ['body {border: 1px;}', 'body {border: 1px;}'], + ['body {margin-top: 1px;}', 'body {margin-top: 1px;}'], + ['body {margin-top: 1em; margin-right: 1em; margin-bottom: 1em; margin-left: 1em;}', 'body {margin: 1em;}'], + ['body {margin-top: 1em; margin-right: 2em; margin-bottom: 1em; margin-left: 2em;}', 'body {margin: 1em 2em;}'], + ['body {margin-top: 1em; margin-right: 2em; margin-bottom: 3em; margin-left: 2em;}', 'body {margin: 1em 2em 3em;}'], + ]; } /** @@ -213,15 +213,15 @@ public function testCreateBackgroundShorthand($sCss, $sExpected) public function createBackgroundShorthandProvider() { - return array( - array('body {border: 1px;}', 'body {border: 1px;}'), - array('body {background-color: #f00;}', 'body {background: #f00;}'), - array('body {background-color: #f00;background-image: url(foobar.png);}', 'body {background: #f00 url("foobar.png");}'), - array('body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;}', 'body {background: #f00 url("foobar.png") no-repeat;}'), - array('body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;}', 'body {background: #f00 url("foobar.png") no-repeat;}'), - array('body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;background-position: center;}', 'body {background: #f00 url("foobar.png") no-repeat center;}'), - array('body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;background-position: top left;}', 'body {background: #f00 url("foobar.png") no-repeat top left;}'), - ); + return [ + ['body {border: 1px;}', 'body {border: 1px;}'], + ['body {background-color: #f00;}', 'body {background: #f00;}'], + ['body {background-color: #f00;background-image: url(foobar.png);}', 'body {background: #f00 url("foobar.png");}'], + ['body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;}', 'body {background: #f00 url("foobar.png") no-repeat;}'], + ['body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;}', 'body {background: #f00 url("foobar.png") no-repeat;}'], + ['body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;background-position: center;}', 'body {background: #f00 url("foobar.png") no-repeat center;}'], + ['body {background-color: #f00;background-image: url(foobar.png);background-repeat: no-repeat;background-position: top left;}', 'body {background: #f00 url("foobar.png") no-repeat top left;}'], + ]; } public function testOverrideRules() @@ -235,7 +235,7 @@ public function testOverrideRules() $oWrapper = $aContents[0]; $this->assertCount(2, $oWrapper->getRules()); - $aContents[0]->setRules(array($oRule)); + $aContents[0]->setRules([$oRule]); $aRules = $oWrapper->getRules(); $this->assertCount(1, $aRules); @@ -296,12 +296,12 @@ public function testOrderOfElementsMatchingOriginalOrderAfterExpandingShorthands $oDeclaration->expandShorthands(); $this->assertEquals( - array( + [ 'padding-top' => 'padding-top: 20px;', 'padding-right' => 'padding-right: 5px;', 'padding-bottom' => 'padding-bottom: 5px;', 'padding-left' => 'padding-left: 5px;', - ), + ], array_map('strval', $oDeclaration->getRulesAssoc()) ); }