Skip to content

Commit 8edf020

Browse files
committed
4.0 dev upgrade guide: update for the change in tokenization of fully qualified exit/die/true/false/null
As per the proposal in PHPCSStandards/PHP_CodeSniffer 1201 and executed in PHPCSStandards/PHP_CodeSniffer 1206 (for 3.x, the implementation will change for 4.x in the "merge-up" to the 4.x branch.
1 parent 8412a46 commit 8edf020

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

wiki/Version-4.0-Developer-Upgrade-Guide.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,32 @@ The `Tokens::FUNCTION_NAME_TOKENS` token array, as well as the deprecated `Token
326326

327327
Typically, search your code base for use of the `Tokens::$functionNameTokens` token array, `T_NS_SEPARATOR`, `T_NAMESPACE` and `T_STRING` tokens and review whether the sniff/code is examining identifier names. If so, update the code to allow for the new tokens.
328328

329-
You may also want to search for checks involving `T_TRUE`, `T_FALSE` and `T_NULL` tokens, as if these are used in a fully qualified form, they will now tokenize as `T_NAME_FULLY_QUALIFIED`, so sniffs may need to take this into account.
329+
<p align="right"><a href="#table-of-contents">back to top</a></p>
330+
331+
332+
#### :new: Fully qualified `\exit`, `\die`, `\true`, `\false`, `\null`
333+
334+
There are only a few keywords which can legitimately be used in their fully qualified form: `\exit`, `\die` (since PHP 8.4), `\true`, `\false` and `\null`.
335+
And for `\true`, `\false` and `\null`, this only applies to their use as a value. PHP does not allow the fully qualified form in type declarations.
336+
337+
Prior to PHP_CodeSniffer 3.13.3, these keywords in their fully qualified form would be tokenized as `T_NS_SEPARATOR` + `T_STRING`.
338+
This was [changed in PHP_CodeSniffer 3.13.3](https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1201) to `T_NS_SEPARATOR` + _keyword token_.
339+
340+
As of PHP_CodeSniffer 4.0, these keywords will always tokenize to their respective _keyword token_ and the `'content'` of the keyword token will include the namespace separator if the keyword was coded as fully qualified.
341+
342+
In other words, these are the tokenization changes related to the fully qualified form of these keywords:
343+
344+
| | PHPCS <= 3.13.2 | PHPCS 3.x (3.13.3+) | PHPCS 4.0+ |
345+
|-----------|-------------------------------|------------------------------|-----------------------------------------------------------------------|
346+
| `\exit()` | `T_NS_SEPARATOR` + `T_STRING` | `T_NS_SEPARATOR` + `T_EXIT` | `T_EXIT` (content: `\exit`, the parentheses are tokenized separately) |
347+
| `\die()` | `T_NS_SEPARATOR` + `T_STRING` | `T_NS_SEPARATOR` + `T_EXIT` | `T_EXIT` (content: `\die`, the parentheses are tokenized separately) |
348+
| `\true` | `T_NS_SEPARATOR` + `T_STRING` | `T_NS_SEPARATOR` + `T_TRUE` | `T_TRUE` (content: `\true`) |
349+
| `\false` | `T_NS_SEPARATOR` + `T_STRING` | `T_NS_SEPARATOR` + `T_FALSE` | `T_FALSE` (content: `\false`) |
350+
| `\null` | `T_NS_SEPARATOR` + `T_STRING` | `T_NS_SEPARATOR` + `T_NULL` | `T_NULL` (content: `\null`)
351+
352+
##### Upgrading
353+
354+
Typically, search your code base for use of the `T_EXIT`, `T_TRUE`, `T_FALSE` and `T_NULL` tokens and check if the sniff examines the `'content'` index for these tokens. If so, the sniff may need to be adjusted to allow for the keyword in both unqualified and fully qualified form.
330355

331356
> [!TIP]
332357
> If you want to forbid the use of the fully qualified form of `true`/`false`/`null`, PHPCSExtra offers the [`Universal.PHP.NoFQNTrueFalseNull` sniff](https://github.com/PHPCSStandards/PHPCSExtra?tab=readme-ov-file#universalphpnofqntruefalsenull-wrench-books), which will do just that.

0 commit comments

Comments
 (0)