diff --git a/Universal/Docs/PHP/LowercasePHPTagStandard.xml b/Universal/Docs/PHP/LowercasePHPTagStandard.xml new file mode 100644 index 00000000..261f93cd --- /dev/null +++ b/Universal/Docs/PHP/LowercasePHPTagStandard.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + diff --git a/Universal/Sniffs/PHP/LowercasePHPTagSniff.php b/Universal/Sniffs/PHP/LowercasePHPTagSniff.php new file mode 100644 index 00000000..b231b225 --- /dev/null +++ b/Universal/Sniffs/PHP/LowercasePHPTagSniff.php @@ -0,0 +1,87 @@ + + */ + public function register() + { + return [\T_OPEN_TAG]; + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @since 1.2.0 + * + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process(File $phpcsFile, $stackPtr) + { + $tokens = $phpcsFile->getTokens(); + $content = $tokens[$stackPtr]['content']; + $contentLC = \strtolower($content); + + if ($contentLC === $content) { + $phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'lowercase'); + return; + } + + $errorCode = ''; + if (\strtoupper($content) === $content) { + $errorCode = 'Uppercase'; + $phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'uppercase'); + } else { + $errorCode = 'Mixedcase'; + $phpcsFile->recordMetric($stackPtr, self::METRIC_NAME, 'mixed case'); + } + + $fix = $phpcsFile->addFixableError( + 'The php open tag should be in lowercase. Found: %s', + $stackPtr, + $errorCode, + [\trim($content)] + ); + + if ($fix === true) { + $phpcsFile->fixer->replaceToken($stackPtr, $contentLC); + } + } +} diff --git a/Universal/Tests/PHP/LowercasePHPTagUnitTest.inc b/Universal/Tests/PHP/LowercasePHPTagUnitTest.inc new file mode 100644 index 00000000..c06d7659 --- /dev/null +++ b/Universal/Tests/PHP/LowercasePHPTagUnitTest.inc @@ -0,0 +1,8 @@ + + + + diff --git a/Universal/Tests/PHP/LowercasePHPTagUnitTest.inc.fixed b/Universal/Tests/PHP/LowercasePHPTagUnitTest.inc.fixed new file mode 100644 index 00000000..51cef43b --- /dev/null +++ b/Universal/Tests/PHP/LowercasePHPTagUnitTest.inc.fixed @@ -0,0 +1,8 @@ + + + + diff --git a/Universal/Tests/PHP/LowercasePHPTagUnitTest.php b/Universal/Tests/PHP/LowercasePHPTagUnitTest.php new file mode 100644 index 00000000..87a824b0 --- /dev/null +++ b/Universal/Tests/PHP/LowercasePHPTagUnitTest.php @@ -0,0 +1,48 @@ + Key is the line number, value is the number of expected errors. + */ + public function getErrorList() + { + return [ + 4 => 1, + 7 => 1, + 8 => 1, + ]; + } + + /** + * Returns the lines where warnings should occur. + * + * @return array Key is the line number, value is the number of expected warnings. + */ + public function getWarningList() + { + return []; + } +}