Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Ensures this is not assigned to any other var but self.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer_MySource
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
 * @version   CVS: $Id: AssignThisSniff.php 253717 2008-02-25 03:25:05Z squiz $
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Ensures this is not assigned to any other var but self.
18
 *
19
 * @category  PHP
20
 * @package   PHP_CodeSniffer_MySource
21
 * @author    Greg Sherwood <gsherwood@squiz.net>
22
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
23
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
24
 * @version   Release: 1.2.1
25
 * @link      http://pear.php.net/package/PHP_CodeSniffer
26
 */
27
class MySource_Sniffs_Objects_AssignThisSniff implements PHP_CodeSniffer_Sniff
28
{
29
 
30
    /**
31
     * A list of tokenizers this sniff supports.
32
     *
33
     * @var array
34
     */
35
    public $supportedTokenizers = array('JS');
36
 
37
 
38
    /**
39
     * Returns an array of tokens this test wants to listen for.
40
     *
41
     * @return array
42
     */
43
    public function register()
44
    {
45
        return array(T_THIS);
46
 
47
    }//end register()
48
 
49
 
50
    /**
51
     * Processes this test, when one of its tokens is encountered.
52
     *
53
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
54
     * @param int                  $stackPtr  The position of the current token
55
     *                                        in the stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60
    {
61
        $tokens = $phpcsFile->getTokens();
62
 
63
        // Ignore this.something and other uses of "this" that are not
64
        // direct assignments.
65
        $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
66
        if ($tokens[$next]['code'] !== T_SEMICOLON) {
67
            if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
68
                return;
69
            }
70
        }
71
 
72
        // Something must be assigned to "this".
73
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
74
        if ($tokens[$prev]['code'] !== T_EQUAL) {
75
            return;
76
        }
77
 
78
        // A variable needs to be assigned to "this".
79
        $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($prev - 1), null, true);
80
        if ($tokens[$prev]['code'] !== T_STRING) {
81
            return;
82
        }
83
 
84
        // We can only assign "this" to a var called "self".
85
        if ($tokens[$prev]['content'] !== 'self') {
86
            $error = 'Keyword "this" can only be assigned to a variable called "self"';
87
            $phpcsFile->addError($error, $prev);
88
        }
89
 
90
    }//end process()
91
 
92
 
93
}//end class
94
 
95
?>