diff --git a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php index 816c5a07bc..0be6118e43 100644 --- a/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php @@ -24,7 +24,10 @@ class EmbeddedPhpSniff implements Sniff */ public function register() { - return [T_OPEN_TAG]; + return [ + T_OPEN_TAG, + T_OPEN_TAG_WITH_ECHO, + ]; }//end register() @@ -46,9 +49,9 @@ public function process(File $phpcsFile, $stackPtr) // then we have an inline embedded PHP block. $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); if ($closeTag === false || $tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) { - $this->validateMultilineEmbeddedPhp($phpcsFile, $stackPtr); + $this->validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag); } else { - $this->validateInlineEmbeddedPhp($phpcsFile, $stackPtr); + $this->validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag); } }//end process() @@ -57,44 +60,40 @@ public function process(File $phpcsFile, $stackPtr) /** * Validates embedded PHP that exists on multiple lines. * - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token in the - * stack passed in $tokens. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the + * stack passed in $tokens. + * @param int|false $closingTag The position of the PHP close tag in the + * stack passed in $tokens. * * @return void */ - private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) + private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag) { $tokens = $phpcsFile->getTokens(); - $prevTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); + $prevTag = $phpcsFile->findPrevious($this->register(), ($stackPtr - 1)); if ($prevTag === false) { // This is the first open tag. return; } $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); - $closingTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr); + if ($firstContent === false) { + // Unclosed PHP open tag at the end of a file. Nothing to do. + return; + } + if ($closingTag !== false) { - $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), $phpcsFile->numTokens, true); - if ($nextContent === false) { + $firstContentAfterBlock = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), $phpcsFile->numTokens, true); + if ($firstContentAfterBlock === false) { // Final closing tag. It will be handled elsewhere. return; } // We have an opening and a closing tag, that lie within other content. if ($firstContent === $closingTag) { - $error = 'Empty embedded PHP tag found'; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); - if ($fix === true) { - $phpcsFile->fixer->beginChangeset(); - for ($i = $stackPtr; $i <= $closingTag; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); - } - + $this->reportEmptyTagSet($phpcsFile, $stackPtr, $closingTag); return; } }//end if @@ -105,9 +104,16 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) if ($fix === true) { $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true); $padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); + $phpcsFile->fixer->beginChangeset(); + $phpcsFile->fixer->replaceToken($stackPtr, rtrim($tokens[$stackPtr]['content'])); $phpcsFile->fixer->addNewline($stackPtr); $phpcsFile->fixer->addContent($stackPtr, str_repeat(' ', $padding)); + + if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); + } + $phpcsFile->fixer->endChangeset(); } } else { @@ -141,10 +147,13 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) } }//end if - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); + $indent = 0; + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); if ($first === false) { - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); - $indent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); + $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); + if ($first !== false) { + $indent = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); + } } else { $indent = ($tokens[($first + 1)]['column'] - 1); } @@ -169,17 +178,20 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) }//end if }//end if - $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); - if ($tokens[$lastContent]['line'] === $tokens[$stackPtr]['line'] - && trim($tokens[$lastContent]['content']) !== '' + $lastContentBeforeBlock = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); + if ($tokens[$lastContentBeforeBlock]['line'] === $tokens[$stackPtr]['line'] + && trim($tokens[$lastContentBeforeBlock]['content']) !== '' ) { $error = 'Opening PHP tag must be on a line by itself'; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ContentBeforeOpen'); if ($fix === true) { - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); + $padding = 0; + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr); if ($first === false) { - $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); - $padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); + $first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr); + if ($first !== false) { + $padding = (strlen($tokens[$first]['content']) - strlen(ltrim($tokens[$first]['content']))); + } } else { $padding = ($tokens[($first + 1)]['column'] - 1); } @@ -188,11 +200,19 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) } } else { // Find the first token on the first non-empty line we find. - for ($first = ($stackPtr - 1); $first > 0; $first--) { + for ($first = ($lastContentBeforeBlock - 1); $first > 0; $first--) { if ($tokens[$first]['line'] === $tokens[$stackPtr]['line']) { continue; } else if (trim($tokens[$first]['content']) !== '') { $first = $phpcsFile->findFirstOnLine([], $first, true); + if ($tokens[$first]['code'] === T_COMMENT + && $tokens[$first]['content'] !== ltrim($tokens[$first]['content']) + ) { + // This is a subsequent line in a star-slash comment containing leading indent. + // We'll need the first line of the comment to correctly determine the indent. + continue; + } + break; } } @@ -225,20 +245,48 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) return; } - $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closingTag - 1), ($stackPtr + 1), true); - $nextContent = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), null, true); + $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($closingTag - 1), ($stackPtr + 1), true); + $firstContentAfterBlock = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), null, true); if ($tokens[$lastContent]['line'] === $tokens[$closingTag]['line']) { $error = 'Closing PHP tag must be on a line by itself'; $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentBeforeEnd'); if ($fix === true) { - $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true); + // Calculate the indent for the close tag. + // If the close tag is on the same line as the first content, re-use the indent + // calculated for the first content line to prevent the indent being based on an + // "old" indent, not the _new_ (fixed) indent. + if ($tokens[$firstContent]['line'] === $tokens[$lastContent]['line'] + && isset($indent) === true + ) { + $closerIndent = $indent; + } else { + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $closingTag, true); + + while ($tokens[$first]['code'] === T_COMMENT + && $tokens[$first]['content'] !== ltrim($tokens[$first]['content']) + ) { + // This is a subsequent line in a star-slash comment containing leading indent. + // We'll need the first line of the comment to correctly determine the indent. + $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, ($first - 1), true); + } + + $closerIndent = ($tokens[$first]['column'] - 1); + } + $phpcsFile->fixer->beginChangeset(); - $phpcsFile->fixer->addContentBefore($closingTag, str_repeat(' ', ($tokens[$first]['column'] - 1))); + + if ($tokens[($closingTag - 1)]['code'] === T_WHITESPACE) { + $phpcsFile->fixer->replaceToken(($closingTag - 1), ''); + } + + $phpcsFile->fixer->addContentBefore($closingTag, str_repeat(' ', $closerIndent)); $phpcsFile->fixer->addNewlineBefore($closingTag); $phpcsFile->fixer->endChangeset(); - } - } else if ($tokens[$nextContent]['line'] === $tokens[$closingTag]['line']) { + }//end if + } else if ($firstContentAfterBlock !== false + && $tokens[$firstContentAfterBlock]['line'] === $tokens[$closingTag]['line'] + ) { $error = 'Closing PHP tag must be on a line by itself'; $fix = $phpcsFile->addFixableError($error, $closingTag, 'ContentAfterEnd'); if ($fix === true) { @@ -246,11 +294,32 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) $phpcsFile->fixer->beginChangeset(); $phpcsFile->fixer->addNewline($closingTag); $phpcsFile->fixer->addContent($closingTag, str_repeat(' ', ($tokens[$first]['column'] - 1))); + + if ($tokens[$firstContentAfterBlock]['code'] === T_INLINE_HTML) { + $trimmedHtmlContent = ltrim($tokens[$firstContentAfterBlock]['content']); + if ($trimmedHtmlContent === '') { + // HTML token contains only whitespace and the next token after is PHP, not HTML, so remove the whitespace. + $phpcsFile->fixer->replaceToken($firstContentAfterBlock, ''); + } else { + // The HTML token has content, so remove leading whitespace in favour of the indent. + $phpcsFile->fixer->replaceToken($firstContentAfterBlock, $trimmedHtmlContent); + } + } + + if ($tokens[$firstContentAfterBlock]['code'] === T_OPEN_TAG + || $tokens[$firstContentAfterBlock]['code'] === T_OPEN_TAG_WITH_ECHO + ) { + // Next token is a PHP open tag which will also have thrown an error. + // Prevent both fixers running in the same loop by making sure the token is "touched" during this loop. + // This prevents a stray new line being added between the close and open tags. + $phpcsFile->fixer->replaceToken($firstContentAfterBlock, $tokens[$firstContentAfterBlock]['content']); + } + $phpcsFile->fixer->endChangeset(); - } + }//end if }//end if - $next = $phpcsFile->findNext(T_OPEN_TAG, ($closingTag + 1)); + $next = $phpcsFile->findNext($this->register(), ($closingTag + 1)); if ($next === false) { return; } @@ -293,42 +362,31 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr) * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. * @param int $stackPtr The position of the current token in the * stack passed in $tokens. + * @param int $closeTag The position of the PHP close tag in the + * stack passed in $tokens. * * @return void */ - private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr) + private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag) { $tokens = $phpcsFile->getTokens(); - // We only want one line PHP sections, so return if the closing tag is - // on the next line. - $closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr, null, false); - if ($tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) { - return; - } - - // Check that there is one, and only one space at the start of the statement. $firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $closeTag, true); if ($firstContent === false) { - $error = 'Empty embedded PHP tag found'; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); - if ($fix === true) { - $phpcsFile->fixer->beginChangeset(); - for ($i = $stackPtr; $i <= $closeTag; $i++) { - $phpcsFile->fixer->replaceToken($i, ''); - } - - $phpcsFile->fixer->endChangeset(); - } - + $this->reportEmptyTagSet($phpcsFile, $stackPtr, $closeTag); return; } - // The open tag token always contains a single space after it. - $leadingSpace = 1; + // Check that there is one, and only one space at the start of the statement. + $leadingSpace = 0; + if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { + // The long open tag token in a single line tag set always contains a single space after it. + $leadingSpace = 1; + } + if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { - $leadingSpace = ($tokens[($stackPtr + 1)]['length'] + 1); + $leadingSpace += $tokens[($stackPtr + 1)]['length']; } if ($leadingSpace !== 1) { @@ -336,7 +394,15 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr) $data = [$leadingSpace]; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterOpen', $data); if ($fix === true) { - $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); + if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) { + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); + } else if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { + // Short open tag with too much whitespace. + $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); + } else { + // Short open tag without whitespace. + $phpcsFile->fixer->addContent($stackPtr, ' '); + } } } @@ -349,7 +415,12 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr) && $tokens[$prev]['code'] !== T_SEMICOLON ) { $error = 'Inline PHP statement must end with a semicolon'; - $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoSemicolon'); + $code = 'NoSemicolon'; + if ($tokens[$stackPtr]['code'] === T_OPEN_TAG_WITH_ECHO) { + $code = 'ShortOpenEchoNoSemicolon'; + } + + $fix = $phpcsFile->addFixableError($error, $stackPtr, $code); if ($fix === true) { $phpcsFile->fixer->addContent($prev, ';'); } @@ -366,7 +437,7 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr) $data = [$statementCount]; $phpcsFile->addError($error, $stackPtr, 'MultipleStatements', $data); } - } + }//end if }//end if $trailingSpace = 0; @@ -399,4 +470,43 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr) }//end validateInlineEmbeddedPhp() + /** + * Report and fix an set of empty PHP tags. + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the + * stack passed in $tokens. + * @param int $closeTag The position of the PHP close tag in the + * stack passed in $tokens. + * + * @return void + */ + private function reportEmptyTagSet(File $phpcsFile, $stackPtr, $closeTag) + { + $tokens = $phpcsFile->getTokens(); + $error = 'Empty embedded PHP tag found'; + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'Empty'); + if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + for ($i = $stackPtr; $i <= $closeTag; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + // Prevent leaving indentation whitespace behind when the empty tag set is the only thing on the affected lines. + if (isset($tokens[($closeTag + 1)]) === true + && $tokens[($closeTag + 1)]['line'] !== $tokens[$closeTag]['line'] + && $tokens[($stackPtr - 1)]['code'] === T_INLINE_HTML + && $tokens[($stackPtr - 1)]['line'] === $tokens[$stackPtr]['line'] + && $tokens[($stackPtr - 1)]['column'] === 1 + && trim($tokens[($stackPtr - 1)]['content']) === '' + ) { + $phpcsFile->fixer->replaceToken(($stackPtr - 1), ''); + } + + $phpcsFile->fixer->endChangeset(); + } + + }//end reportEmptyTagSet() + + }//end class diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc new file mode 100644 index 0000000000..f28c559894 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.1.inc @@ -0,0 +1,271 @@ + + + +<?php echo $title ?> + + + + + hello + + + + + + + + + + + + + + + + + + + + + +section as $section) { + ?> + + + + + + section as $section) { + ?> +
+ + + + + + + + +?> + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+ + + + +
+ + + + + + + + + + + + + + + + +<?php echo $title; ?> + + + + + hello + + + + + + + + + + + + + + + + + + + +section as $section) { + ?> +
+ + + + + section as $section) { + ?> +
+ + + + + + + + + + + + +?> + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.15.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.15.inc new file mode 100644 index 0000000000..d8f0512773 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.15.inc @@ -0,0 +1,9 @@ + + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.16.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.16.inc new file mode 100644 index 0000000000..754c241b8e --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.16.inc @@ -0,0 +1,8 @@ + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.17.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.17.inc new file mode 100644 index 0000000000..71de17bfb4 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.17.inc @@ -0,0 +1,8 @@ + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.18.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.18.inc new file mode 100644 index 0000000000..3d61d4859d --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.18.inc @@ -0,0 +1,15 @@ + + +
+ + +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.18.inc.fixed b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.18.inc.fixed new file mode 100644 index 0000000000..8c906d40c7 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.18.inc.fixed @@ -0,0 +1,13 @@ + + +
+ +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.19.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.19.inc new file mode 100644 index 0000000000..34db64d605 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.19.inc @@ -0,0 +1,17 @@ + + +
+ + +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.19.inc.fixed b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.19.inc.fixed new file mode 100644 index 0000000000..b1738db57f --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.19.inc.fixed @@ -0,0 +1,15 @@ + + +
+ +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.2.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.2.inc new file mode 100644 index 0000000000..755fd355b5 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.2.inc @@ -0,0 +1,7 @@ + + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.2.inc.fixed b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.2.inc.fixed new file mode 100644 index 0000000000..e9073da915 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.2.inc.fixed @@ -0,0 +1,7 @@ + + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.20.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.20.inc new file mode 100644 index 0000000000..28cdbdaf7a --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.20.inc @@ -0,0 +1,15 @@ + + +
+ + +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.20.inc.fixed b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.20.inc.fixed new file mode 100644 index 0000000000..fcc24a278e --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.20.inc.fixed @@ -0,0 +1,16 @@ + + +
+ + +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.21.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.21.inc new file mode 100644 index 0000000000..1da67a2ea7 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.21.inc @@ -0,0 +1,15 @@ + + +
+ + +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.21.inc.fixed b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.21.inc.fixed new file mode 100644 index 0000000000..d7487b507d --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.21.inc.fixed @@ -0,0 +1,16 @@ + + +
+ + +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.22.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.22.inc new file mode 100644 index 0000000000..5196a3b45b --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.22.inc @@ -0,0 +1,30 @@ +
Inline HTML with indent to demonstrate the bug in the indent calculation.
+ + + + + + + + +Inline HTML with indent to demonstrate the bug in the indent calculation.
+ + + + + + + + + + + +
+ + +
+

Some more content after the last PHP tag block.

diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.3.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.3.inc new file mode 100644 index 0000000000..2d608490a8 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.3.inc @@ -0,0 +1,123 @@ + + + +<?= $title ?> + + + + + hello + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +// Safeguard fixing when there is no whitespace between the close tag and the contents. + + + + + + + + + +<?= $title; ?> + + + + + hello + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// Safeguard fixing when there is no whitespace between the close tag and the contents. + + + + + + + + + + + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.4.inc.fixed b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.4.inc.fixed new file mode 100644 index 0000000000..b7985cc508 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.4.inc.fixed @@ -0,0 +1,7 @@ + + + + diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.5.inc b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.5.inc new file mode 100644 index 0000000000..f4d8d8a0c0 --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.5.inc @@ -0,0 +1,48 @@ + + + + + + + +
+ + + + + + + +
+ + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + - - -<?php echo $title ?> - - - - - hello - - - - - - - - - - - - - - - - - - - - - -section as $section) { - ?> -
- - - - - section as $section) { - ?> -
- - - - - - - - -?> - - - - - - - - - - - - - - - diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.inc.fixed b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.inc.fixed deleted file mode 100644 index 5b43d8493d..0000000000 --- a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.inc.fixed +++ /dev/null @@ -1,119 +0,0 @@ - - - -<?php echo $title; ?> - - - - - hello - - - - - - - - - - - - - - - - - - - -section as $section) { - ?> -
- - - - - section as $section) { - ?> -
- - - - - - - - - - - - -?> - - - - - - - - - - - - - - - diff --git a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.php b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.php index 8944fc2aee..29111c8736 100644 --- a/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.php +++ b/src/Standards/Squiz/Tests/PHP/EmbeddedPhpUnitTest.php @@ -26,41 +26,173 @@ final class EmbeddedPhpUnitTest extends AbstractSniffUnitTest * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * + * @param string $testFile The name of the file being tested. + * * @return array */ - public function getErrorList() + public function getErrorList($testFile='') { - return [ - 7 => 1, - 12 => 1, - 18 => 1, - 19 => 2, - 20 => 1, - 21 => 1, - 22 => 3, - 24 => 1, - 26 => 1, - 29 => 1, - 30 => 1, - 31 => 1, - 34 => 1, - 36 => 1, - 40 => 1, - 41 => 1, - 44 => 1, - 45 => 1, - 49 => 1, - 59 => 1, - 63 => 1, - 93 => 1, - 94 => 2, - 100 => 1, - 102 => 1, - 112 => 1, - 113 => 1, - 116 => 1, - 117 => 1, - ]; + switch ($testFile) { + case 'EmbeddedPhpUnitTest.1.inc': + return [ + 7 => 1, + 12 => 1, + 18 => 1, + 19 => 2, + 20 => 1, + 21 => 1, + 22 => 3, + 24 => 1, + 26 => 1, + 29 => 1, + 30 => 1, + 31 => 1, + 34 => 1, + 36 => 1, + 40 => 1, + 41 => 1, + 44 => 1, + 45 => 1, + 49 => 1, + 59 => 1, + 63 => 1, + 93 => 1, + 94 => 2, + 100 => 1, + 102 => 1, + 112 => 1, + 113 => 1, + 116 => 1, + 117 => 1, + 120 => 1, + 121 => 1, + 128 => 1, + 129 => 1, + 132 => 1, + 134 => 1, + 136 => 1, + 138 => 1, + 142 => 1, + 145 => 1, + 151 => 1, + 158 => 1, + 165 => 1, + 169 => 1, + 175 => 1, + 176 => 2, + 178 => 1, + 179 => 1, + 180 => 2, + 181 => 1, + 189 => 1, + 212 => 1, + 214 => 2, + 219 => 1, + 223 => 1, + 225 => 1, + 226 => 1, + 227 => 2, + 228 => 1, + 235 => 1, + 241 => 1, + 248 => 1, + 253 => 1, + 258 => 1, + 263 => 1, + 264 => 1, + ]; + + case 'EmbeddedPhpUnitTest.2.inc': + case 'EmbeddedPhpUnitTest.4.inc': + return [ + 5 => 2, + 6 => 2, + 7 => 2, + ]; + + case 'EmbeddedPhpUnitTest.3.inc': + return [ + 10 => 1, + 15 => 1, + 21 => 1, + 22 => 2, + 23 => 1, + 24 => 1, + 25 => 3, + 28 => 1, + 29 => 1, + 30 => 1, + 33 => 1, + 35 => 1, + 39 => 1, + 40 => 1, + 43 => 1, + 44 => 1, + 48 => 1, + 53 => 1, + 55 => 1, + 61 => 1, + 62 => 1, + 65 => 2, + 66 => 2, + 69 => 1, + 70 => 1, + 75 => 1, + 82 => 1, + 89 => 1, + 93 => 1, + 98 => 2, + 99 => 1, + 103 => 2, + 105 => 1, + 111 => 1, + 112 => 2, + 114 => 1, + 115 => 1, + 116 => 2, + 117 => 1, + ]; + + case 'EmbeddedPhpUnitTest.5.inc': + return [ + 16 => 1, + 18 => 1, + 25 => 1, + 26 => 1, + 29 => 1, + 31 => 1, + 33 => 1, + 35 => 1, + 39 => 1, + 42 => 1, + ]; + + case 'EmbeddedPhpUnitTest.12.inc': + case 'EmbeddedPhpUnitTest.13.inc': + return [ + 10 => 1, + 12 => 1, + ]; + + case 'EmbeddedPhpUnitTest.18.inc': + return [11 => 1]; + + case 'EmbeddedPhpUnitTest.19.inc': + return [13 => 1]; + + case 'EmbeddedPhpUnitTest.20.inc': + case 'EmbeddedPhpUnitTest.21.inc': + return [12 => 2]; + + case 'EmbeddedPhpUnitTest.22.inc': + return [ + 14 => 1, + 22 => 2, + ]; + + default: + return []; + }//end switch }//end getErrorList()