/php/pear/PHP/CodeSniffer/Standards/Squiz/Sniffs/Objects/ObjectMemberCommaSniff.php

https://gitlab.com/trang1104/portable_project · PHP · 84 lines · 25 code · 13 blank · 46 comment · 3 complexity · 28417e20253a8434eb59042cdb92851a MD5 · raw file

  1. <?php
  2. /**
  3. * Squiz_Sniffs_Objects_ObjectInstantiationSniff.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  12. * @link http://pear.php.net/package/PHP_CodeSniffer
  13. */
  14. /**
  15. * Squiz_Sniffs_Objects_ObjectInstantiationSniff.
  16. *
  17. * Ensures objects are assigned to a variable when instantiated.
  18. *
  19. * @category PHP
  20. * @package PHP_CodeSniffer
  21. * @author Greg Sherwood <gsherwood@squiz.net>
  22. * @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
  23. * @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
  24. * @version Release: 1.3.3
  25. * @link http://pear.php.net/package/PHP_CodeSniffer
  26. */
  27. class Squiz_Sniffs_Objects_ObjectMemberCommaSniff implements PHP_CodeSniffer_Sniff
  28. {
  29. /**
  30. * A list of tokenizers this sniff supports.
  31. *
  32. * @var array
  33. */
  34. public $supportedTokenizers = array('JS');
  35. /**
  36. * Registers the token types that this sniff wishes to listen to.
  37. *
  38. * @return array
  39. */
  40. public function register()
  41. {
  42. return array(T_CLOSE_CURLY_BRACKET);
  43. }//end register()
  44. /**
  45. * Process the tokens that this sniff is listening for.
  46. *
  47. * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found.
  48. * @param int $stackPtr The position in the stack where
  49. * the token was found.
  50. *
  51. * @return void
  52. */
  53. public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
  54. {
  55. $tokens = $phpcsFile->getTokens();
  56. // Only interested in orphaned braces (which are objects)
  57. // and object definitions.
  58. if (isset($tokens[$stackPtr]['scope_condition']) === true) {
  59. $condition = $tokens[$stackPtr]['scope_condition'];
  60. if ($tokens[$condition]['code'] !== T_OBJECT) {
  61. return;
  62. }
  63. }
  64. $prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
  65. if ($tokens[$prev]['code'] === T_COMMA) {
  66. $error = 'Last member of object must not be followed by a comma';
  67. $phpcsFile->addError($error, $prev, 'Missing');
  68. }
  69. }//end process()
  70. }//end class
  71. ?>