Skip to content

Drop support for PHP < 7.2 - step 2 (drop polyfills and work-arounds) #973

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 4 commits into from
Apr 14, 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
171 changes: 14 additions & 157 deletions src/Standards/Generic/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,12 @@

namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;

use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class DisallowAlternativePHPTagsSniff implements Sniff
{

/**
* Whether ASP tags are enabled or not.
*
* @var boolean
*/
private $aspTags = false;

/**
* The current PHP version.
*
* @var integer|string|null
*/
private $phpVersion = null;


/**
* Returns an array of tokens this test wants to listen for.
Expand All @@ -40,22 +25,7 @@ class DisallowAlternativePHPTagsSniff implements Sniff
*/
public function register()
{
if ($this->phpVersion === null) {
$this->phpVersion = Config::getConfigData('php_version');
if ($this->phpVersion === null) {
$this->phpVersion = PHP_VERSION_ID;
}
}

if ($this->phpVersion < 70000) {
$this->aspTags = (bool) ini_get('asp_tags');
}

return [
T_OPEN_TAG,
T_OPEN_TAG_WITH_ECHO,
T_INLINE_HTML,
];
return [T_INLINE_HTML];

}//end register()

Expand All @@ -79,61 +49,8 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

if ($openTag['code'] === T_OPEN_TAG) {
if ($content === '<%') {
$error = 'ASP style opening tag used; expected "<?php" but found "%s"';
$closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>');
$errorCode = 'ASPOpenTagFound';
} else if (strpos($content, '<script ') !== false) {
$error = 'Script style opening tag used; expected "<?php" but found "%s"';
$closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '</script>');
$errorCode = 'ScriptOpenTagFound';
}

if (isset($error, $closer, $errorCode) === true) {
$data = [$content];

if ($closer === false) {
$phpcsFile->addError($error, $stackPtr, $errorCode, $data);
} else {
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode, $data);
if ($fix === true) {
$this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer);
}
}
}

return;
}//end if

if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO && $content === '<%=') {
$error = 'ASP style opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
$nextVar = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
$snippet = $this->getSnippet($tokens[$nextVar]['content']);
$data = [
$snippet,
$content,
$snippet,
];

$closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>');

if ($closer === false) {
$phpcsFile->addError($error, $stackPtr, 'ASPShortOpenTagFound', $data);
} else {
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ASPShortOpenTagFound', $data);
if ($fix === true) {
$this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer, true);
}
}

return;
}//end if

// Account for incorrect script open tags.
if ($openTag['code'] === T_INLINE_HTML
&& preg_match('`(<script (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1
) {
// Account for script open tags.
if (preg_match('`(<script (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1) {
$error = 'Script style opening tag used; expected "<?php" but found "%s"';
$snippet = $this->getSnippet($content, $match[1]);
$data = [$match[1].$snippet];
Expand All @@ -142,20 +59,19 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

if ($openTag['code'] === T_INLINE_HTML && $this->aspTags === false) {
if (strpos($content, '<%=') !== false) {
$error = 'Possible use of ASP style short opening tags detected; found: %s';
$snippet = $this->getSnippet($content, '<%=');
$data = ['<%='.$snippet];
// Account for ASP style tags.
if (strpos($content, '<%=') !== false) {
$error = 'Possible use of ASP style short opening tags detected; found: %s';
$snippet = $this->getSnippet($content, '<%=');
$data = ['<%='.$snippet];

$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data);
} else if (strpos($content, '<%') !== false) {
$error = 'Possible use of ASP style opening tags detected; found: %s';
$snippet = $this->getSnippet($content, '<%');
$data = ['<%'.$snippet];
$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data);
} else if (strpos($content, '<%') !== false) {
$error = 'Possible use of ASP style opening tags detected; found: %s';
$snippet = $this->getSnippet($content, '<%');
$data = ['<%'.$snippet];

$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
}
$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
}

}//end process()
Expand Down Expand Up @@ -191,63 +107,4 @@ protected function getSnippet($content, $start='', $length=40)
}//end getSnippet()


/**
* Try and find a matching PHP closing tag.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param array $tokens The token stack.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
* @param string $content The expected content of the closing tag to match the opener.
*
* @return int|false Pointer to the position in the stack for the closing tag or false if not found.
*/
protected function findClosingTag(File $phpcsFile, $tokens, $stackPtr, $content)
{
$closer = $phpcsFile->findNext(T_CLOSE_TAG, ($stackPtr + 1));

if ($closer !== false && $content === trim($tokens[$closer]['content'])) {
return $closer;
}

return false;

}//end findClosingTag()


/**
* Add a changeset to replace the alternative PHP tags.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param array $tokens The token stack.
* @param int $openTagPointer Stack pointer to the PHP open tag.
* @param int $closeTagPointer Stack pointer to the PHP close tag.
* @param bool $echo Whether to add 'echo' or not.
*
* @return void
*/
protected function addChangeset(File $phpcsFile, $tokens, $openTagPointer, $closeTagPointer, $echo=false)
{
// Build up the open tag replacement and make sure there's always whitespace behind it.
$openReplacement = '<?php';
if ($echo === true) {
$openReplacement .= ' echo';
}

if ($tokens[($openTagPointer + 1)]['code'] !== T_WHITESPACE) {
$openReplacement .= ' ';
}

// Make sure we don't remove any line breaks after the closing tag.
$regex = '`'.preg_quote(trim($tokens[$closeTagPointer]['content'])).'`';
$closeReplacement = preg_replace($regex, '?>', $tokens[$closeTagPointer]['content']);

$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($openTagPointer, $openReplacement);
$phpcsFile->fixer->replaceToken($closeTagPointer, $closeReplacement);
$phpcsFile->fixer->endChangeset();

}//end addChangeset()


}//end class
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getErrorList()
{
$errors = [];

if (PHP_VERSION_ID >= 70200 && PHP_VERSION_ID < 80000) {
if (PHP_VERSION_ID < 80000) {
$errors[3] = 1;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,6 @@ final class DisallowAlternativePHPTagsUnitTest extends AbstractSniffUnitTest
{


/**
* Get a list of all test files to check.
*
* @param string $testFileBase The base path that the unit tests files will have.
*
* @return string[]
*/
protected function getTestFiles($testFileBase)
{
$testFiles = [$testFileBase.'1.inc'];

$aspTags = false;
if (PHP_VERSION_ID < 70000) {
$aspTags = (bool) ini_get('asp_tags');
}

if ($aspTags === true) {
$testFiles[] = $testFileBase.'2.inc';
} else {
$testFiles[] = $testFileBase.'3.inc';
}

return $testFiles;

}//end getTestFiles()


/**
* Returns the lines where errors should occur.
*
Expand All @@ -67,13 +40,7 @@ public function getErrorList($testFile='')
8 => 1,
11 => 1,
];
case 'DisallowAlternativePHPTagsUnitTest.2.inc':
return [
2 => 1,
3 => 1,
4 => 1,
5 => 1,
];

default:
return [];
}//end switch
Expand All @@ -93,12 +60,12 @@ public function getErrorList($testFile='')
*/
public function getWarningList($testFile='')
{
if ($testFile === 'DisallowAlternativePHPTagsUnitTest.3.inc') {
if ($testFile === 'DisallowAlternativePHPTagsUnitTest.2.inc') {
return [
2 => 1,
3 => 1,
4 => 1,
5 => 1,
6 => 1,
];
}

Expand Down
Loading