Skip to content

PHP 7.4 | Tokenizer/PHP: handle PHP tag at end of file consistently #937

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 1 commit into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ jobs:
- php: '7.0'
os: 'ubuntu-latest'
custom_ini: true
- php: '8.0'
os: 'ubuntu-latest'
custom_ini: true
- php: '8.2'
os: 'ubuntu-latest'
custom_ini: true

# yamllint disable-line rule:line-length
name: "PHP: ${{ matrix.php }} ${{ matrix.custom_ini && ' with custom ini settings' || '' }}${{ matrix.libxml_minor && format( ' with libxml {0}', matrix.libxml_minor ) || '' }} (${{ matrix.os == 'ubuntu-latest' && 'Linux' || 'Win' }})"
Expand Down Expand Up @@ -177,7 +183,7 @@ jobs:
# Also turn on error_reporting to ensure all notices are shown.
if [[ ${{ matrix.custom_ini }} == true && "${{ matrix.php }}" == '5.5' ]]; then
echo 'PHP_INI=error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On, asp_tags=On' >> "$GITHUB_OUTPUT"
elif [[ ${{ matrix.custom_ini }} == true && "${{ matrix.php }}" == '7.0' ]]; then
elif [[ ${{ matrix.custom_ini }} == true && "${{ matrix.php }}" != '5.5' ]]; then
echo 'PHP_INI=error_reporting=-1, display_errors=On, date.timezone=Australia/Sydney, short_open_tag=On' >> "$GITHUB_OUTPUT"
else
echo 'PHP_INI=error_reporting=-1, display_errors=On' >> "$GITHUB_OUTPUT"
Expand Down Expand Up @@ -233,6 +239,7 @@ jobs:
PHP_CODESNIFFER_CBF: '1'

- name: 'PHPCS: check code style without cache, no parallel'
if: ${{ matrix.custom_ini == false }}
id: phpcs
run: >
php "bin/phpcs" --no-cache --parallel=1
Expand Down
40 changes: 40 additions & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,46 @@ protected function tokenize($string)
}
}//end if

/*
Prior to PHP 7.4, PHP didn't support stand-alone PHP open tags at the end of a file
(without a new line), so we need to make sure that the tokenization in PHPCS is consistent
cross-version PHP by retokenizing to T_OPEN_TAG.
*/

if (PHP_VERSION_ID < 70400
&& $tokenIsArray === true
// PHP < 7.4 with short open tags off.
&& (($stackPtr === ($numTokens - 1)
&& $token[0] === T_INLINE_HTML
&& stripos($token[1], '<?php') === 0)
// PHP < 7.4 with short open tags on.
|| ($stackPtr === ($numTokens - 2)
&& $token[0] === T_OPEN_TAG
&& $token[1] === '<?'
&& is_array($tokens[($stackPtr + 1)]) === true
&& $tokens[($stackPtr + 1)][0] === T_STRING
&& strtolower($tokens[($stackPtr + 1)][1]) === 'php'))
) {
if ($token[0] === T_INLINE_HTML) {
$finalTokens[$newStackPtr] = [
'code' => T_OPEN_TAG,
'type' => 'T_OPEN_TAG',
'content' => $token[1],
];
} else {
$finalTokens[$newStackPtr] = [
'code' => T_OPEN_TAG,
'type' => 'T_OPEN_TAG',
'content' => $token[1].$tokens[($stackPtr + 1)][1],
];

$stackPtr++;
}

$newStackPtr++;
continue;
}//end if

/*
Parse doc blocks into something that can be easily iterated over.
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/Core/Tokenizers/PHP/PHPOpenTagEOF1Test.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
// This test case must be the last (only) test in the file without a new line after it!
/* testLongOpenTagEndOfFileSpaceNoNewLine */ ?>
<?php
54 changes: 54 additions & 0 deletions tests/Core/Tokenizers/PHP/PHPOpenTagEOF1Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Tests the tokenization of PHP open tags.
*
* Prior to PHP 7.4, PHP didn't support stand-alone PHP open tags at the end of a file (without a new line),
* so we need to make sure that the tokenization in PHPCS is consistent and correct.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP;

use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
use PHP_CodeSniffer\Util\Tokens;

