Skip to content

Conversation

@anomiex
Copy link
Contributor

@anomiex anomiex commented Oct 7, 2020

Changes proposed in this Pull Request:

MediaWiki has a large suite of custom phpcs sniffs, several of which would be useful for Jetpack.

  • MediaWiki.AlternativeSyntax.LeadingZeroInFloat: Prefer 0.5 over .5 for better readability.
  • MediaWiki.Classes.UnsortedUseStatements: Require use statements be sorted.
  • MediaWiki.Classes.UnusedUseStatement: Attempts to flag use statements where the used class isn’t actually used.
  • MediaWiki.ExtraCharacters.ParenthesesAroundKeyword: Flags things like clone( $x ), continue( 2 ), and break( 2 ), in addition to things like require_once( '...' ) that are already getting flagged by PEAR.Files.IncludingFile.BracketsNotRequired.
  • MediaWiki.Usage.DirUsage: Prefer __DIR__ to dirname( __FILE__ ).
  • MediaWiki.Usage.DoubleNotOperator: ! ! $x is "clever code", (bool) $x would be clearer.
  • MediaWiki.Usage.InArrayUsage: Flags constructs like in_array( $key, array_keys( $array ) ) and in_array( $key, array_flip( $array ) ) that could be done more clearly and efficiently with isset( $array[ $key ] ) or array_key_exists( $key, $array ).
  • MediaWiki.Usage.MagicConstantClosure: Flag use of __METHOD__ and __FUNCTION__ inside closures, as they don’t produce useful output.
  • MediaWiki.Usage.NestedFunctionsSniff: Defining a function (not a closure) inside a function is "clever code", if not an outright bug.
  • MediaWiki.Usage.PHPUnitAssertEquals: Prefer other PHPUnit assertions over assertEquals() in some cases for better type safety.
  • MediaWiki.Usage.PlusStringConcat: Flag attempts to concatenate strings with + rather than .. Useful for programmers coming from JS.
  • MediaWiki.Usage.ReferenceThis: &$this raises warnings since PHP 7.1, warn against it. Surprisingly not caught by the "PHPCompatibility" sniffs we’re importing.
  • MediaWiki.WhiteSpace.MultipleEmptyLines: Forbids multiple empty lines everywhere, versus Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines which only does so inside functions.
  • MediaWiki.WhiteSpace.SpaceAfterClosure: The WordPress sniffs seem to have good coverage of whitespace stuff, but miss that function() should be function ().

Jetpack product discussion

p9dueE-1Y9-p2

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

  • Run composer php:lint on various files to see what it finds.

Proposed changelog entry for your changes:

  • Developer only, so probably not needed.

@anomiex anomiex requested review from kraftbj and leogermani October 7, 2020 21:12
@anomiex anomiex self-assigned this Oct 7, 2020
@github-actions github-actions bot added the [Status] Needs Package Release This PR made changes to a package. Let's update that package now. label Oct 7, 2020
@anomiex
Copy link
Contributor Author

anomiex commented Oct 7, 2020

Submitting this now as a proof of concept. If we want to go ahead with this, I'll need to poke upstream about a few things:

  • I had to use dev-master here since their latest release requires phpcs 3.5.5 rather than 3.5.6. We'll need them to tag a release so we can depend on a non-dev version.
  • Their package isn't set up for use with dealerdirect/phpcodesniffer-composer-installer, so right now the <rule ref="..." /> are using ugly paths. Hopefully they'll be amenable to a patch adding that support; I think it'd be a one-line change to add a "type" to their composer.json.

@anomiex
Copy link
Contributor Author

anomiex commented Oct 8, 2020

I did some testing by running phpcbf for these rules, to see that they didn't wind up creating a flood of other rule violations or something. Then I uploaded the results at #17419 in case that would be useful to see.

@kraftbj
Copy link
Contributor

kraftbj commented Oct 15, 2020

Submitting this now as a proof of concept. If we want to go ahead with this, I'll need to poke upstream about a few things:

Any success poking upstream?

