|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * DisallowVoidType sniff for yCodeTech PHPCS Standard. |
| 5 | + * |
| 6 | + * Disallows explicit void return type declarations on functions and methods. |
| 7 | + * |
| 8 | + * @category PHP |
| 9 | + * @package PHP_CodeSniffer |
| 10 | + * @author yCodeTech |
| 11 | + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
| 12 | + */ |
| 13 | + |
| 14 | +namespace yCodeTech\Sniffs\Types; |
| 15 | + |
| 16 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 17 | +use PHP_CodeSniffer\Files\File; |
| 18 | + |
| 19 | +/** |
| 20 | + * DisallowVoidType sniff. |
| 21 | + * |
| 22 | + * Disallows explicit `: void` return type declarations on functions, methods, |
| 23 | + * and closures. The absence of a return type already implies void. |
| 24 | + */ |
| 25 | +class DisallowVoidTypeSniff implements Sniff |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Returns an array of tokens this test wants to listen for. |
| 29 | + * |
| 30 | + * @return array<int> |
| 31 | + */ |
| 32 | + public function register() |
| 33 | + { |
| 34 | + return [T_FUNCTION, T_CLOSURE]; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Processes this test, when one of its tokens is encountered. |
| 39 | + * |
| 40 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 41 | + * @param int $stackPtr The position of the current token in the |
| 42 | + * stack passed in $tokens. |
| 43 | + */ |
| 44 | + public function process(File $phpcsFile, $stackPtr) |
| 45 | + { |
| 46 | + $voidPtr = $this->getExplicitVoidTypePtr($phpcsFile, $stackPtr); |
| 47 | + if ($voidPtr === false) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $error = 'Explicit void return type is not allowed and should be removed'; |
| 52 | + $fix = $phpcsFile->addFixableError($error, $voidPtr, 'Found'); |
| 53 | + if ($fix === true) { |
| 54 | + $this->removeExplicitVoidType($phpcsFile, $stackPtr); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the position of the explicit `void` return type token, if present. |
| 60 | + * |
| 61 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 62 | + * @param int $stackPtr The position of the function token. |
| 63 | + * |
| 64 | + * @return int|false The position of the void token, or false if not found. |
| 65 | + */ |
| 66 | + private function getExplicitVoidTypePtr(File $phpcsFile, $stackPtr) |
| 67 | + { |
| 68 | + $tokens = $phpcsFile->getTokens(); |
| 69 | + |
| 70 | + $openParen = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr); |
| 71 | + if ($openParen === false) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + $closeParen = $tokens[$openParen]['parenthesis_closer'] ?? null; |
| 76 | + if ($closeParen === null) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + $scopeOpener = $tokens[$stackPtr]['scope_opener'] ?? null; |
| 81 | + $semicolonPtr = $phpcsFile->findNext(T_SEMICOLON, $closeParen + 1); |
| 82 | + $searchEnd = $scopeOpener ?? ($semicolonPtr !== false ? $semicolonPtr + 1 : null); |
| 83 | + |
| 84 | + $colonPtr = $phpcsFile->findNext(T_COLON, $closeParen + 1, $searchEnd); |
| 85 | + if ($colonPtr === false) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + $returnTypePtr = $phpcsFile->findNext(T_WHITESPACE, $colonPtr + 1, null, true); |
| 90 | + if ($returnTypePtr === false) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + if ($tokens[$returnTypePtr]['code'] === T_STRING && $tokens[$returnTypePtr]['content'] === 'void') { |
| 95 | + return $returnTypePtr; |
| 96 | + } |
| 97 | + |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Remove explicit `: void` return type from a function signature. |
| 103 | + * |
| 104 | + * Removes the colon, any whitespace between colon and void, and the `void` |
| 105 | + * token itself, leaving the rest of the signature intact. |
| 106 | + * |
| 107 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
| 108 | + * @param int $stackPtr The position of the function token. |
| 109 | + */ |
| 110 | + private function removeExplicitVoidType(File $phpcsFile, $stackPtr) |
| 111 | + { |
| 112 | + $tokens = $phpcsFile->getTokens(); |
| 113 | + |
| 114 | + $openParen = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr); |
| 115 | + if ($openParen === false) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + $closeParen = $tokens[$openParen]['parenthesis_closer'] ?? null; |
| 120 | + if ($closeParen === null) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + $scopeOpener = $tokens[$stackPtr]['scope_opener'] ?? null; |
| 125 | + $semicolonPtr = $phpcsFile->findNext(T_SEMICOLON, $closeParen + 1); |
| 126 | + $searchEnd = $scopeOpener ?? ($semicolonPtr !== false ? $semicolonPtr + 1 : null); |
| 127 | + |
| 128 | + $colonPtr = $phpcsFile->findNext(T_COLON, $closeParen + 1, $searchEnd); |
| 129 | + if ($colonPtr === false) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + $voidPtr = $phpcsFile->findNext(T_WHITESPACE, $colonPtr + 1, null, true); |
| 134 | + if ($voidPtr === false || $tokens[$voidPtr]['content'] !== 'void') { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + $phpcsFile->fixer->beginChangeset(); |
| 139 | + for ($i = $colonPtr; $i <= $voidPtr; $i++) { |
| 140 | + $phpcsFile->fixer->replaceToken($i, ''); |
| 141 | + } |
| 142 | + $phpcsFile->fixer->endChangeset(); |
| 143 | + } |
| 144 | +} |
0 commit comments