Skip to content

Commit

Permalink
Separate out the sniffing for extract().
Browse files Browse the repository at this point in the history
Currently only VIP checked for the usage of `extract()`, even though it is an explicit rule for core.

Ref: https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#dont-extract

Solved #90 for real ;-)
  • Loading branch information
jrfnl authored and grappler committed Sep 25, 2016
1 parent a0082f1 commit c6a5a8e
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 10 deletions.
1 change: 1 addition & 0 deletions WordPress-Core/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,6 @@
<rule ref="WordPress.WhiteSpace.CastStructureSpacing"/>
<rule ref="WordPress.PHP.YodaConditions"/>
<rule ref="WordPress.WP.I18n"/>
<rule ref="WordPress.Functions.DontExtract"/>

</ruleset>
48 changes: 48 additions & 0 deletions WordPress/Sniffs/Functions/DontExtractSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* WordPress Coding Standard.
*
* @category PHP
* @package PHP_CodeSniffer
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/
*/

/**
* Restricts the usage of extract().
*
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#dont-extract
*
* @category PHP
* @package PHP_CodeSniffer
* @author Shady Sharaf <[email protected]>
*/
class WordPress_Sniffs_Functions_DontExtractSniff extends WordPress_AbstractFunctionRestrictionsSniff {

/**
* Groups of functions to restrict.
*
* Example: groups => array(
* 'lambda' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Use anonymous functions instead please!',
* 'functions' => array( 'eval', 'create_function' ),
* )
* )
*
* @return array
*/
public function getGroups() {
return array(

'extract' => array(
'type' => 'error',
'message' => '%s() usage is highly discouraged, due to the complexity and unintended issues it might cause.',
'functions' => array(
'extract',
),
),

);
} // end getGroups()

} // end class
8 changes: 0 additions & 8 deletions WordPress/Sniffs/VIP/RestrictedFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ public function getGroups() {
),
),

'extract' => array(
'type' => 'warning',
'message' => '%s() usage is highly discouraged, due to the complexity and unintended issues it might cause.',
'functions' => array(
'extract',
),
),

'custom_role' => array(
'type' => 'error',
'message' => 'Use wpcom_vip_add_role() instead of add_role()',
Expand Down
9 changes: 9 additions & 0 deletions WordPress/Tests/Functions/DontExtractUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

extract( array( 'a' => 1 ) ); // bad

// Similarly named functions or methods however are fine.
my_extract(); // Ok.
My_Object::extract(); // Ok.
$this->extract(); // Ok.
$my_object->extract(); // Ok.
53 changes: 53 additions & 0 deletions WordPress/Tests/Functions/DontExtractUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Unit test class for WordPress Coding Standard.
*
* @category PHP
* @package PHP_CodeSniffer
* @link https://make.wordpress.org/core/handbook/best-practices/coding-standards/
*/

/**
* 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 Akeda Bagus <[email protected]>
* @author Greg Sherwood <[email protected]>
* @author Marc McIntyre <[email protected]>
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class WordPress_Tests_Functions_DontExtractUnitTest 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(
3 => 1,
);

} // end getErrorList()

/**
* 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()

} // end class
2 changes: 1 addition & 1 deletion WordPress/Tests/VIP/RestrictedFunctionsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $ch = curl_init(); // bad

curl_close( $ch ); // bad

extract( array( 'a' => 1 ) ); // bad
// Empty line - function moved to another sniff.

add_role( 'test' ); // bad

Expand Down
1 change: 0 additions & 1 deletion WordPress/Tests/VIP/RestrictedFunctionsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public function getWarningList() {
13 => 1,
15 => 1,
17 => 1,
19 => 1,
58 => 1,
59 => 1,
61 => 1,
Expand Down

0 comments on commit c6a5a8e

Please sign in to comment.