LineLengthSniff.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sniffs\Files;
  7. use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff as FilesLineLengthSniff;
  8. /**
  9. * Line length sniff which ignores long lines in case they contain strings intended for translation.
  10. */
  11. class LineLengthSniff extends FilesLineLengthSniff
  12. {
  13. /**
  14. * Having previous line content allows to ignore long lines in case of multi-line declaration.
  15. *
  16. * @var string
  17. */
  18. protected $previousLineContent = '';
  19. /**
  20. * @inheritdoc
  21. */
  22. protected function checkLineLength($phpcsFile, $stackPtr, $lineContent)
  23. {
  24. $previousLineRegexp = '~__\($|\bPhrase\($~';
  25. $currentLineRegexp = '~__\(.+\)|\bPhrase\(.+\)~';
  26. $currentLineMatch = preg_match($currentLineRegexp, $lineContent) !== 0;
  27. $previousLineMatch = preg_match($previousLineRegexp, $this->previousLineContent) !== 0;
  28. $this->previousLineContent = $lineContent;
  29. if (! $currentLineMatch && !$previousLineMatch) {
  30. parent::checkLineLength($phpcsFile, $stackPtr, $lineContent);
  31. }
  32. }
  33. }