Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
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: ValidFunctionNameSniff.php 283545 2009-07-06 03:16:27Z 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
 * PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
23
 *
24
 * Ensures method names are correct depending on whether they are public
25
 * or private, and that functions are named correctly.
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 PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
37
{
38
 
39
    /**
40
     * A list of all PHP magic methods.
41
     *
42
     * @var array
43
     */
44
    protected $magicMethods = array(
45
                               'construct',
46
                               'destruct',
47
                               'call',
48
                               'callStatic',
49
                               'get',
50
                               'set',
51
                               'isset',
52
                               'unset',
53
                               'sleep',
54
                               'wakeup',
55
                               'toString',
56
                               'set_state',
57
                               'clone',
58
                              );
59
 
60
    /**
61
     * A list of all PHP magic functions.
62
     *
63
     * @var array
64
     */
65
    protected $magicFunctions = array('autoload');
66
 
67
 
68
    /**
69
     * Constructs a PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff.
70
     */
71
    public function __construct()
72
    {
73
        parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true);
74
 
75
    }//end __construct()
76
 
77
 
78
    /**
79
     * Processes the tokens within the scope.
80
     *
81
     * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
82
     * @param int                  $stackPtr  The position where this token was
83
     *                                        found.
84
     * @param int                  $currScope The position of the current scope.
85
     *
86
     * @return void
87
     */
88
    protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
89
    {
90
        $className  = $phpcsFile->getDeclarationName($currScope);
91
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
92
 
93
        // Is this a magic method. IE. is prefixed with "__".
94
        if (preg_match('|^__|', $methodName) !== 0) {
95
            $magicPart = substr($methodName, 2);
96
            if (in_array($magicPart, $this->magicMethods) === false) {
97
                 $error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
98
                 $phpcsFile->addError($error, $stackPtr);
99
            }
100
 
101
            return;
102
        }
103
 
104
        // PHP4 constructors are allowed to break our rules.
105
        if ($methodName === $className) {
106
            return;
107
        }
108
 
109
        // PHP4 destructors are allowed to break our rules.
110
        if ($methodName === '_'.$className) {
111
            return;
112
        }
113
 
114
        $methodProps    = $phpcsFile->getMethodProperties($stackPtr);
115
        $isPublic       = ($methodProps['scope'] === 'private') ? false : true;
116
        $scope          = $methodProps['scope'];
117
        $scopeSpecified = $methodProps['scope_specified'];
118
 
119
        // If it's a private method, it must have an underscore on the front.
120
        if ($isPublic === false && $methodName{0} !== '_') {
121
            $error = "Private method name \"$className::$methodName\" must be prefixed with an underscore";
122
            $phpcsFile->addError($error, $stackPtr);
123
            return;
124
        }
125
 
126
        // If it's not a private method, it must not have an underscore on the front.
127
        if ($isPublic === true && $scopeSpecified === true && $methodName{0} === '_') {
128
            $error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore";
129
            $phpcsFile->addError($error, $stackPtr);
130
            return;
131
        }
132
 
133
        // If the scope was specified on the method, then the method must be
134
        // camel caps and an underscore should be checked for. If it wasn't
135
        // specified, treat it like a public method and remove the underscore
136
        // prefix if there is one because we cant determine if it is private or
137
        // public.
138
        $testMethodName = $methodName;
139
        if ($scopeSpecified === false && $methodName{0} === '_') {
140
            $testMethodName = substr($methodName, 1);
141
        }
142
 
143
        if (PHP_CodeSniffer::isCamelCaps($testMethodName, false, $isPublic, false) === false) {
144
            if ($scopeSpecified === true) {
145
                $error = ucfirst($scope)." method name \"$className::$methodName\" is not in camel caps format";
146
            } else {
147
                $error = "Method name \"$className::$methodName\" is not in camel caps format";
148
            }
149
 
150
            $phpcsFile->addError($error, $stackPtr);
151
            return;
152
        }
153
 
154
    }//end processTokenWithinScope()
