-
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
Add sniff for deprecated Theme related WP Constants. #30
Merged
grappler
merged 1 commit into
feature/theme-review-sniffs
from
WPTRT/feature/issue-23-forbidden-constants
Jul 24, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* WordPress Coding Standard. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ | ||
*/ | ||
|
||
/** | ||
* Forbids usage of deprecated WP CONSTANTS and recommends alternatives. | ||
* | ||
* @link https://make.wordpress.org/themes/handbook/review/required/#core-functionality-and-features | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Juliette Reinders Folmer <[email protected]> | ||
*/ | ||
class WordPress_Sniffs_Theme_DeprecatedWPConstantsSniff implements PHP_CodeSniffer_Sniff { | ||
|
||
/** | ||
* List of deprecated WP constants and their replacements. | ||
* | ||
* @var array | ||
*/ | ||
private $deprecated_constants = array( | ||
'STYLESHEETPATH' => 'get_stylesheet_directory()', | ||
'TEMPLATEPATH' => 'get_template_directory()', | ||
'PLUGINDIR' => 'WP_PLUGIN_DIR', | ||
'MUPLUGINDIR' => 'WPMU_PLUGIN_DIR', | ||
'HEADER_IMAGE' => 'add_theme_support( \'custom-header\' )', | ||
'NO_HEADER_TEXT' => 'add_theme_support( \'custom-header\' )', | ||
'HEADER_TEXTCOLOR' => 'add_theme_support( \'custom-header\' )', | ||
'HEADER_IMAGE_WIDTH' => 'add_theme_support( \'custom-header\' )', | ||
'HEADER_IMAGE_HEIGHT' => 'add_theme_support( \'custom-header\' )', | ||
'BACKGROUND_COLOR' => 'add_theme_support( \'custom-background\' )', | ||
'BACKGROUND_IMAGE' => 'add_theme_support( \'custom-background\' )', | ||
); | ||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() { | ||
return array( | ||
T_STRING, | ||
); | ||
} | ||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* | ||
* @return void | ||
*/ | ||
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { | ||
$tokens = $phpcsFile->getTokens(); | ||
$token = $tokens[ $stackPtr ]; | ||
|
||
if ( ! isset( $this->deprecated_constants[ $tokens[ $stackPtr ]['content'] ] ) ) { | ||
return; | ||
} | ||
|
||
$prev = $phpcsFile->findPrevious( T_WHITESPACE, ( $stackPtr - 1 ), null, true ); | ||
|
||
if ( T_DOUBLE_COLON === $tokens[ $prev ]['code'] ) { | ||
// Class constant of the same name. | ||
return; | ||
} | ||
|
||
if ( T_NS_SEPARATOR === $tokens[ $prev ]['code'] && T_STRING === $tokens[ ( $prev - 1 ) ]['code'] ) { | ||
// Namespaced constant of the same name. | ||
return; | ||
} | ||
|
||
// Ok, this is really one of the deprecated constants. | ||
$error = 'Found usage of constant "%s". Use %s instead.'; | ||
$data = array( $tokens[ $stackPtr ]['content'], $this->deprecated_constants[ $tokens[ $stackPtr ]['content'] ] ); | ||
$phpcsFile->addError( $error, $stackPtr, 'Found', $data ); | ||
|
||
} // end process() | ||
|
||
} // end class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
define( 'STYLESHEETPATH', 'something' ); // Ok - will throw an error about already defined constant anyway. | ||
|
||
if ( defined( 'STYLESHEETPATH' ) ) { // Ok. | ||
// Do something unrelated. | ||
} | ||
|
||
echo \mynamespace\STYLESHEETPATH; // Ok, not WP constant. | ||
|
||
echo My_Class::STYLESHEETPATH; // Ok, not WP constant. | ||
|
||
echo STYLESHEETPATH; // Bad. | ||
$folder = basename( TEMPLATEPATH ); // Bad. | ||
$file = PLUGINDIR . '/js/myfile.js'; // Bad. | ||
echo MUPLUGINDIR; // Bad. | ||
echo HEADER_IMAGE; // Bad. | ||
echo NO_HEADER_TEXT; // Bad. | ||
echo HEADER_TEXTCOLOR; // Bad. | ||
echo HEADER_IMAGE_WIDTH; // Bad. | ||
echo HEADER_IMAGE_HEIGHT; // Bad. | ||
echo BACKGROUND_COLOR; // Bad. | ||
echo BACKGROUND_IMAGE; // Bad. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Unit test class for WordPress Coding Standard. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/ | ||
*/ | ||
|
||
/** | ||
* WordPress_Tests_Theme_DeprecatedWPConstants | ||
* | ||
* A sniff unit test checks a .inc file for expected violations of a single | ||
* coding standard. Expected errors and warnings are stored in this class. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @author Juliette Reinders Folmer <[email protected]> | ||
*/ | ||
class WordPress_Tests_Theme_DeprecatedWPConstantsUnitTest extends AbstractSniffUnitTest { | ||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* 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) | ||
*/ | ||
public function getErrorList() { | ||
return array( | ||
9 => ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ? 0 : 1 ), | ||
13 => 1, | ||
14 => 1, | ||
15 => 1, | ||
16 => 1, | ||
17 => 1, | ||
18 => 1, | ||
19 => 1, | ||
20 => 1, | ||
21 => 1, | ||
22 => 1, | ||
23 => 1, | ||
); | ||
|
||
} | ||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* 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) | ||
*/ | ||
public function getWarningList() { | ||
return array(); | ||
|
||
} // end getWarningList() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we want to keep the closing comment? |
||
|
||
} // end class |
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.
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.
Missing a full stop at the end 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.
Working on improving the standard documentation in WPCS and will port back to any code in the theme review branch if/when my changes are accepted in WPCS. Ok?