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

AlternativeFunctions: improve handling of file_get_contents() #1374

Merged

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jun 9, 2018

The AlternativeFunctions sniff was throwing two different warnings whenever file_get_contents() was encountered, one advising to use wp_remote_get(), one advising to use the WP_FileSystem.

Aside from that, the warning is only really relevant when retrieving a remote URL. Using file_get_contents() for local files should not trigger a warning as discussed in #943.

This PR eliminates the notice about using the WP_FileSystem in favour of wp_remote_get().
It also adds a parameter check to attempt to determine whether a local or a remote file is being retrieved and eliminates the warning when it is clear that file_get_contents() is used only to retrieve a local file.

Includes unit tests.

Fixes #943

The `AlternativeFunctions` sniff was throwing two different warnings whenever `file_get_contents()` was encountered, one advising to use `wp_remote_get()`, one advising to use the `WP_FileSystem`.

Aside from that, the warning is only really relevant when retrieving a remote URL. Using `file_get_contents()` for local files should not trigger a warning as discussed in 943.

This PR eliminates the notice about using the `WP_FileSystem` in favour of `wp_remote_get()`.
It also adds a parameter check to attempt to determine whether a local or a remote file is being retrieved and eliminates the `warning` when it is clear that `file_get_contents()` is used only to retrieve a local file.

Includes unit tests.

Fixes 943
@jrfnl jrfnl added this to the 1.0.0 milestone Jun 9, 2018
break;
}

if ( preg_match( '`\b(?:ABSPATH|WP_(?:CONTENT|PLUGIN)_DIR|WPMU_PLUGIN_DIR|TEMPLATEPATH|STYLESHEETPATH|(?:MU)?PLUGINDIR)\b`', $params[1]['raw'] ) === 1 ) {
Copy link
Member

@GaryJones GaryJones Jun 9, 2018

Choose a reason for hiding this comment

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

Feels like these regexes could be centralised for potential re-use, or the whole preg_match() could be inside a named local / parent sniff method to become a little more self-documenting.

if ( $this->matchesConstantsThatIndicateALocalFile( $params[1]['raw'] ) {
    return;
}

if ( $this->matchesFunctionsThatIndicateALocalFile( $params[1]['raw'] ) {
    return;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

@GaryJones Good question.

IIRC, so far, we haven't needed anything like it elsewhere. Generally speaking, I'd move them to be more re-usable once a secondary use-case has been identified.

All the same, I'm not fussed about this, so what about adding them as class constants to the Sniff class and using the x modifier to spread them over several lines with documentation ?

Moving the whole function call to a boolean return function feels wrong as that would prevent sniffs which use the regexes from getting access to the $matches if so desired.

Copy link
Member

Choose a reason for hiding this comment

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

Could the new method return the matches, and then the use case here cast to a bool if needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Could the new method return the matches, and then the use case here cast to a bool if needed?

The thing is preg_match() has two results:

  • The function return value: 1 for a match, 0 for no match or false for an error/fault regex etc
  • The $matches array which may or may not be filled with matches.

By returning just the $matches, we would loose the nuances the function return value provides.

I'm wondering if we're not trying to create a solution for a non-issue. Have you got a (future) sniff in mind which would need these regex(es) or an existing one which could benefit from using them ?

If not, let's leave the abstraction until the time when we do have a second use-case as that might also clarify what the best way to abstract this would be.

@jrfnl jrfnl mentioned this pull request Jun 25, 2018
10 tasks
@jrfnl
Copy link
Member Author

jrfnl commented Jul 2, 2018

@JDGrimes Just checking - have you got time to have a look at this PR in the near future ? Or was there a specific reason not to approve this PR (yet) ?

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.

4 participants