-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathValidFunctionNameSniff.php
More file actions
111 lines (97 loc) · 3.19 KB
/
ValidFunctionNameSniff.php
File metadata and controls
111 lines (97 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://github.com/cakephp/cakephp-codesniffer
* @since CakePHP CodeSniffer 0.1.1
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Ensures method names are correct depending on whether they are public
* or private, and that functions are named correctly.
*/
namespace CakePHP\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
class ValidFunctionNameSniff extends AbstractScopeSniff
{
/**
* A list of all PHP magic methods.
*
* @var array<string>
*/
protected array $magicMethods = [
'construct',
'destruct',
'call',
'callStatic',
'debugInfo',
'get',
'set',
'isset',
'unset',
'sleep',
'wakeup',
'serialize',
'unserialize',
'toString',
'set_state',
'clone',
'invoke',
];
/**
* @inheritDoc
*/
public function __construct()
{
parent::__construct([T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM], [T_FUNCTION], true);
}
/**
* @inheritDoc
*/
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
{
$methodName = $phpcsFile->getDeclarationName($stackPtr);
if ($methodName === '') {
return;
}
$className = $phpcsFile->getDeclarationName($currScope);
$errorData = [$className . '::' . $methodName];
// Ignore magic methods
if (preg_match('/^__(' . implode('|', $this->magicMethods) . ')$/', $methodName)) {
return;
}
$methodProps = $phpcsFile->getMethodProperties($stackPtr);
if ($methodProps['scope_specified'] === false) {
// Let another sniffer take care of that
return;
}
$isPublic = $methodProps['scope'] === 'public';
if ($isPublic === true && $methodName[0] === '_') {
$error = 'Public method name "%s" must not be prefixed with underscore';
$phpcsFile->addError($error, $stackPtr, 'PublicWithUnderscore', $errorData);
return;
}
// Check non-public methods for underscore prefix
if ($isPublic === false && $methodName[0] === '_') {
// Allow CakePHP Entity accessor/mutator pattern: _getField(), _setField()
if (preg_match('/^_(get|set)[A-Z]/', $methodName)) {
return;
}
$error = 'Non-public method name "%s" should not be prefixed with underscore';
$phpcsFile->addError($error, $stackPtr, 'ProtectedWithUnderscore', $errorData);
}
}
/**
* @inheritDoc
*/
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
{
}
}