-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sniff for required function parameter
- Loading branch information
1 parent
fe8ea10
commit 5bc1d81
Showing
7 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
phpcs-sniffs/PluginCheck/Sniffs/CodeAnalysis/RequiredFunctionParametersSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* RequiredFunctionParametersSniff | ||
* | ||
* Based on code from {@link https://github.com/WordPress/WordPress-Coding-Standards} | ||
* which is licensed under {@link https://opensource.org/licenses/MIT}. | ||
* | ||
* @package PluginCheck | ||
*/ | ||
|
||
namespace PluginCheckCS\PluginCheck\Sniffs\CodeAnalysis; | ||
|
||
use PHPCSUtils\Utils\PassedParameters; | ||
use WordPressCS\WordPress\AbstractFunctionParameterSniff; | ||
|
||
/** | ||
* Detect missing required function parameters. | ||
* | ||
* @link https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/ | ||
* | ||
* @since 1.3.0 | ||
*/ | ||
final class RequiredFunctionParametersSniff extends AbstractFunctionParameterSniff { | ||
|
||
/** | ||
* Array of functions to check. | ||
* | ||
* @since 1.3.0 | ||
* | ||
* @var array<string, array<string, int|string>> Function name as key, array with target parameter and name as value. | ||
*/ | ||
protected $target_functions = array( | ||
'parse_str' => array( | ||
'position' => 2, | ||
'name' => 'result', | ||
), | ||
); | ||
|
||
/** | ||
* Processes this test, when one of its tokens is encountered. | ||
* | ||
* @since 1.3.0 | ||
* | ||
* @param int $stackPtr The position of the current token in the stack. | ||
* @return int|void Integer stack pointer to skip forward or void to continue normal file processing. | ||
*/ | ||
public function process_token( $stackPtr ) { | ||
if ( isset( $this->target_functions[ strtolower( $this->tokens[ $stackPtr ]['content'] ) ] ) ) { | ||
// Disallow excluding function groups for this sniff. | ||
$this->exclude = array(); | ||
|
||
return parent::process_token( $stackPtr ); | ||
} | ||
} | ||
|
||
/** | ||
* Process the parameters of a matched function call. | ||
* | ||
* @since 1.3.0 | ||
* | ||
* @param int $stackPtr The position of the current token in the stack. | ||
* @param string $group_name The name of the group which was matched. | ||
* @param string $matched_content The token content (function name) which was matched in lowercase. | ||
* @param array $parameters Array with information about the parameters. | ||
* @return void | ||
*/ | ||
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) { | ||
$target_param = $this->target_functions[ $matched_content ]; | ||
|
||
$found_param = PassedParameters::getParameterFromStack( $parameters, $target_param['position'], $target_param['name'] ); | ||
|
||
if ( false === $found_param ) { | ||
$this->phpcsFile->addError( | ||
'The "%s" parameter for function %s() is missing.', | ||
$stackPtr, | ||
'Missing', | ||
array( $target_param['name'], $matched_content ) | ||
); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/RequiredFunctionParametersUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
parse_str( $param_one, $param_two ); // Good. | ||
parse_str( $param_one ); // Bad. | ||
|
||
$str = "first=value&arr[]=foo+bar&arr[]=baz"; | ||
parse_str($str, $output); // Good. | ||
parse_str($str); // Bad. | ||
|
||
parse_str("My Value=Something", $output); // Good. | ||
parse_str("My Value=Something"); // Bad. |
59 changes: 59 additions & 0 deletions
59
phpcs-sniffs/PluginCheck/Tests/CodeAnalysis/RequiredFunctionParametersUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Unit tests for RequiredFunctionParametersSniff. | ||
* | ||
* @package PluginCheck | ||
*/ | ||
|
||
namespace PluginCheckCS\PluginCheck\Tests\CodeAnalysis; | ||
|
||
use PluginCheckCS\PluginCheck\Sniffs\CodeAnalysis\RequiredFunctionParametersSniff; | ||
use PluginCheckCS\PluginCheck\Tests\AbstractSniffUnitTest; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
|
||
/** | ||
* Unit tests for RequiredFunctionParametersSniff. | ||
*/ | ||
final class RequiredFunctionParametersUnitTest extends AbstractSniffUnitTest { | ||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* @return array <int line number> => <int number of errors> | ||
*/ | ||
public function getErrorList() { | ||
return array( | ||
4 => 1, | ||
8 => 1, | ||
11 => 1, | ||
); | ||
} | ||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* @return array <int line number> => <int number of warnings> | ||
*/ | ||
public function getWarningList() { | ||
return array(); | ||
} | ||
|
||
/** | ||
* Returns the fully qualified class name (FQCN) of the sniff. | ||
* | ||
* @return string The fully qualified class name of the sniff. | ||
*/ | ||
protected function get_sniff_fqcn() { | ||
return RequiredFunctionParametersSniff::class; | ||
} | ||
|
||
/** | ||
* Sets the parameters for the sniff. | ||
* | ||
* @throws \RuntimeException If unable to set the ruleset parameters required for the test. | ||
* | ||
* @param Sniff $sniff The sniff being tested. | ||
*/ | ||
public function set_sniff_parameters( Sniff $sniff ) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ | |
|
||
query_posts( 'cat=3' ); | ||
wp_reset_query(); | ||
|
||
parse_str( 'first=value&arr[]=foo+bar&arr[]=baz' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters