1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CodingStandard_Sniffs_Classes_ClassCreateInstanceSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHP_CodeSniffer |
9
|
|
|
* @author Peter Philipp <[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
|
|
|
/** |
16
|
|
|
* Class create instance Test. |
17
|
|
|
* |
18
|
|
|
* Checks the declaration of the class is correct. |
19
|
|
|
* |
20
|
|
|
* @category PHP |
21
|
|
|
* @package PHP_CodeSniffer |
22
|
|
|
* @author Peter Philipp <[email protected]> |
23
|
|
|
* @author Alexander Obuhovich <[email protected]> |
24
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
25
|
|
|
* @link https://github.com/aik099/CodingStandard |
26
|
|
|
*/ |
27
|
|
|
class CodingStandard_Sniffs_Classes_ClassCreateInstanceSniff implements PHP_CodeSniffer_Sniff |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns an array of tokens this test wants to listen for. |
33
|
|
|
* |
34
|
|
|
* @return integer[] |
35
|
|
|
*/ |
36
|
|
|
public function register() |
37
|
|
|
{ |
38
|
|
|
return array(T_NEW); |
39
|
|
|
|
40
|
|
|
}//end register() |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Processes this test, when one of its tokens is encountered. |
45
|
|
|
* |
46
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
47
|
|
|
* @param int $stackPtr The position of the current token in the |
48
|
|
|
* stack passed in $tokens. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
53
|
|
|
{ |
54
|
|
|
$scopeEnd = null; |
55
|
|
|
$tokens = $phpcsFile->getTokens(); |
56
|
|
|
|
57
|
|
|
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
58
|
|
|
// New in PHP 5.4: allow to instantiate class and immediately call method on it. |
59
|
|
|
list (, $scopeEnd) = each($tokens[$stackPtr]['nested_parenthesis']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$nextParenthesis = $phpcsFile->findNext( |
63
|
|
|
T_OPEN_PARENTHESIS, |
64
|
|
|
($stackPtr + 1), |
65
|
|
|
$scopeEnd, |
66
|
|
|
false, |
67
|
|
|
null, |
68
|
|
|
true |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
if ($nextParenthesis === false || $tokens[$nextParenthesis]['line'] !== $tokens[$stackPtr]['line']) { |
72
|
|
|
$error = 'Calling class constructors must always include parentheses'; |
73
|
|
|
$fix = $phpcsFile->addFixableError($error, $stackPtr); |
74
|
|
|
if ($fix === true) { |
75
|
|
|
$phpcsFile->fixer->beginChangeset(); |
76
|
|
|
$classNameEnd = $phpcsFile->findNext( |
77
|
|
|
array( |
78
|
|
|
T_WHITESPACE, |
79
|
|
|
T_NS_SEPARATOR, |
80
|
|
|
T_STRING, |
81
|
|
|
), |
82
|
|
|
($stackPtr + 1), |
83
|
|
|
null, |
84
|
|
|
true, |
85
|
|
|
null, |
86
|
|
|
true |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$phpcsFile->fixer->addContentBefore($classNameEnd, '()'); |
|
|
|
|
90
|
|
|
$phpcsFile->fixer->endChangeset(); |
91
|
|
|
}//end if |
92
|
|
|
} else if ($tokens[($nextParenthesis - 1)]['code'] === T_WHITESPACE) { |
93
|
|
|
$error = 'Between the class name and the opening parenthesis spaces are not welcome'; |
94
|
|
|
$fix = $phpcsFile->addFixableError($error, ($nextParenthesis - 1)); |
95
|
|
|
if ($fix === true) { |
96
|
|
|
$phpcsFile->fixer->beginChangeset(); |
97
|
|
|
$phpcsFile->fixer->replaceToken(($nextParenthesis - 1), ''); |
98
|
|
|
$phpcsFile->fixer->endChangeset(); |
99
|
|
|
}//end if |
100
|
|
|
}//end if |
101
|
|
|
|
102
|
|
|
}//end process() |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
}//end class |
106
|
|
|
|
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
.