@anomiex
Copy link
Contributor Author

anomiex commented Oct 16, 2020

Any success poking upstream?

I was waiting for feedback here. I'll go poke them now.

@kraftbj
Copy link
Contributor

kraftbj commented Oct 16, 2020

Sounds good; thanks!

@anomiex
Copy link
Contributor Author

anomiex commented Oct 16, 2020

  • Their package isn't set up for use with dealerdirect/phpcodesniffer-composer-installer, so right now the <rule ref="..." /> are using ugly paths. Hopefully they'll be amenable to a patch adding that support; I think it'd be a one-line change to add a "type" to their composer.json.

Poked upstream at https://phabricator.wikimedia.org/T265735. It did turn out to be a one-line change.

@anomiex anomiex force-pushed the add/codesniffer-package branch from 7eec79d to cadfcaa Compare October 19, 2020 17:09
@anomiex anomiex force-pushed the add/phpcs-rules-from-MediaWiki branch from 0786777 to b6d69e5 Compare October 19, 2020 17:26
@kraftbj
Copy link
Contributor

kraftbj commented Oct 19, 2020

I'd be okay merging this into the codesniffer package PR OR merging that one, then merging this immediately following, as it is now. If/when MediaWiki updates the package so we can pin a specific version, we can do that in a follow-up.

@kraftbj kraftbj added this to the 9.1 milestone Oct 19, 2020
@anomiex
Copy link
Contributor Author

anomiex commented Oct 19, 2020

I'd be okay merging this into the codesniffer package PR OR merging that one, then merging this immediately following

I'd prefer the latter, it seems cleaner to have the major refactor and the added functionality be separate in master so future git archaeologists can see what was what.

Base automatically changed from add/codesniffer-package to master October 19, 2020 20:39
@anomiex anomiex force-pushed the add/phpcs-rules-from-MediaWiki branch from b6d69e5 to 1dc8f56 Compare October 19, 2020 21:13
@jetpackbot
Copy link
Collaborator

jetpackbot commented Oct 19, 2020

Scheduled Jetpack release: November 10, 2020.
Scheduled code freeze: November 3, 2020

E2E results is available here (for debugging purposes): https://jetpack-e2e-dashboard.herokuapp.com/pr-17406

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Generated by 🚫 dangerJS against f2c7886

@kraftbj
Copy link
Contributor

kraftbj commented Oct 19, 2020

We'll likely need to do this https://github.com/Automattic/jetpack/blob/master/tests/setup-travis.sh#L15 for this package as well.

@anomiex
Copy link
Contributor Author

anomiex commented Oct 20, 2020

We'll likely need to do this https://github.com/Automattic/jetpack/blob/master/tests/setup-travis.sh#L15 for this package as well.

Yeah, I see Travis is failing.

