|
| 1 | +<?php declare(strict_types=1); # -*- coding: utf-8 -*- |
| 2 | +/* |
| 3 | + * This file is part of the php-coding-standards package. |
| 4 | + * |
| 5 | + * (c) Inpsyde GmbH |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Inpsyde\Sniffs\CodeQuality; |
| 12 | + |
| 13 | +use PHP_CodeSniffer\Files\File; |
| 14 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 15 | +use PHP_CodeSniffer\Util\Tokens; |
| 16 | + |
| 17 | +class FunctionBodyStartSniff implements Sniff |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @return int[] |
| 21 | + */ |
| 22 | + public function register() |
| 23 | + { |
| 24 | + return [T_FUNCTION]; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param File $phpcsFile |
| 29 | + * @param int $stackPtr |
| 30 | + * @return void |
| 31 | + */ |
| 32 | + public function process(File $phpcsFile, $stackPtr) |
| 33 | + { |
| 34 | + $tokens = $phpcsFile->getTokens(); |
| 35 | + $token = $tokens[$stackPtr] ?? []; |
| 36 | + |
| 37 | + /** @var int $scopeOpener */ |
| 38 | + $scopeOpener = $token['scope_opener'] ?? -1; |
| 39 | + $scopeCloser = $token['scope_closer'] ?? -1; |
| 40 | + |
| 41 | + if ($scopeOpener < 0 || $scopeCloser < 0 || $scopeCloser <= $scopeOpener) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + $bodyStart = $phpcsFile->findNext([T_WHITESPACE], $scopeOpener + 1, null, true); |
| 46 | + if (!$bodyStart |
| 47 | + || !array_key_exists($bodyStart, $tokens) |
| 48 | + || $bodyStart <= $scopeOpener |
| 49 | + || $bodyStart >= $scopeCloser |
| 50 | + ) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + list($code, $message, $expectedLine) = $this->checkBodyStart( |
| 55 | + $bodyStart, |
| 56 | + $tokens[$scopeOpener]['line'] ?? -1, |
| 57 | + $token['line'] ?? -1, |
| 58 | + $phpcsFile |
| 59 | + ); |
| 60 | + |
| 61 | + if ($code && $message && $phpcsFile->addFixableWarning($message, $stackPtr, $code)) { |
| 62 | + $this->fix($bodyStart, $expectedLine, $scopeOpener, $phpcsFile); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param int $bodyStart |
| 68 | + * @param int $openerLine |
| 69 | + * @param int $functionLine |
| 70 | + * @param File $phpcsFile |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + private function checkBodyStart( |
| 74 | + int $bodyStart, |
| 75 | + int $openerLine, |
| 76 | + int $functionLine, |
| 77 | + File $phpcsFile |
| 78 | + ): array { |
| 79 | + |
| 80 | + $tokens = $phpcsFile->getTokens(); |
| 81 | + $bodyLine = $tokens[$bodyStart]['line'] ?? -1; |
| 82 | + |
| 83 | + $isMultiLineDeclare = ($openerLine - $functionLine) > 1; |
| 84 | + $isSingleLineDeclare = $openerLine === ($functionLine + 1); |
| 85 | + $isSingleLineSignature = $openerLine && $openerLine === $functionLine; |
| 86 | + |
| 87 | + $error = |
| 88 | + ($isMultiLineDeclare || $isSingleLineSignature) && $bodyLine !== ($openerLine + 2) |
| 89 | + || $isSingleLineDeclare && $bodyLine !== ($openerLine + 1); |
| 90 | + |
| 91 | + if (!$error) { |
| 92 | + return [null, null, null]; |
| 93 | + } |
| 94 | + |
| 95 | + $startWithComment = in_array($tokens[$bodyStart]['code'], Tokens::$emptyTokens, true); |
| 96 | + |
| 97 | + if (!$startWithComment && ($isMultiLineDeclare || $isSingleLineSignature)) { |
| 98 | + $where = $isSingleLineSignature === 'SingleLineSignature' |
| 99 | + ? 'with single-line signature and open curly bracket on same line' |
| 100 | + : 'where arguments declaration spans across multiple lines'; |
| 101 | + $code = $isSingleLineSignature |
| 102 | + ? 'WrongForSingleLineSignature' |
| 103 | + : 'WrongForMultiLineDeclaration'; |
| 104 | + |
| 105 | + return [ |
| 106 | + $code, |
| 107 | + "In functions {$where}, function body should start with a blank line.", |
| 108 | + $openerLine + 2 |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + if (!$isSingleLineDeclare) { |
| 113 | + return [null, null, null]; |
| 114 | + } |
| 115 | + |
| 116 | + $message = 'In functions where arguments declaration is in a single line and curly bracket ' |
| 117 | + . 'is on next line, function body should start in the line below opened curly bracket.'; |
| 118 | + |
| 119 | + return [ |
| 120 | + 'WrongForSingleLineDeclaration', |
| 121 | + $message, |
| 122 | + $openerLine + 1 |
| 123 | + ]; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param int $bodyStart |
| 128 | + * @param int $expectedLine |
| 129 | + * @param int $scopeOpener |
| 130 | + * @param File $file |
| 131 | + */ |
| 132 | + private function fix(int $bodyStart, int $expectedLine, int $scopeOpener, File $file) |
| 133 | + { |
| 134 | + $tokens = $file->getTokens(); |
| 135 | + $currentLine = $tokens[$bodyStart]['line'] ?? -1; |
| 136 | + |
| 137 | + if ($currentLine === $expectedLine) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + $fixer = $file->fixer; |
| 142 | + $fixer->beginChangeset(); |
| 143 | + |
| 144 | + if ($currentLine < $expectedLine) { |
| 145 | + for ($i = ($expectedLine - $currentLine); $i > 0; $i--) { |
| 146 | + $fixer->addNewline($scopeOpener); |
| 147 | + } |
| 148 | + $fixer->endChangeset(); |
| 149 | + |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + for ($i = $bodyStart - 1; $i > 0; $i--) { |
| 154 | + $line = $tokens[$i]['line']; |
| 155 | + if ($line === $currentLine) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + if ($line < $expectedLine) { |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + $fixer->replaceToken($i, ''); |
| 163 | + } |
| 164 | + |
| 165 | + $fixer->endChangeset(); |
| 166 | + } |
| 167 | +} |
0 commit comments