comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Scope/MethodScopeSniff.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /**
3 * Verifies that class/interface/trait methods have scope modifiers.
4 *
5 * @category PHP
6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */
9
10 /**
11 * Verifies that class/interface/trait methods have scope modifiers.
12 *
13 * Laregely copied from Squiz_Sniffs_Scope_MethodScopeSniff to work on traits
14 * and have a fixer.
15 *
16 * @category PHP
17 * @package PHP_CodeSniffer
18 * @link http://pear.php.net/package/PHP_CodeSniffer
19 */
20 class Drupal_Sniffs_Scope_MethodScopeSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
21 {
22
23
24 /**
25 * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff.
26 */
27 public function __construct()
28 {
29 parent::__construct(array(T_CLASS, T_INTERFACE, T_TRAIT), array(T_FUNCTION));
30
31 }//end __construct()
32
33
34 /**
35 * Processes the function tokens within the class.
36 *
37 * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
38 * @param int $stackPtr The position where the token was found.
39 * @param int $currScope The current scope opener token.
40 *
41 * @return void
42 */
43 protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
44 {
45 $tokens = $phpcsFile->getTokens();
46
47 $methodName = $phpcsFile->getDeclarationName($stackPtr);
48 if ($methodName === null) {
49 // Ignore closures.
50 return;
51 }
52
53 if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true) {
54 // Ignore nested functions.
55 return;
56 }
57
58 $modifier = null;
59 for ($i = ($stackPtr - 1); $i > 0; $i--) {
60 if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) {
61 break;
62 } else if (isset(PHP_CodeSniffer_Tokens::$scopeModifiers[$tokens[$i]['code']]) === true) {
63 $modifier = $i;
64 break;
65 }
66 }
67
68 if ($modifier === null) {
69 $error = 'Visibility must be declared on method "%s"';
70 $data = array($methodName);
71 $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Missing', $data);
72
73 if ($fix === true) {
74 // No scope modifier means the method is public in PHP, fix that
75 // to be explicitly public.
76 $phpcsFile->fixer->addContentBefore($stackPtr, 'public ');
77 }
78 }
79
80 }//end processTokenWithinScope()
81
82
83 }//end class