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

Fix ForbiddenNames sniff for use statements. #271

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 3, 2016

Fix visibility in use statements and improve earlier fix for function/const in use statements.

Visibility: Added logic to check for visibility keywords and handle them correctly.

Function/const: The previous fix would bow out as soon as one of these was encountered. That is AFAICS incorrect as the keyword without an alias is still invalid.
The current fix will move on to the alias to evaluate that instead or error on the function/const keyword if no alias is found.

Also: Removed the extra code which was added in #126 from the test file as it's no longer needed.

Fixes #117
Fixes #124
Fixes #267

/cc @ryanneufeld - I'd appreciate it if you would review as you created the original fix for the function/const keyword within use statements.


Added unit test and test cases for valid usage of public/protected/private/function/const within use statements.

The test case for function/const are in a separate file as they need a specific testVersion to test against.


Regenerated the test case files for the ForbiddenNames sniff.

Added new test case files to generate to the generate-forbidden-names-test-files script:

  • class-use-const
  • class-use-function
  • class-use-trait-alias-public-method
  • class-use-trait-alias-protected-method
  • class-use-trait-alias-private-method
  • interface (file had previously been manually added)
  • interface-extends (file had previously been manually added)

Adjusted the anonymous function for class-use-trait-alias-method to exclude private/protected/public from the generated test file as those are valid.

Added the new test case files to the data provider in the sniff test file.


N.B.: The unit tests are failing on PHPCS 1.x as PHPCS 1.x does not create a scope for a use statement with curly brackets.

I'm tempted to say: sod PHPCS 1.x and skip the failing tests for PHPCS 1.x..... Fixing this to also work on PHPCS 1.x just seems not worth the time.

@wimg what do you say ?

@ryanneufeld
Copy link

This looks like a much more complete version of my original fix.

👍 👍

@jrfnl
Copy link
Member Author

jrfnl commented Oct 3, 2016

@ryanneufeld Glad you approve. Thanks for taking the time to have a look.

@jrfnl
Copy link
Member Author

jrfnl commented Oct 18, 2016

Ok, I think I've solved this for PHPCS 1.x as well now. I've rebased & re-done the commits to have them nice & tidy.

I've added one additional utility function which handles the difference between PHPCS 1.x and 2.x with regards to use statements and added unit tests for the function as well.

* PHPCS cross-version compatibility method.
*
* In PHPCS 1.x no conditions are set for a scoped use statement.
* This method works round that limitation.
Copy link
Contributor

Choose a reason for hiding this comment

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

word is around not round 📍

{
static $isLowPHPCS, $ignoreTokens;

if (isset($isLowPHPCS) === false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

imho $isLowPHPCS === null looks cleaner and has (possibly) less instructions.

but then again i'm not familiar with this project's codestyle guide :)

Copy link
Member Author

@jrfnl jrfnl Oct 18, 2016

Choose a reason for hiding this comment

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

i'm not familiar with this project's codestyle guide

At this moment, there is none - see #137

I've tried to stay close to the PHPCS code style as that's what's used predominantly.

Copy link
Member

Choose a reason for hiding this comment

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

Wondering about something completely different : shouldn't we define the visibility of the static variables ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Only if we can - i.e. class variables.
In this case, they are static variables within a method, so visibility is not an option/does not apply.

…ate/function/const within `use` statements.

The test cases for function/const are in a separate file as they need a specific testVersion to test against.
Added new test case files to generate to the `generate-forbidden-names-test-files` script:
* `class-use-const`
* `class-use-function`
* `class-use-trait-alias-public-method`
* `class-use-trait-alias-protected-method`
* `class-use-trait-alias-private-method`
* `interface` (file had previously been manually added)
* `interface-extends` (file had previously been manually added)

Adjusted the anonymous function for `class-use-trait-alias-method` to exclude `private/protected/public` from the generated test file as those are valid.

Added the new test case files to the data provider in the sniff test file.
This method handles checking whether a token is in a T_USE scope and works around limitations of the PHPCS 1.x implementation where `use {}` did not yet create a scope condition.

This method should be favoured over directly calling `tokenHasScope()` for checking whether a token is in a T_USE scope.

Includes unit tests for the new utility method.
…/const in use statements.

Visibility: Added logic to check for visibility keywords and handle them correctly.

Function/const: The previous fix would bow out as soon as one of these was encountered. That is incorrect as the keyword without an alias is still invalid.
The current fix will move on to the alias to evaluate that instead or error on the function/const keyword if no alias is found.

Also: Removed the extra code which was added in PHPCompatibility#126 from the test file as it's no longer needed.
- Improve some variable names for clarity.
- Add/improve some documentation.
- Prevent skipping over tokens by returning after a function call instead of returning the result.
- Make sure that the `tokenHasScope()` method is tested for both array as well as integer input for the `$validScopes` parameter.
- Remove unnecessary test `setUp()` method.
@jrfnl jrfnl force-pushed the feature/issue-117-124-use-statements branch from 19564ed to cce68b3 Compare October 20, 2016 19:50
@jrfnl
Copy link
Member Author

jrfnl commented Oct 24, 2016

@wimg Any questions or remarks ?

@wimg wimg merged commit f00019c into PHPCompatibility:master Oct 25, 2016
@jrfnl jrfnl deleted the feature/issue-117-124-use-statements branch October 26, 2016 03:43
@jrfnl
Copy link
Member Author

jrfnl commented Oct 26, 2016

Thanks for merging, three more "small" ones and then three really big ones coming up.

@jrfnl jrfnl added this to the 7.0.8 milestone Apr 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants