Skip to content

Commit 61e20af

Browse files
committed
Squiz/EmbeddedPhp: remove some redundant code
The removed condition in the `validateInlineEmbeddedPhp()` method could never be `true` as this is already checked in the `process()` method before this method is ever called. As that means we will always already have determined the valid close tag, we may as well pass it to both underlying methods instead of determining it again. This should also yield a small performance improvement, especially when dealing with long files with only an open tag and no a closing tag, as it saves doing the token walking over the complete file for a second time. Includes moving a comment to a more appropriate place.
1 parent c8414c5 commit 61e20af

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/Standards/Squiz/Sniffs/PHP/EmbeddedPhpSniff.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public function process(File $phpcsFile, $stackPtr)
4646
// then we have an inline embedded PHP block.
4747
$closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr);
4848
if ($closeTag === false || $tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) {
49-
$this->validateMultilineEmbeddedPhp($phpcsFile, $stackPtr);
49+
$this->validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag);
5050
} else {
51-
$this->validateInlineEmbeddedPhp($phpcsFile, $stackPtr);
51+
$this->validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag);
5252
}
5353

5454
}//end process()
@@ -57,13 +57,15 @@ public function process(File $phpcsFile, $stackPtr)
5757
/**
5858
* Validates embedded PHP that exists on multiple lines.
5959
*
60-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
61-
* @param int $stackPtr The position of the current token in the
62-
* stack passed in $tokens.
60+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
61+
* @param int $stackPtr The position of the current token in the
62+
* stack passed in $tokens.
63+
* @param int|false $closingTag The position of the PHP close tag in the
64+
* stack passed in $tokens.
6365
*
6466
* @return void
6567
*/
66-
private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr)
68+
private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr, $closingTag)
6769
{
6870
$tokens = $phpcsFile->getTokens();
6971

@@ -74,7 +76,7 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr)
7476
}
7577

7678
$firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
77-
$closingTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr);
79+
7880
if ($closingTag !== false) {
7981
$nextContent = $phpcsFile->findNext(T_WHITESPACE, ($closingTag + 1), $phpcsFile->numTokens, true);
8082
if ($nextContent === false) {
@@ -293,21 +295,15 @@ private function validateMultilineEmbeddedPhp($phpcsFile, $stackPtr)
293295
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
294296
* @param int $stackPtr The position of the current token in the
295297
* stack passed in $tokens.
298+
* @param int $closeTag The position of the PHP close tag in the
299+
* stack passed in $tokens.
296300
*
297301
* @return void
298302
*/
299-
private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr)
303+
private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr, $closeTag)
300304
{
301305
$tokens = $phpcsFile->getTokens();
302306

303-
// We only want one line PHP sections, so return if the closing tag is
304-
// on the next line.
305-
$closeTag = $phpcsFile->findNext(T_CLOSE_TAG, $stackPtr, null, false);
306-
if ($tokens[$stackPtr]['line'] !== $tokens[$closeTag]['line']) {
307-
return;
308-
}
309-
310-
// Check that there is one, and only one space at the start of the statement.
311307
$firstContent = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $closeTag, true);
312308

313309
if ($firstContent === false) {
@@ -325,6 +321,7 @@ private function validateInlineEmbeddedPhp($phpcsFile, $stackPtr)
325321
return;
326322
}
327323

324+
// Check that there is one, and only one space at the start of the statement.
328325
// The open tag token always contains a single space after it.
329326
$leadingSpace = 1;
330327
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) {

0 commit comments

Comments
 (0)