Skip to content

Commit ed1faae

Browse files
committed
T_PHPCS_SET tokens now have indexes so sniffs can accessed the parsed info
1 parent e33a0e4 commit ed1faae

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

src/Files/File.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -392,26 +392,13 @@ public function process()
392392
} else if (substr($commentTextLower, 0, 9) === 'phpcs:set'
393393
|| substr($commentTextLower, 0, 10) === '@phpcs:set'
394394
) {
395-
// If the @phpcs: syntax is being used, strip the @ to make
396-
// comparisons easier.
397-
if ($commentText[0] === '@') {
398-
$commentText = substr($commentText, 1);
399-
}
400-
401-
// Need to maintain case here, to get the correct sniff code.
402-
$parts = explode(' ', substr($commentText, 10));
403-
if (count($parts) >= 2) {
404-
$sniffParts = explode('.', $parts[0]);
405-
if (count($sniffParts) >= 3) {
406-
// If the sniff code is not known to us, it has not been registered in this run.
407-
// But don't throw an error as it could be there for a different standard to use.
408-
if (isset($this->ruleset->sniffCodes[$parts[0]]) === true) {
409-
$listenerCode = array_shift($parts);
410-
$propertyCode = array_shift($parts);
411-
$propertyValue = rtrim(implode(' ', $parts), " */\r\n");
412-
$listenerClass = $this->ruleset->sniffCodes[$listenerCode];
413-
$this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue);
414-
}
395+
if (isset($token['sniffCode']) === true) {
396+
$listenerCode = $token['sniffCode'];
397+
if (isset($this->ruleset->sniffCodes[$listenerCode]) === true) {
398+
$propertyCode = $token['sniffProperty'];
399+
$propertyValue = $token['sniffPropertyValue'];
400+
$listenerClass = $this->ruleset->sniffCodes[$listenerCode];
401+
$this->ruleset->setSniffProperty($listenerClass, $propertyCode, $propertyValue);
415402
}
416403
}
417404
}//end if

src/Tokenizers/Tokenizer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ private function createPositionMap()
369369
$this->ignoredLines[$this->tokens[$i]['line']] = ['.all' => true];
370370
}
371371

372+
// Need to maintain case here, to get the correct sniff code.
373+
$parts = explode(' ', substr($commentText, 10));
374+
if (count($parts) >= 2) {
375+
$sniffParts = explode('.', $parts[0]);
376+
if (count($sniffParts) >= 3) {
377+
$this->tokens[$i]['sniffCode'] = array_shift($parts);
378+
$this->tokens[$i]['sniffProperty'] = array_shift($parts);
379+
$this->tokens[$i]['sniffPropertyValue'] = rtrim(implode(' ', $parts), " */\r\n");
380+
}
381+
}
382+
372383
$this->tokens[$i]['code'] = T_PHPCS_SET;
373384
$this->tokens[$i]['type'] = 'T_PHPCS_SET';
374385
} else if (substr($commentTextLower, 0, 16) === 'phpcs:ignorefile') {

0 commit comments

Comments
 (0)