1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodingStandard_Sniffs_Classes_ClassNamespaceSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Alexander Obuhovich <[email protected]> |
10
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
11
|
|
|
* @link https://github.com/aik099/CodingStandard |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Namespace Test. |
16
|
|
|
* |
17
|
|
|
* Checks the that of the class is namespaced. |
18
|
|
|
* |
19
|
|
|
* @category PHP |
20
|
|
|
* @package PHP_CodeSniffer |
21
|
|
|
* @author Alexander Obuhovich <[email protected]> |
22
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
23
|
|
|
* @link https://github.com/aik099/CodingStandard |
24
|
|
|
*/ |
25
|
|
|
class CodingStandard_Sniffs_Classes_ClassNamespaceSniff implements PHP_CodeSniffer_Sniff |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns an array of tokens this test wants to listen for. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function register() |
35
|
|
|
{ |
36
|
|
|
// @codeCoverageIgnoreStart |
37
|
|
|
if (version_compare(PHP_VERSION, '5.3.0') < 0) { |
38
|
|
|
return array(); |
39
|
|
|
} |
40
|
|
|
// @codeCoverageIgnoreEnd |
41
|
|
|
|
42
|
|
|
return array( |
43
|
|
|
T_CLASS, |
44
|
|
|
T_INTERFACE, |
45
|
|
|
T_TRAIT, |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
}//end register() |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Processes this test, when one of its tokens is encountered. |
53
|
|
|
* |
54
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
55
|
|
|
* @param integer $stackPtr The position of the current token in the |
56
|
|
|
* stack passed in $tokens. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
61
|
|
|
{ |
62
|
|
|
$tokens = $phpcsFile->getTokens(); |
63
|
|
|
$namespace = $phpcsFile->findPrevious(T_NAMESPACE, ($stackPtr - 1)); |
64
|
|
|
|
65
|
|
|
if ($namespace === false) { |
66
|
|
|
$error = 'Each %s must be in a namespace of at least one level (a top-level vendor name)'; |
67
|
|
|
$data = array($tokens[$stackPtr]['content']); |
68
|
|
|
$phpcsFile->addError($error, $stackPtr, 'MissingNamespace', $data); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
}//end process() |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
}//end class |
75
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.