PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/share/pear/PHP/CodeSniffer/Standards/MySource/Sniffs/Channels/IncludeSystemSniff.php

https://github.com/amumu/modev
PHP | 249 lines | 150 code | 30 blank | 69 comment | 22 complexity | d7cef690580b15a2cdbef298e3e032d9 MD5 | raw file
  1. <?php
  2. /**
  3. * Ensures that systems, asset types and libs are included before they are used.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer_MySource
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  12. * @version CVS: $Id: IncludeSystemSniff.php,v 1.17 2008/07/25 04:18:04 squiz Exp $
  13. * @link http://pear.php.net/package/PHP_CodeSniffer
  14. */
  15. if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
  16. $error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found';
  17. throw new PHP_CodeSniffer_Exception($error);
  18. }
  19. /**
  20. * Ensures that systems, asset types and libs are included before they are used.
  21. *
  22. * @category PHP
  23. * @package PHP_CodeSniffer_MySource
  24. * @author Greg Sherwood <gsherwood@squiz.net>
  25. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  26. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  27. * @version Release: 1.2.0a1
  28. * @link http://pear.php.net/package/PHP_CodeSniffer
  29. */
  30. class MySource_Sniffs_Channels_IncludeSystemSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
  31. {
  32. /**
  33. * A list of classes that don't need to be included.
  34. *
  35. * @var array(string)
  36. */
  37. private $_ignore = array(
  38. 'self',
  39. 'parent',
  40. 'channels',
  41. 'basesystem',
  42. 'dal',
  43. 'init',
  44. 'pdo',
  45. 'util',
  46. 'ziparchive',
  47. );
  48. /**
  49. * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff.
  50. */
  51. public function __construct()
  52. {
  53. parent::__construct(array(T_FUNCTION), array(T_DOUBLE_COLON, T_EXTENDS), true);
  54. }//end __construct()
  55. /**
  56. * Processes the function tokens within the class.
  57. *
  58. * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  59. * @param int $stackPtr The position where the token was found.
  60. * @param int $currScope The current scope opener token.
  61. *
  62. * @return void
  63. */
  64. protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
  65. {
  66. $tokens = $phpcsFile->getTokens();
  67. // Determine the name of the class that the static function
  68. // is being called on.
  69. $classNameToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
  70. $className = $tokens[$classNameToken]['content'];
  71. if (in_array(strtolower($className), $this->_ignore) === true) {
  72. return;
  73. }
  74. $includedClasses = array();
  75. $fileName = strtolower($phpcsFile->getFilename());
  76. $matches = array();
  77. if (preg_match('|/systems/([^/]+)/([^/]+)?actions.inc$|', $fileName, $matches) !== 0) {
  78. // This is an actions file, which means we don't
  79. // have to include the system in which it exists
  80. // We know the system from the path.
  81. $includedClasses[] = $matches[1];
  82. }
  83. // Go searching for includeSystem and includeAsset calls within this
  84. // function, or the inclusion of .inc files, which
  85. // would be library files.
  86. for ($i = ($currScope + 1); $i < $stackPtr; $i++) {
  87. if (strtolower($tokens[$i]['content']) === 'includesystem') {
  88. $systemName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  89. $systemName = trim($tokens[$systemName]['content'], " '");
  90. $includedClasses[] = strtolower($systemName);
  91. } else if (strtolower($tokens[$i]['content']) === 'includeasset') {
  92. $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  93. $typeName = trim($tokens[$typeName]['content'], " '");
  94. $includedClasses[] = strtolower($typeName).'assettype';
  95. } else if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$includeTokens) === true) {
  96. $filePath = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  97. $filePath = $tokens[$filePath]['content'];
  98. $filePath = trim($filePath, " '");
  99. $filePath = basename($filePath, '.inc');
  100. $includedClasses[] = strtolower($filePath);
  101. }
  102. }//end for
  103. // Now go searching for includeSystem, includeAsset or require/include
  104. // calls outside our scope. If we are in a class, look outside the
  105. // class. If we are not, look outside the function.
  106. $condPtr = $currScope;
  107. if ($phpcsFile->hasCondition($stackPtr, T_CLASS) === true) {
  108. foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condType) {
  109. if ($condType === T_CLASS) {
  110. break;
  111. }
  112. }
  113. }
  114. for ($i = 0; $i < $condPtr; $i++) {
  115. // Skip other scopes.
  116. if (isset($tokens[$i]['scope_closer']) === true) {
  117. $i = $tokens[$i]['scope_closer'];
  118. continue;
  119. }
  120. if (strtolower($tokens[$i]['content']) === 'includesystem') {
  121. $systemName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  122. $systemName = trim($tokens[$systemName]['content'], " '");
  123. $includedClasses[] = strtolower($systemName);
  124. } else if (strtolower($tokens[$i]['content']) === 'includeasset') {
  125. $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  126. $typeName = trim($tokens[$typeName]['content'], " '");
  127. $includedClasses[] = strtolower($typeName).'assettype';
  128. } else if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$includeTokens) === true) {
  129. $filePath = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  130. $filePath = $tokens[$filePath]['content'];
  131. $filePath = trim($filePath, " '");
  132. $filePath = basename($filePath, '.inc');
  133. $includedClasses[] = strtolower($filePath);
  134. }
  135. }//end for
  136. if (in_array(strtolower($className), $includedClasses) === false) {
  137. $error = "Static method called on non-included class or system \"$className\"; include system with Channels::includeSystem() or include class with require_once";
  138. $phpcsFile->addError($error, $stackPtr);
  139. }
  140. }//end processTokenWithinScope()
  141. /**
  142. * Processes a token that is found within the scope that this test is
  143. * listening to.
  144. *
  145. * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found.
  146. * @param int $stackPtr The position in the stack where this token
  147. * was found.
  148. *
  149. * @return void
  150. */
  151. protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  152. {
  153. $tokens = $phpcsFile->getTokens();
  154. if ($tokens[$stackPtr]['code'] === T_EXTENDS) {
  155. // Find the class name.
  156. $classNameToken = $phpcsFile->findNext(T_STRING, ($stackPtr + 1));
  157. $className = $tokens[$classNameToken]['content'];
  158. } else {
  159. // Determine the name of the class that the static function
  160. // is being called on.
  161. $classNameToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
  162. $className = $tokens[$classNameToken]['content'];
  163. }
  164. // Some systems are always available.
  165. if (in_array(strtolower($className), $this->_ignore) === true) {
  166. return;
  167. }
  168. $includedClasses = array();
  169. $fileName = strtolower($phpcsFile->getFilename());
  170. $matches = array();
  171. if (preg_match('|/systems/([^/]+)/([^/]+)?actions.inc$|', $fileName, $matches) !== 0) {
  172. // This is an actions file, which means we don't
  173. // have to include the system in which it exists
  174. // We know the system from the path.
  175. $includedClasses[] = $matches[1];
  176. }
  177. // Go searching for includeSystem, includeAsset or require/include
  178. // calls outside our scope.
  179. for ($i = 0; $i < $stackPtr; $i++) {
  180. // Skip other scopes.
  181. if (isset($tokens[$i]['scope_closer']) === true) {
  182. $i = $tokens[$i]['scope_closer'];
  183. continue;
  184. }
  185. if (strtolower($tokens[$i]['content']) === 'includesystem') {
  186. $systemName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  187. $systemName = trim($tokens[$systemName]['content'], " '");
  188. $includedClasses[] = strtolower($systemName);
  189. } else if (strtolower($tokens[$i]['content']) === 'includeasset') {
  190. $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  191. $typeName = trim($tokens[$typeName]['content'], " '");
  192. $includedClasses[] = strtolower($typeName).'assettype';
  193. } else if (strtolower($tokens[$i]['content']) === 'includewidget') {
  194. $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  195. $typeName = trim($tokens[$typeName]['content'], " '");
  196. $includedClasses[] = strtolower($typeName).'widgettype';
  197. } else if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$includeTokens) === true) {
  198. $filePath = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
  199. $filePath = $tokens[$filePath]['content'];
  200. $filePath = trim($filePath, " '");
  201. $filePath = basename($filePath, '.inc');
  202. $includedClasses[] = strtolower($filePath);
  203. }
  204. }//end for
  205. if (in_array(strtolower($className), $includedClasses) === false) {
  206. if ($tokens[$stackPtr]['code'] === T_EXTENDS) {
  207. $error = "Class extends non-included class or system \"$className\"; include system with Channels::includeSystem() or include class with require_once";
  208. } else {
  209. $error = "Static method called on non-included class or system \"$className\"; include system with Channels::includeSystem() or include class with require_once";
  210. }
  211. $phpcsFile->addError($error, $stackPtr);
  212. }
  213. }//end processTokenOutsideScope()
  214. }//end class
  215. ?>