EmptyLineMissedSniff.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sniffs\Whitespace;
  7. use PHP_CodeSniffer\Sniffs\Sniff;
  8. use PHP_CodeSniffer\Files\File;
  9. /**
  10. * Class EmptyLineMissedSniff
  11. */
  12. class EmptyLineMissedSniff implements Sniff
  13. {
  14. /**
  15. * @inheritdoc
  16. */
  17. public function register()
  18. {
  19. return [T_DOC_COMMENT];
  20. }
  21. /**
  22. * @inheritdoc
  23. */
  24. public function process(File $phpcsFile, $stackPtr)
  25. {
  26. $tokens = $phpcsFile->getTokens();
  27. if ($this->doCheck($phpcsFile, $stackPtr, $tokens)) {
  28. $previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
  29. if ($tokens[$stackPtr]['line'] - $tokens[$previous]['line'] < 2) {
  30. $error = 'Empty line missed';
  31. $phpcsFile->addError($error, $stackPtr, '', null);
  32. }
  33. }
  34. }
  35. /**
  36. * Execute empty line missed check.
  37. *
  38. * @param File $phpcsFile
  39. * @param int $stackPtr
  40. * @param array $tokens
  41. * @return bool
  42. */
  43. private function doCheck(File $phpcsFile, $stackPtr, $tokens)
  44. {
  45. $result = false;
  46. if ($phpcsFile->hasCondition($stackPtr, T_CLASS) || $phpcsFile->hasCondition($stackPtr, T_INTERFACE)) {
  47. $result = true;
  48. }
  49. if ($phpcsFile->hasCondition($stackPtr, T_FUNCTION)) {
  50. $result = false;
  51. }
  52. $previous = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
  53. if ($tokens[$previous]['type'] === 'T_OPEN_CURLY_BRACKET') {
  54. $result = false;
  55. }
  56. if (strpos($tokens[$stackPtr]['content'], '/**') === false) {
  57. $result = false;
  58. }
  59. return $result;
  60. }
  61. }