Skip to content

Commit 2036590

Browse files
authored
Merge pull request #973 from PHPCSStandards/phpcs-4.0/feature/drop-support-php-lt-7.2-step-2
Drop support for PHP < 7.2 - step 2 (drop polyfills and work-arounds)
2 parents 89f142f + a694a6d commit 2036590

15 files changed

+44
-641
lines changed

src/Standards/Generic/Sniffs/PHP/DisallowAlternativePHPTagsSniff.php

Lines changed: 14 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,12 @@
1111

1212
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;
1313

14-
use PHP_CodeSniffer\Config;
1514
use PHP_CodeSniffer\Files\File;
1615
use PHP_CodeSniffer\Sniffs\Sniff;
1716

1817
class DisallowAlternativePHPTagsSniff implements Sniff
1918
{
2019

21-
/**
22-
* Whether ASP tags are enabled or not.
23-
*
24-
* @var boolean
25-
*/
26-
private $aspTags = false;
27-
28-
/**
29-
* The current PHP version.
30-
*
31-
* @var integer|string|null
32-
*/
33-
private $phpVersion = null;
34-
3520

3621
/**
3722
* Returns an array of tokens this test wants to listen for.
@@ -40,22 +25,7 @@ class DisallowAlternativePHPTagsSniff implements Sniff
4025
*/
4126
public function register()
4227
{
43-
if ($this->phpVersion === null) {
44-
$this->phpVersion = Config::getConfigData('php_version');
45-
if ($this->phpVersion === null) {
46-
$this->phpVersion = PHP_VERSION_ID;
47-
}
48-
}
49-
50-
if ($this->phpVersion < 70000) {
51-
$this->aspTags = (bool) ini_get('asp_tags');
52-
}
53-
54-
return [
55-
T_OPEN_TAG,
56-
T_OPEN_TAG_WITH_ECHO,
57-
T_INLINE_HTML,
58-
];
28+
return [T_INLINE_HTML];
5929

6030
}//end register()
6131

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

82-
if ($openTag['code'] === T_OPEN_TAG) {
83-
if ($content === '<%') {
84-
$error = 'ASP style opening tag used; expected "<?php" but found "%s"';
85-
$closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>');
86-
$errorCode = 'ASPOpenTagFound';
87-
} else if (strpos($content, '<script ') !== false) {
88-
$error = 'Script style opening tag used; expected "<?php" but found "%s"';
89-
$closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '</script>');
90-
$errorCode = 'ScriptOpenTagFound';
91-
}
92-
93-
if (isset($error, $closer, $errorCode) === true) {
94-
$data = [$content];
95-
96-
if ($closer === false) {
97-
$phpcsFile->addError($error, $stackPtr, $errorCode, $data);
98-
} else {
99-
$fix = $phpcsFile->addFixableError($error, $stackPtr, $errorCode, $data);
100-
if ($fix === true) {
101-
$this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer);
102-
}
103-
}
104-
}
105-
106-
return;
107-
}//end if
108-
109-
if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO && $content === '<%=') {
110-
$error = 'ASP style opening tag used with echo; expected "<?php echo %s ..." but found "%s %s ..."';
111-
$nextVar = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
112-
$snippet = $this->getSnippet($tokens[$nextVar]['content']);
113-
$data = [
114-
$snippet,
115-
$content,
116-
$snippet,
117-
];
118-
119-
$closer = $this->findClosingTag($phpcsFile, $tokens, $stackPtr, '%>');
120-
121-
if ($closer === false) {
122-
$phpcsFile->addError($error, $stackPtr, 'ASPShortOpenTagFound', $data);
123-
} else {
124-
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ASPShortOpenTagFound', $data);
125-
if ($fix === true) {
126-
$this->addChangeset($phpcsFile, $tokens, $stackPtr, $closer, true);
127-
}
128-
}
129-
130-
return;
131-
}//end if
132-
133-
// Account for incorrect script open tags.
134-
if ($openTag['code'] === T_INLINE_HTML
135-
&& preg_match('`(<script (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1
136-
) {
52+
// Account for script open tags.
53+
if (preg_match('`(<script (?:[^>]+)?language=[\'"]?php[\'"]?(?:[^>]+)?>)`i', $content, $match) === 1) {
13754
$error = 'Script style opening tag used; expected "<?php" but found "%s"';
13855
$snippet = $this->getSnippet($content, $match[1]);
13956
$data = [$match[1].$snippet];
@@ -142,20 +59,19 @@ public function process(File $phpcsFile, $stackPtr)
14259
return;
14360
}
14461

145-
if ($openTag['code'] === T_INLINE_HTML && $this->aspTags === false) {
146-
if (strpos($content, '<%=') !== false) {
147-
$error = 'Possible use of ASP style short opening tags detected; found: %s';
148-
$snippet = $this->getSnippet($content, '<%=');
149-
$data = ['<%='.$snippet];
62+
// Account for ASP style tags.
63+
if (strpos($content, '<%=') !== false) {
64+
$error = 'Possible use of ASP style short opening tags detected; found: %s';
65+
$snippet = $this->getSnippet($content, '<%=');
66+
$data = ['<%='.$snippet];
15067

151-
$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data);
152-
} else if (strpos($content, '<%') !== false) {
153-
$error = 'Possible use of ASP style opening tags detected; found: %s';
154-
$snippet = $this->getSnippet($content, '<%');
155-
$data = ['<%'.$snippet];
68+
$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPShortOpenTagFound', $data);
69+
} else if (strpos($content, '<%') !== false) {
70+
$error = 'Possible use of ASP style opening tags detected; found: %s';
71+
$snippet = $this->getSnippet($content, '<%');
72+
$data = ['<%'.$snippet];
15673

157-
$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
158-
}
74+
$phpcsFile->addWarning($error, $stackPtr, 'MaybeASPOpenTagFound', $data);
15975
}
16076

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

193109

194-
/**
195-
* Try and find a matching PHP closing tag.
196-
*
197-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
198-
* @param array $tokens The token stack.
199-
* @param int $stackPtr The position of the current token
200-
* in the stack passed in $tokens.
201-
* @param string $content The expected content of the closing tag to match the opener.
202-
*
203-
* @return int|false Pointer to the position in the stack for the closing tag or false if not found.
204-
*/
205-
protected function findClosingTag(File $phpcsFile, $tokens, $stackPtr, $content)
206-
{
207-
$closer = $phpcsFile->findNext(T_CLOSE_TAG, ($stackPtr + 1));
208-
209-
if ($closer !== false && $content === trim($tokens[$closer]['content'])) {
210-
return $closer;
211-
}
212-
213-
return false;
214-
215-
}//end findClosingTag()
216-
217-
218-
/**
219-
* Add a changeset to replace the alternative PHP tags.
220-
*
221-
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
222-
* @param array $tokens The token stack.
223-
* @param int $openTagPointer Stack pointer to the PHP open tag.
224-
* @param int $closeTagPointer Stack pointer to the PHP close tag.
225-
* @param bool $echo Whether to add 'echo' or not.
226-
*
227-
* @return void
228-
*/
229-
protected function addChangeset(File $phpcsFile, $tokens, $openTagPointer, $closeTagPointer, $echo=false)
230-
{
231-
// Build up the open tag replacement and make sure there's always whitespace behind it.
232-
$openReplacement = '<?php';
233-
if ($echo === true) {
234-
$openReplacement .= ' echo';
235-
}
236-
237-
if ($tokens[($openTagPointer + 1)]['code'] !== T_WHITESPACE) {
238-
$openReplacement .= ' ';
239-
}
240-
241-
// Make sure we don't remove any line breaks after the closing tag.
242-
$regex = '`'.preg_quote(trim($tokens[$closeTagPointer]['content'])).'`';
243-
$closeReplacement = preg_replace($regex, '?>', $tokens[$closeTagPointer]['content']);
244-
245-
$phpcsFile->fixer->beginChangeset();
246-
$phpcsFile->fixer->replaceToken($openTagPointer, $openReplacement);
247-
$phpcsFile->fixer->replaceToken($closeTagPointer, $closeReplacement);
248-
$phpcsFile->fixer->endChangeset();
249-
250-
}//end addChangeset()
251-
252-
253110
}//end class

src/Standards/Generic/Tests/PHP/DeprecatedFunctionsUnitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function getErrorList()
3232
{
3333
$errors = [];
3434

35-
if (PHP_VERSION_ID >= 70200 && PHP_VERSION_ID < 80000) {
35+
if (PHP_VERSION_ID < 80000) {
3636
$errors[3] = 1;
3737
}
3838

src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.1.inc.fixed

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.2.inc.fixed

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.3.inc

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Standards/Generic/Tests/PHP/DisallowAlternativePHPTagsUnitTest.php

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,6 @@ final class DisallowAlternativePHPTagsUnitTest extends AbstractSniffUnitTest
2020
{
2121

2222

23-
/**
24-
* Get a list of all test files to check.
25-
*
26-
* @param string $testFileBase The base path that the unit tests files will have.
27-
*
28-
* @return string[]
29-
*/
30-
protected function getTestFiles($testFileBase)
31-
{
32-
$testFiles = [$testFileBase.'1.inc'];
33-
34-
$aspTags = false;
35-
if (PHP_VERSION_ID < 70000) {
36-
$aspTags = (bool) ini_get('asp_tags');
37-
}
38-
39-
if ($aspTags === true) {
40-
$testFiles[] = $testFileBase.'2.inc';
41-
} else {
42-
$testFiles[] = $testFileBase.'3.inc';
43-
}
44-
45-
return $testFiles;
46-
47-
}//end getTestFiles()
48-
49-
5023
/**
5124
* Returns the lines where errors should occur.
5225
*
@@ -67,13 +40,7 @@ public function getErrorList($testFile='')
6740
8 => 1,
6841
11 => 1,
6942
];
70-
case 'DisallowAlternativePHPTagsUnitTest.2.inc':
71-
return [
72-
2 => 1,
73-
3 => 1,
74-
4 => 1,
75-
5 => 1,
76-
];
43+
7744
default:
7845
return [];
7946
}//end switch
@@ -93,12 +60,12 @@ public function getErrorList($testFile='')
9360
*/
9461
public function getWarningList($testFile='')
9562
{
96-
if ($testFile === 'DisallowAlternativePHPTagsUnitTest.3.inc') {
63+
if ($testFile === 'DisallowAlternativePHPTagsUnitTest.2.inc') {
9764
return [
65+
2 => 1,
9866
3 => 1,
9967
4 => 1,
10068
5 => 1,
101-
6 => 1,
10269
];
10370
}
10471

0 commit comments

Comments
 (0)