-
-
Notifications
You must be signed in to change notification settings - Fork 87
Define polyfilled T_* constants from Tokenizer as int #1292
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
Open
fredden
wants to merge
4
commits into
PHPCSStandards:4.x
Choose a base branch
from
fredden:issue-1286/polyfill-tokenizer-const-as-int
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−82
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/EndToEndPhpt/Util/Tokens/polyfillTokenizerConstants-collision-php.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --TEST-- | ||
| Detect when the value of a polyfilled PHP token collides with a value already used by an existing internal PHP token. | ||
| --SKIPIF-- | ||
| <?php | ||
| if (version_compare(PHP_VERSION, "8.4", ">=")) { | ||
| echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL; | ||
| } | ||
| --FILE-- | ||
| <?php | ||
| define('T_PUBLIC_SET', T_STRING); | ||
| require('src/Util/Tokens.php'); | ||
| --EXPECTF-- | ||
| Fatal error: Uncaught Exception: Externally polyfilled tokenizer constant value collision detected! T_PUBLIC_SET has the same value as T_STRING in %s:%d | ||
| Stack trace: | ||
| #0 %s(%d): PHP_CodeSniffer\Util\Tokens::polyfillTokenizerConstants() | ||
| #1 Standard input code(%d): require('...') | ||
| #2 {main} | ||
| thrown in %s on line %d |
20 changes: 20 additions & 0 deletions
20
tests/EndToEndPhpt/Util/Tokens/polyfillTokenizerConstants-collision-user.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --TEST-- | ||
| Detect when two or more polyfilled PHP tokens have the same value. | ||
| --SKIPIF-- | ||
| <?php | ||
| if (version_compare(PHP_VERSION, "8.4", ">=")) { | ||
| echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL; | ||
| } | ||
| --FILE-- | ||
| <?php | ||
| define('T_PRIVATE_SET', 10000); | ||
| define('T_PROTECTED_SET', 10000); | ||
| define('T_PUBLIC_SET', 10000); | ||
| require('src/Util/Tokens.php'); | ||
| --EXPECTF-- | ||
| Fatal error: Uncaught Exception: Externally polyfilled tokenizer constant value collision detected! T_PROTECTED_SET has the same value as T_PRIVATE_SET in %s:%d | ||
| Stack trace: | ||
| #0 %s(%d): PHP_CodeSniffer\Util\Tokens::polyfillTokenizerConstants() | ||
| #1 Standard input code(%d): require('...') | ||
| #2 {main} | ||
| thrown in %s on line %d |
14 changes: 14 additions & 0 deletions
14
tests/EndToEndPhpt/Util/Tokens/polyfillTokenizerConstants-number.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --TEST-- | ||
| Detect when an external party defines a PHP token polyfill with a number that we would have used. | ||
| --SKIPIF-- | ||
| <?php | ||
| if (version_compare(PHP_VERSION, "8.4", ">=")) { | ||
| echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL; | ||
| } | ||
| --FILE-- | ||
| <?php | ||
| define('T_PUBLIC_SET', 135000); | ||
| require('src/Util/Tokens.php'); | ||
| echo T_PRIVATE_SET, PHP_EOL; // ..0 is used, so this becomes ..1 | ||
| --EXPECT-- | ||
| 135001 |
17 changes: 17 additions & 0 deletions
17
tests/EndToEndPhpt/Util/Tokens/polyfillTokenizerConstants-skip-names.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --TEST-- | ||
| Value collision with a non-tokenizer constant should not cause an error. | ||
| --SKIPIF-- | ||
| <?php | ||
| if (version_compare(PHP_VERSION, "8.4", ">=")) { | ||
| echo "skip because tokens used in this test already exist in PHP 8.4 so we cannot test polyfilling them", PHP_EOL; | ||
| } | ||
| --FILE-- | ||
| <?php | ||
| // Using T_STRING as a value because that would be a collision (and throw) if the constant name looked like a tokenizer constant. | ||
| define('T_', T_STRING); // Too short | ||
| define('ONE', T_STRING); // First character is not 'T' | ||
| define('TWO', T_STRING); // Second character is not '_' | ||
| require('src/Util/Tokens.php'); | ||
| echo 'No conflicts', PHP_EOL; | ||
| --EXPECT-- | ||
| No conflicts |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to change anything, but just pointing out that we can't actually be sure/guarantee that the key will always be an
intas - just like PHPCS did - other tooling could have polyfilled the tokens with some other type of value.