|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCSUtils, utility functions and classes for PHP_CodeSniffer sniff developers. |
| 4 | + * |
| 5 | + * @package PHPCSUtils |
| 6 | + * @copyright 2025 PHPCSUtils Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCSStandards/PHPCSUtils |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHPCSUtils\Internal; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Files\File; |
| 14 | +use PHP_CodeSniffer\Util\Tokens; |
| 15 | +use PHPCSUtils\Exceptions\OutOfBoundsStackPtr; |
| 16 | +use PHPCSUtils\Exceptions\TypeError; |
| 17 | +use PHPCSUtils\Exceptions\UnexpectedTokenType; |
| 18 | +use PHPCSUtils\Exceptions\ValueError; |
| 19 | +use PHPCSUtils\Internal\Cache; |
| 20 | +use PHPCSUtils\Tokens\Collections; |
| 21 | +use PHPCSUtils\Utils\FunctionDeclarations; |
| 22 | +use PHPCSUtils\Utils\Parentheses; |
| 23 | +use PHPCSUtils\Utils\Scopes; |
| 24 | + |
| 25 | +/** |
| 26 | + * Helper methods for PHP attributes. |
| 27 | + * |
| 28 | + * --------------------------------------------------------------------------------------------- |
| 29 | + * This class is only intended for internal use by PHPCSUtils and is not part of the public API. |
| 30 | + * This also means that it has no promise of backward compatibility. |
| 31 | + * |
| 32 | + * End-users should use the {@see \PHPCSUtils\Utils\Constants::getAttributeOpeners()}, |
| 33 | + * {@see \PHPCSUtils\Utils\FunctionDeclarations::getAttributeOpeners()}, |
| 34 | + * {@see \PHPCSUtils\Utils\ObjectDeclarations::getAttributeOpeners()}, |
| 35 | + * or the {@see \PHPCSUtils\Utils\Variables::getAttributeOpeners()} methods instead. |
| 36 | + * --------------------------------------------------------------------------------------------- |
| 37 | + * |
| 38 | + * @internal |
| 39 | + * |
| 40 | + * @since 1.2.0 |
| 41 | + */ |
| 42 | +final class AttributeHelper |
| 43 | +{ |
| 44 | + |
| 45 | + /** |
| 46 | + * Retrieve a list of stack pointers to the attribute openers for any attributes |
| 47 | + * which apply to the current stack pointer. |
| 48 | + * |
| 49 | + * @since 1.2.0 |
| 50 | + * |
| 51 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 52 | + * @param int $stackPtr The position of the token for a construct which can take an attribute. |
| 53 | + * Currently, this means: |
| 54 | + * - All OO declaration tokens; |
| 55 | + * - All function declaration tokens; |
| 56 | + * - T_VARIABLE tokens for function parameters and OO properties; |
| 57 | + * - T_CONST tokens. |
| 58 | + * @param string $type The expected type of construct. |
| 59 | + * Should be one of the following values: |
| 60 | + * 'constant', 'function', 'OO', 'variable'. |
| 61 | + * |
| 62 | + * @return array<int> |
| 63 | + * |
| 64 | + * @throws \PHPCSUtils\Exceptions\TypeError If the $stackPtr parameter is not an integer. |
| 65 | + * @throws \PHPCSUtils\Exceptions\TypeError If the $type parameter is not a string. |
| 66 | + * @throws \PHPCSUtils\Exceptions\OutOfBoundsStackPtr If the token passed does not exist in the $phpcsFile. |
| 67 | + * @throws \PHPCSUtils\Exceptions\UnexpectedTokenType If the token passed is not of a token type accepted for $type. |
| 68 | + * @throws \PHPCSUtils\Exceptions\ValueError For T_VARIABLE tokens: if the token passed does not point |
| 69 | + * to an OO property token or a parameter in a function declaration. |
| 70 | + */ |
| 71 | + public static function getOpeners(File $phpcsFile, $stackPtr, $type) |
| 72 | + { |
| 73 | + $tokens = $phpcsFile->getTokens(); |
| 74 | + |
| 75 | + if (\is_int($stackPtr) === false) { |
| 76 | + throw TypeError::create(2, '$stackPtr', 'integer', $stackPtr); |
| 77 | + } |
| 78 | + |
| 79 | + if (isset($tokens[$stackPtr]) === false) { |
| 80 | + throw OutOfBoundsStackPtr::create(2, '$stackPtr', $stackPtr); |
| 81 | + } |
| 82 | + |
| 83 | + if (\is_string($type) === false) { |
| 84 | + throw TypeError::create(3, '$type', 'string', $type); |
| 85 | + } |
| 86 | + |
| 87 | + $isOOProperty = false; |
| 88 | + $isFunctionParam = false; |
| 89 | + switch ($type) { |
| 90 | + case 'constant': |
| 91 | + if ($tokens[$stackPtr]['code'] !== \T_CONST) { |
| 92 | + throw UnexpectedTokenType::create(2, '$stackPtr', 'T_CONST', $tokens[$stackPtr]['type']); |
| 93 | + } |
| 94 | + break; |
| 95 | + |
| 96 | + case 'function': |
| 97 | + if (isset(Collections::functionDeclarationTokens()[$tokens[$stackPtr]['code']]) === false) { |
| 98 | + $acceptedTokens = 'T_FUNCTION, T_CLOSURE or T_FN'; |
| 99 | + throw UnexpectedTokenType::create(2, '$stackPtr', $acceptedTokens, $tokens[$stackPtr]['type']); |
| 100 | + } |
| 101 | + break; |
| 102 | + |
| 103 | + case 'OO': |
| 104 | + if (isset(Tokens::$ooScopeTokens[$tokens[$stackPtr]['code']]) === false) { |
| 105 | + $acceptedTokens = 'T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM'; |
| 106 | + throw UnexpectedTokenType::create(2, '$stackPtr', $acceptedTokens, $tokens[$stackPtr]['type']); |
| 107 | + } |
| 108 | + break; |
| 109 | + |
| 110 | + case 'variable': |
| 111 | + if ($tokens[$stackPtr]['code'] !== \T_VARIABLE) { |
| 112 | + throw UnexpectedTokenType::create(2, '$stackPtr', 'T_VARIABLE', $tokens[$stackPtr]['type']); |
| 113 | + } |
| 114 | + |
| 115 | + $isOOProperty = Scopes::isOOProperty($phpcsFile, $stackPtr); |
| 116 | + $isFunctionParam = Parentheses::lastOwnerIn($phpcsFile, $stackPtr, Collections::functionDeclarationTokens()); |
| 117 | + |
| 118 | + if ($isOOProperty === false && $isFunctionParam === false) { |
| 119 | + $message = 'must be the pointer to an OO property or a parameter in a function declaration'; |
| 120 | + throw ValueError::create(2, '$stackPtr', $message); |
| 121 | + } |
| 122 | + |
| 123 | + // Allow for multi-property declarations. |
| 124 | + if ($isOOProperty === true) { |
| 125 | + do { |
| 126 | + $prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
| 127 | + if ($tokens[$prevNonEmpty]['code'] !== \T_COMMA) { |
| 128 | + break; |
| 129 | + } |
| 130 | + |
| 131 | + $stackPtr = $phpcsFile->findPrevious(T_VARIABLE, ($prevNonEmpty - 1), null, false, null, true); |
| 132 | + } while ($stackPtr !== false); |
| 133 | + |
| 134 | + if ($stackPtr === false) { |
| 135 | + $message = 'must be the pointer to an OO property or a parameter in a function declaration'; |
| 136 | + throw ValueError::create(2, '$stackPtr', $message); |
| 137 | + } |
| 138 | + } |
| 139 | + break; |
| 140 | + |
| 141 | + default: |
| 142 | + throw ValueError::create(3, '$type', 'must be one of the following: constant, function, OO, variable'); |
| 143 | + } |
| 144 | + |
| 145 | + if (Cache::isCached($phpcsFile, __METHOD__, "$stackPtr-$type") === true) { |
| 146 | + return Cache::get($phpcsFile, __METHOD__, "$stackPtr-$type"); |
| 147 | + } |
| 148 | + |
| 149 | + $allowedBetween = Tokens::$emptyTokens; |
| 150 | + switch ($type) { |
| 151 | + case 'constant': |
| 152 | + if (Scopes::isOOConstant($phpcsFile, $stackPtr) === true) { |
| 153 | + $allowedBetween += Collections::constantModifierKeywords(); |
| 154 | + } |
| 155 | + break; |
| 156 | + |
| 157 | + case 'function': |
| 158 | + $allowedBetween += [\T_STATIC => \T_STATIC]; |
| 159 | + if (Scopes::isOOMethod($phpcsFile, $stackPtr) === true) { |
| 160 | + $allowedBetween += Tokens::$methodPrefixes; |
| 161 | + } |
| 162 | + break; |
| 163 | + |
| 164 | + case 'OO': |
| 165 | + if ($tokens[$stackPtr]['code'] === \T_CLASS) { |
| 166 | + $allowedBetween += Collections::classModifierKeywords(); |
| 167 | + } elseif ($tokens[$stackPtr]['code'] === \T_ANON_CLASS) { |
| 168 | + $allowedBetween[\T_READONLY] = \T_READONLY; |
| 169 | + } |
| 170 | + break; |
| 171 | + |
| 172 | + case 'variable': |
| 173 | + $allowedBetween += [\T_NULLABLE => \T_NULLABLE]; |
| 174 | + if ($isOOProperty === true) { |
| 175 | + $allowedBetween += Collections::propertyModifierKeywords(); |
| 176 | + $allowedBetween += Collections::propertyTypeTokens(); |
| 177 | + } elseif ($isFunctionParam !== false) { |
| 178 | + $allowedBetween += Collections::parameterTypeTokens(); |
| 179 | + $allowedBetween += [ |
| 180 | + \T_BITWISE_AND => \T_BITWISE_AND, |
| 181 | + \T_ELLIPSIS => \T_ELLIPSIS, |
| 182 | + ]; |
| 183 | + |
| 184 | + if ($tokens[$isFunctionParam]['code'] === \T_FUNCTION |
| 185 | + && Scopes::isOOMethod($phpcsFile, $isFunctionParam) === true |
| 186 | + ) { |
| 187 | + $functionName = FunctionDeclarations::getName($phpcsFile, $isFunctionParam); |
| 188 | + if (empty($functionName) === false && \strtolower($functionName) === '__construct') { |
| 189 | + $allowedBetween += Collections::propertyModifierKeywords(); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + break; |
| 195 | + } |
| 196 | + |
| 197 | + $seenAttributes = []; |
| 198 | + |
| 199 | + for ($i = ($stackPtr - 1); $i >= 0; $i--) { |
| 200 | + if (isset($tokens[$i]['comment_opener'])) { |
| 201 | + // Skip over docblocks. |
| 202 | + $i = $tokens[$i]['comment_opener']; |
| 203 | + continue; |
| 204 | + } |
| 205 | + |
| 206 | + if (isset($allowedBetween[$tokens[$i]['code']])) { |
| 207 | + continue; |
| 208 | + } |
| 209 | + |
| 210 | + if (isset($tokens[$i]['attribute_opener'])) { |
| 211 | + $seenAttributes[] = $tokens[$i]['attribute_opener']; |
| 212 | + $i = $tokens[$i]['attribute_opener']; |
| 213 | + continue; |
| 214 | + } |
| 215 | + |
| 216 | + // In all other cases, we've reached the end of our search. |
| 217 | + break; |
| 218 | + } |
| 219 | + |
| 220 | + if ($seenAttributes !== []) { |
| 221 | + $seenAttributes = \array_reverse($seenAttributes); |
| 222 | + } |
| 223 | + |
| 224 | + Cache::set($phpcsFile, __METHOD__, "$stackPtr-$type", $seenAttributes); |
| 225 | + return $seenAttributes; |
| 226 | + } |
| 227 | +} |
0 commit comments