Skip to content

[TASK] Prefix native function calls with a backslash #632

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

Merged
merged 1 commit into from
Jul 1, 2024
Merged
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
4 changes: 2 additions & 2 deletions bin/quickdump.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

require_once(__DIR__ . '/../vendor/autoload.php');

$sSource = file_get_contents('php://stdin');
$sSource = \file_get_contents('php://stdin');
$oParser = new Sabberworm\CSS\Parser($sSource);

$oDoc = $oParser->parse();
echo "\n" . '#### Input' . "\n\n```css\n";
print $sSource;

echo "\n```\n\n" . '#### Structure (`var_dump()`)' . "\n\n```php\n";
var_dump($oDoc);
\var_dump($oDoc);

echo "\n```\n\n" . '#### Output (`render()`)' . "\n\n```css\n";
print $oDoc->render();
Expand Down
4 changes: 2 additions & 2 deletions src/CSSList/CSSBlockList.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ protected function allSelectors(array &$aResult, $sSpecificitySearch = null)
$aResult[] = $oSelector;
} else {
$sComparator = '===';
$aSpecificitySearch = explode(' ', $sSpecificitySearch);
$aSpecificitySearch = \explode(' ', $sSpecificitySearch);
$iTargetSpecificity = $aSpecificitySearch[0];
if (count($aSpecificitySearch) > 1) {
if (\count($aSpecificitySearch) > 1) {
$sComparator = $aSpecificitySearch[0];
$iTargetSpecificity = $aSpecificitySearch[1];
}
Expand Down
44 changes: 22 additions & 22 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public function __construct($iLineNo = 0)
public static function parseList(ParserState $oParserState, CSSList $oList): void
{
$bIsRoot = $oList instanceof Document;
if (is_string($oParserState)) {
if (\is_string($oParserState)) {
$oParserState = new ParserState($oParserState, Settings::create());
}
$bLenientParsing = $oParserState->getSettings()->bLenientParsing;
$aComments = [];
while (!$oParserState->isEnd()) {
$aComments = array_merge($aComments, $oParserState->consumeWhiteSpace());
$aComments = \array_merge($aComments, $oParserState->consumeWhiteSpace());
$oListItem = null;
if ($bLenientParsing) {
try {
Expand Down Expand Up @@ -117,7 +117,7 @@ private static function parseListItem(ParserState $oParserState, CSSList $oList)
$oParserState->currentLine()
);
}
if (count($oList->getContents()) > 0) {
if (\count($oList->getContents()) > 0) {
throw new UnexpectedTokenException(
'@charset must be the first parseable token in a document',
'',
Expand Down Expand Up @@ -164,7 +164,7 @@ private static function parseAtRule(ParserState $oParserState)
$oParserState->consumeWhiteSpace();
$sMediaQuery = null;
if (!$oParserState->comes(';')) {
$sMediaQuery = trim($oParserState->consumeUntil([';', ParserState::EOF]));
$sMediaQuery = \trim($oParserState->consumeUntil([';', ParserState::EOF]));
}
$oParserState->consumeUntil([';', ParserState::EOF], true, true);
return new Import($oLocation, $sMediaQuery ?: null, $iIdentifierLineNum);
Expand All @@ -176,7 +176,7 @@ private static function parseAtRule(ParserState $oParserState)
} elseif (self::identifierIs($sIdentifier, 'keyframes')) {
$oResult = new KeyFrame($iIdentifierLineNum);
$oResult->setVendorKeyFrame($sIdentifier);
$oResult->setAnimationName(trim($oParserState->consumeUntil('{', false, true)));
$oResult->setAnimationName(\trim($oParserState->consumeUntil('{', false, true)));
CSSList::parseList($oParserState, $oResult);
if ($oParserState->comes('}')) {
$oParserState->consume('}');
Expand All @@ -190,7 +190,7 @@ private static function parseAtRule(ParserState $oParserState)
$mUrl = Value::parsePrimitiveValue($oParserState);
}
$oParserState->consumeUntil([';', ParserState::EOF], true, true);
if ($sPrefix !== null && !is_string($sPrefix)) {
if ($sPrefix !== null && !\is_string($sPrefix)) {
throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom', $iIdentifierLineNum);
}
if (!($mUrl instanceof CSSString || $mUrl instanceof URL)) {
Expand All @@ -204,16 +204,16 @@ private static function parseAtRule(ParserState $oParserState)
return new CSSNamespace($mUrl, $sPrefix, $iIdentifierLineNum);
} else {
// Unknown other at rule (font-face or such)
$sArgs = trim($oParserState->consumeUntil('{', false, true));
if (substr_count($sArgs, "(") != substr_count($sArgs, ")")) {
$sArgs = \trim($oParserState->consumeUntil('{', false, true));
if (\substr_count($sArgs, "(") != \substr_count($sArgs, ")")) {
if ($oParserState->getSettings()->bLenientParsing) {
return null;
} else {
throw new SourceException("Unmatched brace count in media query", $oParserState->currentLine());
}
}
$bUseRuleSet = true;
foreach (explode('/', AtRule::BLOCK_RULES) as $sBlockRuleName) {
foreach (\explode('/', AtRule::BLOCK_RULES) as $sBlockRuleName) {
if (self::identifierIs($sIdentifier, $sBlockRuleName)) {
$bUseRuleSet = false;
break;
Expand Down Expand Up @@ -242,8 +242,8 @@ private static function parseAtRule(ParserState $oParserState)
*/
private static function identifierIs($sIdentifier, $sMatch): bool
{
return (strcasecmp($sIdentifier, $sMatch) === 0)
?: preg_match("/^(-\\w+-)?$sMatch$/i", $sIdentifier) === 1;
return (\strcasecmp($sIdentifier, $sMatch) === 0)
?: \preg_match("/^(-\\w+-)?$sMatch$/i", $sIdentifier) === 1;
}

/**
Expand All @@ -261,7 +261,7 @@ public function getLineNo()
*/
public function prepend($oItem): void
{
array_unshift($this->aContents, $oItem);
\array_unshift($this->aContents, $oItem);
}

/**
Expand All @@ -283,7 +283,7 @@ public function append($oItem): void
*/
public function splice($iOffset, $iLength = null, $mReplacement = null): void
{
array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
\array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
}

/**
Expand All @@ -295,7 +295,7 @@ public function splice($iOffset, $iLength = null, $mReplacement = null): void
*/
public function insertBefore($item, $sibling): void
{
if (in_array($sibling, $this->aContents, true)) {
if (\in_array($sibling, $this->aContents, true)) {
$this->replace($sibling, [$item, $sibling]);
} else {
$this->append($item);
Expand All @@ -313,7 +313,7 @@ public function insertBefore($item, $sibling): void
*/
public function remove($oItemToRemove)
{
$iKey = array_search($oItemToRemove, $this->aContents, true);
$iKey = \array_search($oItemToRemove, $this->aContents, true);
if ($iKey !== false) {
unset($this->aContents[$iKey]);
return true;
Expand All @@ -332,12 +332,12 @@ public function remove($oItemToRemove)
*/
public function replace($oOldItem, $mNewItem)
{
$iKey = array_search($oOldItem, $this->aContents, true);
$iKey = \array_search($oOldItem, $this->aContents, true);
if ($iKey !== false) {
if (is_array($mNewItem)) {
array_splice($this->aContents, $iKey, 1, $mNewItem);
if (\is_array($mNewItem)) {
\array_splice($this->aContents, $iKey, 1, $mNewItem);
} else {
array_splice($this->aContents, $iKey, 1, [$mNewItem]);
\array_splice($this->aContents, $iKey, 1, [$mNewItem]);
}
return true;
}
Expand Down Expand Up @@ -366,8 +366,8 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
if ($mSelector instanceof DeclarationBlock) {
$mSelector = $mSelector->getSelectors();
}
if (!is_array($mSelector)) {
$mSelector = explode(',', $mSelector);
if (!\is_array($mSelector)) {
$mSelector = \explode(',', $mSelector);
}
foreach ($mSelector as $iKey => &$mSel) {
if (!($mSel instanceof Selector)) {
Expand Down Expand Up @@ -456,7 +456,7 @@ public function getContents()
*/
public function addComments(array $aComments): void
{
$this->aComments = array_merge($this->aComments, $aComments);
$this->aComments = \array_merge($this->aComments, $aComments);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/CSSList/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function getAllValues($mElement = null, $bSearchInFunctionArguments = fal
$sSearchString = null;
if ($mElement === null) {
$mElement = $this;
} elseif (is_string($mElement)) {
} elseif (\is_string($mElement)) {
$sSearchString = $mElement;
$mElement = $this;
}
Expand Down
30 changes: 15 additions & 15 deletions src/OutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function get($sName)
{
$aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i'];
foreach ($aVarPrefixes as $sPrefix) {
$sFieldName = $sPrefix . ucfirst($sName);
$sFieldName = $sPrefix . \ucfirst($sName);
if (isset($this->$sFieldName)) {
return $this->$sFieldName;
}
Expand All @@ -193,20 +193,20 @@ public function get($sName)
public function set($aNames, $mValue)
{
$aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i'];
if (is_string($aNames) && strpos($aNames, '*') !== false) {
if (\is_string($aNames) && \strpos($aNames, '*') !== false) {
$aNames =
[
str_replace('*', 'Before', $aNames),
str_replace('*', 'Between', $aNames),
str_replace('*', 'After', $aNames),
\str_replace('*', 'Before', $aNames),
\str_replace('*', 'Between', $aNames),
\str_replace('*', 'After', $aNames),
];
} elseif (!is_array($aNames)) {
} elseif (!\is_array($aNames)) {
$aNames = [$aNames];
}
foreach ($aVarPrefixes as $sPrefix) {
$bDidReplace = false;
foreach ($aNames as $sName) {
$sFieldName = $sPrefix . ucfirst($sName);
$sFieldName = $sPrefix . \ucfirst($sName);
if (isset($this->$sFieldName)) {
$this->$sFieldName = $mValue;
$bDidReplace = true;
Expand All @@ -230,12 +230,12 @@ public function set($aNames, $mValue)
*/
public function __call($sMethodName, array $aArguments)
{
if (strpos($sMethodName, 'set') === 0) {
return $this->set(substr($sMethodName, 3), $aArguments[0]);
} elseif (strpos($sMethodName, 'get') === 0) {
return $this->get(substr($sMethodName, 3));
} elseif (method_exists(OutputFormatter::class, $sMethodName)) {
return call_user_func_array([$this->getFormatter(), $sMethodName], $aArguments);
if (\strpos($sMethodName, 'set') === 0) {
return $this->set(\substr($sMethodName, 3), $aArguments[0]);
} elseif (\strpos($sMethodName, 'get') === 0) {
return $this->get(\substr($sMethodName, 3));
} elseif (\method_exists(OutputFormatter::class, $sMethodName)) {
return \call_user_func_array([$this->getFormatter(), $sMethodName], $aArguments);
} else {
throw new \Exception('Unknown OutputFormat method called: ' . $sMethodName);
}
Expand All @@ -248,7 +248,7 @@ public function __call($sMethodName, array $aArguments)
*/
public function indentWithTabs($iNumber = 1)
{
return $this->setIndentation(str_repeat("\t", $iNumber));
return $this->setIndentation(\str_repeat("\t", $iNumber));
}

/**
Expand All @@ -258,7 +258,7 @@ public function indentWithTabs($iNumber = 1)
*/
public function indentWithSpaces($iNumber = 2)
{
return $this->setIndentation(str_repeat(" ", $iNumber));
return $this->setIndentation(\str_repeat(" ", $iNumber));
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function space($sName, $sType = null): string
$sSpaceString = $this->oFormat->get("Space$sName");
// If $sSpaceString is an array, we have multiple values configured
// depending on the type of object the space applies to
if (is_array($sSpaceString)) {
if (\is_array($sSpaceString)) {
if ($sType !== null && isset($sSpaceString[$sType])) {
$sSpaceString = $sSpaceString[$sType];
} else {
$sSpaceString = reset($sSpaceString);
$sSpaceString = \reset($sSpaceString);
}
}
return $this->prepareSpace($sSpaceString);
Expand Down Expand Up @@ -169,14 +169,14 @@ public function removeLastSemicolon($sString)
if ($this->oFormat->get('SemicolonAfterLastRule')) {
return $sString;
}
$sString = explode(';', $sString);
if (count($sString) < 2) {
$sString = \explode(';', $sString);
if (\count($sString) < 2) {
return $sString[0];
}
$sLast = array_pop($sString);
$sNextToLast = array_pop($sString);
array_push($sString, $sNextToLast . $sLast);
return implode(';', $sString);
$sLast = \array_pop($sString);
$sNextToLast = \array_pop($sString);
\array_push($sString, $sNextToLast . $sLast);
return \implode(';', $sString);
}

/**
Expand All @@ -193,7 +193,7 @@ public function comments(Commentable $oCommentable): string

$sResult = '';
$aComments = $oCommentable->getComments();
$iLastCommentIndex = count($aComments) - 1;
$iLastCommentIndex = \count($aComments) - 1;

foreach ($aComments as $i => $oComment) {
$sResult .= $oComment->render($this->oFormat);
Expand All @@ -207,14 +207,14 @@ public function comments(Commentable $oCommentable): string
*/
private function prepareSpace($sSpaceString): string
{
return str_replace("\n", "\n" . $this->indent(), $sSpaceString);
return \str_replace("\n", "\n" . $this->indent(), $sSpaceString);
}

/**
* @return string
*/
private function indent(): string
{
return str_repeat($this->oFormat->sIndentation, $this->oFormat->level());
return \str_repeat($this->oFormat->sIndentation, $this->oFormat->level());
}
}
Loading