Skip to content

Fixed issue in line length sniff for phrase and __ keyword #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions Magento2/Sniffs/Files/LineLengthSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ class LineLengthSniff extends FilesLineLengthSniff
*/
protected $lastLineRegExp = '~__\(.+\)|\bPhrase\(.+\)~';

/**
* Regular expression for finding a __ keyword added in the translation string.
*
* @var string
*/
protected $underscoreKeywordRegExp = '~\'[^\']+\'(*SKIP)(*F)| __~';

/**
* Regular expression for finding a Phrase keyword added in the translation string.
*
* @var string
*/
protected $phaseKeywordRegExp = '~\'[^\']+\'(*SKIP)(*F)| \bPhrase~';

/**
* Having the next-to-last line content allows to ignore long lines in case of translations.
*
Expand Down Expand Up @@ -77,10 +91,13 @@ protected function checkLineLength($phpcsFile, $tokens, $stackPtr)
*/
protected function doesPreviousLineContainTranslationString()
{
$lastLineMatch = preg_match($this->lastLineRegExp, $this->lastLineContent) !== 0;
$nextToLastLineMatch = preg_match($this->nextToLastLineRegexp, $this->nextToLastLineContent) !== 0;

return $lastLineMatch || $nextToLastLineMatch;
if (preg_match($this->phaseKeywordRegExp, $this->lastLineContent) == 0
|| preg_match($this->underscoreKeywordRegExp, $this->lastLineContent) == 0
) {
$lastLineMatch = preg_match($this->lastLineRegExp, $this->lastLineContent) !== 0;
$nextToLastLineMatch = preg_match($this->nextToLastLineRegexp, $this->nextToLastLineContent) !== 0;
return $lastLineMatch || $nextToLastLineMatch;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions Magento2/Tests/Files/LineLengthUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ $phraseObjects = [
$test = '012344567890123445678901234456789012344567890123445678901234456789';
$test1 = '01234456789012344567890123445678901234456789012344567890123445678901234456789';
$test2 = '012344567890123445678901234456789012344567890123445678901234456789012344567890123445678901234456789';
$test3 = "If a string exceeds 120 characters and a part of the string is Phrase('xy'), this would not be caught by the regular expression because it matches any line containing Phrase('xy') or __('xy').";
1 change: 1 addition & 0 deletions Magento2/Tests/Files/LineLengthUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function getErrorList()
return [
9 => 1,
19 => 1,
32 => 1,
];
}

Expand Down