Skip to content
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

NativeConstantInvocationFixer - add the scope option #3876

Merged
merged 1 commit into from Jul 30, 2018

Conversation

stof
Copy link
Contributor

@stof stof commented Jul 5, 2018

Closes #3872

The way to implement the option is heavily inspired by the existing feature in NativeFunctionInvocationFixer, except that I reused the NamespacesAnalyzer implementation instead of copy-pasting \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer::getUserDefinedNamespaces

I also updated the Symfony ruleset to restrict the fixer to the namespaced scope as adding the \ when the code is already in the global scope has no benefit for performance (as both cases are fully qualified) while increasing visual burden (which was the reason to limit it to only a few constants in the Symfony ruleset).

@stof stof mentioned this pull request Jul 5, 2018
3 tasks
@keradus keradus added this to the 2.13.0 milestone Jul 5, 2018
@keradus keradus changed the title Implement the scope option for the native constant invocation fixer NativeConstantInvocationFixer - add the scope option Jul 5, 2018
keradus added a commit that referenced this pull request Jul 6, 2018
This PR was merged into the 2.12 branch.

Discussion
----------

NamespacesAnalyzer - Optimize performance

Instead of looping on all tokens, even inside the namespace it identified, the analyzer now continues the analysis after the end of the identified namespace, thanks to the fact that namespaces cannot be nested.

When implementing #3876, I compared the NamespacesAnalyzer implementation with `\PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer::getUserDefinedNamespaces`. The NamespacesAnalyzer also identifies the namespace name instead of skipping over it (and returns the global namespace too). But I found out that NativeFunctionInvocationFixer was more optimized, as it was not re-analyzing all tokens inside a namespace. This makes NamespacesAnalyzer implement the same optimization.

Commits
-------

8321c26 Optimize the namespaces analyzer
@stof
Copy link
Contributor Author

stof commented Jul 10, 2018

@keradus anything missing here ?

@keradus
Copy link
Member

keradus commented Jul 10, 2018

Hi @stof ,
what is needed is a review. Sadly, another 85 awaits for my attention, but dont worry - PRs can be reviewed by community as well, it doesn't need to be me ;) Thanks for understanding and patience.

@stof stof force-pushed the scope_constant_invocation branch from 8d15a82 to 885a13f Compare July 24, 2018 12:52
$this->doTest($expected, $input);
}

public function testFixScopedOnly()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it matters anything, but I'm missing a (scoped only) test with no namespace keyword at all.

<?php

echo PHP_VERSION . PHP_EOL;

Should be untouched

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test added

@@ -864,6 +864,8 @@ Choose from the list of available rules:
``get_defined_constants``. User constants are not accounted in this list
and must be specified in the include one; defaults to ``true``
- ``include`` (``array``): list of additional constants to fix; defaults to ``[]``
- ``scope`` (``'all'``, ``'namespaced'``): only fix constant invocations that are made
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of options is all, namespaced, however the description order is namespaced, all. Could be confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's exactly the same wording than for the scope option of the native_function_invocation fixer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix that one too? 😇 I dunno, I'll leave that call to (one of) the maintainers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the order is arbitrary and I doubt someone compares the README against the source/describe command, I see no need for an update here (or in the fixer)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't comparing anything to anything, I just ment in this specific line in itself. It reads a, b: description of b, or a.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not saying that you were, still no change needed IMHO

@stof stof force-pushed the scope_constant_invocation branch from 885a13f to 451a81d Compare July 25, 2018 13:20
nicolas-grekas added a commit to symfony/symfony that referenced this pull request Jul 26, 2018
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
@keradus
Copy link
Member

keradus commented Jul 26, 2018

ping @FriendsOfPHP/php-cs-fixer @localheinz @kubawerlos @ntzm
can you help with a review here? thanks !

@@ -225,6 +212,59 @@ protected function createConfigurationDefinition()
->setAllowedValues([$constantChecker])
->setDefault(['null', 'false', 'true'])
->getOption(),
(new FixerOptionBuilder('scope', 'Only fix constant invocations that are made within a namespace or fix all.'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we've added this option, can we also add some a test or two to check that 'scope' => 'all' works correctly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, all the non-modified tests are running with the all scope

]);
}

private function fixConstantInvocations(Tokens $tokens, $start, $end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing PHPDoc to typehint $start and $end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the composer.json says that PHP-Cs-Fixer supports PHP 5.6+

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPDoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, sorry

]);
}

private function fixConstantInvocations(Tokens $tokens, $start, $end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a file with multiple namespaces is fixed this method might be called multiple times (based on the new config option).
The $useDeclarations found and the token analyzer (created later on) are based on the complete tokens collection, these can be reused as long as no tokens are inserted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this method inserts tokens.

Actually, a better way (for both this fixer and the function invocation one) would be to analyze use statements only for the current namespace anyway. but the use statement analyzer does not support that currently.

@stof stof force-pushed the scope_constant_invocation branch 2 times, most recently from 5c9fee3 to 54a6af8 Compare July 30, 2018 11:04
@keradus
Copy link
Member

keradus commented Jul 30, 2018

Thank you @stof.

@keradus keradus merged commit 3dbf8f6 into PHP-CS-Fixer:master Jul 30, 2018
keradus added a commit that referenced this pull request Jul 30, 2018
…tof, keradus)

This PR was squashed before being merged into the 2.13-dev branch (closes #3876).

Discussion
----------

NativeConstantInvocationFixer - add the scope option

Closes #3872

The way to implement the option is heavily inspired by the existing feature in NativeFunctionInvocationFixer, except that I reused the NamespacesAnalyzer implementation instead of copy-pasting `\PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer::getUserDefinedNamespaces`

I also updated the Symfony ruleset to restrict the fixer to the namespaced scope as adding the `\` when the code is already in the global scope has no benefit for performance (as both cases are fully qualified) while increasing visual burden (which was the reason to limit it to only a few constants in the Symfony ruleset).

Commits
-------

3dbf8f6 NativeConstantInvocationFixer - add the scope option
@stof stof deleted the scope_constant_invocation branch August 24, 2018 14:18
joshtrichards pushed a commit to joshtrichards/symfony-finder that referenced this pull request Apr 26, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants