1<?php 2/** 3 * Verifies that class methods have scope modifiers. 4 * 5 * PHP version 5 6 * 7 * @category PHP 8 * @package PHP_CodeSniffer 9 * @author Greg Sherwood <gsherwood@squiz.net> 10 * @author Marc McIntyre <mmcintyre@squiz.net> 11 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 12 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 13 * @link http://pear.php.net/package/PHP_CodeSniffer 14 */ 15 16if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) { 17 throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found'); 18} 19 20/** 21 * Verifies that class methods have scope modifiers. 22 * 23 * @category PHP 24 * @package PHP_CodeSniffer 25 * @author Greg Sherwood <gsherwood@squiz.net> 26 * @author Marc McIntyre <mmcintyre@squiz.net> 27 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600) 28 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence 29 * @version Release: @package_version@ 30 * @link http://pear.php.net/package/PHP_CodeSniffer 31 */ 32class Squiz_Sniffs_Scope_MethodScopeSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff 33{ 34 35 36 /** 37 * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff. 38 */ 39 public function __construct() 40 { 41 parent::__construct(array(T_CLASS, T_INTERFACE, T_TRAIT), array(T_FUNCTION)); 42 43 }//end __construct() 44 45 46 /** 47 * Processes the function tokens within the class. 48 * 49 * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. 50 * @param int $stackPtr The position where the token was found. 51 * @param int $currScope The current scope opener token. 52 * 53 * @return void 54 */ 55 protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) 56 { 57 $tokens = $phpcsFile->getTokens(); 58 59 $methodName = $phpcsFile->getDeclarationName($stackPtr); 60 if ($methodName === null) { 61 // Ignore closures. 62 return; 63 } 64 65 if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION) === true) { 66 // Ignore nested functions. 67 return; 68 } 69 70 $modifier = null; 71 for ($i = ($stackPtr - 1); $i > 0; $i--) { 72 if ($tokens[$i]['line'] < $tokens[$stackPtr]['line']) { 73 break; 74 } else if (isset(PHP_CodeSniffer_Tokens::$scopeModifiers[$tokens[$i]['code']]) === true) { 75 $modifier = $i; 76 break; 77 } 78 } 79 80 if ($modifier === null) { 81 $error = 'Visibility must be declared on method "%s"'; 82 $data = array($methodName); 83 $phpcsFile->addError($error, $stackPtr, 'Missing', $data); 84 } 85 86 }//end processTokenWithinScope() 87 88 89}//end class 90