Mercurial > hg > isophonics-drupal-site
comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/NamingConventions/ValidFunctionNameSniff.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 /** | |
3 * Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff. | |
4 * | |
5 * @category PHP | |
6 * @package PHP_CodeSniffer | |
7 * @link http://pear.php.net/package/PHP_CodeSniffer | |
8 */ | |
9 | |
10 /** | |
11 * Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff. | |
12 * | |
13 * Extends Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff to also check | |
14 * global function names outside the scope of classes and to not allow methods | |
15 * beginning with an underscore. | |
16 * | |
17 * @category PHP | |
18 * @package PHP_CodeSniffer | |
19 * @link http://pear.php.net/package/PHP_CodeSniffer | |
20 */ | |
21 class Drupal_Sniffs_NamingConventions_ValidFunctionNameSniff extends Generic_Sniffs_NamingConventions_CamelCapsFunctionNameSniff | |
22 { | |
23 | |
24 | |
25 /** | |
26 * Processes the tokens within the scope. | |
27 * | |
28 * @param PHP_CodeSniffer_File $phpcsFile The file being processed. | |
29 * @param int $stackPtr The position where this token was | |
30 * found. | |
31 * @param int $currScope The position of the current scope. | |
32 * | |
33 * @return void | |
34 */ | |
35 protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) | |
36 { | |
37 $methodName = $phpcsFile->getDeclarationName($stackPtr); | |
38 if ($methodName === null) { | |
39 // Ignore closures. | |
40 return; | |
41 } | |
42 | |
43 $className = $phpcsFile->getDeclarationName($currScope); | |
44 $errorData = array($className.'::'.$methodName); | |
45 | |
46 // Is this a magic method. i.e., is prefixed with "__" ? | |
47 if (preg_match('|^__|', $methodName) !== 0) { | |
48 $magicPart = strtolower(substr($methodName, 2)); | |
49 if (isset($this->magicMethods[$magicPart]) === false | |
50 && isset($this->methodsDoubleUnderscore[$magicPart]) === false | |
51 ) { | |
52 $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; | |
53 $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData); | |
54 } | |
55 | |
56 return; | |
57 } | |
58 | |
59 $methodProps = $phpcsFile->getMethodProperties($stackPtr); | |
60 if (PHP_CodeSniffer::isCamelCaps($methodName, false, true, $this->strict) === false) { | |
61 if ($methodProps['scope_specified'] === true) { | |
62 $error = '%s method name "%s" is not in lowerCamel format'; | |
63 $data = array( | |
64 ucfirst($methodProps['scope']), | |
65 $errorData[0], | |
66 ); | |
67 $phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data); | |
68 } else { | |
69 $error = 'Method name "%s" is not in lowerCamel format'; | |
70 $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); | |
71 } | |
72 | |
73 $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'no'); | |
74 return; | |
75 } else { | |
76 $phpcsFile->recordMetric($stackPtr, 'CamelCase method name', 'yes'); | |
77 } | |
78 | |
79 }//end processTokenWithinScope() | |
80 | |
81 | |
82 /** | |
83 * Processes the tokens outside the scope. | |
84 * | |
85 * @param PHP_CodeSniffer_File $phpcsFile The file being processed. | |
86 * @param int $stackPtr The position where this token was | |
87 * found. | |
88 * | |
89 * @return void | |
90 */ | |
91 protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr) | |
92 { | |
93 $functionName = $phpcsFile->getDeclarationName($stackPtr); | |
94 if ($functionName === null) { | |
95 // Ignore closures. | |
96 return; | |
97 } | |
98 | |
99 $isApiFile = substr($phpcsFile->getFilename(), -8) === '.api.php'; | |
100 $isHookExample = substr($functionName, 0, 5) === 'hook_'; | |
101 if ($isApiFile === true && $isHookExample === true) { | |
102 // Ignore for examaple hook_ENTITY_TYPE_insert() functions in .api.php | |
103 // files. | |
104 return; | |
105 } | |
106 | |
107 if ($functionName !== strtolower($functionName)) { | |
108 $expected = strtolower(preg_replace('/([^_])([A-Z])/', '$1_$2', $functionName)); | |
109 $error = 'Invalid function name, expected %s but found %s'; | |
110 $data = array( | |
111 $expected, | |
112 $functionName, | |
113 ); | |
114 $phpcsFile->addError($error, $stackPtr, 'InvalidName', $data); | |
115 } | |
116 | |
117 }//end processTokenOutsideScope() | |
118 | |
119 | |
120 }//end class |