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

Added Enqueued functions check for version and in footer #1233

Closed
wants to merge 14 commits into from
Closed

Added Enqueued functions check for version and in footer #1233

wants to merge 14 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Nov 20, 2017

I have created a custom Sniff based from:

#1146

This checks that IF source ($src) if specified then it will check to make sure there is a Version ($ver), and it will error if not.

Also added another check for In Footer ($in_footer) to warn the user that no value is set.

'wp_enqueue_script' => true,
'wp_register_style' => true,
'wp_enqueue_style' => true,
'wp_register_style' => true
Copy link
Member

Choose a reason for hiding this comment

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

Why do you have this two times?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also looks like it was accidentally duplicated in the class docblock.

Copy link
Author

Choose a reason for hiding this comment

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

That would be a duplication error, I shall remove that

Copy link
Contributor

@JDGrimes JDGrimes left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! There are just a few things that need to be cleaned up here (see my comments). The sniff also doesn't follow WPCS's code style, which is one reason the Travis build is failing. That will also need to be corrected.

I would also like to see some unit tests added before this is merged. Would you like to try adding some (you can look at the tests for the other sniffs as an example)?

'wp_enqueue_script' => true,
'wp_register_style' => true,
'wp_enqueue_style' => true,
'wp_register_style' => true
Copy link
Contributor

Choose a reason for hiding this comment

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

Also looks like it was accidentally duplicated in the class docblock.

* wp_enqueue_script
* wp_register_style
* wp_enqueue_style
* wp_register_style
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you accidentally listed wp_register_style() twice here too.

*
* @package WPCS\WordPressCodingStandards
*
* @since 0.14.0 This new sniff will check for a version in an Enqueued WP function
Copy link
Contributor

Choose a reason for hiding this comment

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

All of the @since version numbers should probably be updated to 0.15.0 (presumably the next version that will be released).


// Check to see IF a source ($src) is specified otherwise its fine
if(isset($parameters[2])) {
/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this isn't a docblock for an element, the second * should probably be left off.

return;
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above.

$errorcode = 'MissingVersion';

$this->phpcsFile->addError(
'No Version found for %s; Please supply value for forth argument',
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd change the last part to "Please supply a value for the forth argument"

Copy link
Member

Choose a reason for hiding this comment

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

fourth.

@@ -0,0 +1,119 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you've committed the same sniff twice with two different names. (Or maybe I am missing something?)

Perhaps EnqueuedCheck is a better name than EnqueuedVersion, since the sniff also checks the $in_footer arg.

Does anybody else have an opinion?

Copy link
Author

Choose a reason for hiding this comment

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

@JDGrimes Thanks for checking the code :)

Yes I added this one in as EnqueuedCheck seemed better than EnqueuedVersion hence why I removed the EnqueuedResourcesSniff.php file. Sorry about that

@@ -1,65 +0,0 @@
<?php
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure you intended to remove this sniff. If you did, could you please explain why? I think that it needs to stay, because it checks to make sure that the wp_enqueue_*() functions are actually used, instead of hard-coded script/style tags.

Copy link
Member

Choose a reason for hiding this comment

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

Based on the presence of a seemingly duplicate sniff, and the seemingly accidental removal of this one, I think it was a simple mistake of deleting the wrong file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point; that's easy to do. 😄

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

@snightingale Welcome & congrats on your first PR to WPCS.

Don't be alarmed by the amount of comments you will receive. Any new sniff is generally reviewed very critically here in an effort to keep the quality of WPCS high and to teach contributors about writing sniffs. We're here to help.

Having said that, I agree with @JDGrimes that unit tests are needed.
I've also left some comments inline.
Please also review all inline comments and docblocks in the sniff for correct capitalization and not having duplicate info.

* @since 0.11.0
* @var string
*/
protected $group_name = 'strict';
Copy link
Member

Choose a reason for hiding this comment

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

Copy/paste artifact - please update for this sniff.

protected $group_name = 'strict';

/**
* List of array functions to which a $strict parameter can be passed.
Copy link
Member

Choose a reason for hiding this comment

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

Copy/paste - please update the documentation to be applicable for this sniff.

* @return void
*/
public function process_parameters($stackPtr, $group_name, $matched_content, $parameters) {
// Check if the strict check is actually needed.
Copy link
Member

Choose a reason for hiding this comment

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

Copy/paste artifact - is this condition valid the way it is for this sniff ? If so, please update the comment. Otherwise remove.

}
}

