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

Better cover the regex related rules. #608

Merged
merged 2 commits into from
Jul 27, 2016

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 16, 2016

From the handbook:

Perl compatible regular expressions (PCRE, preg_ functions) should be used in preference to their POSIX counterparts. Never use the /e switch, use preg_replace_callback instead.

It’s most convenient to use single-quoted strings for regular expressions since, contrary to double-quoted strings, they have only two metasequences: ' and .

Ref: https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#regular-expressions

This can be split into three distinct rules:

  1. Don't use POSIX functions
  2. Don't use the /e modifier
  3. Prefer single quoted stings.

Rule 1 was up to now not covered in core. Parts of it were covered in extra and in vip, but the implementations were inconsistent and both were incomplete.
Rule 2 was not covered at all.
Rule 3 is already covered by the single vs double quotes sniff.

This PR fixes the issues with covering rule 1 and 2 with the following two commits:

Split off POSIX Functions restrictions to its own sniff.

  • Removes similar checks from existing sniffs (PHP.DiscouragedFunctions and VIP.RestrictedFunctions).
  • Makes sure that all functions in the POSIX extension are covered by this check - they previously were not.
  • Add unit tests for the new sniff covering all POSIX functions.

Copy in the PregReplaceEModifierSniff from the PHPCompatibility Sniff library.

  • Add a new PHPCompatibility folder to the Sniff folder.
  • Copy in the PregReplaceEModifierSniff.
  • Copy in the related parent class PHPCompatibility_Sniff.
  • Renamed both classes to follow the PSR1 autoloading pattern.
  • Added a readme to the PHPCompatibility sniff folder about its usage.
  • Excluded the PHPCompatibility folder from the PHPCS code style checks - this way we can just drop-in replace the file (ok, except for the class renaming) when there is a new version available.

