|
| 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\Attributes; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Files\File; |
| 14 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 15 | +use PHP_CodeSniffer\Util\Tokens; |
| 16 | +use PHPCSUtils\Utils\AttributeBlock; |
| 17 | + |
| 18 | +/** |
| 19 | + * Requires that when an attribute block is multi-line, it only contains a single attribute instantiation. |
| 20 | + * |
| 21 | + * @since 1.5.0 |
| 22 | + */ |
| 23 | +final class SingleAttributeInMultilineBlockSniff implements Sniff |
| 24 | +{ |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns an array of tokens this test wants to listen for. |
| 28 | + * |
| 29 | + * @since 1.5.0 |
| 30 | + * |
| 31 | + * @return array<int|string> |
| 32 | + */ |
| 33 | + public function register() |
| 34 | + { |
| 35 | + return [\T_ATTRIBUTE]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Processes this test, when one of its tokens is encountered. |
| 40 | + * |
| 41 | + * @since 1.5.0 |
| 42 | + * |
| 43 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 44 | + * @param int $stackPtr The position of the current token |
| 45 | + * in the stack passed in $tokens. |
| 46 | + * |
| 47 | + * @return void |
| 48 | + */ |
| 49 | + public function process(File $phpcsFile, $stackPtr) |
| 50 | + { |
| 51 | +// TODO |
| 52 | + |
| 53 | +/* |
| 54 | +This needs clarification from FIG |
| 55 | +- if an attribute is multiline because of params, that's one thing, but what if an attribute is multiline because there are a lot of attributes ? |
| 56 | +=> should those be split and made single-line attributes for each ? |
| 57 | +=> Actually, that will probably sort itself out automatically once the bracket spacing sniff kicks in |
| 58 | +*/ |
| 59 | + |
| 60 | + $tokens = $phpcsFile->getTokens(); |
| 61 | + |
| 62 | + if (isset($tokens[$stackPtr]['attribute_closer']) === false) { |
| 63 | + // Live coding/parse error. Ignore. |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if ($tokens[$stackPtr]['line'] === $tokens[$tokens[$stackPtr]['attribute_closer']]['line']) { |
| 68 | + // Single line, nothing to do. |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $nrOfAttributes = AttributeBlock::countAttributes($phpcsFile, $stackPtr); |
| 73 | + if ($nrOfAttributes <= 1) { |
| 74 | + // Single attribute, nothing to do. |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + $fix = $phpcsFile->addFixableError( |
| 79 | + 'A multi-line attribute block must only contain a single attribute instantiation. Found %d instantiations', |
| 80 | + $stackPtr, |
| 81 | + 'MultipleFound', |
| 82 | + [$nrOfAttributes] |
| 83 | + ); |
| 84 | + |
| 85 | + if ($fix === true) { |
| 86 | + // TODO: Figure out _original_ indent of opener |
| 87 | + $instantiations = AttributeBlock::getAttributes($phpcsFile, $stackPtr); |
| 88 | + // Remove last one of the attributes ??? (as that one doesn't need action _after_, though might be easier to skip the first one and make the fix at the start of the attribute ? |
| 89 | + // Should probably also remove leading whitespace, unless it is new line/indent |
| 90 | + // Should we also figure out if closer is on own line and perpetuate that if that's the case ? |
| 91 | +
|
| 92 | + $phpcsFile->fixer->beginChangeset(); |
| 93 | + foreach ($instantiations as $instantiation) { |
| 94 | + // If start and end of instantiation is on the same line -> add attribute closer on same line + new line char + indent + attribute opener |
| 95 | + // If start and end of instantiation are not on the same line -> ??? |
| 96 | + } |
| 97 | + $phpcsFile->fixer->endChangeset(); |
| 98 | + } |
| 99 | +/* |
| 100 | + if ($fix === true) { |
| 101 | + $phpcsFile->fixer->beginChangeset(); |
| 102 | +
|
| 103 | + for ($i = $opener; $i <= $closer; $i++) { |
| 104 | + if (isset(Tokens::$commentTokens[$tokens[$i]['code']]) === true) { |
| 105 | + continue; |
| 106 | + } |
| 107 | +
|
| 108 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 109 | + } |
| 110 | +
|
| 111 | + $phpcsFile->fixer->endChangeset(); |
| 112 | + } |
| 113 | +
|
| 114 | + $opener = $stackPtr; |
| 115 | + $closer = $tokens[$stackPtr]['attribute_closer']; |
| 116 | +
|
| 117 | + $instantiations = AttributeBlock::getAttributes($phpcsFile, $stackPtr); |
| 118 | +
|
| 119 | + foreach ($instantiations as $attribute) { |
| 120 | + $nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, ($attribute['name_token'] + 1), null, true); |
| 121 | +
|
| 122 | + // Note: no need to check for `false` as there will always be something after, if only the attribute closer. |
| 123 | + if ($tokens[$nextNonEmpty]['code'] !== \T_OPEN_PARENTHESIS) { |
| 124 | + // No parentheses found. |
| 125 | + $phpcsFile->recordMetric($attribute['name_token'], self::METRIC_NAME, 'no'); |
| 126 | + continue; |
| 127 | + } |
| 128 | +
|
| 129 | + if (isset($tokens[$nextNonEmpty]['parenthesis_closer']) === false) { |
| 130 | + /* |
| 131 | + * Incomplete set of parentheses. Ignore. |
| 132 | + * Shouldn't be possible as PHPCS won't have matched the attribute opener with the closer in that case. |
| 133 | + * / |
| 134 | + // @codeCoverageIgnoreStart |
| 135 | + $phpcsFile->recordMetric($attribute['name_token'], self::METRIC_NAME, 'yes'); |
| 136 | + continue; |
| 137 | + // @codeCoverageIgnoreEnd |
| 138 | + } |
| 139 | +
|
| 140 | + $opener = $nextNonEmpty; |
| 141 | + $closer = $tokens[$opener]['parenthesis_closer']; |
| 142 | + $hasParams = $phpcsFile->findNext(Tokens::$emptyTokens, ($opener + 1), $closer, true); |
| 143 | + if ($hasParams !== false) { |
| 144 | + // There is something between the parentheses. Ignore. |
| 145 | + $phpcsFile->recordMetric($attribute['name_token'], self::METRIC_NAME, 'yes, with parameter(s)'); |
| 146 | + continue; |
| 147 | + } |
| 148 | +
|
| 149 | + $phpcsFile->recordMetric($attribute['name_token'], self::METRIC_NAME, 'yes'); |
| 150 | +
|
| 151 | + $fix = $phpcsFile->addFixableError( |
| 152 | + 'Parenthesis not allowed when instantiating an attribute class without passing parameters', |
| 153 | + $opener, |
| 154 | + 'Found' |
| 155 | + ); |
| 156 | +
|
| 157 | + if ($fix === true) { |
| 158 | + $phpcsFile->fixer->beginChangeset(); |
| 159 | +
|
| 160 | + for ($i = $opener; $i <= $closer; $i++) { |
| 161 | + if (isset(Tokens::$commentTokens[$tokens[$i]['code']]) === true) { |
| 162 | + continue; |
| 163 | + } |
| 164 | +
|
| 165 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 166 | + } |
| 167 | +
|
| 168 | + $phpcsFile->fixer->endChangeset(); |
| 169 | + } |
| 170 | + } |
| 171 | + */ |
| 172 | + } |
| 173 | +} |
0 commit comments