Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Feature/templates/required #214
base: develop
Are you sure you want to change the base?
Feature/templates/required #214
Changes from 5 commits
355f42f
f6dc4f2
f521b85
6c4c1bc
3226883
c19ea43
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public
sniff properties can be changed from a custom ruleset. Was this done intentionally? If not you can change visibility toprotected
🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I don't know the stance on that for this repo, but it was intentional mostly for my own use case, which probably isn't very common. I figured if I had a need to tweak something, then someone somewhere might as well. I did name this as a "config" to indicate it was able to be configured and is an open point, so others can implement as needed. I generally like to leave configurations open as much as possible because you never know what someone might want to modify 😄, but I completely understand if that's not a standard approach here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timelsass Sorry, but this won't work. Please make this property
protected
orprivate
.While you can have
public
array properties which can be changed from within a ruleset, those can only be simple arrays, not complex multi-level arrays like the one you are using here. And even then, if this were to bepublic
you'd need a validation routine to ensure the user-input received would be usable.If you'd want to be able to extend the array I suggest making it a
protected
property so other sniffs can extend this sniff and overload it.Other than that, I think modular error codes is the way to go and you're already using those, so 👍 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole property can be removed. The PHPCS default is
PHP
only, so you only need to set this property when you also want to examineJS
and/orCSS
files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This description needs to be expanded as from the description alone, it is unclear what the property is for.
Also: should this property maybe be
private
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment about the
clean_str()
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 on keeping track of which file you are in for matching the tags 👍
I think this can be simplified a little though for better performance.
The way this is currently set up, the
$this->tag
array can become quite large for themes with a lot of files.What about just having a string
$current_file
property and string$last_tag
property ?If the
$filename
doesn't match$current_file
, you assign the new$filename
to$this->current_file
and can then just clear the$last_tag
value or set it to an empty string.This should lower memory usage when scanning a large theme.
The only thing which would need to be verified in that case would be that this will work OK when running PHPCS with the
parallel
option. I haven't looked into the implementation of that deeply enough to be sure of whether or not the logic I'm proposing would cause problems when running inparallel
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also wondering if
$this->tag[ $filename ]
shouldn't be set tofalse
each time the sniff is called anyway as when the closer of the HTML tag is found it should be atfalse
to start with anyway, but if that isn't found (HTML error), you may still have a tag set from a previously call to the sniff.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When not using the
clean_str()
method:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just keep track of the matched
$tag
? The$tagsConfig
array be accessed from anywhere in this sniff, so there is no need to (partially) duplicate it for each matched tag.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This transformation is not needed unless an error will be thrown. I suggest moving it lower down.
Also have a look at the WPCS
string_to_errorcode()
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small code-consistency note (which isn't checked for by the CS check yet): generally it is considered best practice to have the boolean operators at the start of the line containing the next condition as this improves readability.
While not a formal requirement, as this is consistently done throughout the rest of the code-base, I'd like to suggest complying with that here and in other places in the sniff, as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code would also match
MyClass::language_attributes()
,$obj->language_attributes()
andMy\Name\Space\language_attributes()
, none of which are the WP native function which is required to be used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also: I'm not sure what you're trying to do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
if ( false === $next ) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PHPCS
addError()
andaddWarning()
methods have build in(s)printf()
like functionality. Please use it.See: https://pear.php.net/package/PHP_CodeSniffer/docs/3.4.2/apidoc/PHP_CodeSniffer/File.html#methodaddError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a return here effectively hides one error behind another which will make fixing things unwieldy, so it is better not to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
return ( $TagEndToken + 1 );
at the very end of the function (outside of the conditions).This will make the sniff more efficient as PHPCS won't call the sniff again until it's reached that token, preventing it from needlessly (re-)examining strings which have already been examined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this could be used in other sniffs, we could do what @jrfnl suggested in the #213 PR about
strip_quotes
method - create aWPThemeReview\Utils\Common
class with these kind of utilities 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny you mentioned it - I initially made a Util class for this while testing it out, but after looking over WPThemeReview I didn't see one anywhere else, or a common area, so I just figured the "norm" was to keep functions self contained per class. I noticed some WPCS stuff that would be useful, but I haven't worked my way to that level lol.
It looks like we are all trying to address the same issues revolving around
T_CONSTANT_ENCAPSED_STRING
andT_DOUBLE_QUOTED_STRING
though. I don't recall all of the specifics, but I think I needed to trim single quotes in addition to the double quotes added for ensuring the functions were being caught inside of the actual HTML tags where attributes are added, not inside of any quoted areas if it's broken up between HTML/PHP, and accounting for an attribute containing a>
character. Not very common but does happen sometimes. Something like this scenario - if I remember correctly (I could be wrong):There were some other weird scenarios with doing things like mixing heredoc/functions(print,
echo, printf etc)/HTML together which doesn't work out perfectly all of the time, but I also think if someone is doing something that far out of the ordinary they obviously have no coding standards or desire to follow any. I thought of a solution afterwards, but it would require doing some weird tracking of the opening/closing quotes, which seemed like a waste of time to cover those fringe cases ( realistically probably 0.000000000001%/never encountering it ).
I'm not sure if this function will be useful anywhere else though, but I could see just the
strip_quotes
being used commonly. I think a Util class would be nice to have though, and there might be more things as we expand the sniffs further where we realize overlap in some functionality. I honestly have no clue since this is the first time writing a phpcs sniff 😆There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would break the HTML. The
>
inSome > Title
needs to be encoded as>
for this to be valid HTML.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re:
Util
class or something along those lines.I think this would be a good idea.
If I remember correctly, the reasons why in WPCS all utility methods are in the abstract
Sniff
class and all classes extend it, is historic.PHPCS 2.x file auto-loading didn't support helper files as easily, but the PHPCS 3.x file auto-loading is more flexible.
As far as I can see, as long as the file is within the standards directory, is namespaced based on the path and the file name doesn't end on
Sniff
, this should work in PHPCS 3.x out of the box.A
use
statement for the class in sniffs using it should then be able to take care of the rest.Maybe I suggest a
WPThemeReview\Helpers\StringUtils.php
file containing a class namedStringUtils
which would then contain the (static
) functions ?This will allow for adding more
Util
type classes later for functions addressing other language structures, keeping it all modular instead of having one massiveUtil
class.Alternatively, for the
strip_quotes()
method from WPCS, I'd happily accept a PR to make that astatic
method in WPCS.For the time being, that should allow you to use it even when a sniff doesn't extend the WPCS
Sniff
class (to bridge the time until the method is available in PHPCS).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but I'm going to be horrible and say this function is a bad idea.
If a text string spans multiple lines, start/end string quotes may be on different lines. By "cleaning" those quotes of the strings in individual lines without matching them, you will be removing legitimate characters which are part of the string itself.
If you want to clear the string quotes of PHP strings:
strip_quotes
function.T_INLINE_HTML
,T_HEREDOC
orT_NOWDOC
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class
attribute with a different HTML tag like<div>
.class = "..."
. If I remember correct HTML ignores most whitespace, so this should still work in HTML.