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

Improve removedFunctionParameters sniff. #163

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 42 additions & 15 deletions Sniffs/PHP/RemovedFunctionParametersSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,39 @@ class PHPCompatibility_Sniffs_PHP_RemovedFunctionParametersSniff extends PHPComp
protected $patternMatch = false;

/**
* A list of Removed function parameters, not present in older versions.
* A list of removed function parameters, which were present in older versions.
*
* The array lists : version number with false (not present) or true (present).
* The array lists : version number with true (deprecated and false (removed).
* The index is the location of the parameter in the parameter list, starting at 0 !
* If's sufficient to list the first version where the function appears.
* If's sufficient to list the first version where the function was deprecated/removed.
*
* @var array
*/
protected $removedFunctionParameters = array(
'mktime' => array(
'gmmktime' => array(
6 => array(
'name' => 'is_dst',
'5.1' => true,
'7.0' => false
'5.1' => true, // deprecated
'7.0' => false,
),
),
'gmmktime' => array(
'ldap_first_attribute' => array(
2 => array(
'name' => 'ber_identifier',
'5.2.4' => false,
),
),
'ldap_next_attribute' => array(
2 => array(
'name' => 'ber_identifier',
'5.2.4' => false,
),
),
'mktime' => array(
6 => array(
'name' => 'is_dst',
'7.0' => false
'5.1' => true, // deprecated
'7.0' => false,
),
),
);
Expand Down Expand Up @@ -142,23 +155,37 @@ protected function addError($phpcsFile, $stackPtr, $function, $parameterLocation
{
$error = '';

$isError = false;
$isError = false;
$previousStatus = null;
foreach ($this->removedFunctionParameters[$function][$parameterLocation] as $version => $present) {
if ($version != 'name' && $this->supportsAbove($version)) {
if ($present === false) {
$isError = true;
$error .= 'in PHP version ' . $version . ' or later';

if ($previousStatus !== $present) {
$previousStatus = $present;
if ($present === false) {
$isError = true;
$error .= 'removed';
} else {
$error .= 'deprecated';
}
$error .= ' in PHP version ' . $version . ' and ';
}
}
}

if (strlen($error) > 0) {
$error = 'The function ' . $function . ' does not have a parameter ' . $this->removedFunctionParameters[$function][$parameterLocation]['name'] . ' ' . $error;
$error = 'The "%s" parameter for function %s was ' . $error;
$error = substr($error, 0, strlen($error) - 5);
$errorCode = 'RemovedParameter';
$data = array(
$this->removedFunctionParameters[$function][$parameterLocation]['name'],
$function,
);

if ($isError === true) {
$phpcsFile->addError($error, $stackPtr);
$phpcsFile->addError($error, $stackPtr, $errorCode, $data);
} else {
$phpcsFile->addWarning($error, $stackPtr);
$phpcsFile->addWarning($error, $stackPtr, $errorCode, $data);
}
}

Expand Down
64 changes: 59 additions & 5 deletions Tests/Sniffs/PHP/RemovedFunctionParameterSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ class RemovedFunctionParameterSniffTest extends BaseSniffTest
* @param string $removedIn The PHP version in which the parameter was removed.
* @param array $lines The line numbers in the test file which apply to this class.
* @param string $okVersion A PHP version in which the parameter was ok to be used.
* @param string $testVersion Optional. A PHP version in which to test for the removal message.
*
* @return void
*/
public function testRemovedParameter($functionName, $parameterName, $removedIn, $lines, $okVersion)
public function testRemovedParameter($functionName, $parameterName, $removedIn, $lines, $okVersion, $testVersion = null)
{
$file = $this->sniffFile(self::TEST_FILE, $okVersion);
foreach ($lines as $line) {
$this->assertNoViolation($file, $line);
}

$file = $this->sniffFile(self::TEST_FILE, $removedIn);
if (isset($testVersion)){
$file = $this->sniffFile(self::TEST_FILE, $testVersion);
}
else {
$file = $this->sniffFile(self::TEST_FILE, $removedIn);
}
foreach ($lines as $line) {
$this->assertError($file, $line, "The function {$functionName} does not have a parameter {$parameterName} in PHP version {$removedIn} or later");
$this->assertError($file, $line, "The \"{$parameterName}\" parameter for function {$functionName} was removed in PHP version {$removedIn}");
}
}

Expand All @@ -54,8 +60,56 @@ public function testRemovedParameter($functionName, $parameterName, $removedIn,
public function dataRemovedParameter()
{
return array(
array('mktime', 'is_dst', '7.0', array(8), '5.6'),
array('gmmktime', 'is_dst', '7.0', array(9), '5.6'),
array('ldap_first_attribute', 'ber_identifier', '5.2.4', array(11), '5.2', '5.3'),
array('ldap_next_attribute', 'ber_identifier', '5.2.4', array(12), '5.2', '5.3'),
);
}


/**
* testDeprecatedRemovedParameter
*
* @dataProvider dataDeprecatedRemovedParameter
*
* @param string $functionName Function name.
* @param string $parameterName Parameter name.
* @param string $deprecatedIn The PHP version in which the parameter was deprecated.
* @param string $removedIn The PHP version in which the parameter was removed.
* @param array $lines The line numbers in the test file which apply to this class.
* @param string $okVersion A PHP version in which the parameter was ok to be used.
*
* @return void
*/
public function testDeprecatedRemovedParameter($functionName, $parameterName, $deprecatedIn, $removedIn, $lines, $okVersion)
{
$file = $this->sniffFile(self::TEST_FILE, $okVersion);
foreach ($lines as $line) {
$this->assertNoViolation($file, $line);
}

$file = $this->sniffFile(self::TEST_FILE, $deprecatedIn);
foreach ($lines as $line) {
$this->assertWarning($file, $line, "The \"{$parameterName}\" parameter for function {$functionName} was deprecated in PHP version {$deprecatedIn}");
}

$file = $this->sniffFile(self::TEST_FILE, $removedIn);
foreach ($lines as $line) {
$this->assertError($file, $line, "The \"{$parameterName}\" parameter for function {$functionName} was deprecated in PHP version {$deprecatedIn} and removed in PHP version {$removedIn}");
}
}

/**
* Data provider.
*
* @see testDeprecatedRemovedParameter()
*
* @return array
*/
public function dataDeprecatedRemovedParameter()
{
return array(
array('mktime', 'is_dst', '5.1', '7.0', array(8), '5.0'),
array('gmmktime', 'is_dst', '5.1', '7.0', array(9), '5.0'),
);
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/sniff-examples/removed_function_parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
// These are not.
mktime(1, 2, 3, 4, 5, 6, true);
gmmktime(1, 2, 3, 4, 5, 6, true);

ldap_first_attribute( $link_identifier, $result_entry_identifier, $ber_identifier );
ldap_next_attribute( $link_identifier, $result_entry_identifier, $ber_identifier );