Skip to content

Commit 073f87c

Browse files
committed
Merge branch 'master' into report-memory-improvements
Conflicts: package.xml
2 parents dc67b41 + c01aee2 commit 073f87c

File tree

2 files changed

+11
-146
lines changed

2 files changed

+11
-146
lines changed

CodeSniffer/Tokenizers/CSS.php

Lines changed: 10 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public function tokenizeString($string, $eolChar='\n')
6262
for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
6363
$token = $tokens[$stackPtr];
6464

65+
// CSS files don't have lists or break tags, so convert these to
66+
// standard strings early so they can be converted into T_STYLE
67+
// tokens and joined with other strings if needed.
68+
if ($token['code'] === T_BREAK || $token['code'] === T_LIST) {
69+
$token['type'] = 'T_STRING';
70+
$token['code'] = T_STRING;
71+
}
72+
6573
if (PHP_CODESNIFFER_VERBOSITY > 1) {
6674
$type = $token['type'];
6775
$content = str_replace($eolChar, '\n', $token['content']);
@@ -373,159 +381,15 @@ public function tokenizeString($string, $eolChar='\n')
373381
/**
374382
* Performs additional processing after main tokenizing.
375383
*
376-
* This additional processing converts T_LIST tokens to T_STRING
377-
* because there are no list constructs in CSS and list-* styles
378-
* look like lists to the PHP tokenizer.
379-
*
380384
* @param array &$tokens The array of tokens to process.
381385
* @param string $eolChar The EOL character to use for splitting strings.
382386
*
383387
* @return void
384388
*/
385389
public function processAdditional(&$tokens, $eolChar)
386390
{
387-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
388-
echo "\t*** START ADDITIONAL CSS PROCESSING ***".PHP_EOL;
389-
}
390-
391-
$numTokens = (count($tokens) - 1);
392-
$changeMade = false;
393-
394-
for ($i = 0; $i < $numTokens; $i++) {
395-
if ($tokens[($i + 1)]['code'] !== T_STYLE) {
396-
continue;
397-
}
398-
399-
$style = ($i + 1);
400-
401-
if ($tokens[$i]['code'] === T_LIST) {
402-
$tokens[$style]['content'] = $tokens[$i]['content'].$tokens[$style]['content'];
403-
$tokens[$style]['column'] = $tokens[$i]['column'];
404-
405-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
406-
$line = $tokens[$i]['line'];
407-
echo "\t* T_LIST token $i on line $line merged into T_STYLE token $style *".PHP_EOL;
408-
}
409-
410-
// Now fix the brackets that surround this token as they will
411-
// be pointing too far ahead now that we have removed a token.
412-
for ($t = $i; $t >= 0; $t--) {
413-
if (isset($tokens[$t]['bracket_closer']) === true) {
414-
$old = $tokens[$t]['bracket_closer'];
415-
$tokens[$t]['bracket_closer']--;
416-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
417-
$new = $tokens[$t]['bracket_closer'];
418-
$type = $tokens[$t]['type'];
419-
$line = $tokens[$t]['line'];
420-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
421-
}
422-
423-
// Only need to fix one set of brackets.
424-
break;
425-
}
426-
}
427-
428-
// Now fix all future brackets as they are no longer pointing
429-
// to the correct tokens either.
430-
for ($t = $i; $t <= $numTokens; $t++) {
431-
if (isset($tokens[$t]) === false) {
432-
break;
433-
}
434-
435-
if ($tokens[$t]['code'] === T_OPEN_CURLY_BRACKET) {
436-
$old = $tokens[$t]['bracket_closer'];
437-
$tokens[$t]['bracket_closer']--;
438-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
439-
$new = $tokens[$t]['bracket_closer'];
440-
$type = $tokens[$t]['type'];
441-
$line = $tokens[$t]['line'];
442-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
443-
}
444-
445-
$t = $old;
446-
}
447-
}
448-
449-
unset($tokens[$i]);
450-
$changeMade = true;
451-
$i++;
452-
} else if ($tokens[$i]['code'] === T_BREAK) {
453-
// Break is sometimes used in style definitions, like page-break-inside
454-
// so we need merge the elements around it into the next T_STYLE.
455-
$newStyle = 'break'.$tokens[$style]['content'];
456-
for ($x = ($i - 1); $x >= 0; $x--) {
457-
if ($tokens[$x]['code'] !== T_STRING && $tokens[$x]['code'] !== T_MINUS) {
458-
break;
459-
}
460-
461-
$newStyle = $tokens[$x]['content'].$newStyle;
462-
}
463-
464-
$x++;
465-
$tokens[$style]['content'] = $newStyle;
466-
$tokens[$style]['column'] = $tokens[$x]['column'];
467-
468-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
469-
$line = $tokens[$i]['line'];
470-
echo "\t* tokens $x - $i on line $line merged into T_STYLE token $style due to T_BREAK at token $i *".PHP_EOL;
471-
}
472-
473-
// Now fix the brackets that surround this token as they will
474-
// be pointing too far ahead now that we have removed tokens.
475-
$diff = ($style - $x);
476-
for ($t = $style; $t >= 0; $t--) {
477-
if (isset($tokens[$t]['bracket_closer']) === true) {
478-
$old = $tokens[$t]['bracket_closer'];
479-
$tokens[$t]['bracket_closer'] -= $diff;
480-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
481-
$new = $tokens[$t]['bracket_closer'];
482-
$type = $tokens[$t]['type'];
483-
$line = $tokens[$t]['line'];
484-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
485-
}
486-
487-
// Only need to fix one set of brackets.
488-
break;
489-
}
490-
}
491-
492-
// Now fix all future brackets as they are no longer pointing
493-
// to the correct tokens either.
494-
for ($t = $style; $t <= $numTokens; $t++) {
495-
if (isset($tokens[$t]) === false) {
496-
break;
497-
}
498-
499-
if ($tokens[$t]['code'] === T_OPEN_CURLY_BRACKET) {
500-
$old = $tokens[$t]['bracket_closer'];
501-
$tokens[$t]['bracket_closer'] -= $diff;
502-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
503-
$new = $tokens[$t]['bracket_closer'];
504-
$type = $tokens[$t]['type'];
505-
$line = $tokens[$t]['line'];
506-
echo "\t\t* $type token $t on line $line closer changed from $old to $new *".PHP_EOL;
507-
}
508-
509-
$t = $old;
510-
}
511-
}
512-
513-
for ($x; $x <= $i; $x++) {
514-
unset($tokens[$x]);
515-
}
516-
517-
$changeMade = true;
518-
$i++;
519-
}//end if
520-
}//end for
521-
522-
if ($changeMade === true) {
523-
$tokens = array_values($tokens);
524-
}
525-
526-
if (PHP_CODESNIFFER_VERBOSITY > 1) {
527-
echo "\t*** END ADDITIONAL CSS PROCESSING ***".PHP_EOL;
528-
}
391+
// We override this method because we don't want the PHP version to
392+
// run during CSS processing because it is wasted processing time.
529393

530394
}//end processAdditional()
531395

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
3939
- Fixed problem where some file content could be ignored when checking STDIN
4040
- Version information is now printed when installed via composer or run from a Git clone (request #20050)
4141
- Added Squiz DisallowBooleanStatementSniff to ban boolean operators outside of control structure conditions
42+
- The CSS tokenizer is now more reliable when encountering 'list' and 'break' strings
4243
- Coding standard ignore comments can now appear instead doc blocks as well as inline comments
4344
-- Thanks to Stuart Langley for the patch
4445
- Generic LineLengthSniff now ignores SVN URL and Head URL comments

0 commit comments

Comments
 (0)