Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * PEAR_Sniffs_Files_IncludingFileSniff.
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: IncludingFileSniff.php,v 1.5 2007/10/22 23:11:56 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * PEAR_Sniffs_Files_IncludingFileSniff.
19
 *
20
 * Checks that the include_once is used in conditional situations, and
21
 * require_once is used elsewhere. Also checks that brackets do not surround
22
 * the file being included.
23
 *
24
 * @category  PHP
25
 * @package   PHP_CodeSniffer
26
 * @author    Greg Sherwood <gsherwood@squiz.net>
27
 * @author    Marc McIntyre <mmcintyre@squiz.net>
28
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
29
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
30
 * @version   Release: 1.2.0RC1
31
 * @link      http://pear.php.net/package/PHP_CodeSniffer
32
 */
33
class PEAR_Sniffs_Files_IncludingFileSniff implements PHP_CodeSniffer_Sniff
34
{
35
 
36
    /**
37
     * Conditions that should use include_once
38
     *
39
     * @var array(int)
40
     */
41
    private static $_conditions = array(
42
                                   T_IF,
43
                                   T_ELSE,
44
                                   T_ELSEIF,
45
                                   T_SWITCH,
46
                                  );
47
 
48
 
49
    /**
50
     * Returns an array of tokens this test wants to listen for.
51
     *
52
     * @return array
53
     */
54
    public function register()
55
    {
56
        return array(
57
                T_INCLUDE_ONCE,
58
                T_REQUIRE_ONCE,
59
                T_REQUIRE,
60
                T_INCLUDE,
61
               );
62
 
63
    }//end register()
64
 
65
 
66
    /**
67
     * Processes this test, when one of its tokens is encountered.
68
     *
69
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
70
     * @param int                  $stackPtr  The position of the current token in the
71
     *                                        stack passed in $tokens.
72
     *
73
     * @return void
74
     */
75
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
76
    {
77
        $tokens = $phpcsFile->getTokens();
78
 
79
        $nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
80
        if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS) {
81
            $error  = '"'.$tokens[$stackPtr]['content'].'"';
82
            $error .= ' is a statement, not a function; ';
83
            $error .= 'no parentheses are required';
84
            $phpcsFile->addError($error, $stackPtr);
85
        }
86
 
87
        $inCondition = (count($tokens[$stackPtr]['conditions']) !== 0) ? true : false;
88
 
89
        // Check to see if this including statement is within the parenthesis of a condition.
90
        // If that's the case then we need to process it as being within a condition, as they
91
        // are checking the return value.
92
        if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
93
            foreach ($tokens[$stackPtr]['nested_parenthesis'] as $left => $right) {
94
                if (isset($tokens[$left]['parenthesis_owner']) === true) {
95
                    $inCondition = true;
96
                }
97
            }
98
        }
99
 
100
        // Check to see if they are assigning the return value of this including call.
101
        // If they are then they are probably checking it, so its conditional.
102
        $previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
103
        if (in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true) {
104
            // The have assigned the return value to it, so its conditional.
105
            $inCondition = true;
106
        }
107
 
108
        $tokenCode = $tokens[$stackPtr]['code'];
109
        if ($inCondition === true) {
110
            // We are inside a conditional statement. We need an include_once.
111
            if ($tokenCode === T_REQUIRE_ONCE) {
112
                $error  = 'File is being conditionally included; ';
113
                $error .= 'use "include_once" instead';
114
                $phpcsFile->addError($error, $stackPtr);
115
            } else if ($tokenCode === T_REQUIRE) {
116
                $error  = 'File is being conditionally included; ';
117
                $error .= 'use "include" instead';
118
                $phpcsFile->addError($error, $stackPtr);
119
            }
120
        } else {
121
            // We are unconditionally including, we need a require_once.
122
            if ($tokenCode === T_INCLUDE_ONCE) {
123
                $error  = 'File is being unconditionally included; ';
124
                $error .= 'use "require_once" instead';
125
                $phpcsFile->addError($error, $stackPtr);
126
            } else if ($tokenCode === T_INCLUDE) {
127
                $error  = 'File is being unconditionally included; ';
128
                $error .= 'use "require" instead';
129
                $phpcsFile->addError($error, $stackPtr);
130
            }
131
        }//end if
132
 
133
    }//end process()
134
 
135
 
136
}//end class
137
 
138
?>