// Check to see IF a source ($src) is specified otherwise its fine
Copy link
Member

Choose a reason for hiding this comment

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

its => it's

This check could be reversed to reduce nesting level:

if ( ! isset( $parameters[2] ) ) {
    return;
}

* Otherwise it will Show a warning to add a Src (url) to the Enqueued function
*/
if (false === isset($parameters[4]) || !$parameters[4]['raw']) {
$errorcode = 'MissingVersion';
Copy link
Member

Choose a reason for hiding this comment

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

No need for a variable, just set this in the function call.

if (false === isset($parameters[4]) || !$parameters[4]['raw']) {
$errorcode = 'MissingVersion';

$this->phpcsFile->addError(
Copy link
Member

Choose a reason for hiding this comment

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

The documentation a few lines above, says it will throw a warning, so why is an error being thrown ?

* Version Check
*
* Check to make sure a Version ($ver) is set
* Otherwise it will Show a warning to add a Src (url) to the Enqueued function
Copy link
Member

Choose a reason for hiding this comment

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

The documentation does not seem to be in line with the functionality.


$this->phpcsFile->addError(
'No Version found for %s; Please supply value for forth argument',
(isset( $parameters[4]['start']) ? $parameters[4]['start'] : $parameters[1]['start']),
Copy link
Member

Choose a reason for hiding this comment

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

Why not simply use the $stackPtr ?

* Check to make sure that $in_footer is set to true
* Otherwise it will warn the user to make sure if its correct
*/
if (false === isset($parameters[5]) || 'true' !== $parameters[5]['raw']) {
Copy link
Member

Choose a reason for hiding this comment

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

Same comments as above for this whole snippet + I'm wondering whether if false is supplied as an explicit argument, the warning should still be thrown. In that case, it's a conscious choice of the dev to not enqueue in the footer.

@JDGrimes
Copy link
Contributor

@snightingale I think the remaining failures on Travis may possibly be unrelated. So don't worry about them right now. It may be a little while before we merge any new sniffs anyway, because the next release may end up being focused just on re-organizing some existing things. So hopefully whatever is causing this will get fixed in the mean time.

Copy link
Contributor

@JDGrimes JDGrimes left a comment

Choose a reason for hiding this comment

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

This is getting closer, just a few more things I spotted.

* wp_register_script
* wp_enqueue_script
* wp_register_style
* wp_enqueue_style
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess maybe this should be rewritten as a list:

* - wp_register_script()
* - wp_enqueue_script()
* - ...

* @since 0.11.0 Renamed from $array_functions to $target_functions.
*
* @var array <string function_name> => <bool always needed ?>
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

This docblock can be removed now that the property it was for has been removed.

* Check to make sure that $in_footer is set to true
* Otherwise it will warn the user to make sure if its correct
*/
// print_r($parameters);
Copy link
Contributor

Choose a reason for hiding this comment

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

This debug code can be removed. 😃

@JDGrimes
Copy link
Contributor

Actually, a few of the Travis issues might actually be related to this sniff: https://travis-ci.org/WordPress-Coding-Standards/WordPress-Coding-Standards/jobs/305880198#L780

But I think some of the others are not.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

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

Definitely going in the right direction.

The code-style needs some attention still to comply with WPCS itself:
https://travis-ci.org/WordPress-Coding-Standards/WordPress-Coding-Standards/jobs/305880198#L780-L831
https://travis-ci.org/WordPress-Coding-Standards/WordPress-Coding-Standards/jobs/305880198#L903-L905

I suspect that when these things are fixed, the other errors will magically disappear. (or not, but in that case, you can leave them to me to have a look at)

Reminder to self: look into the fixer conflict being flagged.

/**
* The group name for this group of functions.
*
* @since 0.11.0
Copy link
Member

@jrfnl jrfnl Nov 22, 2017

Choose a reason for hiding this comment

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

Outdated @since tag. This is a new sniff, so the @since should indicate the version the sniff will be merged into, most likely 0.16.0 1.0.0 (see @JDGrimes's comment about the 0.15.0 release)

* @link https://developer.wordpress.org/reference/functions/wp_enqueue_style/
*
* @since 0.10.0
* @since 0.11.0 Renamed from $array_functions to $target_functions.
Copy link
Member

Choose a reason for hiding this comment

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

Copy/paste issue - please update @since tag


/**
* List of Enqueued functions that need to be check to make sure
* IF a source ($src) value is passed, then version ($ver) needs to have a value.
Copy link
Member

@jrfnl jrfnl Nov 22, 2017

Choose a reason for hiding this comment

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

This line and the two below it should be part of the class docblock. They are not directly related to this parameter so this info shouldn't be placed here.

/**
* Process the parameters of a matched function.
*
* @since 0.11.0
Copy link
Member

Choose a reason for hiding this comment

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

Copy/paste issue - please update @since tag

* @package WPCS\WordPressCodingStandards
*
* @since 0.9.0
* @since 0.13.0 Class name changed: this class is now namespaced.
Copy link
Member

Choose a reason for hiding this comment

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

Copy/paste issue - please update @since tag

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the FunctionEnqueued sniff.
Copy link
Member

Choose a reason for hiding this comment

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

Copy/paste issue - please update the sniff name

* Otherwise it will warn the user to make sure if its correct
*/
// print_r($parameters);
if ( isset( $parameters[5] ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

The sniff will now not throw an error if the $in_footer param is missing, which I believe it should.

If an explicit true or false is given, IMHO, the sniff should not report on it at all as that's a conscious developer choice.
If at all, it should be a warning for any non-true value, but with a separate errorcode for a false value so that particular warning can be silenced easily.


wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , array( 'jquery' ), '1.1.1', true ); // Ok.
wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , array( 'jquery' ), '1.1.1', false ); // Warning.
wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , array( 'jquery' ) ); // Error.
Copy link
Member

Choose a reason for hiding this comment

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

I think the following two cases should be added:

wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , array( 'jquery' ), '1.1.1' ); // Error - missing $in_footer.
wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , array( 'jquery' ), '1.1.1', $in_footer ); // Warning - $in_footer is not a boolean.

@jrfnl
Copy link
Member

jrfnl commented Nov 22, 2017

Last point: please look into the grammar of both the messages being thrown as well as the grammar of the text in the docblocks. IF => If, and other capitalization errors are the most notable issues.

@jrfnl
Copy link
Member

jrfnl commented Nov 23, 2017

@snightingale Could you rebase the PR against the current develop branch ? That way the unrelated build failure will no longer trouble you. The codestyle issues in this PR will still need to be fixed though.

@darthvader666uk darthvader666uk mentioned this pull request Feb 19, 2018
@jrfnl
Copy link
Member

jrfnl commented Mar 1, 2018

@snightingale Just checking: Do you have any intention of updating this PR based on the above feedback or should we close the PR or open it up to one of the WPCS maintainer to update the PR ?

@darthvader666uk
Copy link

@jrfnl (have to use this account for now) Yes, I managed to rebase this branch from Master changes and I have a spare time this weekend to have a look at the requested changes. So sorry fo the delay :)

@jrfnl
Copy link
Member

jrfnl commented Mar 1, 2018

@darthvader666uk No worries, just glad to hear you're still interested in continuing work on this.

@darthvader666uk
Copy link

@jrfnl I have now tweaked the code from your comments / travis fails and it appears to be ok. Let me know if you see any further issues

use WordPress\AbstractFunctionParameterSniff;

/**
* Based off the StrictInArraySniff.php Sniff, THis checks the Enqueued 4th Parameter to make sure a Version is available.
Copy link
Member

Choose a reason for hiding this comment

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

Several unnecessary uppercase characters here.

Copy link
Member

Choose a reason for hiding this comment

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

Also, the description doesn't represent all the checks that happen here.

/**
* Based off the StrictInArraySniff.php Sniff, THis checks the Enqueued 4th Parameter to make sure a Version is available.
* The Enqueued functions are:
* wp_register_script()
Copy link
Member

Choose a reason for hiding this comment

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

Use:

* - `wp_register_script()`

for each item here, so that extracted docs will display it as a list, with code markup.

* wp_enqueue_script()
* wp_register_style()
* wp_enqueue_style()
* IF a source ($src) value is passed, then version ($ver) needs to have a value.
Copy link
Member

Choose a reason for hiding this comment

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

Leave a line space under the list, and no need for uppercase IF or other random capitals.

/**
* List of Enqueued functions that need to be check to make sure
*
* @link https://developer.wordpress.org/reference/functions/wp_register_script/
Copy link
Member

Choose a reason for hiding this comment

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

Include the protocol if using URLs here.

* Version Check
*
* Check to make sure a Version ($ver) is set
* Otherwise it will Show an error to add a Src (url) to the Enqueued function
Copy link
Member

Choose a reason for hiding this comment

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

This comment is incorrect, but the whole comment block is redundant anyway.

// Check version is set.

would be sufficient.

Strongly consider moving each check to its own protected/private method, and document that instead, and then add calls to those new methods in this method.

use WordPress\AbstractFunctionParameterSniff;

/**
* Based off the StrictInArraySniff.php Sniff, THis checks the Enqueued 4th Parameter to make sure a Version is available.
Copy link
Member

Choose a reason for hiding this comment

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

Also, the description doesn't represent all the checks that happen here.

case 'wp_register_script':
case 'wp_enqueue_script':
if ( 'true' !== $parameters[5]['raw'] ) {
$this->phpcsFile->addWarning( 'If the Footer is not set to True for %s; Double check if correct or set to True', $stackPtr, 'MissingInFooter', array( $matched_content ) );
Copy link
Member

Choose a reason for hiding this comment

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

If is not needed here.

Copy link
Member

Choose a reason for hiding this comment

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

"The $in_footer is not set to true. ..."

Choose a reason for hiding this comment

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

Hi @GaryJones With the IF removed, all checks fail. THis check needs to be here in order to get the warning. Unless I missed something?

Copy link
Member

Choose a reason for hiding this comment

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

@darthvader666uk I think @GaryJones's remark was not about the conditional, but about the phrasing of the error message.

For that matter, mind capitalizing words in the error message when it's not needed.

wp_register_style( 'script-name', 'https://domain.com/someScript.js' ); // Error.

wp_enqueue_style( 'script-name', 'https://domain.com/someScript.js', false, '1.0.0'); // Ok.
wp_enqueue_style( 'script-name', 'https://domain.com/someScript.js' ); // Error.
Copy link
Member

Choose a reason for hiding this comment

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

Clear line space needed at the end.

@@ -0,0 +1,15 @@
<?php

Copy link
Member

Choose a reason for hiding this comment

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

What about cases for a couple of the functions that have a version but no in_footer param?

@GaryJones
Copy link
Member

Note that between https://core.trac.wordpress.org/ticket/12009 and https://core.trac.wordpress.org/ticket/22249 the parameters for the functions covered here may change.

@jrfnl
Copy link
Member

jrfnl commented Jun 11, 2018

@snightingale @darthvader666uk Hiya, just checking in again to see how work is progressing. If you haven't got time to finish this, please let us know and we'll see if we can find someone to take over.

@darthvader666uk
Copy link

@jrfnl I do appologies for the crazy late reply to this issue. However, with work commitments I am unable to proceed with the fix. If you have someone else ready to take over, I think that would be for the best :)

@jrfnl
Copy link
Member

jrfnl commented Jun 12, 2018

@darthvader666uk Thanks for your response! I understand and I'll see if we can find someone to take over.

@moorscode
Copy link
Contributor

PR #1378 supersedes this one.

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

jrfnl commented Jul 12, 2018

@darthvader666uk Thanks for initiating this sniff and your initial work on it. The continuation of your work by @moorscode has now been merged 🎉

@jrfnl jrfnl added this to the 1.0.0 milestone Jul 12, 2018
@GaryJones GaryJones removed this from the 1.0.0 milestone Aug 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants