|
| 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 | +use PHP_CodeSniffer\Util\Tokens; |
| 16 | + |
| 17 | +/** |
| 18 | + * Forbids the use of `true`/`false`/`null` as fully qualified constants. |
| 19 | + * |
| 20 | + * @since 1.3.0 |
| 21 | + */ |
| 22 | +final class NoFQNTrueFalseNullSniff implements Sniff |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * Registers the tokens that this sniff wants to listen for. |
| 27 | + * |
| 28 | + * @since 1.3.0 |
| 29 | + * |
| 30 | + * @return array<int|string> |
| 31 | + */ |
| 32 | + public function register() |
| 33 | + { |
| 34 | + return [ |
| 35 | + // PHP < 8.0. |
| 36 | + \T_TRUE, |
| 37 | + \T_FALSE, |
| 38 | + \T_NULL, |
| 39 | + |
| 40 | + // PHP >= 8.0. |
| 41 | + \T_STRING, |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Processes this test, when one of its tokens is encountered. |
| 47 | + * |
| 48 | + * @since 1.3.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 !== 'true' && $contentLC !== 'false' && $contentLC !== 'null') { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
| 67 | + if ($tokens[$prev]['code'] !== \T_NS_SEPARATOR) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + $prevPrev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true); |
| 72 | + if ($tokens[$prevPrev]['code'] === \T_STRING || $tokens[$prevPrev]['code'] === \T_NAMESPACE) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
| 77 | + if ($tokens[$next]['code'] === \T_NS_SEPARATOR) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + $fix = $phpcsFile->addFixableError( |
| 82 | + 'The special PHP constant "%s" should not be fully qualified.', |
| 83 | + $prev, |
| 84 | + 'Found', |
| 85 | + [$contentLC] |
| 86 | + ); |
| 87 | + |
| 88 | + if ($fix === true) { |
| 89 | + $phpcsFile->fixer->replaceToken($prev, ''); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments