|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodingStandard_Sniffs_NamingConventions_ValidClassNameSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHP_CodeSniffer |
|
9
|
|
|
* @author Symfony2-phpcs-authors <[email protected]> |
|
10
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
11
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
12
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace CodingStandard\Sniffs\NamingConventions; |
|
16
|
|
|
|
|
17
|
|
|
use PHP_CodeSniffer\Files\File; |
|
18
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* CodingStandard_Sniffs_NamingConventions_ValidClassNameSniff. |
|
22
|
|
|
* |
|
23
|
|
|
* Throws errors if abstract class names are not prefixed with "Abstract". |
|
24
|
|
|
* |
|
25
|
|
|
* @category PHP |
|
26
|
|
|
* @package PHP_CodeSniffer |
|
27
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
28
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
29
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
30
|
|
|
*/ |
|
31
|
|
|
class ValidClassNameSniff implements Sniff |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A list of tokenizers this sniff supports. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
public $supportedTokenizers = array('PHP'); |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
44
|
|
|
* |
|
45
|
|
|
* @return integer[] |
|
46
|
|
|
*/ |
|
47
|
|
|
public function register() |
|
48
|
|
|
{ |
|
49
|
|
|
return array(T_CLASS); |
|
50
|
|
|
}//end register() |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
55
|
|
|
* |
|
56
|
|
|
* @param File $phpcsFile The file being scanned. |
|
57
|
|
|
* @param int $stackPtr The position of the current token in the |
|
58
|
|
|
* stack passed in $tokens. |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function process(File $phpcsFile, $stackPtr) |
|
63
|
|
|
{ |
|
64
|
|
|
$className = $phpcsFile->getDeclarationName($stackPtr); |
|
65
|
|
|
|
|
66
|
|
|
if (!$className) { |
|
|
|
|
|
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$classProperties = $phpcsFile->getClassProperties($stackPtr); |
|
71
|
|
|
$hasAbstractName = substr($className, 0, 8) === 'Abstract'; |
|
72
|
|
|
|
|
73
|
|
|
if ($classProperties['is_abstract'] === true && $hasAbstractName === false) { |
|
74
|
|
|
$phpcsFile->addError( |
|
75
|
|
|
'Abstract class name "%s" is not prefixed with "Abstract"', |
|
76
|
|
|
$stackPtr, |
|
77
|
|
|
'AbstractWrongName', |
|
78
|
|
|
array($className) |
|
79
|
|
|
); |
|
80
|
|
|
} elseif ($classProperties['is_abstract'] === false && $hasAbstractName === true) { |
|
81
|
|
|
$phpcsFile->addError( |
|
82
|
|
|
'Non-abstract class name "%s" is prefixed with "Abstract"', |
|
83
|
|
|
$stackPtr, |
|
84
|
|
|
'AbstractMissingModifier', |
|
85
|
|
|
array($className) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
}//end process() |
|
89
|
|
|
}//end class |
|
90
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: