|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodingStandard_Sniffs_CodeAnalysis_WrongParentCallSniff. |
|
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
|
|
|
* CodingStandard_Sniffs_CodeAnalysis_WrongParentCallSniff. |
|
17
|
|
|
* |
|
18
|
|
|
* Checks that method is invoking it's own parent and not other function. |
|
19
|
|
|
* |
|
20
|
|
|
* Correct: |
|
21
|
|
|
* function nameOne() { |
|
22
|
|
|
* parent::nameOne(); |
|
23
|
|
|
* |
|
24
|
|
|
* .... |
|
25
|
|
|
* } |
|
26
|
|
|
* |
|
27
|
|
|
* Wrong: |
|
28
|
|
|
* function nameOne() { |
|
29
|
|
|
* parent::nameTwo(); |
|
30
|
|
|
* |
|
31
|
|
|
* .... |
|
32
|
|
|
* } |
|
33
|
|
|
* |
|
34
|
|
|
* @category PHP |
|
35
|
|
|
* @package PHP_CodeSniffer |
|
36
|
|
|
* @author Alexander Obuhovich <[email protected]> |
|
37
|
|
|
* @license https://github.com/aik099/CodingStandard/blob/master/LICENSE BSD 3-Clause |
|
38
|
|
|
* @link https://github.com/aik099/CodingStandard |
|
39
|
|
|
*/ |
|
40
|
|
|
class CodingStandard_Sniffs_CodeAnalysis_WrongParentCallSniff implements PHP_CodeSniffer_Sniff |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
46
|
|
|
* |
|
47
|
|
|
* @return integer[] |
|
48
|
|
|
*/ |
|
49
|
|
|
public function register() |
|
50
|
|
|
{ |
|
51
|
|
|
return array(T_PARENT); |
|
52
|
|
|
|
|
53
|
|
|
}//end register() |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
58
|
|
|
* |
|
59
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
60
|
|
|
* @param int $stackPtr The position of the current token in the |
|
61
|
|
|
* stack passed in $tokens. |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
66
|
|
|
{ |
|
67
|
|
|
$functionPtr = $phpcsFile->findPrevious(T_FUNCTION, ($stackPtr - 1)); |
|
68
|
|
|
|
|
69
|
|
|
if ($functionPtr !== false) { |
|
70
|
|
|
$doubleColonPtr = $phpcsFile->findNext(T_DOUBLE_COLON, ($stackPtr + 1)); |
|
71
|
|
|
|
|
72
|
|
|
if ($doubleColonPtr !== false) { |
|
73
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
74
|
|
|
$functionName = $phpcsFile->getDeclarationName($functionPtr); |
|
|
|
|
|
|
75
|
|
|
$methodNamePtr = $phpcsFile->findNext(T_STRING, ($stackPtr + 1)); |
|
76
|
|
|
|
|
77
|
|
|
if ($methodNamePtr !== false && $tokens[$methodNamePtr]['content'] !== $functionName) { |
|
78
|
|
|
$error = 'Method name mismatch in parent:: call'; |
|
79
|
|
|
$phpcsFile->addError($error, $stackPtr, 'WrongName'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
}//end if |
|
83
|
|
|
|
|
84
|
|
|
}//end process() |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
}//end class |
|
88
|
|
|
|
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.