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

PHPCS 3.x prep: work round a function which will no longer exist #1045

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.