Skip to content

Commit 8b52fe3

Browse files
authored
Merge pull request #276 from PHPCSStandards/universal/new-lowercase-php-tag-sniff
✨ New `Universal.PHP.LowercasePHPTag` sniff
2 parents 746c319 + 64db8a3 commit 8b52fe3

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0"?>
2+
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
4+
title="Lowercase PHP Tag"
5+
>
6+
<standard>
7+
<![CDATA[
8+
Enforces that the PHP open tag is lowercase.
9+
]]>
10+
</standard>
11+
<code_comparison>
12+
<code title="Valid: Lowercase open tag.">
13+
<![CDATA[
14+
<?php
15+
echo 'hello!';
16+
]]>
17+
</code>
18+
<code title="Invalid: Uppercase open tag.">
19+
<![CDATA[
20+
<?PHP
21+
echo 'hello!';
22+
]]>
23+
</code>
24+
</code_comparison>
25+
</documentation>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer.
4+
*
5+
* @package PHPCSExtra
6+
* @copyright 2020 PHPCSExtra Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSExtra
9+
*/
10+
11+
namespace PHPCSExtra\Universal\Sniffs\PHP;
12+
13+
use PHP_CodeSniffer\Files\File;
14+
use PHP_CodeSniffer\Sniffs\Sniff;
15+
16+
/**
17+
* Enforce that the "PHP" in a PHP open tag is lowercase.
18+
*
19+
* @since 1.2.0
20+
*/
21+
final class LowercasePHPTagSniff implements Sniff
22+
{
23+
24+
/**
25+
* Name of the metric.
26+
*
27+
* @since 1.2.0
28+
*
29+
* @var string
30+
*/
31+
const METRIC_NAME = 'PHP open tag case';
32+
33+
/**
34+
* Registers the tokens that this sniff wants to listen for.
35+
*
36+
* @since 1.2.0
37+
*
38+
* @return array<int>
39+
*/
40+
public function register()
41+
{
42+
return [\T_OPEN_TAG];
43+
}
44+
45+
/**
46+
* Processes this test, when one of its tokens is encountered.
47+
*
48+
* @since 1.2.0
49+
*
50+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
51+
* @param int $stackPtr The position of the current token
52+
* in the stack passed in $tokens.
53+
*
54+
* @return void
55+
*/
56+
public function process(File $phpcsFile, $stackPtr)
57+
{
58+
$tokens = $phpcsFile->getTokens();
59+
$content = $tokens[$stackPtr]['content'];
60+
$contentLC = \strtolower($content);
61+
62+
if ($contentLC === $content) {
63+
$phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'lowercase');
64+
return;
65+
}
66+
67+
$errorCode = '';
68+
if (\strtoupper($content) === $content) {
69+
$errorCode = 'Uppercase';
70+
$phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'uppercase');
71+
} else {
72+
$errorCode = 'Mixedcase';
73+
$phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'mixed case');
74+
}
75+
76+
$fix = $phpcsFile->addFixableError(
77+
'The php open tag should be in lowercase. Found: %s',
78+
$stackPtr,
79+
$errorCode,
80+
[\trim($content)]
81+
);
82+
83+
if ($fix === true) {
84+
$phpcsFile->fixer->replaceToken($stackPtr, $contentLC);
85+
}
86+
}
87+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
echo 'The above is fine';
3+
?>
4+
<?PHP
5+
echo 'The above is incorrect';
6+
?>
7+
<?PhP echo 'This is incorrect'; ?>
8+
<?Php echo 'This is incorrect'; ?>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
echo 'The above is fine';
3+
?>
4+
<?php
5+
echo 'The above is incorrect';
6+
?>
7+
<?php echo 'This is incorrect'; ?>
8+
<?php echo 'This is incorrect'; ?>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* PHPCSExtra, a collection of sniffs and standards for use with PHP_CodeSniffer.
4+
*
5+
* @package PHPCSExtra
6+
* @copyright 2020 PHPCSExtra Contributors
7+
* @license https://opensource.org/licenses/LGPL-3.0 LGPL3
8+
* @link https://github.com/PHPCSStandards/PHPCSExtra
9+
*/
10+
11+
namespace PHPCSExtra\Universal\Tests\PHP;
12+
13+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
14+
15+
/**
16+
* Unit test class for the LowercasePHPTag sniff.
17+
*
18+
* @covers PHPCSExtra\Universal\Sniffs\PHP\LowercasePHPTagSniff
19+
*
20+
* @since 1.2.0
21+
*/
22+
final class LowercasePHPTagUnitTest extends AbstractSniffUnitTest
23+
{
24+
25+
/**
26+
* Returns the lines where errors should occur.
27+
*
28+
* @return array<int, int> Key is the line number, value is the number of expected errors.
29+
*/
30+
public function getErrorList()
31+
{
32+
return [
33+
4 => 1,
34+
7 => 1,
35+
8 => 1,
36+
];
37+
}
38+
39+
/**
40+
* Returns the lines where warnings should occur.
41+
*
42+
* @return array<int, int> Key is the line number, value is the number of expected warnings.
43+
*/
44+
public function getWarningList()
45+
{
46+
return [];
47+
}
48+
}

0 commit comments

Comments
 (0)