Skip to content

Commit 516b322

Browse files
authored
[TASK] Prefix native function calls with a backslash (#632)
This slightly improves performance.
1 parent 27aa880 commit 516b322

26 files changed

+220
-220
lines changed

bin/quickdump.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

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

10-
$sSource = file_get_contents('php://stdin');
10+
$sSource = \file_get_contents('php://stdin');
1111
$oParser = new Sabberworm\CSS\Parser($sSource);
1212

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

1717
echo "\n```\n\n" . '#### Structure (`var_dump()`)' . "\n\n```php\n";
18-
var_dump($oDoc);
18+
\var_dump($oDoc);
1919

2020
echo "\n```\n\n" . '#### Output (`render()`)' . "\n\n```css\n";
2121
print $oDoc->render();

src/CSSList/CSSBlockList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ protected function allSelectors(array &$aResult, $sSpecificitySearch = null)
107107
$aResult[] = $oSelector;
108108
} else {
109109
$sComparator = '===';
110-
$aSpecificitySearch = explode(' ', $sSpecificitySearch);
110+
$aSpecificitySearch = \explode(' ', $sSpecificitySearch);
111111
$iTargetSpecificity = $aSpecificitySearch[0];
112-
if (count($aSpecificitySearch) > 1) {
112+
if (\count($aSpecificitySearch) > 1) {
113113
$sComparator = $aSpecificitySearch[0];
114114
$iTargetSpecificity = $aSpecificitySearch[1];
115115
}

src/CSSList/CSSList.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public function __construct($iLineNo = 0)
6363
public static function parseList(ParserState $oParserState, CSSList $oList): void
6464
{
6565
$bIsRoot = $oList instanceof Document;
66-
if (is_string($oParserState)) {
66+
if (\is_string($oParserState)) {
6767
$oParserState = new ParserState($oParserState, Settings::create());
6868
}
6969
$bLenientParsing = $oParserState->getSettings()->bLenientParsing;
7070
$aComments = [];
7171
while (!$oParserState->isEnd()) {
72-
$aComments = array_merge($aComments, $oParserState->consumeWhiteSpace());
72+
$aComments = \array_merge($aComments, $oParserState->consumeWhiteSpace());
7373
$oListItem = null;
7474
if ($bLenientParsing) {
7575
try {
@@ -117,7 +117,7 @@ private static function parseListItem(ParserState $oParserState, CSSList $oList)
117117
$oParserState->currentLine()
118118
);
119119
}
120-
if (count($oList->getContents()) > 0) {
120+
if (\count($oList->getContents()) > 0) {
121121
throw new UnexpectedTokenException(
122122
'@charset must be the first parseable token in a document',
123123
'',
@@ -164,7 +164,7 @@ private static function parseAtRule(ParserState $oParserState)
164164
$oParserState->consumeWhiteSpace();
165165
$sMediaQuery = null;
166166
if (!$oParserState->comes(';')) {
167-
$sMediaQuery = trim($oParserState->consumeUntil([';', ParserState::EOF]));
167+
$sMediaQuery = \trim($oParserState->consumeUntil([';', ParserState::EOF]));
168168
}
169169
$oParserState->consumeUntil([';', ParserState::EOF], true, true);
170170
return new Import($oLocation, $sMediaQuery ?: null, $iIdentifierLineNum);
@@ -176,7 +176,7 @@ private static function parseAtRule(ParserState $oParserState)
176176
} elseif (self::identifierIs($sIdentifier, 'keyframes')) {
177177
$oResult = new KeyFrame($iIdentifierLineNum);
178178
$oResult->setVendorKeyFrame($sIdentifier);
179-
$oResult->setAnimationName(trim($oParserState->consumeUntil('{', false, true)));
179+
$oResult->setAnimationName(\trim($oParserState->consumeUntil('{', false, true)));
180180
CSSList::parseList($oParserState, $oResult);
181181
if ($oParserState->comes('}')) {
182182
$oParserState->consume('}');
@@ -190,7 +190,7 @@ private static function parseAtRule(ParserState $oParserState)
190190
$mUrl = Value::parsePrimitiveValue($oParserState);
191191
}
192192
$oParserState->consumeUntil([';', ParserState::EOF], true, true);
193-
if ($sPrefix !== null && !is_string($sPrefix)) {
193+
if ($sPrefix !== null && !\is_string($sPrefix)) {
194194
throw new UnexpectedTokenException('Wrong namespace prefix', $sPrefix, 'custom', $iIdentifierLineNum);
195195
}
196196
if (!($mUrl instanceof CSSString || $mUrl instanceof URL)) {
@@ -204,16 +204,16 @@ private static function parseAtRule(ParserState $oParserState)
204204
return new CSSNamespace($mUrl, $sPrefix, $iIdentifierLineNum);
205205
} else {
206206
// Unknown other at rule (font-face or such)
207-
$sArgs = trim($oParserState->consumeUntil('{', false, true));
208-
if (substr_count($sArgs, "(") != substr_count($sArgs, ")")) {
207+
$sArgs = \trim($oParserState->consumeUntil('{', false, true));
208+
if (\substr_count($sArgs, "(") != \substr_count($sArgs, ")")) {
209209
if ($oParserState->getSettings()->bLenientParsing) {
210210
return null;
211211
} else {
212212
throw new SourceException("Unmatched brace count in media query", $oParserState->currentLine());
213213
}
214214
}
215215
$bUseRuleSet = true;
216-
foreach (explode('/', AtRule::BLOCK_RULES) as $sBlockRuleName) {
216+
foreach (\explode('/', AtRule::BLOCK_RULES) as $sBlockRuleName) {
217217
if (self::identifierIs($sIdentifier, $sBlockRuleName)) {
218218
$bUseRuleSet = false;
219219
break;
@@ -242,8 +242,8 @@ private static function parseAtRule(ParserState $oParserState)
242242
*/
243243
private static function identifierIs($sIdentifier, $sMatch): bool
244244
{
245-
return (strcasecmp($sIdentifier, $sMatch) === 0)
246-
?: preg_match("/^(-\\w+-)?$sMatch$/i", $sIdentifier) === 1;
245+
return (\strcasecmp($sIdentifier, $sMatch) === 0)
246+
?: \preg_match("/^(-\\w+-)?$sMatch$/i", $sIdentifier) === 1;
247247
}
248248

249249
/**
@@ -261,7 +261,7 @@ public function getLineNo()
261261
*/
262262
public function prepend($oItem): void
263263
{
264-
array_unshift($this->aContents, $oItem);
264+
\array_unshift($this->aContents, $oItem);
265265
}
266266

267267
/**
@@ -283,7 +283,7 @@ public function append($oItem): void
283283
*/
284284
public function splice($iOffset, $iLength = null, $mReplacement = null): void
285285
{
286-
array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
286+
\array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
287287
}
288288

289289
/**
@@ -295,7 +295,7 @@ public function splice($iOffset, $iLength = null, $mReplacement = null): void
295295
*/
296296
public function insertBefore($item, $sibling): void
297297
{
298-
if (in_array($sibling, $this->aContents, true)) {
298+
if (\in_array($sibling, $this->aContents, true)) {
299299
$this->replace($sibling, [$item, $sibling]);
300300
} else {
301301
$this->append($item);
@@ -313,7 +313,7 @@ public function insertBefore($item, $sibling): void
313313
*/
314314
public function remove($oItemToRemove)
315315
{
316-
$iKey = array_search($oItemToRemove, $this->aContents, true);
316+
$iKey = \array_search($oItemToRemove, $this->aContents, true);
317317
if ($iKey !== false) {
318318
unset($this->aContents[$iKey]);
319319
return true;
@@ -332,12 +332,12 @@ public function remove($oItemToRemove)
332332
*/
333333
public function replace($oOldItem, $mNewItem)
334334
{
335-
$iKey = array_search($oOldItem, $this->aContents, true);
335+
$iKey = \array_search($oOldItem, $this->aContents, true);
336336
if ($iKey !== false) {
337-
if (is_array($mNewItem)) {
338-
array_splice($this->aContents, $iKey, 1, $mNewItem);
337+
if (\is_array($mNewItem)) {
338+
\array_splice($this->aContents, $iKey, 1, $mNewItem);
339339
} else {
340-
array_splice($this->aContents, $iKey, 1, [$mNewItem]);
340+
\array_splice($this->aContents, $iKey, 1, [$mNewItem]);
341341
}
342342
return true;
343343
}
@@ -366,8 +366,8 @@ public function removeDeclarationBlockBySelector($mSelector, $bRemoveAll = false
366366
if ($mSelector instanceof DeclarationBlock) {
367367
$mSelector = $mSelector->getSelectors();
368368
}
369-
if (!is_array($mSelector)) {
370-
$mSelector = explode(',', $mSelector);
369+
if (!\is_array($mSelector)) {
370+
$mSelector = \explode(',', $mSelector);
371371
}
372372
foreach ($mSelector as $iKey => &$mSel) {
373373
if (!($mSel instanceof Selector)) {
@@ -456,7 +456,7 @@ public function getContents()
456456
*/
457457
public function addComments(array $aComments): void
458458
{
459-
$this->aComments = array_merge($this->aComments, $aComments);
459+
$this->aComments = \array_merge($this->aComments, $aComments);
460460
}
461461

462462
/**

src/CSSList/Document.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function getAllValues($mElement = null, $bSearchInFunctionArguments = fal
7878
$sSearchString = null;
7979
if ($mElement === null) {
8080
$mElement = $this;
81-
} elseif (is_string($mElement)) {
81+
} elseif (\is_string($mElement)) {
8282
$sSearchString = $mElement;
8383
$mElement = $this;
8484
}

src/OutputFormat.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function get($sName)
176176
{
177177
$aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i'];
178178
foreach ($aVarPrefixes as $sPrefix) {
179-
$sFieldName = $sPrefix . ucfirst($sName);
179+
$sFieldName = $sPrefix . \ucfirst($sName);
180180
if (isset($this->$sFieldName)) {
181181
return $this->$sFieldName;
182182
}
@@ -193,20 +193,20 @@ public function get($sName)
193193
public function set($aNames, $mValue)
194194
{
195195
$aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i'];
196-
if (is_string($aNames) && strpos($aNames, '*') !== false) {
196+
if (\is_string($aNames) && \strpos($aNames, '*') !== false) {
197197
$aNames =
198198
[
199-
str_replace('*', 'Before', $aNames),
200-
str_replace('*', 'Between', $aNames),
201-
str_replace('*', 'After', $aNames),
199+
\str_replace('*', 'Before', $aNames),
200+
\str_replace('*', 'Between', $aNames),
201+
\str_replace('*', 'After', $aNames),
202202
];
203-
} elseif (!is_array($aNames)) {
203+
} elseif (!\is_array($aNames)) {
204204
$aNames = [$aNames];
205205
}
206206
foreach ($aVarPrefixes as $sPrefix) {
207207
$bDidReplace = false;
208208
foreach ($aNames as $sName) {
209-
$sFieldName = $sPrefix . ucfirst($sName);
209+
$sFieldName = $sPrefix . \ucfirst($sName);
210210
if (isset($this->$sFieldName)) {
211211
$this->$sFieldName = $mValue;
212212
$bDidReplace = true;
@@ -230,12 +230,12 @@ public function set($aNames, $mValue)
230230
*/
231231
public function __call($sMethodName, array $aArguments)
232232
{
233-
if (strpos($sMethodName, 'set') === 0) {
234-
return $this->set(substr($sMethodName, 3), $aArguments[0]);
235-
} elseif (strpos($sMethodName, 'get') === 0) {
236-
return $this->get(substr($sMethodName, 3));
237-
} elseif (method_exists(OutputFormatter::class, $sMethodName)) {
238-
return call_user_func_array([$this->getFormatter(), $sMethodName], $aArguments);
233+
if (\strpos($sMethodName, 'set') === 0) {
234+
return $this->set(\substr($sMethodName, 3), $aArguments[0]);
235+
} elseif (\strpos($sMethodName, 'get') === 0) {
236+
return $this->get(\substr($sMethodName, 3));
237+
} elseif (\method_exists(OutputFormatter::class, $sMethodName)) {
238+
return \call_user_func_array([$this->getFormatter(), $sMethodName], $aArguments);
239239
} else {
240240
throw new \Exception('Unknown OutputFormat method called: ' . $sMethodName);
241241
}
@@ -248,7 +248,7 @@ public function __call($sMethodName, array $aArguments)
248248
*/
249249
public function indentWithTabs($iNumber = 1)
250250
{
251-
return $this->setIndentation(str_repeat("\t", $iNumber));
251+
return $this->setIndentation(\str_repeat("\t", $iNumber));
252252
}
253253

254254
/**
@@ -258,7 +258,7 @@ public function indentWithTabs($iNumber = 1)
258258
*/
259259
public function indentWithSpaces($iNumber = 2)
260260
{
261-
return $this->setIndentation(str_repeat(" ", $iNumber));
261+
return $this->setIndentation(\str_repeat(" ", $iNumber));
262262
}
263263

264264
/**

src/OutputFormatter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public function space($sName, $sType = null): string
2626
$sSpaceString = $this->oFormat->get("Space$sName");
2727
// If $sSpaceString is an array, we have multiple values configured
2828
// depending on the type of object the space applies to
29-
if (is_array($sSpaceString)) {
29+
if (\is_array($sSpaceString)) {
3030
if ($sType !== null && isset($sSpaceString[$sType])) {
3131
$sSpaceString = $sSpaceString[$sType];
3232
} else {
33-
$sSpaceString = reset($sSpaceString);
33+
$sSpaceString = \reset($sSpaceString);
3434
}
3535
}
3636
return $this->prepareSpace($sSpaceString);
@@ -169,14 +169,14 @@ public function removeLastSemicolon($sString)
169169
if ($this->oFormat->get('SemicolonAfterLastRule')) {
170170
return $sString;
171171
}
172-
$sString = explode(';', $sString);
173-
if (count($sString) < 2) {
172+
$sString = \explode(';', $sString);
173+
if (\count($sString) < 2) {
174174
return $sString[0];
175175
}
176-
$sLast = array_pop($sString);
177-
$sNextToLast = array_pop($sString);
178-
array_push($sString, $sNextToLast . $sLast);
179-
return implode(';', $sString);
176+
$sLast = \array_pop($sString);
177+
$sNextToLast = \array_pop($sString);
178+
\array_push($sString, $sNextToLast . $sLast);
179+
return \implode(';', $sString);
180180
}
181181

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

194194
$sResult = '';
195195
$aComments = $oCommentable->getComments();
196-
$iLastCommentIndex = count($aComments) - 1;
196+
$iLastCommentIndex = \count($aComments) - 1;
197197

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

213213
/**
214214
* @return string
215215
*/
216216
private function indent(): string
217217
{
218-
return str_repeat($this->oFormat->sIndentation, $this->oFormat->level());
218+
return \str_repeat($this->oFormat->sIndentation, $this->oFormat->level());
219219
}
220220
}

0 commit comments

Comments
 (0)