PageRenderTime 72ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 98 lines | 39 code | 16 blank | 43 comment | 6 complexity | 4e4f1aaf9ce61fa4e11fe3923f0faea6 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. * Squiz_Sniffs_Whitespace_ScopeIndentSniff.
  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: ScopeIndentSniff.php 254096 2008-03-03 03:28:15Z squiz $
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. if (class_exists('Generic_Sniffs_WhiteSpace_ScopeIndentSniff', true) === false)
  17. {
  18. throw new PHP_CodeSniffer_Exception('Class Generic_Sniffs_WhiteSpace_ScopeIndentSniff not found');
  19. }
  20. /**
  21. * Squiz_Sniffs_Whitespace_ScopeIndentSniff.
  22. *
  23. * Checks that control structures are structured correctly, and their content
  24. * is indented correctly.
  25. *
  26. * @category PHP
  27. * @package PHP_CodeSniffer
  28. * @author Greg Sherwood <gsherwood@squiz.net>
  29. * @author Marc McIntyre <mmcintyre@squiz.net>
  30. * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
  31. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  32. * @version Release: 1.2.2
  33. * @link http://pear.php.net/package/PHP_CodeSniffer
  34. */
  35. class Squiz_Sniffs_WhiteSpace_ScopeIndentSniff extends Generic_Sniffs_WhiteSpace_ScopeIndentSniff
  36. {
  37. /**
  38. * Calculates the expected indent of a token.
  39. *
  40. * Looks for ob_start() calls because those act as scope openers in the
  41. * Squiz coding standard, and so require additional indentation.
  42. *
  43. * @param array $tokens The stack of tokens for this file.
  44. * @param int $stackPtr The position of the token to get indent for.
  45. *
  46. * @return int
  47. */
  48. protected function calculateExpectedIndent(array $tokens, $stackPtr)
  49. {
  50. $expectedIndent = parent :: calculateExpectedIndent($tokens, $stackPtr);
  51. // If we are in a function, check all tokens to the start of the
  52. // function. If we are not in a function, check all tokens to the
  53. // start of the file.
  54. $checkTo = 0;
  55. $tokenConditions = $tokens[$stackPtr]['conditions'];
  56. foreach ($tokenConditions as $id => $condition)
  57. {
  58. if ($condition === T_FUNCTION)
  59. {
  60. $checkTo = ($tokens[$id]['scope_opener'] + 1);
  61. }
  62. }
  63. for($i = ($stackPtr - 1); $i >= $checkTo; $i --)
  64. {
  65. if ($tokens[$i]['code'] !== T_STRING)
  66. {
  67. continue;
  68. }
  69. if ($tokens[$i]['content'] === 'ob_start')
  70. {
  71. $expectedIndent += $this->indent;
  72. }
  73. $bufferClosers = array('ob_end_clean', 'ob_end_flush', 'ob_get_clean', 'ob_get_flush');
  74. if (in_array($tokens[$i]['content'], $bufferClosers) === true)
  75. {
  76. $expectedIndent -= $this->indent;
  77. }
  78. } //end for
  79. return $expectedIndent;
  80. } //end calculateExpectedIndent()
  81. } //end class
  82. ?>