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

Separate out the sniffing for extract(). #604

Merged
merged 1 commit into from
Jul 17, 2016
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
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