~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.5.3/CodeSniffer/Standards/MySource/Sniffs/Channels/UnusedSystemSniff.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-07-21 14:42:41 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20140721144241-g4orlcuk4jzn9mhs
Tags: 1.5.3-1
* Team upload
* Focus on stable release
* Update copyright
* Bump standards version to 3.9.5
* Update Homepage
* Use ${phppear:…} instead of hardcoding them
* Run tests in dh_auto_test
* Update patch with gbp pq
* Simplify configuration installation
* Edit package.xml to move script
* Add DEP-8 tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Ensures that systems, asset types and libs are included before they are used.
 
4
 *
 
5
 * PHP version 5
 
6
 *
 
7
 * @category  PHP
 
8
 * @package   PHP_CodeSniffer_MySource
 
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
10
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
11
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
12
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
13
 */
 
14
 
 
15
/**
 
16
 * Ensures that systems and asset types are used if they are included.
 
17
 *
 
18
 * @category  PHP
 
19
 * @package   PHP_CodeSniffer_MySource
 
20
 * @author    Greg Sherwood <gsherwood@squiz.net>
 
21
 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
 
22
 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 
23
 * @version   Release: 1.5.3
 
24
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
25
 */
 
26
class MySource_Sniffs_Channels_UnusedSystemSniff implements PHP_CodeSniffer_Sniff
 
27
{
 
28
 
 
29
 
 
30
    /**
 
31
     * Returns an array of tokens this test wants to listen for.
 
32
     *
 
33
     * @return array
 
34
     */
 
35
    public function register()
 
36
    {
 
37
        return array(T_DOUBLE_COLON);
 
38
 
 
39
    }//end register()
 
40
 
 
41
 
 
42
    /**
 
43
     * Processes this sniff, when one of its tokens is encountered.
 
44
     *
 
45
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
46
     * @param int                  $stackPtr  The position of the current token in
 
47
     *                                        the stack passed in $tokens.
 
48
     *
 
49
     * @return void
 
50
     */
 
51
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
52
    {
 
53
        $tokens = $phpcsFile->getTokens();
 
54
 
 
55
        // Check if this is a call to includeSystem, includeAsset or includeWidget.
 
56
        $methodName = strtolower($tokens[($stackPtr + 1)]['content']);
 
57
        if (in_array($methodName, array('includesystem', 'includeasset', 'includewidget')) === true) {
 
58
            $systemName = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 3), null, true);
 
59
            if ($systemName === false || $tokens[$systemName]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
 
60
                // Must be using a variable instead of a specific system name.
 
61
                // We can't accurately check that.
 
62
                return;
 
63
            }
 
64
 
 
65
            $systemName = trim($tokens[$systemName]['content'], " '");
 
66
        } else {
 
67
            return;
 
68
        }
 
69
 
 
70
        if ($methodName === 'includeasset') {
 
71
            $systemName .= 'assettype';
 
72
        } else if ($methodName === 'includewidget') {
 
73
            $systemName .= 'widgettype';
 
74
        }
 
75
 
 
76
        $systemName = strtolower($systemName);
 
77
 
 
78
        // Now check if this system is used anywhere in this scope.
 
79
        $level = $tokens[$stackPtr]['level'];
 
80
        for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
 
81
            if ($tokens[$i]['level'] < $level) {
 
82
                // We have gone out of scope.
 
83
                // If the original include was inside an IF statement that
 
84
                // is checking if the system exists, check the outer scope
 
85
                // as well.
 
86
                if ($tokens[$stackPtr]['level'] === $level) {
 
87
                    // We are still in the base level, so this is the first
 
88
                    // time we have got here.
 
89
                    $conditions = array_keys($tokens[$stackPtr]['conditions']);
 
90
                    if (empty($conditions) === false) {
 
91
                        $cond = array_pop($conditions);
 
92
                        if ($tokens[$cond]['code'] === T_IF) {
 
93
                            $i = $tokens[$cond]['scope_closer'];
 
94
                            $level--;
 
95
                            continue;
 
96
                        }
 
97
                    }
 
98
                }
 
99
 
 
100
                break;
 
101
            }//end if
 
102
 
 
103
            $validTokens = array(
 
104
                            T_DOUBLE_COLON,
 
105
                            T_EXTENDS,
 
106
                            T_IMPLEMENTS,
 
107
                           );
 
108
 
 
109
            if (in_array($tokens[$i]['code'], $validTokens) === false) {
 
110
                continue;
 
111
            }
 
112
 
 
113
            switch ($tokens[$i]['code']) {
 
114
            case T_DOUBLE_COLON:
 
115
                $usedName = strtolower($tokens[($i - 1)]['content']);
 
116
                if ($usedName === $systemName) {
 
117
                    // The included system was used, so it is fine.
 
118
                    return;
 
119
                }
 
120
 
 
121
                break;
 
122
            case T_EXTENDS:
 
123
                $classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1));
 
124
                $className      = strtolower($tokens[$classNameToken]['content']);
 
125
                if ($className === $systemName) {
 
126
                    // The included system was used, so it is fine.
 
127
                    return;
 
128
                }
 
129
 
 
130
                break;
 
131
            case T_IMPLEMENTS:
 
132
                $endImplements = $phpcsFile->findNext(array(T_EXTENDS, T_OPEN_CURLY_BRACKET), ($i + 1));
 
133
                for ($x = ($i + 1); $x < $endImplements; $x++) {
 
134
                    if ($tokens[$x]['code'] === T_STRING) {
 
135
                        $className = strtolower($tokens[$x]['content']);
 
136
                        if ($className === $systemName) {
 
137
                            // The included system was used, so it is fine.
 
138
                            return;
 
139
                        }
 
140
                    }
 
141
                }
 
142
 
 
143
                break;
 
144
            }//end switch
 
145
        }//end for
 
146
 
 
147
        // If we get to here, the system was not use.
 
148
        $error = 'Included system "%s" is never used';
 
149
        $data  = array($systemName);
 
150
        $phpcsFile->addError($error, $stackPtr, 'Found', $data);
 
151
 
 
152
    }//end process()
 
153
 
 
154
 
 
155
}//end class
 
156
 
 
157
?>