155
 
156
 
157
    /**
158
     * Processes the tokens outside the scope.
159
     *
160
     * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
161
     * @param int                  $stackPtr  The position where this token was
162
     *                                        found.
163
     *
164
     * @return void
165
     */
166
    protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
167
    {
168
        $functionName = $phpcsFile->getDeclarationName($stackPtr);
169
 
170
        // Is this a magic function. IE. is prefixed with "__".
171
        if (preg_match('|^__|', $functionName) !== 0) {
172
            $magicPart = substr($functionName, 2);
173
            if (in_array($magicPart, $this->magicFunctions) === false) {
174
                 $error = "Function name \"$functionName\" is invalid; only PHP magic methods should be prefixed with a double underscore";
175
                 $phpcsFile->addError($error, $stackPtr);
176
            }
177
 
178
            return;
179
        }
180
 
181
        // Function names can be in two parts; the package name and
182
        // the function name.
183
        $packagePart   = '';
184
        $camelCapsPart = '';
185
        $underscorePos = strrpos($functionName, '_');
186
        if ($underscorePos === false) {
187
            $camelCapsPart = $functionName;
188
        } else {
189
            $packagePart   = substr($functionName, 0, $underscorePos);
190
            $camelCapsPart = substr($functionName, ($underscorePos + 1));
191
 
192
            // We don't care about _'s on the front.
193
            $packagePart = ltrim($packagePart, '_');
194
        }
195
 
196
        // If it has a package part, make sure the first letter is a capital.
197
        if ($packagePart !== '') {
198
            if ($functionName{0} === '_') {
199
                $error = "Function name \"$functionName\" is invalid; only private methods should be prefixed with an underscore";
200
                $phpcsFile->addError($error, $stackPtr);
201
                return;
202
            }
203
 
204
            if ($functionName{0} !== strtoupper($functionName{0})) {
205
                $error = "Function name \"$functionName\" is prefixed with a package name but does not begin with a capital letter";
206
                $phpcsFile->addError($error, $stackPtr);
207
                return;
208
            }
209
        }
210
 
211
        // If it doesn't have a camel caps part, it's not valid.
212
        if (trim($camelCapsPart) === '') {
213
            $error = "Function name \"$functionName\" is not valid; name appears incomplete";
214
            $phpcsFile->addError($error, $stackPtr);
215
            return;
216
        }
217
 
218
        $validName        = true;
219
        $newPackagePart   = $packagePart;
220
        $newCamelCapsPart = $camelCapsPart;
221
 
222
        // Every function must have a camel caps part, so check that first.
223
        if (PHP_CodeSniffer::isCamelCaps($camelCapsPart, false, true, false) === false) {
224
            $validName        = false;
225
            $newCamelCapsPart = strtolower($camelCapsPart{0}).substr($camelCapsPart, 1);
226
        }
227
 
228
        if ($packagePart !== '') {
229
            // Check that each new word starts with a capital.
230
            $nameBits = explode('_', $packagePart);
231
            foreach ($nameBits as $bit) {
232
                if ($bit{0} !== strtoupper($bit{0})) {
233
                    $newPackagePart = '';
234
                    foreach ($nameBits as $bit) {
235
                        $newPackagePart .= strtoupper($bit{0}).substr($bit, 1).'_';
236
                    }
237
 
238
                    $validName = false;
239
                    break;
240
                }
241
            }
242
        }
243
 
244
        if ($validName === false) {
245
            $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
246
            if ($newPackagePart === '') {
247
                $newName = $newCamelCapsPart;
248
            } else {
249
                $newName = rtrim($newPackagePart, '_').'_'.$newCamelCapsPart;
250
            }
251
 
252
            $error = "Function name \"$functionName\" is invalid; consider \"$newName\" instead";
253
            $phpcsFile->addError($error, $stackPtr);
254
        }
255
 
256
    }//end processTokenOutsideScope()
257
 
258
 
259
}//end class
260
 
261
?>