Permalink
Browse files

Merge pull request #5 from benmatselby/prefix-errors-with-version

Prefix the error with the php version being checked for, so you can use it together with the new PHPCompatibility codesniffer standard (https://github.com/wimg/PHPCompatibility) and see which standard is generating which warning/error
  • Loading branch information...
2 parents aa616de + 132ffae commit bc750fcd2927b6b90f0bb339dead0ad9a3b72941 @wimg committed Mar 5, 2012
View
41 Sniffs/PHP/DeprecatedFunctionsSniff.php
@@ -59,4 +59,45 @@ class PHP53Compatibility_Sniffs_PHP_DeprecatedFunctionsSniff extends Generic_Sni
*/
public $error = false;
+ /**
+ * Generates the error or warning for this sniff.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the forbidden function
+ * in the token array.
+ * @param string $function The name of the forbidden function.
+ * @param string $pattern The pattern used for the match.
+ *
+ * @return void
+ */
+ protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
+ {
+ $data = array($function);
+ $error = 'The use of function %s() is ';
+ if ($this->error === true) {
+ $type = 'Found';
+ $error .= 'forbidden';
+ } else {
+ $type = 'Discouraged';
+ $error .= 'discouraged';
+ }
+
+ if ($pattern === null) {
+ $pattern = $function;
+ }
+
+ if ($this->forbiddenFunctions[$pattern] !== null) {
+ $type .= 'WithAlternative';
+ $data[] = $this->forbiddenFunctions[$pattern];
+ $error .= '; use %s() instead';
+ }
+
+ if ($this->error === true) {
+ $phpcsFile->addError("[PHP 5.3] $error", $stackPtr, $type, $data);
+ } else {
+ $phpcsFile->addWarning("[PHP 5.3] $error", $stackPtr, $type, $data);
+ }
+
+ }
+
}//end class
View
2 Sniffs/PHP/DeprecatedIniDirectivesSniff.php
@@ -82,7 +82,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if (in_array(str_replace("'", "", $tokens[$iniToken]['content']), $this->deprecatedIniDirectives) === false) {
return;
}
- $error = "INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";
+ $error = "[PHP 5.3] INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";
$phpcsFile->addWarning($error, $stackPtr);
View
2 Sniffs/PHP/DeprecatedNewReferenceSniff.php
@@ -58,7 +58,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr - 1]['type'] == 'T_BITWISE_AND' || $tokens[$stackPtr - 2]['type'] == 'T_BITWISE_AND') {
- $error = 'Assigning the return value of new by reference is deprecated in PHP 5.3';
+ $error = '[PHP 5.3] Assigning the return value of new by reference is deprecated in PHP 5.3';
$phpcsFile->addError($error, $stackPtr);
}
View
4 Sniffs/PHP/ForbiddenNamesSniff.php
@@ -146,7 +146,7 @@ public function processNonString(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $to
if (in_array(strtolower($tokens[$stackPtr + 2]['content']), array_keys($this->invalidNames)) === false) {
return;
}
- $error = "Function name, class name, namespace name or constant name can not be reserved keyword '" . $tokens[$stackPtr + 2]['content'] . "' (since version " . $this->invalidNames[strtolower($tokens[$stackPtr + 2]['content'])] . ")";
+ $error = "[PHP 5.3] Function name, class name, namespace name or constant name can not be reserved keyword '" . $tokens[$stackPtr + 2]['content'] . "' (since version " . $this->invalidNames[strtolower($tokens[$stackPtr + 2]['content'])] . ")";
$phpcsFile->addError($error, $stackPtr);
}//end process()
@@ -181,7 +181,7 @@ public function processString(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $token
foreach ($this->invalidNames as $key => $value) {
if (substr(strtolower($tokens[$defineContent]['content']), 1, strlen($tokens[$defineContent]['content']) - 2) == $key) {
- $error = "Function name, class name, namespace name or constant name can not be reserved keyword '" . $key . "' (since version " . $value . ")";
+ $error = "[PHP 5.3] Function name, class name, namespace name or constant name can not be reserved keyword '" . $key . "' (since version " . $value . ")";
$phpcsFile->addError($error, $stackPtr);
}
}
View
4 Sniffs/PHP/NonStaticMagicMethodsSniff.php
@@ -73,14 +73,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}
if ($tokens[$scopeToken]['type'] != 'T_PUBLIC') {
- $error = "Magic methods must be public (since PHP 5.3) !";
+ $error = "[PHP 5.3] Magic methods must be public (since PHP 5.3) !";
$phpcsFile->addError($error, $stackPtr);
}
$staticToken = $phpcsFile->findPrevious(T_STATIC, $scopeToken, $scopeToken - 2);
if ($staticToken === false) {
return;
} else {
- $error = "Magic methods can not be static (since PHP 5.3) !";
+ $error = "[PHP 5.3] Magic methods can not be static (since PHP 5.3) !";
$phpcsFile->addError($error, $stackPtr);
}
View
4 Sniffs/PHP/RemovedExtensionsSniff.php
@@ -115,9 +115,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
foreach ($this->removedExtensions as $extension => $alternative) {
if (strpos($tokens[$stackPtr]['content'], $extension) === 0) {
if (!is_null($alternative)) {
- $error = "Extension '" . $extension . "' is not available in PHP 5.3 - use the '" . $alternative . "' extension instead";
+ $error = "[PHP 5.3] Extension '" . $extension . "' is not available in PHP 5.3 - use the '" . $alternative . "' extension instead";
} else {
- $error = "Extension '" . $extension . "' is not available in PHP 5.3 anymore";
+ $error = "[PHP 5.3] Extension '" . $extension . "' is not available in PHP 5.3 anymore";
}
$phpcsFile->addError($error, $stackPtr);
}

0 comments on commit bc750fc

Please sign in to comment.