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

No Favicon #78

Merged
merged 4 commits into from
Oct 24, 2016
Merged

No Favicon #78

merged 4 commits into from
Oct 24, 2016

Conversation

khacoder
Copy link
Contributor

First Commit
Issue #70

);
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' );
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 "Code for Favicons found."?

@grappler grappler force-pushed the feature/theme-review-sniffs branch 2 times, most recently from 563cd1d to 6671823 Compare September 27, 2016 12:28
*/
public function register() {
return array(
T_STRING,
Copy link
Contributor

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,
Copy link
Contributor

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/
*/
Copy link
Contributor

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
*/
Copy link
Contributor

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(
Copy link
Contributor

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' ); ?>">
Copy link
Contributor

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/
*/
Copy link
Contributor

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
*/
Copy link
Contributor

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)
Copy link
Contributor

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)
Copy link
Contributor

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.

@grappler grappler self-assigned this Oct 12, 2016
@jrfnl
Copy link
Contributor

jrfnl commented Oct 14, 2016

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[\'"]';
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

@jrfnl jrfnl Oct 14, 2016

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 );
Copy link
Contributor

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 ), '`' ) );

@jrfnl
Copy link
Contributor

jrfnl commented Oct 14, 2016

Unit tests still failing, but I can see why - we're matching on the whole attribute attr="value" and some of the values for rel at the moment are partial values, so they won't match.

'icon',
'apple-touch-icon',
'apple-touch-icon-precomposed',
),
Copy link
Contributor

@jrfnl jrfnl Oct 14, 2016

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__ ) ), '" />';

Copy link
Member

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.

Copy link
Contributor

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.

@grappler
Copy link
Member

The checks are passing now.

* @return array
*/
public function register() {
$regex_parts = array();
Copy link
Contributor

@jrfnl jrfnl Oct 22, 2016

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' );
Copy link
Contributor

@jrfnl jrfnl Oct 22, 2016

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__ ) ), '" />
Copy link
Contributor

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.

@jrfnl
Copy link
Contributor

jrfnl commented Oct 22, 2016

Great. Three more minor comments to dot the i's...

@jrfnl jrfnl merged commit 6cbc3d9 into WPTT:feature/theme-review-sniffs Oct 24, 2016
@jrfnl jrfnl added this to the 0.1.0 milestone Aug 20, 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.

3 participants