|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Unleashed\Sniffs\Namespaces; |
| 6 | + |
| 7 | +use PHP_CodeSniffer\Files\File; |
| 8 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 9 | +use SlevomatCodingStandard\Sniffs\Classes\ModernClassNameReferenceSniff; |
| 10 | +use Unleashed\SniffHelper; |
| 11 | + |
| 12 | +final class FullyQualifiedGlobalFunctionsSniff implements Sniff |
| 13 | +{ |
| 14 | + /** @var bool */ |
| 15 | + public $onlyOptimizedFunctions = false; |
| 16 | + |
| 17 | + /** @var array<string, bool> */ |
| 18 | + private $optimizedFunctions = [ |
| 19 | + // @see https://github.com/php/php-src/blob/PHP-7.4/Zend/zend_compile.c "zend_try_compile_special_func" |
| 20 | + 'array_key_exists' => true, |
| 21 | + 'array_slice' => true, |
| 22 | + 'assert' => true, |
| 23 | + 'boolval' => true, |
| 24 | + 'call_user_func' => true, |
| 25 | + 'call_user_func_array' => true, |
| 26 | + 'chr' => true, |
| 27 | + 'count' => true, |
| 28 | + 'defined' => true, |
| 29 | + 'doubleval' => true, |
| 30 | + 'floatval' => true, |
| 31 | + 'func_get_args' => true, |
| 32 | + 'func_num_args' => true, |
| 33 | + 'get_called_class' => true, |
| 34 | + 'get_class' => true, |
| 35 | + 'gettype' => true, |
| 36 | + 'in_array' => true, |
| 37 | + 'intval' => true, |
| 38 | + 'is_array' => true, |
| 39 | + 'is_bool' => true, |
| 40 | + 'is_double' => true, |
| 41 | + 'is_float' => true, |
| 42 | + 'is_int' => true, |
| 43 | + 'is_integer' => true, |
| 44 | + 'is_long' => true, |
| 45 | + 'is_null' => true, |
| 46 | + 'is_object' => true, |
| 47 | + 'is_real' => true, |
| 48 | + 'is_resource' => true, |
| 49 | + 'is_string' => true, |
| 50 | + 'ord' => true, |
| 51 | + 'sizeof' => true, |
| 52 | + 'strlen' => true, |
| 53 | + 'strval' => true, |
| 54 | + // @see https://github.com/php/php-src/blob/php-7.2.6/ext/opcache/Optimizer/pass1_5.c |
| 55 | + 'constant' => true, |
| 56 | + 'define' => true, |
| 57 | + 'dirname' => true, |
| 58 | + 'extension_loaded' => true, |
| 59 | + 'function_exists' => true, |
| 60 | + 'is_callable' => true, |
| 61 | + ]; |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns an array of tokens this test wants to listen for. |
| 65 | + * We're looking for all functions, so use T_STRING. |
| 66 | + * |
| 67 | + * @inheritDoc |
| 68 | + */ |
| 69 | + public function register() |
| 70 | + { |
| 71 | + return [\T_STRING]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Processes this test, when one of its tokens is encountered. |
| 76 | + * |
| 77 | + * Code from ForbiddenFunctionsSniff: |
| 78 | + * |
| 79 | + * @link https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php#L118 |
| 80 | + * |
| 81 | + * @inheritDoc |
| 82 | + */ |
| 83 | + public function process(File $phpcsFile, $stackPtr) |
| 84 | + { |
| 85 | + if ($this->onlyOptimizedFunctions !== null && \filter_var($this->onlyOptimizedFunctions, FILTER_VALIDATE_BOOLEAN) !== false) { |
| 86 | + $globalFunctions = $this->optimizedFunctions; |
| 87 | + } else { |
| 88 | + $globalFunctions = \array_flip(\get_defined_functions()['internal']); |
| 89 | + } |
| 90 | + |
| 91 | + $whitelist = SniffHelper::getAliasesAndNonGlobalFunctionsDefinedInUseStatements($phpcsFile); |
| 92 | + |
| 93 | + $tokens = $phpcsFile->getTokens(); |
| 94 | + $ignore = [ |
| 95 | + T_DOUBLE_COLON => true, |
| 96 | + T_OBJECT_OPERATOR => true, |
| 97 | + T_FUNCTION => true, |
| 98 | + T_CONST => true, |
| 99 | + T_PUBLIC => true, |
| 100 | + T_PRIVATE => true, |
| 101 | + T_PROTECTED => true, |
| 102 | + T_AS => true, |
| 103 | + T_NEW => true, |
| 104 | + T_INSTEADOF => true, |
| 105 | + T_NS_SEPARATOR => true, |
| 106 | + T_IMPLEMENTS => true, |
| 107 | + ]; |
| 108 | + $prevToken = $phpcsFile->findPrevious([T_WHITESPACE, T_COMMENT], $stackPtr - 1, null, true); |
| 109 | + |
| 110 | + // If function call is directly preceded by a NS_SEPARATOR don't try to fix it. |
| 111 | + if ($tokens[$prevToken]['code'] === T_NS_SEPARATOR && $tokens[$stackPtr]['code'] === T_STRING) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (isset($ignore[$tokens[$prevToken]['code']]) === true) { |
| 116 | + // Not a call to a PHP function. |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + $nextToken = $phpcsFile->findNext([T_WHITESPACE, T_COMMENT], $stackPtr + 1, null, true); |
| 121 | + if (isset($ignore[$tokens[$nextToken]['code']]) === true) { |
| 122 | + // Not a call to a PHP function. |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + if ($tokens[$nextToken]['code'] !== T_OPEN_PARENTHESIS) { |
| 127 | + // Not a call to a PHP function. |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + $function = \strtolower($tokens[$stackPtr]['content']); |
| 132 | + $functionNormalized = \strtolower($function); |
| 133 | + |
| 134 | + // Is it a whitelisted alias? |
| 135 | + if (isset($whitelist[$functionNormalized])) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + // Is it an global PHP function? |
| 140 | + if (isset($globalFunctions[$functionNormalized]) === false) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + $error = \sprintf('Function %1$s() should be referenced via a fully qualified name, e.g.: \%1$s()', $function); |
| 145 | + $fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotFullyQualified'); |
| 146 | + |
| 147 | + if ($fix === true) { |
| 148 | + $this->applyFix($phpcsFile, $stackPtr, $function); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private function applyFix(File $phpcsFile, int $stackPtr, string $function): void |
| 153 | + { |
| 154 | + // This sniff conflicts with ModernClassNameReferenceSniff, so don't bother fixing things it will attempt to fix |
| 155 | + if (\array_key_exists(ModernClassNameReferenceSniff::class, $phpcsFile->ruleset->sniffs) && \in_array($function, ['get_class', 'get_parent_class', 'get_called_class'], true)) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + $phpcsFile->fixer->beginChangeset(); |
| 160 | + $phpcsFile->fixer->addContentBefore($stackPtr, '\\'); |
| 161 | + $phpcsFile->fixer->endChangeset(); |
| 162 | + } |
| 163 | +} |
0 commit comments