@@ -31,13 +31,6 @@ class WordPress_Sniffs_PHP_DiscouragedFunctionsSniff extends Generic_Sniffs_PHP_
* @var array(string => string|null)
*/
public $forbiddenFunctions = array(
// Deprecated.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe leave a comment that this is handled with the new sniff?

Copy link
Member Author

@jrfnl jrfnl Jul 17, 2016

Choose a reason for hiding this comment

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

Leaving a comment like that in the code will become very untidy in combination with the upcoming #582 work. I'd rather suggest at some point also re-ordering the vip ruleset according to their documentation and making a note in the ruleset about rules like this which are already covered in core. Ok ?

@JDGrimes
Copy link
Contributor

Is there no way that we can just have the PHPCompatibility sniff library as a dependency? Maybe it is best to just copy the sniff over, but I shy away from anything that might require routine maintenance. 😄

@jrfnl
Copy link
Member Author

jrfnl commented Jul 17, 2016

Is there no way that we can just have the PHPCompatibility sniff library as a dependency?

See #575 as well where I originally suggested that.

The only way I can think of would be to make it a dependency via composer and add it to the ruleset. Not sure how well that would work for the re-hashing to get PHPCS to recognize the installed standards, but I'd leave that up to @Rarst to advise on.

This is not fool-proof no matter what. Not everyone uses Composer, so this could mean people will start complaining about WPCS not working as it breaks on the missing ruleset.

Maybe it is best to just copy the sniff over, but I shy away from anything that might require routine maintenance.

As for now, for the handbook coverage, we'd only need the one sniff, that seemed like the simplest way to handle it.
As noted above, I've excluded it from the WPCS code standards check to make copy/paste maintenance as easy as possible.

If/when WPCS would want to start incorporating more sniffs from PHPCompatibility, then this should be revisited IMHO..

@JDGrimes
Copy link
Contributor

If/when WPCS would want to start incorporating more sniffs from PHPCompatibility, then this should be revisited IMHO..

OK, sounds good to me.

@jrfnl jrfnl force-pushed the WPCS/feature/cover-regex-rules branch from 490b775 to 41d4cf8 Compare July 17, 2016 18:56
@jrfnl
Copy link
Member Author

jrfnl commented Jul 17, 2016

Rebased for merge conflicts.

@jrfnl jrfnl force-pushed the WPCS/feature/cover-regex-rules branch from 41d4cf8 to 3cc54fe Compare July 17, 2016 22:50
@Rarst
Copy link
Contributor

Rarst commented Jul 18, 2016

I am concerned about licensing here, that library is LGPL. By copy/pasting pieces of it here (into MIT project) we are making a potential mess of that.

@jrfnl
Copy link
Member Author

jrfnl commented Jul 18, 2016

Good point. Would it help if we had formal permission of the project owner ?

@Rarst
Copy link
Contributor

Rarst commented Jul 18, 2016

Not really, it's just a... GPL mess in such case. Using it as a library would be clear separation of stuff, which LGPL allows without issue.

@jrfnl jrfnl force-pushed the WPCS/feature/cover-regex-rules branch 3 times, most recently from 094de1b to b2aad8b Compare July 19, 2016 23:09
@jrfnl
Copy link
Member Author

jrfnl commented Jul 25, 2016

FYI: Working on an alternative solution for the e modifier sniff.

* Removes similar checks from existing sniffs (PHP.DiscouragedFunctions and VIP.RestrictedFunctions).
* Makes sure that *all* functions in the POSIX extension are covered by this check - they previously were not.
* Add unit tests for the new sniff covering *all* POSIX functions.
@jrfnl jrfnl force-pushed the WPCS/feature/cover-regex-rules branch from b2aad8b to 7721698 Compare July 27, 2016 12:17
@jrfnl
Copy link
Member Author

jrfnl commented Jul 27, 2016

What with the upcoming release and the work @grappler is doing in #633, I'm removing the commit which added the e modifier sniff from PHPCompatibility for now, so that the splitting off of the POSIX functions to their own file can be merged.

I'm working on an abstract class to detect regular expressions and concrete classes for the e modifier sniff and best practices sniffs, but that will need more time and more testing, so won't be ready for the 0.10.0 release.

@JDGrimes
Copy link
Contributor

@jrfnl Just to confirm—this is ready for merge?

@jrfnl
Copy link
Member Author

jrfnl commented Jul 27, 2016

@JDGrimes Absolutely, it now only contains the splitting off of the POSIX check from the VIP sniff to an individual sniff and adding that rule to the core ruleset where it belongs.

The other part of the regex rules will be covered in a separate PR.

@JDGrimes JDGrimes added this to the 0.10.0 milestone Jul 27, 2016
@JDGrimes JDGrimes merged commit b7ca090 into develop Jul 27, 2016
@JDGrimes JDGrimes deleted the WPCS/feature/cover-regex-rules branch July 27, 2016 16:06
@Rarst
Copy link
Contributor

Rarst commented Jul 27, 2016

To be clear phpcombatibility got thrown out of this, so I can stop worry about licensing stuff? ;)

@jrfnl
Copy link
Member Author

jrfnl commented Jul 27, 2016

@Rarst You can (for now) ;-)

@jrfnl
Copy link
Member Author

jrfnl commented Aug 18, 2016

All and @Rarst in particular:

I've been doing quite some work on the PHPCompatibility sniff library in the mean time and have created a number of utility functions which would also be useful for WPCS and could get rid of a sh*tload of semi-duplicate code and allow us to check certain things with higher precision.

As I created these functions - though sometimes based on existing code - and contributed them to PHPCompatibility, will it be ok license-wise if I port them over to WPCS ?

At a later time - once these functions have been used a bit more and gone through the wrangler, I'd even like to PR them to PHPCS itself.

@Rarst
Copy link
Contributor

Rarst commented Aug 18, 2016

As I created these functions - though sometimes based on existing code - and contributed them to PHPCompatibility, will it be ok license-wise if I port them over to WPCS ?

If by "based on existing code" you mean copy/paste/modify of its code then it's a GPL derivative work and ugh ugh ugh mess for us.

I am not familiar with specific case of LGPL into GPL move to say confidently about it. As above it would be clearly fine if LGPL lib was kept as dependency, but I don't really know about mixing their code.

