PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file DeprecatedFunctionsSniff.php

Documentation is available at DeprecatedFunctionsSniff.php

  1. <?php
  2. /**
  3.  * Discourages the use of deprecated PHP functions.
  4.  *
  5.  * @author    Sebastian Bergmann <sb@sebastian-bergmann.de>
  6.  * @author    Greg Sherwood <gsherwood@squiz.net>
  7.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  8.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  9.  */
  10.  
  11. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
  12.  
  13. class DeprecatedFunctionsSniff extends ForbiddenFunctionsSniff
  14. {
  15.  
  16.     /**
  17.      * A list of forbidden functions with their alternatives.
  18.      *
  19.      * The value is NULL if no alternative exists. IE, the
  20.      * function should just not be used.
  21.      *
  22.      * @var array(string => string|null)
  23.      */
  24.     public $forbiddenFunctions = array();
  25.  
  26.  
  27.     /**
  28.      * Constructor.
  29.      *
  30.      * Uses the Reflection API to get a list of deprecated functions.
  31.      */
  32.     public function __construct()
  33.     {
  34.         $functions get_defined_functions();
  35.  
  36.         foreach ($functions['internal'as $functionName{
  37.             $function = new \ReflectionFunction($functionName);
  38.             if (method_exists($function'isDeprecated'=== false{
  39.                 break;
  40.             }
  41.  
  42.             if ($function->isDeprecated(=== true{
  43.                 $this->forbiddenFunctions[$functionName= null;
  44.             }
  45.         }
  46.  
  47.     }//end __construct()
  48.  
  49.  
  50.     /**
  51.      * Generates the error or warning for this sniff.
  52.      *
  53.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  54.      * @param int                         $stackPtr  The position of the forbidden function
  55.      *                                                in the token array.
  56.      * @param string                      $function  The name of the forbidden function.
  57.      * @param string                      $pattern   The pattern used for the match.
  58.      *
  59.      * @return void 
  60.      */
  61.     protected function addError($phpcsFile$stackPtr$function$pattern=null)
  62.     {
  63.         $data  = array($function);
  64.         $error 'Function %s() has been deprecated';
  65.         $type  'Deprecated';
  66.  
  67.         if ($this->error === true{
  68.             $phpcsFile->addError($error$stackPtr$type$data);
  69.         else {
  70.             $phpcsFile->addWarning($error$stackPtr$type$data);
  71.         }
  72.  
  73.     }//end addError()
  74.  
  75.  
  76. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.