55 */
66namespace Magento2 \Sniffs \Files ;
77
8+ use PHP_CodeSniffer \Files \File ;
89use PHP_CodeSniffer \Standards \Generic \Sniffs \Files \LineLengthSniff as FilesLineLengthSniff ;
910
1011/**
1314class LineLengthSniff extends FilesLineLengthSniff
1415{
1516 /**
16- * Having previous line content allows to ignore long lines in case of multi-line declaration .
17+ * Having previous line content allows to ignore long lines in case of translations .
1718 *
1819 * @var string
1920 */
20- protected $ previousLineContent = '' ;
21+ protected $ lastLineContent = '' ;
22+
23+ /**
24+ * Regular expression for finding a translation in the last line.
25+ *
26+ * @var string
27+ */
28+ protected $ lastLineRegExp = '~__\(.+\)|\bPhrase\(.+\)~ ' ;
29+
30+ /**
31+ * Having the next-to-last line content allows to ignore long lines in case of translations.
32+ *
33+ * @var string
34+ */
35+ protected $ nextToLastLineContent = '' ;
36+
37+ /**
38+ * Regular expression for finding a translation in the next-to-last line.
39+ *
40+ * @var string
41+ */
42+ protected $ nextToLastLineRegexp = '~__\($|\bPhrase\($~ ' ;
2143
2244 /**
2345 * @inheritdoc
@@ -32,15 +54,79 @@ class LineLengthSniff extends FilesLineLengthSniff
3254 /**
3355 * @inheritdoc
3456 */
35- protected function checkLineLength ($ phpcsFile , $ stackPtr , $ lineContent )
57+ protected function checkLineLength ($ phpcsFile , $ tokens , $ stackPtr )
58+ {
59+ /*
60+ * The parent sniff checks the length of the previous line, so we have to inspect the previous line instead of
61+ * the current one.
62+ */
63+ if (!$ this ->doesPreviousLineContainTranslationString ()) {
64+ parent ::checkLineLength ($ phpcsFile , $ tokens , $ stackPtr );
65+ }
66+
67+ $ this ->updateLineBuffer ($ phpcsFile , $ tokens , $ stackPtr );
68+ }
69+
70+ /**
71+ * Checks whether the previous line is part of a translation.
72+ *
73+ * The generic line sniff (which we are falling back to if there is no translation) always checks the length of the
74+ * last line, so we have to check the last and next-to-last line for translations.
75+ *
76+ * @return bool
77+ */
78+ protected function doesPreviousLineContainTranslationString ()
3679 {
37- $ previousLineRegexp = '~__\($|\bPhrase\($~ ' ;
38- $ currentLineRegexp = '~__\(.+\)|\bPhrase\(.+\)~ ' ;
39- $ currentLineMatch = preg_match ($ currentLineRegexp , $ lineContent ) !== 0 ;
40- $ previousLineMatch = preg_match ($ previousLineRegexp , $ this ->previousLineContent ) !== 0 ;
41- $ this ->previousLineContent = $ lineContent ;
42- if (! $ currentLineMatch && !$ previousLineMatch ) {
43- parent ::checkLineLength ($ phpcsFile , $ stackPtr , $ lineContent );
80+ $ lastLineMatch = preg_match ($ this ->lastLineRegExp , $ this ->lastLineContent ) !== 0 ;
81+ $ nextToLastLineMatch = preg_match ($ this ->nextToLastLineRegexp , $ this ->nextToLastLineContent ) !== 0 ;
82+
83+ return $ lastLineMatch || $ nextToLastLineMatch ;
84+ }
85+
86+ /**
87+ * Assembles and returns the content for the code line of the provided stack pointer.
88+ *
89+ * @param File $phpcsFile
90+ * @param array $tokens
91+ * @param int $stackPtr
92+ * @return string
93+ */
94+ protected function getLineContent (File $ phpcsFile , array $ tokens , $ stackPtr )
95+ {
96+ $ lineContent = '' ;
97+
98+ /*
99+ * Avoid out of range error at the end of the file
100+ */
101+ if (!array_key_exists ($ stackPtr , $ tokens )) {
102+ return $ lineContent ;
44103 }
104+
105+ $ codeLine = $ tokens [$ stackPtr ]['line ' ];
106+
107+ /*
108+ * Concatenate the string until we jump to the next line or reach the end of line character.
109+ */
110+ while (array_key_exists ($ stackPtr , $ tokens ) &&
111+ $ tokens [$ stackPtr ]['line ' ] === $ codeLine &&
112+ $ tokens [$ stackPtr ]['content ' ] !== $ phpcsFile ->eolChar ) {
113+ $ lineContent .= $ tokens [$ stackPtr ]['content ' ];
114+ $ stackPtr ++;
115+ }
116+
117+ return $ lineContent ;
118+ }
119+
120+ /**
121+ * Pre-fills the line buffer for the next iteration.
122+ *
123+ * @param File $phpcsFile
124+ * @param array $tokens
125+ * @param int $stackPtr
126+ */
127+ protected function updateLineBuffer (File $ phpcsFile , array $ tokens , $ stackPtr )
128+ {
129+ $ this ->nextToLastLineContent = $ this ->lastLineContent ;
130+ $ this ->lastLineContent = $ this ->getLineContent ($ phpcsFile , $ tokens , $ stackPtr );
45131 }
46132}
0 commit comments