/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace/ObjectOperatorIndentSniff.php

https://gitlab.com/link233/bootmw · PHP · 193 lines · 110 code · 27 blank · 56 comment · 26 complexity · b98aafbdf047883a09cb90604da27451 MD5 · raw file

  1. <?php
  2. /**
  3. * PEAR_Sniffs_WhiteSpace_ObjectOperatorIndentSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12. * @link http://pear.php.net/package/PHP_CodeSniffer
  13. */
  14. /**
  15. * PEAR_Sniffs_WhiteSpace_ObjectOperatorIndentSniff.
  16. *
  17. * Checks that object operators are indented 4 spaces if they are the first
  18. * thing on a line.
  19. *
  20. * @category PHP
  21. * @package PHP_CodeSniffer
  22. * @author Greg Sherwood <gsherwood@squiz.net>
  23. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  24. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  25. * @version Release: @package_version@
  26. * @link http://pear.php.net/package/PHP_CodeSniffer
  27. */
  28. class PEAR_Sniffs_WhiteSpace_ObjectOperatorIndentSniff implements PHP_CodeSniffer_Sniff
  29. {
  30. /**
  31. * The number of spaces code should be indented.
  32. *
  33. * @var int
  34. */
  35. public $indent = 4;
  36. /**
  37. * Returns an array of tokens this test wants to listen for.
  38. *
  39. * @return array
  40. */
  41. public function register()
  42. {
  43. return array(T_OBJECT_OPERATOR);
  44. }//end register()
  45. /**
  46. * Processes this test, when one of its tokens is encountered.
  47. *
  48. * @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
  49. * @param int $stackPtr The position of the current token
  50. * in the stack passed in $tokens.
  51. *
  52. * @return void
  53. */
  54. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  55. {
  56. $tokens = $phpcsFile->getTokens();
  57. // Make sure this is the first object operator in a chain of them.
  58. $varToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
  59. if ($varToken === false || $tokens[$varToken]['code'] !== T_VARIABLE) {
  60. return;
  61. }
  62. // Make sure this is a chained call.
  63. $next = $phpcsFile->findNext(
  64. T_OBJECT_OPERATOR,
  65. ($stackPtr + 1),
  66. null,
  67. false,
  68. null,
  69. true
  70. );
  71. if ($next === false) {
  72. // Not a chained call.
  73. return;
  74. }
  75. // Determine correct indent.
  76. for ($i = ($varToken - 1); $i >= 0; $i--) {
  77. if ($tokens[$i]['line'] !== $tokens[$varToken]['line']) {
  78. $i++;
  79. break;
  80. }
  81. }
  82. $requiredIndent = 0;
  83. if ($i >= 0 && $tokens[$i]['code'] === T_WHITESPACE) {
  84. $requiredIndent = strlen($tokens[$i]['content']);
  85. }
  86. $requiredIndent += $this->indent;
  87. // Determine the scope of the original object operator.
  88. $origBrackets = null;
  89. if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
  90. $origBrackets = $tokens[$stackPtr]['nested_parenthesis'];
  91. }
  92. $origConditions = null;
  93. if (isset($tokens[$stackPtr]['conditions']) === true) {
  94. $origConditions = $tokens[$stackPtr]['conditions'];
  95. }
  96. // Check indentation of each object operator in the chain.
  97. // If the first object operator is on a different line than
  98. // the variable, make sure we check its indentation too.
  99. if ($tokens[$stackPtr]['line'] > $tokens[$varToken]['line']) {
  100. $next = $stackPtr;
  101. }
  102. while ($next !== false) {
  103. // Make sure it is in the same scope, otherwise don't check indent.
  104. $brackets = null;
  105. if (isset($tokens[$next]['nested_parenthesis']) === true) {
  106. $brackets = $tokens[$next]['nested_parenthesis'];
  107. }
  108. $conditions = null;
  109. if (isset($tokens[$next]['conditions']) === true) {
  110. $conditions = $tokens[$next]['conditions'];
  111. }
  112. if ($origBrackets === $brackets && $origConditions === $conditions) {
  113. // Make sure it starts a line, otherwise dont check indent.
  114. $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($next - 1), $stackPtr, true);
  115. $indent = $tokens[($next - 1)];
  116. if ($tokens[$prev]['line'] !== $tokens[$next]['line']
  117. && $indent['code'] === T_WHITESPACE
  118. ) {
  119. if ($indent['line'] === $tokens[$next]['line']) {
  120. $foundIndent = strlen($indent['content']);
  121. } else {
  122. $foundIndent = 0;
  123. }
  124. if ($foundIndent !== $requiredIndent) {
  125. $error = 'Object operator not indented correctly; expected %s spaces but found %s';
  126. $data = array(
  127. $requiredIndent,
  128. $foundIndent,
  129. );
  130. $fix = $phpcsFile->addFixableError($error, $next, 'Incorrect', $data);
  131. if ($fix === true) {
  132. $spaces = str_repeat(' ', $requiredIndent);
  133. if ($foundIndent === 0) {
  134. $phpcsFile->fixer->addContentBefore($next, $spaces);
  135. } else {
  136. $phpcsFile->fixer->replaceToken(($next - 1), $spaces);
  137. }
  138. }
  139. }
  140. }//end if
  141. // It cant be the last thing on the line either.
  142. $content = $phpcsFile->findNext(T_WHITESPACE, ($next + 1), null, true);
  143. if ($tokens[$content]['line'] !== $tokens[$next]['line']) {
  144. $error = 'Object operator must be at the start of the line, not the end';
  145. $fix = $phpcsFile->addFixableError($error, $next, 'StartOfLine');
  146. if ($fix === true) {
  147. $phpcsFile->fixer->beginChangeset();
  148. for ($x = ($next + 1); $x < $content; $x++) {
  149. $phpcsFile->fixer->replaceToken($x, '');
  150. }
  151. $phpcsFile->fixer->addNewlineBefore($next);
  152. $phpcsFile->fixer->endChangeset();
  153. }
  154. }
  155. }//end if
  156. $next = $phpcsFile->findNext(
  157. T_OBJECT_OPERATOR,
  158. ($next + 1),
  159. null,
  160. false,
  161. null,
  162. true
  163. );
  164. }//end while
  165. }//end process()
  166. }//end class