-
Notifications
You must be signed in to change notification settings - Fork 37
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
No Favicon #78
No Favicon #78
Conversation
); | ||
foreach ( $checks as $check ) { | ||
if ( false !== strpos( $token['content'], $check ) ) { | ||
$phpcsFile->addError( 'Possible Favicon found. Favicons are handled by the Site Icon setting in the customizer since version 4.3.' , $stackPtr, 'NoFavicon' ); |
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.
What about "Code for Favicons found."?
563cd1d
to
6671823
Compare
*/ | ||
public function register() { | ||
return array( | ||
T_STRING, |
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 are you sniffing T_STRING
?
public function register() { | ||
return array( | ||
T_STRING, | ||
T_CONSTANT_ENCAPSED_STRING, |
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.
Better to use the build in PHP_CodeSniffer_Tokens::$stringTokens
and add T_INLINE_HTML
and any other tokens you really need, than hard-code T_CONSTANT_ENCAPSED_STRING
.
$tokens = PHP_CodeSniffer_Tokens::$stringTokens;
$tokens[] = T_INLINE_HTML;
retuen $tokens;
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ | ||
*/ |
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 update the doc block to comply with the current standards.
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author khacoder | ||
*/ |
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 update the doc block to comply with the current standards.
$tokens = $phpcsFile->getTokens(); | ||
$token = $tokens[ $stackPtr ]; | ||
|
||
$checks = array( |
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 list should be a class property.
@@ -0,0 +1,6 @@ | |||
/* ----- Bad ------- ERROR ---- */ | |||
<link rel="shortcut icon" href="<?php myfunction('link' ); ?>"> |
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 make the test files lead by example and not include other bad practices, the href functions should all have esc_url()
s around them. Alternatively just hardcode a fake url.
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ | ||
*/ |
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 update the doc block to comply with the current standards.
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author khacoder | ||
*/ |
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 update the doc block to comply with the current standards.
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @return array(int => int) |
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 update the doc block to comply with the current standards.
* The key of the array should represent the line number and the value | ||
* should represent the number of warnings that should occur on that line. | ||
* | ||
* @return array(int => int) |
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 update the doc block to comply with the current standards.
Unit tests are failing, but I think I can see why - will leave the solution inline. P.S.: I did say it was pseudo-code with the code example, so untested, untried.... |
class WordPress_Sniffs_Theme_NoFaviconSniff implements PHP_CodeSniffer_Sniff { | ||
|
||
const REGEX_TEMPLATE = '` (%s)`i'; | ||
const REGEX_ATTR_TEMPLATE = '%1$s=[\'"]%2$s[\'"]'; |
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.
Replace these two lines like so and I believe the unit tests will pass (fingers crossed - still doing this without running actual tests 😎 ):
const REGEX_TEMPLATE = '` (?:%s)`i';
const REGEX_ATTR_TEMPLATE = '%1$s=[\'"](?:%2$s)[\'"]';
Change in the first line isn't really needed, but can't hurt either.
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's why we have unit tests. 😎 How would you write the inline docs for these constants.
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.
Similar to how you document the class properties. I'm not sure about the tag to use at the moment - I believe at some point there was a @const
tag, but would need to check if that's still a thing.
To check you could have a look through the current version of the PSR proposal for documentation and at the PHPDocumentor documentation.
public function register() { | ||
$regex_parts = array(); | ||
foreach( $this->attribute_blacklist as $key => $values ) { | ||
$values = array_map( 'preg_quote', $values ); |
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.
Oh and to be extremely precise, we should pass the delimiter to preg_quote, though it's not going to make any difference in this case:
$values = array_map( 'preg_quote', $values, array_fill ( 0, count( $values ), '`' ) );
Unit tests still failing, but I can see why - we're matching on the whole attribute |
'icon', | ||
'apple-touch-icon', | ||
'apple-touch-icon-precomposed', | ||
), |
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.
We should add:
- 'shortcut icon'
- 'bookmark icon'
Not sure about 'icon shortcut'
as I'm not sure that's even a valid value. If it is, it should be added to the list as well.
These could also be considered for addition (both in unit tests as well as in the list):
<meta name="apple-mobile-web-app-title" content="<?php echo self::APP_NAME; ?>" />
<?php
echo '
<meta name="msapplication-config" content="/my/path/browserconfig.xml" />
<meta name="msapplication-TileColor" content="#d6001e" />
<meta name="msapplication-TileImage" content="', esc_url( plugins_url( 'images/icons/mstile-144x144.png?v=' . self::VERSION, __FILE__ ) ), '" />
<meta name="msapplication-square70x70logo" content="', esc_url( plugins_url( 'images/icons/tiny.png?v=' . self::VERSION, __FILE__ ) ), '" />
<meta name="msapplication-square150x150logo" content="', esc_url( plugins_url( 'images/icons/square.png?v=' . self::VERSION, __FILE__ ) ), '" />
<meta name="msapplication-wide310x150logo" content="', esc_url( plugins_url( 'images/icons/wide.png?v=' . self::VERSION, __FILE__ ) ), '" />
<meta name="msapplication-square310x310logo" content="', esc_url( plugins_url( 'images/icons/large.png?v=' . self::VERSION, __FILE__ ) ), '" />';
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 previous best practice was to have 'shortcut icon'
. I don't think the reverse was ever valid. https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types
I wonder what the benafits of adding extra checks would be. I expect most implementations of the favicon would all or nothing.
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 agree that they probably wouldn't catch any more, but the benefit is of course completeness.
First Commit Issue #70
The checks are passing now. |
* @return array | ||
*/ | ||
public function register() { | ||
$regex_parts = array(); |
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.
Maybe add a comment above this line // Build the regex to be used only once.
?
$token = $tokens[ $stackPtr ]; | ||
|
||
if ( preg_match( $this->favicon_regex, $token['content'] ) > 0 ) { | ||
$phpcsFile->addError( 'Code for Favicon found. Favicons are handled by the Site Icon setting in the customizer since version 4.3.' , $stackPtr, 'NoFavicon' ); |
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 capitalization in the error sentence seems a bit strange.
What about :
'Code for favicon found. Favicons are handled by the "Site Icon" setting in the customizer since version 4.3.'
<?php | ||
echo ' | ||
<meta name="msapplication-config" content="/my/path/browserconfig.xml" /> | ||
<meta name="msapplication-TileImage" content="', esc_url( plugins_url( 'images/icons/mstile-144x144.png?v=' . self::VERSION, __FILE__ ) ), '" /> |
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 realize I provided the example code, but it came from a plugin... as this is a theme check sniff, using plugins_url()
in the example code feels misleading to me.
Great. Three more minor comments to dot the i's... |
First Commit
Issue #70