1<?php
2/**
3 * Squiz_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-2014 Squiz Pty Ltd (ABN 77 084 670 600)
12 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
13 * @link      http://pear.php.net/package/PHP_CodeSniffer
14 */
15
16if (class_exists('PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff', true) === false) {
17    throw new PHP_CodeSniffer_Exception('Class PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff not found');
18}
19
20/**
21 * Squiz_Sniffs_NamingConventions_ValidFunctionNameSniff.
22 *
23 * Ensures method names are correct depending on whether they are public
24 * or private, and that functions are named correctly.
25 *
26 * @category  PHP
27 * @package   PHP_CodeSniffer
28 * @author    Greg Sherwood <gsherwood@squiz.net>
29 * @author    Marc McIntyre <mmcintyre@squiz.net>
30 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
31 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
32 * @version   Release: @package_version@
33 * @link      http://pear.php.net/package/PHP_CodeSniffer
34 */
35class Squiz_Sniffs_NamingConventions_ValidFunctionNameSniff extends PEAR_Sniffs_NamingConventions_ValidFunctionNameSniff
36{
37
38
39    /**
40     * Processes the tokens outside the scope.
41     *
42     * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
43     * @param int                  $stackPtr  The position where this token was
44     *                                        found.
45     *
46     * @return void
47     */
48    protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
49    {
50        $functionName = $phpcsFile->getDeclarationName($stackPtr);
51        if ($functionName === null) {
52            return;
53        }
54
55        $errorData = array($functionName);
56
57        // Does this function claim to be magical?
58        if (preg_match('|^__[^_]|', $functionName) !== 0) {
59            $error = 'Function name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
60            $phpcsFile->addError($error, $stackPtr, 'DoubleUnderscore', $errorData);
61            return;
62        }
63
64        if (PHP_CodeSniffer::isCamelCaps($functionName, false, true, false) === false) {
65            $error = 'Function name "%s" is not in camel caps format';
66            $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
67        }
68
69    }//end processTokenOutsideScope()
70
71
72}//end class
73