@jrfnl
Copy link
Member Author

jrfnl commented Aug 18, 2016

By "based on existing code", I mean inspired by. The actual code was rewritten.

All the same, I find this whole licensing thing very confusing. If I write code and choose to contribute the same code to three different projects, it now sounds like the licensing of the first project to merge the code would rule over it all. That just feels wrong and discourages from contributing at all.

I also wonder if code snippets like that can be licensed or even copyrighted at all, after all, writing it from scratch someone else will just end up with 90% the same code with maybe some minor personalizations.
Licensing IMHO is more about the library as a whole and the concept of it, rather than bits and pieces of the code itself (though I know that's not how it works, but it is how I think it should work).

Being able to re-use bits and pieces will encourage best of breed coding much more than everyone re-inventing the wheel.

On a side-note: I remember seeing somewhere a note in the WPCS code base about a piece of code which was taken from WP Core. As WP Core is GPL, according to the licensing, that code shouldn't have been allowed in in that case. Correct ?

@Rarst
Copy link
Contributor

Rarst commented Aug 18, 2016

By "based on existing code", I mean inspired by. The actual code was rewritten.

If you wrote the code from scratch then it's fine. It's your work to be licensed as you see fit.

If I write code and choose to contribute the same code to three different projects, it now sounds like the licensing of the first project to merge the code would rule over it all.

Nope. Since it's your code you would be free to contribute it to three different project and license it differently for each one. After the code is contributed to the project it's subject to its license, but you as copyright holder of your code are free to license it differently for another project.

I also wonder if code snippets like that can be licensed or even copyrighted at all, after all, writing it from scratch someone else will just end up with 90% the same code with maybe some minor personalizations.
Licensing IMHO is more about the library as a whole and the concept of it, rather than bits and pieces of the code itself (though I know that's not how it works, but it is how I think it should work).

Yes, it's vague and often people close eyes at technicalities. My completely personal rule of a thumb — copy/paste of actual code is a line behind which it becomes a clear unarguable derivative code. Many people (WordPress, cough) would disagree though and draw a line differently.

On a side-note: I remember seeing somewhere a note in the WPCS code base about a piece of code which was taken from WP Core. As WP Core is GPL, according to the licensing, that code shouldn't have been allowed in in that case. Correct ?

Correct. If it's still the case we should get rid of it.

@jrfnl
Copy link
Member Author

jrfnl commented Aug 27, 2016

@Rarst Thanks for your detailed reply! Will keep this in mind 👍

@khacoder
Copy link
Contributor

So we find ways to not do something rather then ways to do something?

What about dual licensing code sniffer to make it easier to expand it?

@Rarst
Copy link
Contributor

Rarst commented Aug 29, 2016

So we find ways to not do something rather then ways to do something?

Welcome to GPL.

What about dual licensing code sniffer to make it easier to expand it?

Dual licensing makes things harder, not easier. Now you have two licenses to be compatible with at the same time.

@khacoder
Copy link
Contributor

Actually no :) Dual licensing means either one applies, from everything that I have read.

The user would pick the one to use. In the case of GPLv2 or later versus MIT, WordPress would then pick GPLv2 or later.

@Rarst
Copy link
Contributor

Rarst commented Aug 29, 2016

Actually no :) Dual licensing means either one applies, from everything that I have read.

Yes, user can pick which license to apply, good for them. And user can do that because project is obliged to have project's code comply with both.

Think about it. I don't like GPL? I put GPL into dual license project and say it's now MIT... Doesn't work.

@khacoder
Copy link
Contributor

So what code in Code Sniffer is MIT specific?

@Rarst
Copy link
Contributor

Rarst commented Aug 29, 2016

So what code in Code Sniffer is MIT specific?

All of it, as long as that's the license project declares.

I am not too keen to continue general licensing chatter in this ticket. If you have specific licensing concerns or proposals for the project please open a dedicated issue.

@khacoder
Copy link
Contributor

Nah I won't pursue it but thanks for asking :)

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.

5 participants