Skip to content
This repository
Browse code

Prefix the error with the php version being checked for

- This is following on from the new git repo to check for 5.4 compatibilities, so when running both standards you want it to be very clear as to which version is affected
  • Loading branch information...
commit 132ffaee533267ab0e6473165b46fa77a2761275 1 parent aa616de
Ben Selby authored March 05, 2012
41  Sniffs/PHP/DeprecatedFunctionsSniff.php
@@ -59,4 +59,45 @@ class PHP53Compatibility_Sniffs_PHP_DeprecatedFunctionsSniff extends Generic_Sni
59 59
      */
60 60
     public $error = false;
61 61
 
  62
+    /**
  63
+     * Generates the error or warning for this sniff.
  64
+     *
  65
+     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  66
+     * @param int                  $stackPtr  The position of the forbidden function
  67
+     *                                        in the token array.
  68
+     * @param string               $function  The name of the forbidden function.
  69
+     * @param string               $pattern   The pattern used for the match.
  70
+     *
  71
+     * @return void
  72
+     */
  73
+    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
  74
+    {
  75
+        $data  = array($function);
  76
+        $error = 'The use of function %s() is ';
  77
+        if ($this->error === true) {
  78
+            $type   = 'Found';
  79
+            $error .= 'forbidden';
  80
+        } else {
  81
+            $type   = 'Discouraged';
  82
+            $error .= 'discouraged';
  83
+        }
  84
+
  85
+        if ($pattern === null) {
  86
+            $pattern = $function;
  87
+        }
  88
+
  89
+        if ($this->forbiddenFunctions[$pattern] !== null) {
  90
+            $type  .= 'WithAlternative';
  91
+            $data[] = $this->forbiddenFunctions[$pattern];
  92
+            $error .= '; use %s() instead';
  93
+        }
  94
+
  95
+        if ($this->error === true) {
  96
+            $phpcsFile->addError("[PHP 5.3] $error", $stackPtr, $type, $data);
  97
+        } else {
  98
+            $phpcsFile->addWarning("[PHP 5.3] $error", $stackPtr, $type, $data);
  99
+        }
  100
+
  101
+    }
  102
+
62 103
 }//end class
2  Sniffs/PHP/DeprecatedIniDirectivesSniff.php
@@ -82,7 +82,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
82 82
         if (in_array(str_replace("'", "", $tokens[$iniToken]['content']), $this->deprecatedIniDirectives) === false) {
83 83
             return;
84 84
         }
85  
-        $error = "INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";
  85
+        $error = "[PHP 5.3] INI directive " . $tokens[$iniToken]['content'] . " is deprecated.";
86 86
 
87 87
         $phpcsFile->addWarning($error, $stackPtr);
88 88
 
2  Sniffs/PHP/DeprecatedNewReferenceSniff.php
@@ -58,7 +58,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58 58
     {
59 59
         $tokens = $phpcsFile->getTokens();
60 60
         if ($tokens[$stackPtr - 1]['type'] == 'T_BITWISE_AND' || $tokens[$stackPtr - 2]['type'] == 'T_BITWISE_AND') {
61  
-            $error = 'Assigning the return value of new by reference is deprecated in PHP 5.3';
  61
+            $error = '[PHP 5.3] Assigning the return value of new by reference is deprecated in PHP 5.3';
62 62
             $phpcsFile->addError($error, $stackPtr);
63 63
         }
64 64
 
4  Sniffs/PHP/ForbiddenNamesSniff.php
@@ -146,7 +146,7 @@ public function processNonString(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $to
146 146
         if (in_array(strtolower($tokens[$stackPtr + 2]['content']), array_keys($this->invalidNames)) === false) {
147 147
             return;
148 148
         }
149  
-        $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'])] . ")";
  149
+        $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'])] . ")";
150 150
         $phpcsFile->addError($error, $stackPtr);
151 151
 
152 152
     }//end process()
@@ -181,7 +181,7 @@ public function processString(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $token
181 181
 
182 182
         foreach ($this->invalidNames as $key => $value) {
183 183
             if (substr(strtolower($tokens[$defineContent]['content']), 1, strlen($tokens[$defineContent]['content']) - 2) == $key) {
184  
-                $error = "Function name, class name, namespace name or constant name can not be reserved keyword '" . $key . "' (since version " . $value . ")";
  184
+                $error = "[PHP 5.3] Function name, class name, namespace name or constant name can not be reserved keyword '" . $key . "' (since version " . $value . ")";
185 185
                 $phpcsFile->addError($error, $stackPtr);
186 186
             }
187 187
         }
4  Sniffs/PHP/NonStaticMagicMethodsSniff.php
@@ -73,14 +73,14 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
73 73
             return;
74 74
         }
75 75
         if ($tokens[$scopeToken]['type'] != 'T_PUBLIC') {
76  
-            $error = "Magic methods must be public (since PHP 5.3) !";
  76
+            $error = "[PHP 5.3] Magic methods must be public (since PHP 5.3) !";
77 77
             $phpcsFile->addError($error, $stackPtr);
78 78
         }
79 79
         $staticToken = $phpcsFile->findPrevious(T_STATIC, $scopeToken, $scopeToken - 2);
80 80
         if ($staticToken === false) {
81 81
             return;
82 82
         } else {
83  
-            $error = "Magic methods can not be static (since PHP 5.3) !";
  83
+            $error = "[PHP 5.3] Magic methods can not be static (since PHP 5.3) !";
84 84
             $phpcsFile->addError($error, $stackPtr);
85 85
         }
86 86
 
4  Sniffs/PHP/RemovedExtensionsSniff.php
@@ -115,9 +115,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
115 115
         foreach ($this->removedExtensions as $extension => $alternative) {
116 116
             if (strpos($tokens[$stackPtr]['content'], $extension) === 0) {
117 117
                 if (!is_null($alternative)) {
118  
-                    $error = "Extension '" . $extension . "' is not available in PHP 5.3 - use the '" . $alternative . "' extension instead";
  118
+                    $error = "[PHP 5.3] Extension '" . $extension . "' is not available in PHP 5.3 - use the '" . $alternative . "' extension instead";
119 119
                 } else {
120  
-                    $error = "Extension '" . $extension . "' is not available in PHP 5.3 anymore";
  120
+                    $error = "[PHP 5.3] Extension '" . $extension . "' is not available in PHP 5.3 anymore";
121 121
                 }
122 122
                 $phpcsFile->addError($error, $stackPtr);
123 123
             }

0 notes on commit 132ffae

Please sign in to comment.
Something went wrong with that request. Please try again.