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
|
|
|
namespace CodingStandard\Sniffs\Classes; |
16
|
|
|
|
17
|
|
|
use PHP_CodeSniffer\Files\File; |
18
|
|
|
use PHP_CodeSniffer\Sniffs\Sniff; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class create instance Test. |
22
|
|
|
* |
23
|
|
|
* Checks the declaration of the class is correct. |
24
|
|
|
* |
25
|
|
|
* @category PHP |
26
|
|
|
* @package PHP_CodeSniffer |
27
|
|
|
* @author Peter Philipp <[email protected]> |
28
|
|
|
* @author Alexander Obuhovich <[email protected]> |
29
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
30
|
|
|
* @link https://github.com/aik099/CodingStandard |
31
|
|
|
*/ |
32
|
|
|
class ClassCreateInstanceSniff implements Sniff |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns an array of tokens this test wants to listen for. |
38
|
|
|
* |
39
|
|
|
* @return integer[] |
40
|
|
|
*/ |
41
|
1 |
|
public function register() |
42
|
|
|
{ |
43
|
1 |
|
return array(T_NEW); |
44
|
|
|
}//end register() |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Processes this test, when one of its tokens is encountered. |
49
|
|
|
* |
50
|
|
|
* @param File $phpcsFile The file being scanned. |
51
|
|
|
* @param int $stackPtr The position of the current token in the |
52
|
|
|
* stack passed in $tokens. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
1 |
|
public function process(File $phpcsFile, $stackPtr) |
57
|
|
|
{ |
58
|
1 |
|
$scopeEnd = null; |
59
|
1 |
|
$tokens = $phpcsFile->getTokens(); |
60
|
|
|
|
61
|
1 |
|
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
62
|
|
|
// New in PHP 5.4: allow to instantiate class and immediately call method on it. |
63
|
1 |
|
$scopeStart = key($tokens[$stackPtr]['nested_parenthesis']); |
64
|
1 |
|
$scopeEnd = $tokens[$stackPtr]['nested_parenthesis'][$scopeStart]; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
$nextParenthesis = $phpcsFile->findNext( |
68
|
1 |
|
T_OPEN_PARENTHESIS, |
69
|
1 |
|
($stackPtr + 1), |
70
|
1 |
|
$scopeEnd, |
71
|
1 |
|
false, |
72
|
1 |
|
null, |
73
|
|
|
true |
74
|
1 |
|
); |
75
|
|
|
|
76
|
1 |
|
if ($nextParenthesis === false || $tokens[$nextParenthesis]['line'] !== $tokens[$stackPtr]['line']) { |
77
|
1 |
|
$error = 'Calling class constructors must always include parentheses'; |
78
|
1 |
|
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'MissingParenthesis'); |
79
|
1 |
|
if ($fix === true) { |
80
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
81
|
1 |
|
$classNameEnd = $phpcsFile->findNext( |
82
|
|
|
array( |
83
|
1 |
|
T_WHITESPACE, |
84
|
1 |
|
T_NS_SEPARATOR, |
85
|
1 |
|
T_STRING, |
86
|
1 |
|
), |
87
|
1 |
|
($stackPtr + 1), |
88
|
1 |
|
null, |
89
|
1 |
|
true, |
90
|
1 |
|
null, |
91
|
|
|
true |
92
|
1 |
|
); |
93
|
|
|
|
94
|
1 |
|
$phpcsFile->fixer->addContentBefore($classNameEnd, '()'); |
|
|
|
|
95
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
96
|
1 |
|
}//end if |
97
|
1 |
|
} elseif ($tokens[($nextParenthesis - 1)]['code'] === T_WHITESPACE) { |
98
|
1 |
|
$error = 'Between the class name and the opening parenthesis spaces are not welcome'; |
99
|
1 |
|
$fix = $phpcsFile->addFixableError($error, ($nextParenthesis - 1), 'ExtraSpaces'); |
100
|
1 |
|
if ($fix === true) { |
101
|
1 |
|
$phpcsFile->fixer->beginChangeset(); |
102
|
1 |
|
$phpcsFile->fixer->replaceToken(($nextParenthesis - 1), ''); |
103
|
1 |
|
$phpcsFile->fixer->endChangeset(); |
104
|
1 |
|
}//end if |
105
|
1 |
|
}//end if |
106
|
1 |
|
}//end process() |
107
|
|
|
}//end class |
108
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.