Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Squiz_Sniffs_Scope_StaticThisUsageSniff.
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 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
13
 * @version   CVS: $Id: StaticThisUsageSniff.php 240383 2007-07-27 05:38:59Z squiz $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
18
    throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found');
19
}
20
 
21
/**
22
 * Squiz_Sniffs_Scope_StaticThisUsageSniff.
23
 *
24
 * Checks for usage of "$this" in static methods, which will cause
25
 * runtime errors.
26
 *
27
 * @category  PHP
28
 * @package   PHP_CodeSniffer
29
 * @author    Greg Sherwood <gsherwood@squiz.net>
30
 * @author    Marc McIntyre <mmcintyre@squiz.net>
31
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
32
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
33
 * @version   Release: 1.2.1
34
 * @link      http://pear.php.net/package/PHP_CodeSniffer
35
 */
36
class Squiz_Sniffs_Scope_StaticThisUsageSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
37
{
38
 
39
 
40
    /**
41
     * Constructs the test with the tokens it wishes to listen for.
42
     *
43
     * @return void
44
     */
45
    public function __construct()
46
    {
47
        parent::__construct(array(T_CLASS), array(T_FUNCTION));
48
 
49
    }//end __construct()
50
 
51
 
52
    /**
53
     * Processes this test, when one of its tokens is encountered.
54
     *
55
     * @param PHP_CodeSniffer_File $phpcsFile The current file being scanned.
56
     * @param int                  $stackPtr  The position of the current token in the
57
     *                                        stack passed in $tokens.
58
     * @param int                  $currScope A pointer to the start of the scope.
59
     *
60
     * @return void
61
     */
62
    public function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
63
    {
64
        $tokens   = $phpcsFile->getTokens();
65
        $function = $tokens[($stackPtr + 2)];
66
 
67
        if ($function['code'] !== T_STRING) {
68
            return;
69
        }
70
 
71
        $functionName = $function['content'];
72
        $classOpener  = $tokens[$currScope]['scope_condition'];
73
        $className    = $tokens[($classOpener + 2)]['content'];
74
 
75
        $methodProps = $phpcsFile->getMethodProperties($stackPtr);
76
 
77
        if ($methodProps['is_static'] === true) {
78
            if (isset($tokens[$stackPtr]['scope_closer']) === false) {
79
                // There is no scope opener or closer, so the function
80
                // must be abstract.
81
                return;
82
            }
83
 
84
            $thisUsage = $stackPtr;
85
            while (($thisUsage = $phpcsFile->findNext(array(T_VARIABLE), ($thisUsage + 1), $tokens[$stackPtr]['scope_closer'], false, '$this')) !== false) {
86
                if ($thisUsage === false) {
87
                    return;
88
                }
89
 
90
                $error = 'Usage of "$this" in static methods will cause runtime errors';
91
                $phpcsFile->addError($error, $thisUsage);
92
            }
93
        }//end if
94
 
95
    }//end processTokenWithinScope()
96
 
97
 
98
}//end class
99
 
100
?>