Skip to content

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Jul 18, 2017

New sniff to verify that objects being instantiated comply with some basic rules:

  • Always use parenthesis.
  • Don't have spaces (or comments) between the class name and the parenthesis.
  • Don't assign new by reference (PHP only).

Sniffs both PHP as well as JS code.

Auto-fixes missing parentheses and space between class name and parentheses.

Includes unit tests.

Currently doesn't handle cases of object instantiations where the class name is held in a variable variable.

Fixes #919

New sniff to verify that objects being instantiated comply with some basic rules: * Always use parenthesis. * Don't have spaces (or comments) between the class name and the parenthesis. * Don't assign new by reference (PHP only). Sniffs both PHP as well as JS code. Auto-fixes _missing parentheses_ and _space between class name and parentheses_. Includes unit tests. Currently doesn't handle cases of object instantiations where the class name is held in a variable variable. Fixes 919
@jrfnl jrfnl added this to the 0.12.0 milestone Jul 18, 2017
@jrfnl jrfnl requested a review from westonruter July 18, 2017 16:17
@jrfnl jrfnl changed the title ✨ New ClassInstantiation sniff to WordPress-Extra. New ClassInstantiation sniff to WordPress-Extra. Jul 18, 2017
@jrfnl jrfnl mentioned this pull request Jul 18, 2017
@GaryJones
Copy link
Member

Looks good. Were you planning to send this upstream?

@JDGrimes
Copy link
Contributor

@jrfnl For the record, is there a particular reason why variable variable class names are not supported?

@jrfnl
Copy link
Member Author

jrfnl commented Jul 18, 2017

Were you planning to send this upstream?

@GaryJones Potentially this could be pulled upstream at some point, though without the assigning by reference check.

For the record, is there a particular reason why variable variable class names are not supported?

@JDGrimes Yes there is. Variable variables can take a lot of forms and it would take a lot of code to attempt to reliably detect whether the class is instantiated with or without parenthesis in that case. It also would most likely still lead to false positives with the sniff getting confused over whether parentheses are part of the variable variable or for the object instantiation.
See https://github.com/wimg/PHPCompatibility/blob/master/PHPCompatibility/Tests/sniff-examples/variable_variables.php for some examples of what would need to be covered.

@GaryJones GaryJones merged commit 6eea7d8 into develop Jul 19, 2017
@GaryJones GaryJones deleted the feature/issue-919-new-braces-on-class-instantiation-sniff branch July 19, 2017 00:44
jrfnl added a commit to dd32/debug-bar that referenced this pull request Jul 19, 2017
* Adjust the way the PHPCompatibility PHPCS standard is installed. See: PHPCompatibility/PHPCompatibility#446 * Anticipate the Travis change over to using `trusty` containers which would start breaking the builds for PHP 5.2 and 5.3 soon enough. See: https://blog.travis-ci.com/2017-07-11-trusty-as-default-linux-is-coming * Fix one object instantiation without parenthesis - new sniff which was added to WPCS: WordPress/WordPress-Coding-Standards#1033 * Show summary and source reports at the top of the PHPCS output to be able to quickly identify the error files and error types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment