Skip to content

Commit

Permalink
Add unit tests for the NewFunctions sniff.
Browse files Browse the repository at this point in the history
The functions in the `NewFunctions` sniff appeared not to be covered by unit tests.
These have now been added.

Also:
* Make function name comparison case-insensitive as function calls are case-insensitive in PHP.
* Exclude namespaced functions from this sniff (basic check).
* Removed empty item from functions list
* Added numerous extra functions to sniff for.

Additionally some extra `noViolation` tests have been set up to make sure namespaced function calls will not be reported by this sniff.

Removed the new methods which were mixed into the functions list as they can not be tested for in this way. Most cannot be called statically and if not called statically, there is little way to tell if the method belongs to that class.
  • Loading branch information
jrfnl committed Aug 13, 2016
1 parent 9cb63b0 commit e66086f
Show file tree
Hide file tree
Showing 3 changed files with 771 additions and 67 deletions.
159 changes: 103 additions & 56 deletions Sniffs/PHP/NewFunctionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ class PHPCompatibility_Sniffs_PHP_NewFunctionsSniff extends PHPCompatibility_Sni
'5.1' => false,
'5.2' => true
),
'swfmovie::namedanchor' => array(
'5.1' => false,
'5.2' => true
),
'swfmovie::protect' => array(
'5.1' => false,
'5.2' => true
),
'openssl_csr_get_public_key' => array(
'5.1' => false,
'5.2' => true
Expand Down Expand Up @@ -322,14 +314,6 @@ class PHPCompatibility_Sniffs_PHP_NewFunctionsSniff extends PHPCompatibility_Sni
'5.2' => false,
'5.3' => true
),
'streamWrapper::stream_cast' => array(
'5.2' => false,
'5.3' => true
),
'streamWrapper::stream_set_option' => array(
'5.2' => false,
'5.3' => true
),
'date_add' => array(
'5.2' => false,
'5.3' => true
Expand Down Expand Up @@ -382,6 +366,10 @@ class PHPCompatibility_Sniffs_PHP_NewFunctionsSniff extends PHPCompatibility_Sni
'5.2' => false,
'5.3' => true
),
'mysqli_get_cache_stats' => array(
'5.2' => false,
'5.3' => true
),
'mysqli_fetch_all' => array(
'5.2' => false,
'5.3' => true
Expand Down Expand Up @@ -434,6 +422,22 @@ class PHPCompatibility_Sniffs_PHP_NewFunctionsSniff extends PHPCompatibility_Sni
'5.2' => false,
'5.3' => true
),
'asinh' => array(
'5.2' => false,
'5.3' => true
),
'atanh' => array(
'5.2' => false,
'5.3' => true
),
'expm1' => array(
'5.2' => false,
'5.3' => true
),
'log1p' => array(
'5.2' => false,
'5.3' => true
),
'enchant_broker_describe' => array(
'5.2' => false,
'5.3' => true
Expand Down Expand Up @@ -1080,14 +1084,83 @@ class PHPCompatibility_Sniffs_PHP_NewFunctionsSniff extends PHPCompatibility_Sni
'5.4' => false,
'5.5' => true
),
'SplFixedArray::_wakup' => array(
'5.4' => false,
'5.5' => true

'gmp_root' => array(
'5.5' => false,
'5.6' => true
),
'Closure::call' => array(
'5.6' => false,
'7.0' => true
'gmp_rootrem' => array(
'5.5' => false,
'5.6' => true
),
'hash_equals' => array(
'5.5' => false,
'5.6' => true
),
'ldap_escape' => array(
'5.5' => false,
'5.6' => true
),
'ldap_modify_batch' => array(
'5.4.25' => false,
'5.5.9' => false,
'5.4.26' => true,
'5.5.10' => true,
'5.6.0' => true,
),
'mysqli_get_links_stats' => array(
'5.5' => false,
'5.6' => true
),
'openssl_get_cert_locations' => array(
'5.5' => false,
'5.6' => true
),
'openssl_x509_fingerprint' => array(
'5.5' => false,
'5.6' => true
),
'openssl_spki_new' => array(
'5.5' => false,
'5.6' => true
),
'openssl_spki_verify' => array(
'5.5' => false,
'5.6' => true
),
'openssl_spki_export_challenge' => array(
'5.5' => false,
'5.6' => true
),
'openssl_spki_export' => array(
'5.5' => false,
'5.6' => true
),
'pg_connect_poll' => array(
'5.5' => false,
'5.6' => true
),
'pg_consume_input' => array(
'5.5' => false,
'5.6' => true
),
'pg_flush' => array(
'5.5' => false,
'5.6' => true
),
'pg_socket' => array(
'5.5' => false,
'5.6' => true
),
'session_abort' => array(
'5.5' => false,
'5.6' => true
),
'session_reset' => array(
'5.5' => false,
'5.6' => true
),

'random_bytes' => array(
'5.6' => false,
'7.0' => true
Expand All @@ -1100,10 +1173,6 @@ class PHPCompatibility_Sniffs_PHP_NewFunctionsSniff extends PHPCompatibility_Sni
'5.6' => false,
'7.0' => true
),
'Generator::getReturn' => array(
'5.6' => false,
'7.0' => true
),
'gmp_random_seed' => array(
'5.6' => false,
'7.0' => true
Expand All @@ -1128,34 +1197,6 @@ class PHPCompatibility_Sniffs_PHP_NewFunctionsSniff extends PHPCompatibility_Sni
'5.6' => false,
'7.0' => true
),
'ReflectionParameter::getType' => array(
'5.6' => false,
'7.0' => true
),
'' => array(
'5.6' => false,
'7.0' => true
),
'ReflectionParameter::hasType' => array(
'5.6' => false,
'7.0' => true
),
'ReflectionFunctionAbstract::getReturnType' => array(
'5.6' => false,
'7.0' => true
),
'ReflectionFunctionAbstract::hasReturnType' => array(
'5.6' => false,
'7.0' => true
),
'ZipArchive::setCompressionIndex' => array(
'5.6' => false,
'7.0' => true
),
'ZipArchive::setCompressionName' => array(
'5.6' => false,
'7.0' => true
),
'inflate_add' => array(
'5.6' => false,
'7.0' => true
Expand Down Expand Up @@ -1192,6 +1233,8 @@ public function register()
// Everyone has had a chance to figure out what forbidden functions
// they want to check for, so now we can cache out the list.
$this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
$this->forbiddenFunctionNames = array_map('strtolower', $this->forbiddenFunctionNames);
$this->forbiddenFunctions = array_combine($this->forbiddenFunctionNames, $this->forbiddenFunctions);

if ($this->patternMatch === true) {
foreach ($this->forbiddenFunctionNames as $i => $name) {
Expand Down Expand Up @@ -1228,6 +1271,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// Not a call to a PHP function.
return;
}
else if($tokens[$prevToken]['code'] === T_NS_SEPARATOR && $tokens[$prevToken - 1]['code'] === T_STRING) {
// Namespaced function.
return;
}

$function = strtolower($tokens[$stackPtr]['content']);
$pattern = null;
Expand All @@ -1254,7 +1301,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}

$this->addError($phpcsFile, $stackPtr, $function, $pattern);
$this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content'], $pattern);

}//end process()

Expand All @@ -1273,7 +1320,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
{
if ($pattern === null) {
$pattern = $function;
$pattern = strtolower($function);
}

$error = '';
Expand Down

0 comments on commit e66086f

Please sign in to comment.