You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: wiki/Version-4.0-Developer-Upgrade-Guide.md
+36-36Lines changed: 36 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,7 +51,7 @@ There is a separate [[Upgrade Guide for Ruleset Maintainers and End-Users|Versio
51
51
52
52
It is highly recommended to upgrade external standards and integrations as soon as you are able.
53
53
54
-
Once PHP_CodeSniffer 4.0 has been released, the PHP_CodeSniffer 3.x branch will no longer receive updates, with the exception of security fixes and runtime compatibility fixes for new PHP versions.
54
+
Once PHP_CodeSniffer 4.0 has been released, the PHP_CodeSniffer 3.x branch will no longer receive updates, with the exception of security fixes and runtime compatibility fixes for new PHP versions.
55
55
This "limited support" will last a maximum of one year from the date of the PHP_CodeSniffer 4.0.0 release.
56
56
57
57
> [!IMPORTANT]
@@ -106,7 +106,7 @@ Please carefully read the naming conventions for standards and sniffs as outline
106
106
107
107
... either directly or indirectly via an abstract sniff. This is now actively enforced.
108
108
109
-
PHP_CodeSniffer 3.13.0 will throw a deprecation notice for any sniffs which don't implement the interface.
109
+
PHP_CodeSniffer 3.13.0 will throw a deprecation notice for any sniffs which don't implement the interface.
110
110
As of PHP_CodeSniffer 4.0.0, PHP_CodeSniffer will error out on such sniffs.
111
111
112
112
<palign="right"><ahref="#table-of-contents">back to top</a></p>
@@ -116,9 +116,9 @@ As of PHP_CodeSniffer 4.0.0, PHP_CodeSniffer will error out on such sniffs.
116
116
117
117
Support for the JS/CSS tokenizers has been removed. To cause the least amount of friction, this has been executed as follows:
118
118
119
-
* Sniffs which specify the `$supportedTokenizers` property and don't include `'PHP'` in the array value will no longer run on PHPCS 4.0.
120
-
The end-user will be shown an error message about this and PHP_CodeSniffer will exit with this error.
121
-
This error message can be silenced by implementing the `PHP_CodeSniffer\Sniffs\DeprecatedSniff` interface (PHPCS 3.9.0+) to mark the sniff as deprecated.
119
+
* Sniffs which specify the `$supportedTokenizers` property and don't include `'PHP'` in the array value will no longer run on PHPCS 4.0.
120
+
The end-user will be shown an error message about this and PHP_CodeSniffer will exit with this error.
121
+
This error message can be silenced by implementing the `PHP_CodeSniffer\Sniffs\DeprecatedSniff` interface (PHPCS 3.9.0+) to mark the sniff as deprecated.
122
122
In that case, the sniff will show a deprecation notice when running on PHPCS 3.x and the sniff will be silently ignored on PHPCS 4.x.
123
123
* Sniffs which specify the `$supportedTokenizers` property and include both `'PHP'` as well as something else, will run against the scanned files, with all files being treated as PHP, as of PHPCS 4.0.0 and will not cause any notices about the sniff to be shown to the end-user.
124
124
@@ -171,10 +171,10 @@ Additionally, when a name could not be determined, like during live coding, the
171
171
It is recommended to search your codebase for all uses of the `File::getDeclarationName()` method and to review whether the code needs updating.
172
172
173
173
Typically:
174
-
* Verify that the sniff does not pass anonymous constructs to the method.
174
+
* Verify that the sniff does not pass anonymous constructs to the method.
175
175
If necessary, either add guard-code to prevent this, or wrap the method call in a `try ... catch`.
176
-
* Verify if the return value of the method is checked correctly.
177
-
For PHPCS cross-version compatibility, check the return value with `empty($name)`.
176
+
* Verify if the return value of the method is checked correctly.
177
+
For PHPCS cross-version compatibility, check the return value with `empty($name)`.
178
178
If cross-version compatibility is not a concern, replace checks against `null` with a check against an empty string.
@@ -228,7 +228,7 @@ Typically, this may impact projects which call `new Config` consecutive times pr
228
228
229
229
##### Upgrading
230
230
231
-
Typical work-arounds for the old behaviour will use `Reflection` to reset the `Config::$overriddenDefaults` property between instantiations.
231
+
Typical work-arounds for the old behaviour will use `Reflection` to reset the `Config::$overriddenDefaults` property between instantiations.
232
232
These type of work-arounds can now be removed.
233
233
234
234
<p align="right"><a href="#table-of-contents">back to top</a></p>
@@ -302,8 +302,8 @@ The following tokens have been removed:
302
302
303
303
##### Upgrading
304
304
305
-
If these tokens are used in PHP-only sniffs, they can be safely removed.
306
-
If these tokens are used in JS/CSS only sniffs, as mentioned in ["Support for JS/CSS has been removed"](#support-for-jscss-has-been-removed): deprecate or remove the sniff.
305
+
If these tokens are used in PHP-only sniffs, they can be safely removed.
306
+
If these tokens are used in JS/CSS only sniffs, as mentioned in ["Support for JS/CSS has been removed"](#support-for-jscss-has-been-removed): deprecate or remove the sniff.
307
307
If these tokens are used in mixed sniffs, which also scan PHP files, remove the CSS/JS specific code or, for cross-version compatibility, check for the existance of the tokens before using them.
308
308
309
309
<p align="right"><a href="#table-of-contents">back to top</a></p>
@@ -317,10 +317,10 @@ These tokens have been added to the `Tokens::$parenthesisOpeners` array and will
317
317
318
318
##### Upgrading
319
319
320
-
* Check if any of your sniffs/code uses the `Tokens::$parenthesisOpeners` array.
321
-
It is generally discouraged to use that token array as it is mostly intended for internal use by PHPCS itself, but if you do use it, be aware that the above mentioned tokens have been added to the array.
320
+
* Check if any of your sniffs/code uses the `Tokens::$parenthesisOpeners` array.
321
+
It is generally discouraged to use that token array as it is mostly intended for internal use by PHPCS itself, but if you do use it, be aware that the above mentioned tokens have been added to the array.
322
322
You may want to exclude them from your sniff, or in case of `T_USE`, you may need to validate that it is a closure `use` token and not an import/trait `use` token before acting on the token.
323
-
* Search your codebase for `T_USE`, `T_ISSET`, `T_UNSET`, `T_EMPTY`, `T_EVAL`, `T_EXIT`.
323
+
* Search your codebase for `T_USE`, `T_ISSET`, `T_UNSET`, `T_EMPTY`, `T_EVAL`, `T_EXIT`.
324
324
Anywhere the parentheses for those tokens are "manually" determined, either for use in the sniff or to skip over them, you can now use the `parenthesis_owner`, `parenthesis_opener` and `parenthesis_closer` keys instead.
325
325
326
326
<p align="right"><a href="#table-of-contents">back to top</a></p>
@@ -330,7 +330,7 @@ These tokens have been added to the `Tokens::$parenthesisOpeners` array and will
330
330
331
331
The tokenization of identifier names has changed.
332
332
333
-
This change was made in PHP itself in 8.0 and was "undone" in PHP_CodeSniffer for the PHPCS 3.x releases.
333
+
This change was made in PHP itself in 8.0 and was "undone" in PHP_CodeSniffer for the PHPCS 3.x releases.
334
334
As of PHPCS 4.0, identifier names will tokenize following the PHP 8.0+ tokenization.
335
335
336
336
The following type of code is affected:
@@ -373,7 +373,7 @@ This re-tokenization has now been removed.
373
373
374
374
#### T_OPEN_TAG
375
375
376
-
Long open tags will no longer include any whitespace. Previously the `T_OPEN_TAG` when used for long open tags could potentially include a single space or a new line character.
376
+
Long open tags will no longer include any whitespace. Previously the `T_OPEN_TAG` when used for long open tags could potentially include a single space or a new line character.
377
377
Any potential whitespace previously included will now be tokenized as `T_WHITESPACE` following the normal whitespace tokenization rules.
378
378
379
379
> [!NOTE]
@@ -418,10 +418,10 @@ doSomething();
418
418
419
419
#### Other Tokenizer Changes
420
420
421
-
* All `T_DOC_COMMENT_*` tokens will now have the `comment_opener` and `comment_closer` indexes set.
421
+
* All `T_DOC_COMMENT_*` tokens will now have the `comment_opener` and `comment_closer` indexes set.
422
422
This should allow for more flexibility for sniffs examining aspects of docblocks.
423
423
424
-
* The `Tokens::FUNCTION_NAME_TOKENS` token array, as well as the deprecated `Tokens::$functionNameTokens`, now also contains the `T_ANON_CLASS` token.
424
+
* The `Tokens::FUNCTION_NAME_TOKENS` token array, as well as the deprecated `Tokens::$functionNameTokens`, now also contains the `T_ANON_CLASS` token.
425
425
If you use this token array in your sniffs, you will need to evaluate whether this is a desired change or will need excluding.
426
426
427
427
<palign="right"><ahref="#table-of-contents">back to top</a></p>
@@ -431,7 +431,7 @@ doSomething();
431
431
432
432
The `AbstractPatternSniff::__construct()` method no longer takes any arguments. The `$ignoreComments` parameter was deprecated in PHPCS 1.4.0.
433
433
434
-
Since PHPCS 1.4.0, the AbstractPatternSniff sets the `ignoreComments` option using a `public` var rather than through the constructor.
434
+
Since PHPCS 1.4.0, the AbstractPatternSniff sets the `ignoreComments` option using a `public` var rather than through the constructor.
435
435
This allows the setting to be overwritten in `ruleset.xml` files.
436
436
437
437
#### Upgrading
@@ -490,11 +490,11 @@ The `protected` `getDeclarationNameWithNamespace()` and `getNamespaceOfScope()`
490
490
491
491
* The signature of the `DummyFile::setErrorCounts()` method has changed and now expects the following parameters: `$errorCount, $warningCount, $fixableErrorCount, $fixableWarningCount, $fixedErrorCount, $fixedWarningCount`.
492
492
493
-
* The abstract `PHP_CodeSniffer\Filters\ExactMatch::getBlacklist()` and `PHP_CodeSniffer\Filters\ExactMatch::getWhitelist()` methods have been replaced with abstract `PHP_CodeSniffer\ExactMatch::getDisallowedFiles()` and `PHP_CodeSniffer\ExactMatch::getAllowedFiles()` methods.
494
-
If you have custom classes which extend the `ExactMatch` class, implement the new `getDisallowedFiles()` and `getAllowedFiles()` methods instead.
493
+
* The abstract `PHP_CodeSniffer\Filters\ExactMatch::getBlacklist()` and `PHP_CodeSniffer\Filters\ExactMatch::getWhitelist()` methods have been replaced with abstract `PHP_CodeSniffer\ExactMatch::getDisallowedFiles()` and `PHP_CodeSniffer\ExactMatch::getAllowedFiles()` methods.
494
+
If you have custom classes which extend the `ExactMatch` class, implement the new `getDisallowedFiles()` and `getAllowedFiles()` methods instead.
495
495
Note: this is a name change only. The functionality remains the same.
496
496
497
-
* Various `print*(): void` methods in the Generator classes have been removed in favour of `get*(): string` methods.
497
+
* Various `print*(): void` methods in the Generator classes have been removed in favour of `get*(): string` methods.
498
498
If you extend any of the `Generator` classes...:
499
499
500
500
| Search for | Replace with |
@@ -514,16 +514,16 @@ The `protected` `getDeclarationNameWithNamespace()` and `getNamespaceOfScope()`
514
514
515
515
* The `PHP_CodeSniffer\Reporter::$startTime` property has been removed. This property was unused since PHPCS 3.0.0.
516
516
517
-
* The `PHP_CodeSniffer\Reporter::$totalFixable` and `Reporter::$totalFixed` properties are deprecated and should no longer be used.
517
+
* The `PHP_CodeSniffer\Reporter::$totalFixable` and `Reporter::$totalFixed` properties are deprecated and should no longer be used.
518
518
Use respectively `(Reporter::$totalFixableErrors + Reporter::$totalFixableWarnings)` and `(Reporter::$totalFixedErrors + Reporter::$totalFixedWarnings)` instead.
519
519
520
-
* `PHP_CodeSniffer\Ruleset::setSniffProperty()`: the BC-layer supporting the old array format for the `$settings` parameter for the method has been removed.
521
-
The `$settings` parameter must be passed as an array with the following two keys: `'scope'` and `'value'`, with `'scope'` being set to either `'sniff'` or `'standard'`, and `'value'` containing the new property value.
520
+
* `PHP_CodeSniffer\Ruleset::setSniffProperty()`: the BC-layer supporting the old array format for the `$settings` parameter for the method has been removed.
521
+
The `$settings` parameter must be passed as an array with the following two keys: `'scope'` and `'value'`, with `'scope'` being set to either `'sniff'` or `'standard'`, and `'value'` containing the new property value.
522
522
Also see [squizlabs/PHP_CodeSniffer#3629](https://github.com/squizlabs/PHP_CodeSniffer/pull/3629).
523
523
524
-
* Various class properties have been replaced with class constants. Where these were in the public API (= the below list), the properties still exist, but are now (soft) deprecated and will be removed in PHP_CodeSniffer 5.0.
525
-
The visibility of the (deprecated) properties and their class constant replacements is the same.
526
-
If you previously overloaded one of these properties in a custom sniff extending one of the affected sniffs, you will now need to overload the class constant.
524
+
* Various class properties have been replaced with class constants. Where these were in the public API (= the below list), the properties still exist, but are now (soft) deprecated and will be removed in PHP_CodeSniffer 5.0.
525
+
The visibility of the (deprecated) properties and their class constant replacements is the same.
526
+
If you previously overloaded one of these properties in a custom sniff extending one of the affected sniffs, you will now need to overload the class constant.
527
527
To obtain cross-version compatibility with PHPCS 3.x as well as 4.x, you may need to overload both the property as well as the constant.
528
528
529
529
| Search for | Replace with | Notes |
@@ -575,8 +575,8 @@ Typically, these type of work-around can be found by searching for calls to the
575
575
External standards which use the PHP_CodeSniffer native test framework as the basis for the sniff tests need to be aware of the following changes.
576
576
If an external standard uses its own test framework, this section can be skipped.
577
577
578
-
* The test setup now supports PHPUnit 8, 9, 10 and 11, which is in line with the new minimum PHP version of 7.2.
579
-
Please read the changelogs for PHPUnit itself for information about the changes.
578
+
* The test setup now supports PHPUnit 8, 9, 10 and 11, which is in line with the new minimum PHP version of 7.2.
579
+
Please read the changelogs for PHPUnit itself for information about the changes.
580
580
Most notable changes which will likely impact your tests:
581
581
* The test suite may need a separate PHPUnit config file for PHPUnit < 10 and PHPUnit 10+.
582
582
* The custom `TestSuite` setup has been removed from the framework as it is no longer needed since PEAR support was dropped and was incompatible with PHPUnit 10.
@@ -587,17 +587,17 @@ If an external standard uses its own test framework, this section can be skipped
* Fixture methods no longer use the `@before|after[Class]` annotations, but use the PHPUnit `setUp|tearDown[BeforeClass|AfterClass]()` methods again.
590
+
* Fixture methods no longer use the `@before|after[Class]` annotations, but use the PHPUnit `setUp|tearDown[BeforeClass|AfterClass]()` methods again.
591
591
These methods now have `void` return type declarations.
592
592
* The global `printPHPCodeSnifferTestOutput()` function, which printed a "# sniff test files generated # unique error codes; # were fixable (#%)" summary after the tests is no longer available.
593
593
* The following properties which were previously available in the `AbstractSniffUnitTest` class have been removed:
594
594
* `protected $backupGlobals`
595
595
* `public $standardsDir`
596
596
* `public $testsDir`
597
-
The first is (very) old-school PHPUnit and should not be used anyway.
597
+
The first is (very) old-school PHPUnit and should not be used anyway.
598
598
The last two were never intended to be overwritten by concrete test classes, so shouldn't have been public properties.
599
599
* The following property which was previously available in the `AbstractMethodUnitTest` class has been removed:
600
-
* `protected static $fileExtension`
600
+
* `protected static $fileExtension`
601
601
This property is redundant now support for JS/CSS files has been dropped.
602
602
* The test framework no longer uses global variables. I.e. the following are no longer available:
603
603
* `$GLOBALS['PHP_CODESNIFFER_STANDARD_DIRS']`
@@ -616,10 +616,10 @@ This is not really any different from before, just even more important now.
616
616
#### Upgrading
617
617
618
618
In practice this means the following for most test suites for external standards which extend the PHP_CodeSniffer native test suite:
619
-
1. In `composer.json`: update the PHPUnit version requirements.
619
+
1. In `composer.json`: update the PHPUnit version requirements.
620
620
The PHP_CodeSniffer native test framework supports PHPUnit `^8.0 || ^9.3.4 || ^10.5.32 || ^11.3.3` as of PHP_CodeSniffer 4.0.0.
621
621
2. In the `phpunit.xml[.dist]` file: ensure that this file contains a `<testsuites>` element which points to your test directory/directories.
622
-
3. In the test bootstrap file: register your own standard and any other external standards your standard needs with the autoloader.
622
+
3. In the test bootstrap file: register your own standard and any other external standards your standard needs with the autoloader.
623
623
Typically, this can be done using the following code snippet after loading the PHPCS `autoload.php` file:
@@ -638,7 +638,7 @@ In practice this means the following for most test suites for external standards
638
638
+final class YourSniffNameUnitTest extends AbstractSniffTestCase {
639
639
```
640
640
External standards which want to support PHP_CodeSniffer 3 and 4 and run their tests on both could consider aliasing the name of the test case class in their test bootstrap file.
641
-
6. Make sure that all `inc` test case files which test fixers are accompanied by a `.inc.fixed` file.
641
+
6. Make sure that all `inc` test case files which test fixers are accompanied by a `.inc.fixed` file.
642
642
In PHPCS 3.x, the test framework would already warn about any missing `.fixed` files. This has now become an error which will fail a test run.
643
643
7. For running the tests: do not pass the `./vendor/squizlabs/php_codesniffer/tests/AllTests.php` file anymore and the `--filter ExternalStndName` argument should also no longer be needed. Run the tests for your external standard by calling plain `phpunit`.
0 commit comments