Skip to content

Commit

Permalink
Merge pull request #1045 from WordPress-Coding-Standards/WPCS-0.13.0/…
Browse files Browse the repository at this point in the history
…feature/phpcs-3.x-prep-getgroups-in-abstracts

PHPCS 3.x prep: work round a function which will no longer exist
  • Loading branch information
GaryJones authored Jul 22, 2017
2 parents 1c4399f + b14688d commit 99146fd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
47 changes: 38 additions & 9 deletions WordPress/AbstractArrayAssignmentRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,26 @@ abstract class WordPress_AbstractArrayAssignmentRestrictionsSniff extends WordPr
*/
protected $excluded_groups = array();

/**
* Cache for the group information.
*
* @since 0.13.0
*
* @var array
*/
protected $groups_cache = array();

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
// Retrieve the groups only once and don't set up a listener if there are no groups.
if ( false === $this->setup_groups() ) {
return array();
}

return array(
T_DOUBLE_ARROW,
T_CLOSE_SQUARE_BRACKET,
Expand Down Expand Up @@ -80,6 +94,28 @@ public function register() {
*/
abstract public function getGroups();

/**
* Cache the groups.
*
* @since 0.13.0
*
* @return bool True if the groups were setup. False if not.
*/
protected function setup_groups() {
$this->groups_cache = $this->getGroups();

if ( empty( $this->groups_cache ) && empty( self::$groups ) ) {
return false;
}

// Allow for adding extra unit tests.
if ( ! empty( self::$groups ) ) {
$this->groups_cache = array_merge( $this->groups_cache, self::$groups );
}

return true;
}

/**
* Processes this test, when one of its tokens is encountered.
*
Expand All @@ -89,15 +125,8 @@ abstract public function getGroups();
*/
public function process_token( $stackPtr ) {

$groups = $this->getGroups();

if ( empty( $groups ) ) {
$this->phpcsFile->removeTokenListener( $this, $this->register() );
return;
}

$this->excluded_groups = $this->merge_custom_array( $this->exclude );
if ( array_diff_key( $groups, $this->excluded_groups ) === array() ) {
if ( array_diff_key( $this->groups_cache, $this->excluded_groups ) === array() ) {
// All groups have been excluded.
// Don't remove the listener as the exclude property can be changed inline.
return;
Expand Down Expand Up @@ -149,7 +178,7 @@ public function process_token( $stackPtr ) {
return;
}

foreach ( $groups as $groupName => $group ) {
foreach ( $this->groups_cache as $groupName => $group ) {

if ( isset( $this->excluded_groups[ $groupName ] ) ) {
continue;
Expand Down
48 changes: 39 additions & 9 deletions WordPress/AbstractVariableRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,26 @@ abstract class WordPress_AbstractVariableRestrictionsSniff extends WordPress_Sni
*/
protected $excluded_groups = array();

/**
* Cache for the group information.
*
* @since 0.13.0
*
* @var array
*/
protected $groups_cache = array();

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
// Retrieve the groups only once and don't set up a listener if there are no groups.
if ( false === $this->setup_groups() ) {
return array();
}

return array(
T_VARIABLE,
T_OBJECT_OPERATOR,
Expand Down Expand Up @@ -84,6 +98,28 @@ public function register() {
*/
abstract public function getGroups();

/**
* Cache the groups.
*
* @since 0.13.0
*
* @return bool True if the groups were setup. False if not.
*/
protected function setup_groups() {
$this->groups_cache = $this->getGroups();

if ( empty( $this->groups_cache ) && empty( self::$groups ) ) {
return false;
}

// Allow for adding extra unit tests.
if ( ! empty( self::$groups ) ) {
$this->groups_cache = array_merge( $this->groups_cache, self::$groups );
}

return true;
}

/**
* Processes this test, when one of its tokens is encountered.
*
Expand All @@ -94,16 +130,10 @@ abstract public function getGroups();
*/
public function process_token( $stackPtr ) {

$token = $this->tokens[ $stackPtr ];
$groups = $this->getGroups();

if ( empty( $groups ) ) {
$this->phpcsFile->removeTokenListener( $this, $this->register() );
return;
}
$token = $this->tokens[ $stackPtr ];

$this->excluded_groups = $this->merge_custom_array( $this->exclude );
if ( array_diff_key( $groups, $this->excluded_groups ) === array() ) {
if ( array_diff_key( $this->groups_cache, $this->excluded_groups ) === array() ) {
// All groups have been excluded.
// Don't remove the listener as the exclude property can be changed inline.
return;
Expand All @@ -118,7 +148,7 @@ public function process_token( $stackPtr ) {
}
}

foreach ( $groups as $groupName => $group ) {
foreach ( $this->groups_cache as $groupName => $group ) {

if ( isset( $this->excluded_groups[ $groupName ] ) ) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff extends WordPress
* @return array
*/
public function getGroups() {
return parent::$groups;
return array();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Sniffs/Variables/VariableRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WordPress_Sniffs_Variables_VariableRestrictionsSniff extends WordPress_Abs
* @return array
*/
public function getGroups() {
return parent::$groups;
return array();
}

} // End class.

0 comments on commit 99146fd

Please sign in to comment.