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