1<?php
2/**
3 * PSR1_Sniffs_Methods_CamelCapsMethodNameSniff.
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PHP_CodeSniffer
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
15if (class_exists('Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff', true) === false) {
16    throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff not found');
17}
18
19/**
20 * PSR1_Sniffs_Methods_CamelCapsMethodNameSniff.
21 *
22 * Ensures method names are defined using camel case.
23 *
24 * @category  PHP
25 * @package   PHP_CodeSniffer
26 * @author    Greg Sherwood <gsherwood@squiz.net>
27 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
28 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
29 * @version   Release: @package_version@
30 * @link      http://pear.php.net/package/PHP_CodeSniffer
31 */
32class PSR1_Sniffs_Methods_CamelCapsMethodNameSniff extends Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff
33{
34
35
36    /**
37     * Processes the tokens within the scope.
38     *
39     * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
40     * @param int                  $stackPtr  The position where this token was
41     *                                        found.
42     * @param int                  $currScope The position of the current scope.
43     *
44     * @return void
45     */
46    protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
47    {
48        $methodName = $phpcsFile->getDeclarationName($stackPtr);
49        if ($methodName === null) {
50            // Ignore closures.
51            return;
52        }
53
54        // Ignore magic methods.
55        if (preg_match('|^__[^_]|', $methodName) !== 0) {
56            $magicPart = strtolower(substr($methodName, 2));
57            if (isset($this->magicMethods[$magicPart]) === true
58                || isset($this->methodsDoubleUnderscore[$magicPart]) === true
59            ) {
60                return;
61            }
62        }
63
64        $testName = ltrim($methodName, '_');
65        if ($testName !== '' && PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) {
66            $error     = 'Method name "%s" is not in camel caps format';
67            $className = $phpcsFile->getDeclarationName($currScope);
68            $errorData = array($className.'::'.$methodName);
69            $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
70            $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no');
71        } else {
72            $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes');
73        }
74
75    }//end processTokenWithinScope()
76
77
78    /**
79     * Processes the tokens outside 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     *
85     * @return void
86     */
87    protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
88    {
89
90    }//end processTokenOutsideScope()
91
92
93}//end class
94