PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/script/lib/PHP/CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 229 lines | 138 code | 27 blank | 64 comment | 30 complexity | 5ac8713977b93f46fe830035d80cfb20 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * Sniffs_Squiz_WhiteSpace_OperatorSpacingSniff.
  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 Squiz Pty Ltd (ABN 77 084 670 600)
  12. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  13. * @version CVS: $Id: OperatorSpacingSniff.php 291903 2009-12-09 00:55:05Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. /**
  17. * Sniffs_Squiz_WhiteSpace_OperatorSpacingSniff.
  18. *
  19. * Verifies that operators have valid spacing surrounding them.
  20. *
  21. * @category PHP
  22. * @package PHP_CodeSniffer
  23. * @author Greg Sherwood <gsherwood@squiz.net>
  24. * @author Marc McIntyre <mmcintyre@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.2
  28. * @link http://pear.php.net/package/PHP_CodeSniffer
  29. */
  30. class Squiz_Sniffs_WhiteSpace_OperatorSpacingSniff implements PHP_CodeSniffer_Sniff
  31. {
  32. /**
  33. * A list of tokenizers this sniff supports.
  34. *
  35. * @var array
  36. */
  37. public $supportedTokenizers = array('PHP', 'JS');
  38. /**
  39. * Returns an array of tokens this test wants to listen for.
  40. *
  41. * @return array
  42. */
  43. public function register()
  44. {
  45. $comparison = PHP_CodeSniffer_Tokens :: $comparisonTokens;
  46. $operators = PHP_CodeSniffer_Tokens :: $operators;
  47. $assignment = PHP_CodeSniffer_Tokens :: $assignmentTokens;
  48. return array_unique(array_merge($comparison, $operators, $assignment));
  49. } //end register()
  50. /**
  51. * Processes this sniff, when one of its tokens is encountered.
  52. *
  53. * @param PHP_CodeSniffer_File $phpcsFile The current file being checked.
  54. * @param int $stackPtr The position of the current token in the
  55. * stack passed in $tokens.
  56. *
  57. * @return void
  58. */
  59. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  60. {
  61. $tokens = $phpcsFile->getTokens();
  62. // Skip default values in function declarations.
  63. if ($tokens[$stackPtr]['code'] === T_EQUAL || $tokens[$stackPtr]['code'] === T_MINUS)
  64. {
  65. if (isset($tokens[$stackPtr]['nested_parenthesis']) === true)
  66. {
  67. $bracket = end($tokens[$stackPtr]['nested_parenthesis']);
  68. if (isset($tokens[$bracket]['parenthesis_owner']) === true)
  69. {
  70. $function = $tokens[$bracket]['parenthesis_owner'];
  71. if ($tokens[$function]['code'] === T_FUNCTION)
  72. {
  73. return;
  74. }
  75. }
  76. }
  77. }
  78. if ($tokens[$stackPtr]['code'] === T_EQUAL)
  79. {
  80. // Skip for '=&' case.
  81. if (isset($tokens[($stackPtr + 1)]) === true && $tokens[($stackPtr + 1)]['code'] === T_BITWISE_AND)
  82. {
  83. return;
  84. }
  85. }
  86. if ($tokens[$stackPtr]['code'] === T_BITWISE_AND)
  87. {
  88. // If its not a reference, then we expect one space either side of the
  89. // bitwise operator.
  90. if ($phpcsFile->isReference($stackPtr) === false)
  91. {
  92. // Check there is one space before the & operator.
  93. if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE)
  94. {
  95. $error = 'Expected 1 space before "&" operator; 0 found';
  96. $phpcsFile->addError($error, $stackPtr);
  97. }
  98. else
  99. {
  100. if (strlen($tokens[($stackPtr - 1)]['content']) !== 1)
  101. {
  102. $found = strlen($tokens[($stackPtr - 1)]['content']);
  103. $error = "Expected 1 space before \"&\" operator; $found found";
  104. $phpcsFile->addError($error, $stackPtr);
  105. }
  106. }
  107. // Check there is one space after the & operator.
  108. if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE)
  109. {
  110. $error = 'Expected 1 space after "&" operator; 0 found';
  111. $phpcsFile->addError($error, $stackPtr);
  112. }
  113. else
  114. {
  115. if (strlen($tokens[($stackPtr + 1)]['content']) !== 1)
  116. {
  117. $found = strlen($tokens[($stackPtr + 1)]['content']);
  118. $error = "Expected 1 space after \"&\" operator; $found found";
  119. $phpcsFile->addError($error, $stackPtr);
  120. }
  121. }
  122. } //end if
  123. }
  124. else
  125. {
  126. if ($tokens[$stackPtr]['code'] === T_MINUS)
  127. {
  128. // Check minus spacing, but make sure we aren't just assigning
  129. // a minus value or returning one.
  130. $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
  131. if ($tokens[$prev]['code'] === T_RETURN)
  132. {
  133. // Just returning a negative value; eg. return -1.
  134. return;
  135. }
  136. if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens :: $operators) === true)
  137. {
  138. // Just trying to operate on a negative value; eg. ($var * -1).
  139. return;
  140. }
  141. if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens :: $comparisonTokens) === true)
  142. {
  143. // Just trying to compare a negative value; eg. ($var === -1).
  144. return;
  145. }
  146. // A list of tokens that indicate that the token is not
  147. // part of an arithmetic operation.
  148. $invalidTokens = array(T_COMMA, T_OPEN_PARENTHESIS,
  149. T_OPEN_SQUARE_BRACKET, T_DOUBLE_ARROW, T_COLON);
  150. if (in_array($tokens[$prev]['code'], $invalidTokens) === true)
  151. {
  152. // Just trying to use a negative value; eg. myFunction($var, -2).
  153. return;
  154. }
  155. $number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
  156. if (in_array($tokens[$number]['code'], array(T_LNUMBER, T_VARIABLE)) === true)
  157. {
  158. $semi = $phpcsFile->findNext(T_WHITESPACE, ($number + 1), null, true);
  159. if ($tokens[$semi]['code'] === T_SEMICOLON)
  160. {
  161. if ($prev !== false && (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens :: $assignmentTokens) === true))
  162. {
  163. // This is a negative assignment.
  164. return;
  165. }
  166. }
  167. }
  168. } //end if
  169. $operator = $tokens[$stackPtr]['content'];
  170. if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE)
  171. {
  172. $error = "Expected 1 space before \"$operator\"; 0 found";
  173. $phpcsFile->addError($error, $stackPtr);
  174. }
  175. else
  176. if (strlen($tokens[($stackPtr - 1)]['content']) !== 1)
  177. {
  178. // Don't throw an error for assignments, because other standards allow
  179. // multiple spaces there to align multiple assignments.
  180. if (in_array($tokens[$stackPtr]['code'], PHP_CodeSniffer_Tokens :: $assignmentTokens) === false)
  181. {
  182. $found = strlen($tokens[($stackPtr - 1)]['content']);
  183. $error = "Expected 1 space before \"$operator\"; $found found";
  184. $phpcsFile->addError($error, $stackPtr);
  185. }
  186. }
  187. if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE)
  188. {
  189. $error = "Expected 1 space after \"$operator\"; 0 found";
  190. $phpcsFile->addError($error, $stackPtr);
  191. }
  192. else
  193. if (strlen($tokens[($stackPtr + 1)]['content']) !== 1)
  194. {
  195. $found = strlen($tokens[($stackPtr + 1)]['content']);
  196. $error = "Expected 1 space after \"$operator\"; $found found";
  197. $phpcsFile->addError($error, $stackPtr);
  198. }
  199. } //end if
  200. } //end process()
  201. } //end class
  202. ?>