Skip to content

Commit

Permalink
Fix the PR issues and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dingo-d committed May 9, 2019
1 parent 42ded99 commit 217e2f2
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 44 deletions.
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a

_No documentation available about unreleased changes as of yet._

## [0.2.0] - 2019-04-13



## [0.1.0] - 2018-10-31

### Added
Expand Down
14 changes: 6 additions & 8 deletions WPThemeReview/Sniffs/CoreFunctionality/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ 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.
* If you are using PHPCS prior to the version 3.4.0 you would overwrite the current value by setting:
*
* <property name="allowed_folders" type="array" values="template-parts,templates,partials,my-template-folder">
*
* If you are using PHPCS 3.4.0 or greater you can extend this array by setting extends="true" attribute:
*
* <property name="allowed_folders" type="array" extends="true" values="my-template-folder">
*
* @var array
*/
Expand Down Expand Up @@ -68,7 +62,7 @@ protected function process_variable_assignment( $stackPtr ) {
$fileName = basename( $file );

// Don't process in case of a template file or folder.
if ( preg_match( FileNameSniff::THEME_EXCEPTIONS_REGEX, $fileName ) === 1 && $this->is_from_allowed_folder( $file ) ) {
if ( preg_match( FileNameSniff::THEME_EXCEPTIONS_REGEX, $fileName ) === 1 || $this->is_from_allowed_folder( $file ) ) {
return;
}

Expand All @@ -82,6 +76,10 @@ protected function process_variable_assignment( $stackPtr ) {
* @return boolean
*/
private function is_from_allowed_folder( $path ) {
if ( empty( $this->allowed_folders ) || ! is_array( $this->allowed_folders ) ) {
return false;
}

foreach ( $this->allowed_folders as $folder ) {
if ( strrpos( $path, '/' . $folder . '/' ) !== false ) {
return true;
Expand Down
46 changes: 22 additions & 24 deletions WPThemeReview/Tests/CoreFunctionality/PrefixAllGlobalsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
*/
class PrefixAllGlobalsUnitTest extends AbstractSniffUnitTest {

/**
* Get a list of all test files to check.
*
* @param string $testFileBase The base path that the unit tests files will have.
*
* @return string[]
*/
protected function getTestFiles( $testFileBase ) {
$sep = \DIRECTORY_SEPARATOR;
$test_files = glob( dirname( $testFileBase ) . $sep . 'ThemeTemplatesException{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', \GLOB_BRACE );

if ( ! empty( $test_files ) ) {
return $test_files;
}

return array( $testFileBase . '.inc' );
}

/**
* Returns the lines where errors should occur.
*
Expand All @@ -28,15 +46,13 @@ class PrefixAllGlobalsUnitTest extends AbstractSniffUnitTest {
*/
public function getErrorList( $testFile = 'partials/post-edit.inc' ) {
switch ( $testFile ) {
case 'social-share.inc':
return array(
6 => 1,
);
case 'header.inc':
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.
Expand All @@ -45,24 +61,6 @@ public function getErrorList( $testFile = 'partials/post-edit.inc' ) {
}
}

/**
* Get a list of all test files to check.
*
* @param string $testFileBase The base path that the unit tests files will have.
*
* @return string[]
*/
protected function getTestFiles( $testFileBase ) {
$sep = \DIRECTORY_SEPARATOR;
$test_files = glob( dirname( $testFileBase ) . $sep . 'ThemeTemplatesException{' . $sep . ',' . $sep . '*' . $sep . '}*.inc', \GLOB_BRACE );

if ( ! empty( $test_files ) ) {
return $test_files;
}

return array( $testFileBase . '.inc' );
}

/**
* Returns the lines where warnings should occur.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
<?php

$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // OK, template file.
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!-- Annotation has to be on the second line as errors are thrown on line 1 and errors on annotation lines are ignored. -->
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
<?php

$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // Error.
$var = 'Value'; // Error. Not in a template file.
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<!-- Annotation has to be on the second line as errors are thrown on line 1 and errors on annotation lines are ignored. -->
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
<?php

$title = 'Value'; // OK, template file.
$my_theme_var = 123; // OK, prefixed.
$var = 'Value'; // Error. Not in a template file.
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[]

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- Annotation has to be on the second line as errors are thrown on line 1 and errors on annotation lines are ignored. -->
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
<?php

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!-- Annotation has to be on the second line as errors are thrown on line 1 and errors on annotation lines are ignored. -->
// phpcs:set WPThemeReview.CoreFunctionality.PrefixAllGlobals prefixes[] my_theme
<?php

Expand Down
9 changes: 9 additions & 0 deletions WPThemeReview/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@
<!-- 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">
<element value="template-parts"/>
<element value="templates"/>
<element value="partials"/>
</property>
</properties>
</rule>

<!-- Check for correct spelling of WordPress. -->
<!-- Covers: https://make.wordpress.org/themes/handbook/review/required/#naming - third bullet. -->
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
],
"require" : {
"php" : ">=5.4",
"squizlabs/php_codesniffer": "^3.4.1",
"squizlabs/php_codesniffer": "^3.4.2",
"wp-coding-standards/wpcs" : "^2.1.0",
"phpcompatibility/phpcompatibility-wp": "^2.0"
},
Expand Down

0 comments on commit 217e2f2

Please sign in to comment.