Skip to content

Remove CSS/JS support (HUGE PR, reviews welcome!) #983

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 9 commits into from
Apr 14, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 2 additions & 4 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"dictionaries": ["php"],
"allowCompoundWords": true,
"ignorePaths": [
"src/Standards/*/Tests/*/*.{inc,js,css}",
"src/Standards/*/Tests/*/*.{inc,js,css}.fixed"
"src/Standards/*/Tests/*/*.inc",
"src/Standards/*/Tests/*/*.inc.fixed"
],
"ignoreRegExpList": ["email", "^\\s+\\*\\s+@(author|copyright)\\s+.*$"],
"words": [
Expand All @@ -28,9 +28,7 @@
"dnumber",
"docblock",
"encapsed",
"gjslint",
"groupid",
"gsjlint",
"hashbang",
"heredoc",
"hgblame",
Expand Down
2 changes: 0 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ To help you with this, a number of convenience scripts are available:
which will be placed in a `build/coverage-html` subdirectory.
* `composer build` will build the phpcs.phar and phpcbf.phar files.

N.B.: You can ignore any skipped tests as these are for external tools.


### Writing Tests

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/quicktest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ concurrency:
jobs:
#### QUICK TEST ####
# This is a much quicker test run which only runs the unit tests against the low/medium/high
# supported PHP versions and skips the PHAR test and the tests for external JS/CSS tooling.
# supported PHP versions and skips the PHAR test.
# These are basically the same builds as in the Test->Coverage workflow, but then without doing
# the code-coverage.
quicktest:
Expand Down
30 changes: 0 additions & 30 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,6 @@ jobs:
- name: "DEBUG: show libxml loaded version (php)"
run: php -r 'echo "libxml loaded version = ", LIBXML_LOADED_VERSION, PHP_EOL;'

# This action also handles the caching of the dependencies.
- name: Set up node
if: ${{ matrix.custom_ini == false }}
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install external tools used in tests
if: ${{ matrix.custom_ini == false }}
run: >
npm install -g --fund false
csslint
eslint
jshint

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
- name: Install Composer dependencies
Expand Down Expand Up @@ -291,21 +276,6 @@ jobs:
ini-values: error_reporting=-1, display_errors=On${{ steps.set_ini.outputs.PHP_INI }}
coverage: xdebug

# This action also handles the caching of the dependencies.
- name: Set up node
if: ${{ matrix.custom_ini == false }}
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install external tools used in tests
if: ${{ matrix.custom_ini == false }}
run: >
npm install -g --fund false
csslint
eslint
jshint

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
- name: Install Composer dependencies
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

## About

PHP_CodeSniffer is a set of two PHP scripts; the main `phpcs` script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second `phpcbf` script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.
PHP_CodeSniffer is a set of two PHP scripts; the main `phpcs` script that tokenizes PHP files to detect violations of a defined coding standard, and a second `phpcbf` script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.


## Requirements
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "squizlabs/php_codesniffer",
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"description": "PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.",
"license": "BSD-3-Clause",
"type": "library",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<file>src</file>
<file>tests</file>

<exclude-pattern>*/src/Standards/*/Tests/*\.(inc|css|js)$</exclude-pattern>
<exclude-pattern>*/tests/Core/*/*\.(inc|css|js)$</exclude-pattern>
<exclude-pattern>*/src/Standards/*/Tests/*\.inc$</exclude-pattern>
<exclude-pattern>*/tests/Core/*/*\.inc$</exclude-pattern>
<exclude-pattern>*/tests/Core/*/Fixtures/*\.php$</exclude-pattern>

<arg name="basepath" value="."/>
Expand Down
25 changes: 13 additions & 12 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@
* @property string $stdinPath The path to use for content passed on STDIN.
* @property bool $trackTime Whether or not to track sniff run time.
*
* @property array<string, string> $extensions File extensions that should be checked, and what tokenizer to use.
* @property array<string, string> $extensions File extensions that should be checked, and what tokenizer is used.
* E.g., array('inc' => 'PHP');
* Note: since PHPCS 4.0.0, the tokenizer used will always be 'PHP',
* but the array format of the property has not been changed to prevent
* breaking integrations which may be accessing this property.
* @property array<string, string|null> $reports The reports to use for printing output after the run.
* The format of the array is:
* array(
Expand Down Expand Up @@ -545,8 +548,6 @@ public function restoreDefaults()
$this->extensions = [
'php' => 'PHP',
'inc' => 'PHP',
'js' => 'JS',
'css' => 'CSS',
];
$this->sniffs = [];
$this->exclude = [];
Expand Down Expand Up @@ -1146,19 +1147,19 @@ public function processLongArgument($arg, $pos)
if (empty($extensionsString) === false) {
$extensions = explode(',', $extensionsString);
foreach ($extensions as $ext) {
$slash = strpos($ext, '/');
if ($slash !== false) {
if (strpos($ext, '/') !== false) {
// They specified the tokenizer too.
list($ext, $tokenizer) = explode('/', $ext);
$newExtensions[$ext] = strtoupper($tokenizer);
continue;
if (strtoupper($tokenizer) !== 'PHP') {
$error = 'ERROR: Specifying the tokenizer to use for an extension is no longer supported.'.PHP_EOL;
$error .= 'PHP_CodeSniffer >= 4.0 only supports scanning PHP files.'.PHP_EOL;
$error .= 'Received: '.substr($arg, 11).PHP_EOL.PHP_EOL;
$error .= $this->printShortUsage(true);
throw new DeepExitException($error, 3);
}
}

if (isset($this->extensions[$ext]) === true) {
$newExtensions[$ext] = $this->extensions[$ext];
} else {
$newExtensions[$ext] = 'PHP';
}
$newExtensions[$ext] = 'PHP';
}
}

Expand Down
77 changes: 22 additions & 55 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHP_CodeSniffer\Exceptions\TokenizerException;
use PHP_CodeSniffer\Fixer;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tokenizers\PHP;
use PHP_CodeSniffer\Util\Common;
use PHP_CodeSniffer\Util\Tokens;

Expand Down Expand Up @@ -72,17 +73,10 @@ class File
/**
* The tokenizer being used for this file.
*
* @var \PHP_CodeSniffer\Tokenizers\Tokenizer
* @var \PHP_CodeSniffer\Tokenizers\PHP
*/
public $tokenizer = null;

/**
* The name of the tokenizer being used for this file.
*
* @var string
*/
public $tokenizerType = 'PHP';

/**
* Was the file loaded from cache?
*
Expand Down Expand Up @@ -240,15 +234,6 @@ public function __construct($path, Ruleset $ruleset, Config $config)
$this->config = $config;
$this->fixer = new Fixer();

$parts = explode('.', $path);
$extension = array_pop($parts);
if (isset($config->extensions[$extension]) === true) {
$this->tokenizerType = $config->extensions[$extension];
} else {
// Revert to default.
$this->tokenizerType = 'PHP';
}

$this->configCache['cache'] = $this->config->cache;
$this->configCache['sniffs'] = array_map('strtolower', $this->config->sniffs);
$this->configCache['exclude'] = array_map('strtolower', $this->config->exclude);
Expand Down Expand Up @@ -412,14 +397,8 @@ public function process()
continue;
}

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

if (isset($listenerData['tokenizers'][$this->tokenizerType]) === false) {
continue;
}

if (trim($this->path, '\'"') !== 'STDIN') {
// If the file path matches one of our ignore patterns, skip it.
// While there is support for a type of each pattern
Expand Down Expand Up @@ -505,7 +484,7 @@ public function process()
// We don't show this error for STDIN because we can't be sure the content
// actually came directly from the user. It could be something like
// refs from a Git pre-push hook.
if ($foundCode === false && $this->tokenizerType === 'PHP' && $this->path !== 'STDIN') {
if ($foundCode === false && $this->path !== 'STDIN') {
$shortTags = (bool) ini_get('short_open_tag');
if ($shortTags === false) {
$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.';
Expand Down Expand Up @@ -543,14 +522,13 @@ public function parse()
}

try {
$tokenizerClass = 'PHP_CodeSniffer\Tokenizers\\'.$this->tokenizerType;
$this->tokenizer = new $tokenizerClass($this->content, $this->config, $this->eolChar);
$this->tokenizer = new PHP($this->content, $this->config, $this->eolChar);
$this->tokens = $this->tokenizer->getTokens();
} catch (TokenizerException $e) {
$this->ignored = true;
$this->addWarning($e->getMessage(), null, 'Internal.Tokenizer.Exception');
if (PHP_CODESNIFFER_VERBOSITY > 0) {
echo "[$this->tokenizerType => tokenizer error]... ";
echo '[tokenizer error]... ';
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
}
Expand Down Expand Up @@ -582,7 +560,7 @@ public function parse()
$numLines = $this->tokens[($this->numTokens - 1)]['line'];
}

echo "[$this->tokenizerType => $this->numTokens tokens in $numLines lines]... ";
echo "[$this->numTokens tokens in $numLines lines]... ";
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL;
}
Expand Down Expand Up @@ -1249,14 +1227,6 @@ public function getDeclarationName($stackPtr)
throw new RuntimeException('Token type "'.$this->tokens[$stackPtr]['type'].'" is not T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
}

if ($tokenCode === T_FUNCTION
&& strtolower($this->tokens[$stackPtr]['content']) !== 'function'
) {
// This is a function declared without the "function" keyword.
// So this token is the function name.
return $this->tokens[$stackPtr]['content'];
}

$stopPoint = $this->numTokens;
if (isset($this->tokens[$stackPtr]['parenthesis_opener']) === true) {
// For functions, stop searching at the parenthesis opener.
Expand Down Expand Up @@ -1637,22 +1607,20 @@ public function getMethodProperties($stackPtr)

if ($this->tokens[$stackPtr]['code'] === T_FUNCTION) {
$valid = [
T_PUBLIC => T_PUBLIC,
T_PRIVATE => T_PRIVATE,
T_PROTECTED => T_PROTECTED,
T_STATIC => T_STATIC,
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
T_DOC_COMMENT => T_DOC_COMMENT,
T_PUBLIC => T_PUBLIC,
T_PRIVATE => T_PRIVATE,
T_PROTECTED => T_PROTECTED,
T_STATIC => T_STATIC,
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
];
} else {
$valid = [
T_STATIC => T_STATIC,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
T_DOC_COMMENT => T_DOC_COMMENT,
T_STATIC => T_STATIC,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
];
}

Expand Down Expand Up @@ -2011,12 +1979,11 @@ public function getClassProperties($stackPtr)
}

$valid = [
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
T_DOC_COMMENT => T_DOC_COMMENT,
T_FINAL => T_FINAL,
T_ABSTRACT => T_ABSTRACT,
T_READONLY => T_READONLY,
T_WHITESPACE => T_WHITESPACE,
T_COMMENT => T_COMMENT,
];

$isAbstract = false;
Expand Down
Loading
Loading