BTW, I wish Travis would email me when it fails on one of my PRs. It looks like it would by default, but specifying recipients unfortunately overrides that (travis-ci/travis-ci#8868). And I don't want emails about everyone's Travis failures...

@jeherve
Copy link
Member

jeherve commented Oct 20, 2020

I wish Travis would email me when it fails on one of my PRs.

We've discussed this a bit over the years. I think updating the status of the PR itself (via a comment from a bot for example) would allow us to hit 3 birds with one stone:

  • It would send out an email to folks who rely on email for GitHub management / notifications
  • It would make it clear when looking at a PR that tests are failing
  • We could couple that to an action that would update the labels on the PR at the same time.

Edit: cross-linking to #17094, where @brbrr has already started looking into moving some of our tests to GitHub actions; doing so would allow to update the PR at the end of the action run.

@anomiex anomiex force-pushed the add/phpcs-rules-from-MediaWiki branch from 494df99 to 781f334 Compare October 20, 2020 16:54
@anomiex anomiex force-pushed the add/phpcs-rules-from-MediaWiki branch 2 times, most recently from 2670c53 to 59f51a6 Compare October 20, 2020 19:06
@anomiex
Copy link
Contributor Author

anomiex commented Oct 20, 2020

Ok, Travis is passing now (other than the flaky sync tests). I'm not entirely happy with what I had to do to make that happen, but it works.

MediaWiki has a large suite of custom phpcs sniffs, several of which
would be useful for Jetpack.

* MediaWiki.AlternativeSyntax.LeadingZeroInFloat: Prefer `0.5` over `.5`
  for better readability.
* MediaWiki.Classes.UnsortedUseStatements: Require `use` statements be
  sorted.
* MediaWiki.Classes.UnusedUseStatement: Attempts to flag `use`
  statements where the used class isn’t actually used.
* MediaWiki.ExtraCharacters.ParenthesesAroundKeyword: Flags things like
  `clone( $x )`, `continue( 2 )`, and `break( 2 )`, in addition to
  things like `require_once( '...' )` that are already getting flagged
  by PEAR.Files.IncludingFile.BracketsNotRequired.
* MediaWiki.Usage.DirUsage: Prefer `__DIR__` to `dirname( __FILE__ )`.
* MediaWiki.Usage.DoubleNotOperator: `! ! $x` is "clever code", `(bool) $x`
  would be clearer.
* MediaWiki.Usage.InArrayUsage: Flags constructs like
  `in_array( $key, array_keys( $array ) )` and
  `in_array( $key, array_flip( $array ) )` that could be done more
  clearly and efficiently with `isset( $array[ $key ] )` or
  `array_key_exists( $key, $array )`.
* MediaWiki.Usage.MagicConstantClosure: Flag use of `__METHOD__` and
  `__FUNCTION__` inside closures, as they don’t produce useful output.
* MediaWiki.Usage.NestedFunctionsSniff: Defining a function (not a
  closure) inside a function is "clever code", if not an outright bug.
* MediaWiki.Usage.PHPUnitAssertEquals: Prefer other PHPUnit assertions
  over `assertEquals()` in some cases for better type safety.
* MediaWiki.Usage.PlusStringConcat: Flag attempts to concatenate strings
  with `+` rather than `.`. Useful for programmers coming from JS.
* MediaWiki.Usage.ReferenceThis: `&$this` raises warnings since PHP 7.1,
  warn against it. Surprisingly not caught by the "PHPCompatibility"
  sniffs we’re importing.
* MediaWiki.WhiteSpace.MultipleEmptyLines: Forbids multiple empty lines
  everywhere, versus Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines
  which only does so inside functions.
* MediaWiki.WhiteSpace.SpaceAfterClosure: The WordPress sniffs seem to
  have good coverage of whitespace stuff, but miss that `function()`
  should be `function ()`.
Uninstall automattic/jetpack-codesniffer for 5.6 and 7.0, since
mediawiki/mediawiki-codesniffer requires 7.2+.

Also, since we're uninstalling the phpcs standard, don't try to run
phpcs.

And we need a way to avoid having run-travis.sh run the tests for the
codesniffer package when PHP is too old. Sigh.
@anomiex anomiex force-pushed the add/phpcs-rules-from-MediaWiki branch from 59f51a6 to 8985c17 Compare October 22, 2020 15:35
@kraftbj
Copy link
Contributor

kraftbj commented Oct 26, 2020

@anomiex What's outstanding on this before ready for review?

@anomiex
Copy link
Contributor Author

anomiex commented Oct 26, 2020

If you're still willing to merge it with the dev-master dependency, nothing. Otherwise, I should poke upstream to make a release.

edit: poked them on one of their IRC channels.

edit: They say they'll have a new version in a few minutes.

@anomiex anomiex added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Oct 26, 2020
Copy link
Contributor

@kraftbj kraftbj left a comment

Choose a reason for hiding this comment

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

I'll follow up with a few PRs to implement these to prevent too much churn in a single PR.

Marking for 9.2 so we don't get caught with PHPCS updates needed during beta week. Let's merge this ASAP after code freeze.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Codesniffer [Pri] Normal [Status] Needs Package Release This PR made changes to a package. Let's update that package now. [Type] Janitorial

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants