Skip to content

Commit

Permalink
PrefixAllGlobals: minor tidying up
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed May 10, 2019
1 parent a3fc9cf commit 162c67e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 21 deletions.
17 changes: 11 additions & 6 deletions WPThemeReview/Sniffs/CoreFunctionality/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
* Verify that everything defined in the global namespace is prefixed with a theme specific prefix.
*
* This sniff extends the upstream WPCS PrefixAllGlobalsSniff. The differences are:
* - For non-prefixed global variables, an error will only be thrown when the variable is created outside of a theme template file.
* - For non-prefixed global variables, an error will only be thrown when the variable
* is created outside of a theme template file.
*
* @link https://github.com/WPTRT/WPThemeReview/issues/205
* @link https://github.com/WPTRT/WPThemeReview/issues/201
* @link https://github.com/WPTRT/WPThemeReview/issues/200
* @link https://github.com/WPTRT/WPThemeReview/issues/205
* @link https://github.com/WPTRT/WPThemeReview/issues/201
* @link https://github.com/WPTRT/WPThemeReview/issues/200
*
* @since 0.2.0
*/
Expand All @@ -30,7 +31,9 @@ class PrefixAllGlobalsSniff extends WPCSPrefixAllGlobalsSniff {
* The list of allowed folders to check the file path against.
*
* The WPThemereview standards contains a base set for this property in the ruleset.xml.
* This array can be extended in the custom ruleset.
* This array can be extended in a custom ruleset.
*
* @since 0.2.0
*
* @var array
*/
Expand Down Expand Up @@ -66,7 +69,9 @@ protected function process_variable_assignment( $stackPtr ) {
}

/**
* Checks if the given file path is located in the $allowed_folders array
* Checks if the given file path is located in the $allowed_folders array.
*
* @since 0.2.0
*
* @param string $path Full path of the sniffed file.
* @return boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,17 @@ protected function getTestFiles( $testFileBase ) {
* @param string $testFile The name of the file being tested.
* @return array <int line number> => <int number of errors>
*/
public function getErrorList( $testFile = 'partials/post-edit.inc' ) {
public function getErrorList( $testFile = '' ) {
switch ( $testFile ) {
case 'attachment.inc':
// Template file - all OK, fall through to the default case.
case 'footer_widgets.inc':
case 'header.inc':
case 'social-share.inc':
return array(
6 => 1,
5 => 1,
);
case 'post-edit.inc':
// Template file - all OK, fall through to the default case.

default:
return array();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[] template-parts,templates,partials
<?php

$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // OK, template file.

// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[]
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[] template-parts,templates,partials

<?php

$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // Error. Not in a template file.

// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[]
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[] template-parts,templates,partials
<?php

$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // Error. Not in a template file.

// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[]
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[] template-parts,templates,partials
<?php

$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // OK, template file.

// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[]
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[] template-parts,templates,partials
<?php

$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // Error. Not in a template file.

// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals allowed_folders[]
2 changes: 0 additions & 2 deletions WPThemeReview/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
<!-- Covers: https://make.wordpress.org/themes/handbook/review/required/#code - last bullet. -->
<!-- NOTE: this sniff needs a custom property to be set for it to be activated. -->
<!-- See: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties#naming-conventions-prefix-everything-in-the-global-namespace -->
<!-- Checked via the WPThemeReview.CoreFunctionality.PrefixAllGlobals sniff -->
<rule ref="WPThemeReview.CoreFunctionality.PrefixAllGlobals">
<properties>
<property name="allowed_folders" type="array">
Expand Down Expand Up @@ -159,5 +158,4 @@
<!-- Themes should never touch the timezone. -->
<rule ref="WordPress.WP.TimezoneChange"/>


</ruleset>

0 comments on commit 162c67e

Please sign in to comment.