|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PHPCompatibility, an external standard for PHP_CodeSniffer. |
| 4 | + * |
| 5 | + * @package PHPCompatibility |
| 6 | + * @copyright 2012-2020 PHPCompatibility Contributors |
| 7 | + * @license https://opensource.org/licenses/LGPL-3.0 LGPL3 |
| 8 | + * @link https://github.com/PHPCompatibility/PHPCompatibility |
| 9 | + */ |
| 10 | + |
| 11 | +namespace PHPCompatibility\Sniffs\InitialValue; |
| 12 | + |
| 13 | +use PHPCompatibility\AbstractInitialValueSniff; |
| 14 | +use PHP_CodeSniffer\Files\File; |
| 15 | +use PHP_CodeSniffer\Util\Tokens; |
| 16 | +use PHPCSUtils\Tokens\Collections; |
| 17 | +use PHPCSUtils\Utils\MessageHelper; |
| 18 | +use PHPCSUtils\Utils\Parentheses; |
| 19 | +use PHPCSUtils\Utils\Scopes; |
| 20 | + |
| 21 | +/** |
| 22 | + * Detect object instantiation in initial values as allowed per PHP 8.1. |
| 23 | + * |
| 24 | + * As of PHP 8.1, `new` expressions are allowed in parameter default values, |
| 25 | + * attribute arguments, static variable initializers and global class constant initializers. |
| 26 | + * Parameter default values also include defaults for promoted properties. |
| 27 | + * |
| 28 | + * The use of a dynamic or non-string class name or an anonymous class is not allowed. |
| 29 | + * The use of argument unpacking is not allowed. The use of unsupported expressions as arguments is not allowed. |
| 30 | + * |
| 31 | + * PHP version 8.1 |
| 32 | + * |
| 33 | + * @link https://wiki.php.net/rfc/new_in_initializers |
| 34 | + * @link https://www.php.net/manual/en/migration81.new-features.php#migration81.new-features.core.new-in-initializer |
| 35 | + * |
| 36 | + * @since 10.0.0 |
| 37 | + */ |
| 38 | +final class NewNewInInitializersSniff extends AbstractInitialValueSniff |
| 39 | +{ |
| 40 | + |
| 41 | + /** |
| 42 | + * Error message. |
| 43 | + * |
| 44 | + * @since 10.0.0 |
| 45 | + * |
| 46 | + * @var string |
| 47 | + */ |
| 48 | + const ERROR_PHRASE = 'New in initializers is not supported in PHP 8.0 or earlier for %s.'; |
| 49 | + |
| 50 | + /** |
| 51 | + * Partial error phrases to be used in combination with the error message constant. |
| 52 | + * |
| 53 | + * @since 10.0.0. |
| 54 | + * |
| 55 | + * @var array |
| 56 | + */ |
| 57 | + protected $initialValueTypes = [ |
| 58 | + 'const' => 'global/namespaced constants declared using the const keyword', |
| 59 | + 'property' => '', // Not supported. |
| 60 | + 'staticvar' => 'static variables', |
| 61 | + 'default' => 'default parameter values', |
| 62 | + ]; |
| 63 | + |
| 64 | + /** |
| 65 | + * Do a version check to determine if this sniff needs to run at all. |
| 66 | + * |
| 67 | + * @since 10.0.0 |
| 68 | + * |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + protected function bowOutEarly() |
| 72 | + { |
| 73 | + return ($this->supportsBelow('8.0') === false); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Process a token which has an initial value. |
| 78 | + * |
| 79 | + * @since 10.0.0 |
| 80 | + * |
| 81 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 82 | + * @param int $stackPtr The position of the variable/constant name token |
| 83 | + * in the stack passed in $tokens. |
| 84 | + * @param int $start The stackPtr to the start of the initial value. |
| 85 | + * @param int $end The stackPtr to the end of the initial value. |
| 86 | + * This will normally be a comma or semi-colon. |
| 87 | + * @param string $type The "type" of initial value declaration being examined. |
| 88 | + * The type will match one of the keys in the |
| 89 | + * `AbstractInitialValueSniff::$initialValueTypes` property. |
| 90 | + * |
| 91 | + * @return void |
| 92 | + */ |
| 93 | + protected function processInitialValue(File $phpcsFile, $stackPtr, $start, $end, $type) |
| 94 | + { |
| 95 | + if ($type === 'property' |
| 96 | + || ($type === 'const' |
| 97 | + && Scopes::validDirectScope($phpcsFile, $stackPtr, Collections::ooConstantScopes()) !== false) |
| 98 | + ) { |
| 99 | + // New is (still) not allowed in OO constants or properties. |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + $tokens = $phpcsFile->getTokens(); |
| 104 | + |
| 105 | + $targetNestingLevel = 0; |
| 106 | + if (isset($tokens[$start]['nested_parenthesis'])) { |
| 107 | + $targetNestingLevel = \count($tokens[$start]['nested_parenthesis']); |
| 108 | + } |
| 109 | + |
| 110 | + $error = self::ERROR_PHRASE; |
| 111 | + $errorCode = 'Found'; |
| 112 | + $phrase = ''; |
| 113 | + |
| 114 | + if (isset($this->initialValueTypes[$type]) === true) { |
| 115 | + $errorCode = MessageHelper::stringToErrorCode($type) . 'Found'; |
| 116 | + $phrase = $this->initialValueTypes[$type]; |
| 117 | + } |
| 118 | + |
| 119 | + $data = [$phrase]; |
| 120 | + |
| 121 | + $allowedNameTokens = Collections::namespacedNameTokens(); |
| 122 | + $allowedNameTokens[\T_SELF] = \T_SELF; |
| 123 | + $allowedNameTokens[\T_PARENT] = \T_PARENT; |
| 124 | + |
| 125 | + $current = $start; |
| 126 | + while (($hasNew = $phpcsFile->findNext(\T_NEW, $current, $end)) !== false) { |
| 127 | + // Handle nesting within arrays. |
| 128 | + $currentNestingLevel = 0; |
| 129 | + if (isset($tokens[$hasNew]['nested_parenthesis'])) { |
| 130 | + foreach ($tokens[$hasNew]['nested_parenthesis'] as $opener => $closer) { |
| 131 | + // Always count outer parentheses. |
| 132 | + if ($opener < $start) { |
| 133 | + ++$currentNestingLevel; |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + // Only count inner parentheses when they are not for an array. |
| 138 | + $owner = Parentheses::getOwner($phpcsFile, $opener); |
| 139 | + if ($owner === false || $tokens[$owner]['code'] !== \T_ARRAY) { |
| 140 | + ++$currentNestingLevel; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + $current = ($hasNew + 1); |
| 146 | + |
| 147 | + if ($currentNestingLevel !== $targetNestingLevel) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + |
| 151 | + // Only throw an error if this is a non-dynamic object instantiation. Dynamic is still not supported. |
| 152 | + $isNameInvalid = $phpcsFile->findNext($allowedNameTokens + Tokens::$emptyTokens, ($hasNew + 1), $end, true); |
| 153 | + $hasValidNameToken = $phpcsFile->findNext($allowedNameTokens, ($hasNew + 1), $end); |
| 154 | + |
| 155 | + if ($hasValidNameToken !== false |
| 156 | + && ($isNameInvalid === false |
| 157 | + || ($tokens[$isNameInvalid]['code'] === \T_OPEN_PARENTHESIS && $hasValidNameToken < $isNameInvalid)) |
| 158 | + ) { |
| 159 | + $phpcsFile->addError($error, $hasNew, $errorCode, $data); |
| 160 | + return; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments