-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathValidPropertyNameSniff.php
More file actions
69 lines (61 loc) · 2.24 KB
/
ValidPropertyNameSniff.php
File metadata and controls
69 lines (61 loc) · 2.24 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
<?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 6.0.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Ensures property names follow PSR naming conventions.
*
* Non-public properties should not be prefixed with an underscore.
*/
namespace CakePHP\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
class ValidPropertyNameSniff extends AbstractVariableSniff
{
/**
* @inheritDoc
*/
protected function processMemberVar(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$propName = ltrim($tokens[$stackPtr]['content'], '$');
// Only check properties starting with underscore
if ($propName[0] !== '_') {
return;
}
$props = $phpcsFile->getMemberProperties($stackPtr);
// Public properties with underscore are also bad, but less common
// Focus on protected/private which was the old convention
if ($props['scope'] !== 'public') {
$error = 'Non-public property "$%s" should not be prefixed with underscore';
$phpcsFile->addError($error, $stackPtr, 'PropertyWithUnderscore', [$propName]);
} else {
$error = 'Public property "$%s" must not be prefixed with underscore';
$phpcsFile->addError($error, $stackPtr, 'PublicPropertyWithUnderscore', [$propName]);
}
}
/**
* @inheritDoc
*/
protected function processVariable(File $phpcsFile, $stackPtr)
{
// We only care about member variables (properties)
}
/**
* @inheritDoc
*/
protected function processVariableInString(File $phpcsFile, $stackPtr)
{
// We only care about member variables (properties)
}
}