Completed
Push — feature/new-reserved-function-... ( a826b6 )
by Juliette
01:42
created

processTokenWithinScope()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 5
nop 3
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\ReservedFunctionNamesSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
namespace PHPCompatibility\Sniffs\PHP;
11
12
use PHPCompatibility\Sniff;
13
14
/**
15
 * \PHPCompatibility\Sniffs\PHP\ReservedFunctionNamesSniff.
16
 *
17
 * All function and method names starting with double underscore are reserved by PHP.
18
 *
19
 * {@internal Extends an upstream sniff to benefit from the properties contained therein.
20
 *            The properties are lists of valid PHP magic function and method names, which
21
 *            should be ignored for the purposes of this sniff.
22
 *            As this sniff is not PHP version specific, we don't need access to the utility
23
 *            methods in the PHPCompatibility\Sniff, so extending the upstream sniff is fine.
24
 *            As the upstream sniff checks the same (and more, but we don't need the rest),
25
 *            the logic in this sniff is largely the same as used upstream.
26
 *            Extending the upstream sniff instead of including it via the ruleset, however,
27
 *            prevents hard to debug issues of errors not being reported from the upstream sniff
28
 *            if this library is used in combination with other rulesets.}}
29
 *
30
 * @category PHP
31
 * @package  PHPCompatibility
32
 * @author   Juliette Reinders Folmer <[email protected]>
33
 */
34
class ReservedFunctionNamesSniff extends \Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff
35
{
36
37
    /**
38
     * Processes the tokens within the scope.
39
     *
40
     * @param \PHP_CodeSniffer_File $phpcsFile The file being processed.
41
     * @param int                   $stackPtr  The position where this token was
42
     *                                         found.
43
     * @param int                   $currScope The position of the current scope.
44
     *
45
     * @return void
46
     */
47
    protected function processTokenWithinScope(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
48
    {
49
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
50
        if ($methodName === null) {
51
            // Ignore closures.
52
            return;
53
        }
54
55
        // Is this a magic method. i.e., is prefixed with "__" ?
56
        if (preg_match('|^__[^_]|', $methodName) > 0) {
57
            $magicPart = strtolower(substr($methodName, 2));
58
            if (isset($this->magicMethods[$magicPart]) === false
59
                && isset($this->methodsDoubleUnderscore[$magicPart]) === false
60
            ) {
61
                $className = $phpcsFile->getDeclarationName($currScope);
62
                if (empty($className)) {
63
                    $className = '[anonymous class]';
64
                }
65
66
                $phpcsFile->addWarning(
67
                    'Method name "%s" is discouraged; PHP has reserved all method names with a double underscore prefix for future use.',
68
                    $stackPtr,
69
                    'MethodDoubleUnderscore',
70
                    array($className.'::'.$methodName)
71
                );
72
            }
73
        }
74
    }
75
76
    /**
77
     * Processes the tokens outside the scope.
78
     *
79
     * @param \PHP_CodeSniffer_File $phpcsFile The file being processed.
80
     * @param int                   $stackPtr  The position where this token was
81
     *                                         found.
82
     *
83
     * @return void
84
     */
85
    protected function processTokenOutsideScope(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
86
    {
87
        $functionName = $phpcsFile->getDeclarationName($stackPtr);
88
        if ($functionName === null) {
89
            // Ignore closures.
90
            return;
91
        }
92
93
        // Is this a magic function. i.e., it is prefixed with "__".
94
        if (preg_match('|^__[^_]|', $functionName) > 0) {
95
            $magicPart = strtolower(substr($functionName, 2));
96
            if (isset($this->magicFunctions[$magicPart]) === false) {
97
                $phpcsFile->addWarning(
98
                    'Function name "%s" is discouraged; PHP has reserved all method names with a double underscore prefix for future use.',
99
                    $stackPtr,
100
                    'FunctionDoubleUnderscore',
101
                    array($functionName)
102
                );
103
            }
104
        }
105
    }
106
107
}//end class
108