Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * DrupalPractice_Sniffs_Objects_UnusedPrivateMethodSniff.
|
Chris@0
|
4 *
|
Chris@0
|
5 * @category PHP
|
Chris@0
|
6 * @package PHP_CodeSniffer
|
Chris@0
|
7 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Checks that private methods are actually used in a class.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @category PHP
|
Chris@0
|
14 * @package PHP_CodeSniffer
|
Chris@0
|
15 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
16 */
|
Chris@0
|
17 class DrupalPractice_Sniffs_Objects_UnusedPrivateMethodSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
|
Chris@0
|
18 {
|
Chris@0
|
19
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Constructor.
|
Chris@0
|
23 */
|
Chris@0
|
24 public function __construct()
|
Chris@0
|
25 {
|
Chris@0
|
26 parent::__construct([T_CLASS], [T_FUNCTION], false);
|
Chris@0
|
27
|
Chris@0
|
28 }//end __construct()
|
Chris@0
|
29
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Processes the tokens within the scope.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
|
Chris@0
|
35 * @param int $stackPtr The position where this token was
|
Chris@0
|
36 * found.
|
Chris@0
|
37 * @param int $currScope The position of the current scope.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @return void
|
Chris@0
|
40 */
|
Chris@0
|
41 protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
|
Chris@0
|
42 {
|
Chris@0
|
43 // Only check private methods.
|
Chris@0
|
44 $methodProperties = $phpcsFile->getMethodProperties($stackPtr);
|
Chris@0
|
45 if ($methodProperties['scope'] !== 'private' || $methodProperties['is_static'] === true) {
|
Chris@0
|
46 return;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 $tokens = $phpcsFile->getTokens();
|
Chris@0
|
50 $methodName = $phpcsFile->getDeclarationName($stackPtr);
|
Chris@0
|
51
|
Chris@0
|
52 $classPtr = key($tokens[$stackPtr]['conditions']);
|
Chris@0
|
53
|
Chris@0
|
54 // Search for direct $this->methodCall() or indirect callbacks [$this,
|
Chris@0
|
55 // 'methodCall'].
|
Chris@0
|
56 $current = $tokens[$classPtr]['scope_opener'];
|
Chris@0
|
57 $end = $tokens[$classPtr]['scope_closer'];
|
Chris@0
|
58 while (($current = $phpcsFile->findNext(T_VARIABLE, ($current + 1), $end)) !== false) {
|
Chris@0
|
59 if ($tokens[$current]['content'] !== '$this') {
|
Chris@0
|
60 continue;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($current + 1), null, true);
|
Chris@0
|
64 if ($next === false) {
|
Chris@0
|
65 continue;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 if ($tokens[$next]['code'] === T_OBJECT_OPERATOR) {
|
Chris@0
|
69 $call = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
Chris@0
|
70 if ($call === false || $tokens[$call]['content'] !== $methodName) {
|
Chris@0
|
71 continue;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 $parenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($call + 1), null, true);
|
Chris@0
|
75 if ($parenthesis === false || $tokens[$parenthesis]['code'] !== T_OPEN_PARENTHESIS) {
|
Chris@0
|
76 continue;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 // At this point this is a method call to the private method, so we
|
Chris@0
|
80 // can stop.
|
Chris@0
|
81 return;
|
Chris@0
|
82 } else if ($tokens[$next]['code'] === T_COMMA) {
|
Chris@0
|
83 $call = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
Chris@0
|
84 if ($call === false || substr($tokens[$call]['content'], 1, -1) !== $methodName) {
|
Chris@0
|
85 continue;
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 // At this point this is likely the private method as callback on a
|
Chris@0
|
89 // function such as array_filter().
|
Chris@0
|
90 return;
|
Chris@0
|
91 }//end if
|
Chris@0
|
92 }//end while
|
Chris@0
|
93
|
Chris@0
|
94 $warning = 'Unused private method %s()';
|
Chris@0
|
95 $data = [$methodName];
|
Chris@0
|
96 $phpcsFile->addWarning($warning, $stackPtr, 'UnusedMethod', $data);
|
Chris@0
|
97
|
Chris@0
|
98 }//end processTokenWithinScope()
|
Chris@0
|
99
|
Chris@0
|
100
|
Chris@0
|
101 }//end class
|