Skip to content

Commit addad25

Browse files
jrfnlgsherwood
andcommitted
Ruleset: drop support for sniffs setting the $supportedTokenizers property
Ref: * squizlabs/PHP_CodeSniffer 2448 Co-authored-by: Greg Sherwood <[email protected]>
1 parent f2e1d78 commit addad25

15 files changed

+154
-293
lines changed

src/Files/File.php

+6-28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHP_CodeSniffer\Exceptions\TokenizerException;
1515
use PHP_CodeSniffer\Fixer;
1616
use PHP_CodeSniffer\Ruleset;
17+
use PHP_CodeSniffer\Tokenizers\PHP;
1718
use PHP_CodeSniffer\Util\Common;
1819
use PHP_CodeSniffer\Util\Tokens;
1920

@@ -72,17 +73,10 @@ class File
7273
/**
7374
* The tokenizer being used for this file.
7475
*
75-
* @var \PHP_CodeSniffer\Tokenizers\Tokenizer
76+
* @var \PHP_CodeSniffer\Tokenizers\PHP
7677
*/
7778
public $tokenizer = null;
7879

79-
/**
80-
* The name of the tokenizer being used for this file.
81-
*
82-
* @var string
83-
*/
84-
public $tokenizerType = 'PHP';
85-
8680
/**
8781
* Was the file loaded from cache?
8882
*
@@ -240,15 +234,6 @@ public function __construct($path, Ruleset $ruleset, Config $config)
240234
$this->config = $config;
241235
$this->fixer = new Fixer();
242236

243-
$parts = explode('.', $path);
244-
$extension = array_pop($parts);
245-
if (isset($config->extensions[$extension]) === true) {
246-
$this->tokenizerType = $config->extensions[$extension];
247-
} else {
248-
// Revert to default.
249-
$this->tokenizerType = 'PHP';
250-
}
251-
252237
$this->configCache['cache'] = $this->config->cache;
253238
$this->configCache['sniffs'] = array_map('strtolower', $this->config->sniffs);
254239
$this->configCache['exclude'] = array_map('strtolower', $this->config->exclude);
@@ -412,14 +397,8 @@ public function process()
412397
continue;
413398
}
414399

415-
// Make sure this sniff supports the tokenizer
416-
// we are currently using.
417400
$class = $listenerData['class'];
418401

419-
if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) {
420-
continue;
421-
}
422-
423402
if (trim($this->path, '\'"') !== 'STDIN') {
424403
// If the file path matches one of our ignore patterns, skip it.
425404
// While there is support for a type of each pattern
@@ -505,7 +484,7 @@ public function process()
505484
// We don't show this error for STDIN because we can't be sure the content
506485
// actually came directly from the user. It could be something like
507486
// refs from a Git pre-push hook.
508-
if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN') {
487+
if ($foundCode === false && $this->path !== 'STDIN') {
509488
$shortTags = (bool) ini_get('short_open_tag');
510489
if ($shortTags === false) {
511490
$error = 'No PHP code was found in this file and short open tags are not allowed by this install of PHP. This file may be using short open tags but PHP does not allow them.';
@@ -543,14 +522,13 @@ public function parse()
543522
}
544523

545524
try {
546-
$tokenizerClass = 'PHP_CodeSniffer\Tokenizers\\'.$this->tokenizerType;
547-
$this->tokenizer = new $tokenizerClass($this->content, $this->config, $this->eolChar);
525+
$this->tokenizer = new PHP($this->content, $this->config, $this->eolChar);
548526
$this->tokens = $this->tokenizer->getTokens();
549527
} catch (TokenizerException $e) {
550528
$this->ignored = true;
551529
$this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception');
552530
if (PHP_CODESNIFFER_VERBOSITY > 0) {
553-
echo "[$this->tokenizerType => tokenizer error]... ";
531+
echo '[tokenizer error]... ';
554532
if (PHP_CODESNIFFER_VERBOSITY > 1) {
555533
echo PHP_EOL;
556534
}
@@ -582,7 +560,7 @@ public function parse()
582560
$numLines = $this->tokens[($this->numTokens - 1)]['line'];
583561
}
584562

585-
echo "[$this->tokenizerType => $this->numTokens tokens in $numLines lines]... ";
563+
echo "[$this->numTokens tokens in $numLines lines]... ";
586564
if (PHP_CODESNIFFER_VERBOSITY > 1) {
587565
echo PHP_EOL;
588566
}

src/Ruleset.php

+25-35
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,27 @@ public function registerSniffs($files, $restrictions, $exclusions)
14251425
continue;
14261426
}
14271427

1428+
if ($reflection->hasProperty('supportedTokenizers') === true) {
1429+
// Using the default value as the class is not yet instantiated and this is not a property which should get changed anyway.
1430+
$value = $reflection->getDefaultProperties()['supportedTokenizers'];
1431+
1432+
if (is_array($value) === true
1433+
&& empty($value) === false
1434+
&& in_array('PHP', $value, true) === false
1435+
) {
1436+
if ($reflection->implementsInterface('PHP_CodeSniffer\\Sniffs\\DeprecatedSniff') === true) {
1437+
// Silently ignore the sniff if the sniff is marked as deprecated.
1438+
continue;
1439+
}
1440+
1441+
$message = 'Support for scanning files other than PHP, like CSS/JS files, has been removed in PHP_CodeSniffer 4.0.'.PHP_EOL;
1442+
$message .= 'The %s sniff is listening for %s.';
1443+
$message = sprintf($message, Common::getSniffCode($className), implode(', ', $value));
1444+
$this->msgCache->add($message, MessageCollector::ERROR);
1445+
continue;
1446+
}
1447+
}//end if
1448+
14281449
$listeners[$className] = $className;
14291450

14301451
if (PHP_CODESNIFFER_VERBOSITY > 2) {
@@ -1464,9 +1485,7 @@ public function populateTokenListeners()
14641485
$this->sniffs[$sniffClass] = new $sniffClass();
14651486
$this->sniffCodes[$sniffCode] = $sniffClass;
14661487

1467-
$isDeprecated = false;
14681488
if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) {
1469-
$isDeprecated = true;
14701489
$this->deprecatedSniffs[$sniffCode] = $sniffClass;
14711490
}
14721491

@@ -1477,34 +1496,6 @@ public function populateTokenListeners()
14771496
}
14781497
}
14791498

1480-
$tokenizers = [];
1481-
$vars = get_class_vars($sniffClass);
1482-
if (empty($vars['supportedTokenizers']) === false
1483-
&& $isDeprecated === false
1484-
&& in_array('PHP', $vars['supportedTokenizers'], true) === false
1485-
) {
1486-
if (in_array('CSS', $vars['supportedTokenizers'], true) === true
1487-
|| in_array('JS', $vars['supportedTokenizers'], true) === true
1488-
) {
1489-
$message = 'Scanning CSS/JS files is deprecated and support will be removed in PHP_CodeSniffer 4.0.'.PHP_EOL;
1490-
} else {
1491-
// Just in case someone has an integration with a custom tokenizer.
1492-
$message = 'Support for custom tokenizers will be removed in PHP_CodeSniffer 4.0.'.PHP_EOL;
1493-
}
1494-
1495-
$message .= 'The %s sniff is listening for %s.';
1496-
$message = sprintf($message, $sniffCode, implode(', ', $vars['supportedTokenizers']));
1497-
$this->msgCache->add($message, MessageCollector::DEPRECATED);
1498-
}
1499-
1500-
if (isset($vars['supportedTokenizers']) === true) {
1501-
foreach ($vars['supportedTokenizers'] as $tokenizer) {
1502-
$tokenizers[$tokenizer] = $tokenizer;
1503-
}
1504-
} else {
1505-
$tokenizers = ['PHP' => 'PHP'];
1506-
}
1507-
15081499
$tokens = $this->sniffs[$sniffClass]->register();
15091500
if (is_array($tokens) === false) {
15101501
$msg = "The sniff {$sniffClass}::register() method must return an array.";
@@ -1544,11 +1535,10 @@ public function populateTokenListeners()
15441535

15451536
if (isset($this->tokenListeners[$token][$sniffClass]) === false) {
15461537
$this->tokenListeners[$token][$sniffClass] = [
1547-
'class' => $sniffClass,
1548-
'source' => $sniffCode,
1549-
'tokenizers' => $tokenizers,
1550-
'ignore' => $ignorePatterns,
1551-
'include' => $includePatterns,
1538+
'class' => $sniffClass,
1539+
'source' => $sniffCode,
1540+
'ignore' => $ignorePatterns,
1541+
'include' => $includePatterns,
15521542
];
15531543
}
15541544
}

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ImplementsDeprecatedInterfaceSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ListensForCSSAndJSSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ListensForCSSAndUnrecognizedSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ListensForCSSSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ListensForEmptySniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ListensForJSSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ListensForPHPAndCSSAndJSSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/Fixtures/TestStandard/Sniffs/SupportedTokenizers/ListensForUnrecognizedTokenizersSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Test fixture.
44
*
5-
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersSupportedTokenizersTest
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\RegisterSniffsRemovedTokenizersTest
66
*/
77

88
namespace Fixtures\TestStandard\Sniffs\SupportedTokenizers;

tests/Core/Ruleset/PopulateTokenListenersSupportedTokenizersTest.php

-111
This file was deleted.

0 commit comments

Comments
 (0)