-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add NativeConstantInvocationFixer #3127
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
Add NativeConstantInvocationFixer #3127
Conversation
eab0454
to
d5e498f
Compare
} | ||
|
||
$constantNamePrefix = $tokens->getPrevMeaningfulToken($index); | ||
if ($tokens[$constantNamePrefix]->isGivenKind([CT::T_USE_TRAIT, T_CLASS, T_CONST, T_DOUBLE_COLON, T_EXTENDS, T_FUNCTION, T_IMPLEMENTS, T_INTERFACE, T_NAMESPACE, T_NEW, T_NS_SEPARATOR, T_OBJECT_OPERATOR, T_TRAIT, T_USE])) { |
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.
I don't like this whitelist very much, it's too brittle: suggestions welcome
I'll fix this tomorrow, splitting all the stuff in separate cases. |
cc @nicolas-grekas , you might find this usefull |
Can we set a list for the ones we'd like to always namespace? |
so, you would say there is no need to add |
I think I will not advocate that the maybe perf benefit is worth the noise when writing code yes. |
Then maybe try to add a combined behavior described in other ticket you raised @nicolas-grekas
? |
I have to say I don't understand this statement. In my humble opinion:
As far as I can see, there is no difference, in theory and in practice, between this fixer and |
ref #3048 |
I admit I know nothing about php source code and opcode sources.
Repeated tests always show noticeable improvements around 40%, and with all constants. |
Tests do the same with opcache present, opcache.enable=1, opcache.enable_cli=1 |
Writing is one thing, reading another. Reading a code full of |
I understand your point, and I agree this fixer should be adopted only where it makes sense. This library access T_* constants millions of times, I would be very happy to apply my fixers in half the time. |
0ea4229
to
1f6c7b9
Compare
1f6c7b9
to
5217656
Compare
@nicolas-grekas continuing what written in #3222 we could use the print_r(array_keys(get_defined_constants(true)));
/*
Array
(
[0] => Core
[1] => date
[2] => libxml
[3] => pcre
[4] => sqlite3
[5] => dom
[6] => fileinfo
[7] => filter
[8] => hash
[9] => iconv
[10] => json
[11] => posix
[12] => session
[13] => standard
[14] => tokenizer
[15] => xml
[16] => openssl
[17] => curl
[18] => gd
[19] => imap
[20] => mbstring
[21] => mysqli
[22] => zlib
)
*/ By default only WDYT? |
I can't tell for everyone else, but on Symfony, we might just cherry-pick a few constants only (and not by "group"), less than 10 for sure (DIRECTORY_SEPARATOR, PHP_VERSION_ID, PHP_SAPI, maybe no more). I don't know how to help much more sorry. |
Symfony is a leader project not only for its adoption, but also for the good standards it spreads and all the edge cases it faces. I am willing to make this fixer configurable this way:
@nicolas-grekas would you be so kind to research what would be the Symfony needs and list an "official" Symfony list of the constants to be escaped? |
If can be of any help, at current commit at master EDITEDExcluding the
Of course a constants may occur only one time but can be called a lot of times, but still this may help. |
Cool, thanks for the analysis. PHP_VERSION_ID is missing, maybe because you did that on master? What about 3.4? |
Yes the previous statistic was for master, where
|
OK, so I confirm: only |
5217656
to
867724c
Compare
I've thought about what you said "we should select only those involved in conditional branching" and in fact I've found various projects where conditional branching vary a lot from the Symfony one. Thus I've added the |
|
I've added the rule in the |
that's great. please regenerate readme, as it has to update rule now belongs to ruleset |
6b65db4
to
a3c4642
Compare
a3c4642
to
baf07ff
Compare
whats open to get this feature landed? |
d49fae5
to
542bfd3
Compare
// Case sensitive constants handling | ||
$constantsToEscape = array_values($this->configuration['include']); | ||
if (true === $this->configuration['fix_built_in']) { | ||
$constantsToEscape = array_merge($constantsToEscape, \array_keys(\get_defined_constants())); |
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.
get_defined_constants
also includes userland constants (which would be constants define by the run of the PHP-CS-Fixer in such case). This could cause issues (and contradicts the fixer name saying native
)
A solution would be to use the categorized output of get_defined_constants
, to remove constants belonging to the user
category.
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.
user
category now skipped
|
||
if (!isset($this->constantsToEscape[$tokenContent]) && !isset($this->caseInsensitiveConstantsToEscape[strtolower($tokenContent)])) { | ||
continue; | ||
} |
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.
This fixer should also identify the constants imported with use const
to avoid prefixing them. It would make the fixer much less risky (and it is necessary to support use statements if you want to later make this fixer configurable to allow injecting such use statements instead of injecting a \
).
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.
Done.
@stof you are right on both your statements. By the way this feature seems controversial, and it's almost 6 months is stalled by the maintainers; I'm not going to develop any further improvement without a clear direction by them. If you are willing to fix them, I'll be happy to review & merge them as a PR upon my branch in my fork 👍 |
sadly, whole Can you, please:
|
542bfd3
to
d86f790
Compare
|
$useDeclarations = (new NamespaceUsesAnalyzer())->getDeclarationsFromTokens($tokens); | ||
$useConstantDeclarations = []; | ||
foreach ($useDeclarations as $use) { | ||
if ($use->isConstant()) { |
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.
@keradus in order to have this clean API, I extended the NamespaceUsesAnalyzer
behaviour, which is backward compatible
Done both. |
$indexes[] = $index; | ||
} | ||
|
||
$indexes = \array_reverse($indexes); |
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.
👍
*/ | ||
public function __construct($fullName, $shortName, $isAliased, $startIndex, $endIndex) | ||
public function __construct($fullName, $shortName, $isAliased, $startIndex, $endIndex, $type) |
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.
👍 for change in this file
// Function and constant imports | ||
// ['<?php use function My\count;', [], []], | ||
// ['<?php use function My\count as myCount;', [], []], | ||
// ['<?php use const My\Full\CONSTANT;', [], []], |
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.
nice side effect of this PR 👍
d8c458c
to
aabbbc2
Compare
Thank you @Slamdunk. |
This PR was squashed before being merged into the 2.12-dev branch (closes #3127). Discussion ---------- Add NativeConstantInvocationFixer Following [NativeFunctionInvocationFixer](#2462) here I propose: ```diff -echo M_PI; +echo \M_PI; ``` VLD before: https://3v4l.org/43Sci/vld VLD after: https://3v4l.org/FXkg2/vld Commits ------- aabbbc2 Add NativeConstantInvocationFixer
This PR was squashed before being merged into the 2.8 branch (closes #27852). Discussion ---------- Fix coding standards | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This PR is mostly about running the PHP-CS-Fixer (v2.12.1) in the whole codebase. - I updated the exclude rule to avoid some false positives for the `error_suppression` fixer (we have more files triggering unsilenced deprecations on purpose than when building the initial whitelist, mostly). - I ran the fixer with this updated config. Most changes were related to fully-qualifying some constants, with the new fixer implemented in PHP-CS-Fixer/PHP-CS-Fixer#3127, for which @nicolas-grekas and I suggested a config to include in the Symfony ruleset. Based on the output, I suggested a feature request in PHP-CS-Fixer/PHP-CS-Fixer#3872 as we might want to avoid the `\` in non-namespaced files to improve readability. We might want to remove the second commit of this PR if we decide to wait for the feature to be implemented (update: implementation is contributed in PHP-CS-Fixer/PHP-CS-Fixer#3876) - I added the `native_function_invocation` fixer explicitly, to automatically fully-qualify calls to compiler-optimized functions. This feature was implemented in PHP-CS-Fixer based on our feature request (as currently, we do such thing only manually in some hot path, because it could not be automated). I opened PHP-CS-Fixer/PHP-CS-Fixer#3873 to include it in the ruleset automatically. TODOs: - [x] agree on the updated rules - [x] update fabbot to use the new version of PHP-CS-Fixer - [ ] make separate PRs for newer branches with their own updates (exclude rules, and CS fixes), once this PR gets merged. Commits ------- 538c69d Fix Clidumper tests 04654cf Enable the fixer enforcing fully-qualified calls for compiler-optimized functions f00b327 Apply fixers 720ed4d Disable the native_constant_invocation fixer until it can be scoped 8892b98 Update the list of excluded files for the CS fixer
…e set (kubawerlos) This PR was merged into the 2.17-dev branch. Discussion ---------- NativeConstantInvocation - Add "PHP_INT_SIZE" to SF rule set As [requested](#4943 (comment)) by @nicolas-grekas. Added as a feature because it is [change](#3127 (comment)) request. Commits ------- fd92a7e Add "PHP_INT_SIZE" to "native_constant_invocation" in Symfony rule set configuration
This PR was squashed before being merged into the 2.8 branch (closes #27852). Discussion ---------- Fix coding standards | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a This PR is mostly about running the PHP-CS-Fixer (v2.12.1) in the whole codebase. - I updated the exclude rule to avoid some false positives for the `error_suppression` fixer (we have more files triggering unsilenced deprecations on purpose than when building the initial whitelist, mostly). - I ran the fixer with this updated config. Most changes were related to fully-qualifying some constants, with the new fixer implemented in PHP-CS-Fixer/PHP-CS-Fixer#3127, for which @nicolas-grekas and I suggested a config to include in the Symfony ruleset. Based on the output, I suggested a feature request in PHP-CS-Fixer/PHP-CS-Fixer#3872 as we might want to avoid the `\` in non-namespaced files to improve readability. We might want to remove the second commit of this PR if we decide to wait for the feature to be implemented (update: implementation is contributed in PHP-CS-Fixer/PHP-CS-Fixer#3876) - I added the `native_function_invocation` fixer explicitly, to automatically fully-qualify calls to compiler-optimized functions. This feature was implemented in PHP-CS-Fixer based on our feature request (as currently, we do such thing only manually in some hot path, because it could not be automated). I opened PHP-CS-Fixer/PHP-CS-Fixer#3873 to include it in the ruleset automatically. TODOs: - [x] agree on the updated rules - [x] update fabbot to use the new version of PHP-CS-Fixer - [ ] make separate PRs for newer branches with their own updates (exclude rules, and CS fixes), once this PR gets merged. Commits ------- 538c69dc26 Fix Clidumper tests 39d2079 Enable the fixer enforcing fully-qualified calls for compiler-optimized functions f00b3279ea Apply fixers 720ed4d379 Disable the native_constant_invocation fixer until it can be scoped 8892b98627 Update the list of excluded files for the CS fixer
Following NativeFunctionInvocationFixer here I propose:
VLD before: https://3v4l.org/43Sci/vld
VLD after: https://3v4l.org/FXkg2/vld