/**
* Tests the tokenization of PHP open tags.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
*/
final class PHPOpenTagEOF1Test extends AbstractTokenizerTestCase
{


/**
* Test that the tokenization of a long PHP open tag at the very end of a file is correct and consistent.
*
* @return void
*/
public function testLongOpenTagAtEndOfFile()
{
$tokens = $this->phpcsFile->getTokens();
$stackPtr = $this->getTargetToken('/* testLongOpenTagEndOfFileSpaceNoNewLine */', [T_OPEN_TAG, T_STRING, T_INLINE_HTML]);

$this->assertSame(
T_OPEN_TAG,
$tokens[$stackPtr]['code'],
'Token tokenized as '.Tokens::tokenName($tokens[$stackPtr]['code']).', not T_OPEN_TAG (code)'
);
$this->assertSame(
'T_OPEN_TAG',
$tokens[$stackPtr]['type'],
'Token tokenized as '.$tokens[$stackPtr]['type'].', not T_OPEN_TAG (type)'
);
$this->assertSame('<?php ', $tokens[$stackPtr]['content']);

// Now make sure that this is the very last token in the file and there are no tokens after it.
$this->assertArrayNotHasKey(($stackPtr + 1), $tokens);

}//end testLongOpenTagAtEndOfFile()


}//end class
4 changes: 4 additions & 0 deletions tests/Core/Tokenizers/PHP/PHPOpenTagEOF2Test.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
// This test case must be the last (only) test in the file without a new line after it!
/* testLongOpenTagEndOfFileNoSpaceNoNewLine */ ?>
<?php
54 changes: 54 additions & 0 deletions tests/Core/Tokenizers/PHP/PHPOpenTagEOF2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Tests the tokenization of PHP open tags.
*
* Prior to PHP 7.4, PHP didn't support stand-alone PHP open tags at the end of a file (without a new line),
* so we need to make sure that the tokenization in PHPCS is consistent and correct.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP;

use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
use PHP_CodeSniffer\Util\Tokens;

/**
* Tests the tokenization of PHP open tags.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
*/
final class PHPOpenTagEOF2Test extends AbstractTokenizerTestCase
{


/**
* Test that the tokenization of a long PHP open tag at the very end of a file is correct and consistent.
*
* @return void
*/
public function testLongOpenTagAtEndOfFile()
{
$tokens = $this->phpcsFile->getTokens();
$stackPtr = $this->getTargetToken('/* testLongOpenTagEndOfFileNoSpaceNoNewLine */', [T_OPEN_TAG, T_STRING, T_INLINE_HTML]);

$this->assertSame(
T_OPEN_TAG,
$tokens[$stackPtr]['code'],
'Token tokenized as '.Tokens::tokenName($tokens[$stackPtr]['code']).', not T_OPEN_TAG (code)'
);
$this->assertSame(
'T_OPEN_TAG',
$tokens[$stackPtr]['type'],
'Token tokenized as '.$tokens[$stackPtr]['type'].', not T_OPEN_TAG (type)'
);
$this->assertSame('<?php', $tokens[$stackPtr]['content']);

// Now make sure that this is the very last token in the file and there are no tokens after it.
$this->assertArrayNotHasKey(($stackPtr + 1), $tokens);

}//end testLongOpenTagAtEndOfFile()


}//end class
4 changes: 4 additions & 0 deletions tests/Core/Tokenizers/PHP/PHPOpenTagEOF3Test.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
// This test case must be the last (only) test in the file without a new line after it!
/* testLongOpenTagEndOfFileNoSpaceNoNewLineUppercase */ ?>
<?PHP
54 changes: 54 additions & 0 deletions tests/Core/Tokenizers/PHP/PHPOpenTagEOF3Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Tests the tokenization of PHP open tags.
*
* Prior to PHP 7.4, PHP didn't support stand-alone PHP open tags at the end of a file (without a new line),
* so we need to make sure that the tokenization in PHPCS is consistent and correct.
*
* @copyright 2025 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Tokenizers\PHP;

use PHP_CodeSniffer\Tests\Core\Tokenizers\AbstractTokenizerTestCase;
use PHP_CodeSniffer\Util\Tokens;

/**
* Tests the tokenization of PHP open tags.
*
* @covers PHP_CodeSniffer\Tokenizers\PHP::tokenize
*/
final class PHPOpenTagEOF3Test extends AbstractTokenizerTestCase
{


/**
* Test that the tokenization of a long PHP open tag at the very end of a file is correct and consistent.
*
* @return void
*/
public function testLongOpenTagAtEndOfFile()
{
$tokens = $this->phpcsFile->getTokens();
$stackPtr = $this->getTargetToken('/* testLongOpenTagEndOfFileNoSpaceNoNewLineUppercase */', [T_OPEN_TAG, T_STRING, T_INLINE_HTML]);

$this->assertSame(
T_OPEN_TAG,
$tokens[$stackPtr]['code'],
'Token tokenized as '.Tokens::tokenName($tokens[$stackPtr]['code']).', not T_OPEN_TAG (code)'
);
$this->assertSame(
'T_OPEN_TAG',
$tokens[$stackPtr]['type'],
'Token tokenized as '.$tokens[$stackPtr]['type'].', not T_OPEN_TAG (type)'
);
$this->assertSame('<?PHP', $tokens[$stackPtr]['content']);

// Now make sure that this is the very last token in the file and there are no tokens after it.
$this->assertArrayNotHasKey(($stackPtr + 1), $tokens);

}//end testLongOpenTagAtEndOfFile()


}//end class