|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SymPressCS\SymPress\Sniffs\Classes; |
| 6 | + |
| 7 | +use PHP_CodeSniffer\Files\File; |
| 8 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 9 | +use SymPressCS\SymPress\Helpers\Names; |
| 10 | + |
| 11 | +final class ClassLengthSniff implements Sniff |
| 12 | +{ |
| 13 | + public bool $ignoreBlankLines = true; |
| 14 | + public bool $ignoreComments = true; |
| 15 | + public bool $ignoreDocBlocks = true; |
| 16 | + public int $maxLength = 500; |
| 17 | + |
| 18 | + /** @return list<int|string> */ |
| 19 | + public function register(): array |
| 20 | + { |
| 21 | + return [ |
| 22 | + T_CLASS, |
| 23 | + T_ENUM, |
| 24 | + T_INTERFACE, |
| 25 | + T_TRAIT, |
| 26 | + ]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param File $phpcsFile |
| 31 | + * @param int $stackPtr |
| 32 | + */ |
| 33 | + public function process(File $phpcsFile, $stackPtr): void |
| 34 | + { |
| 35 | + $length = $this->structureLinesCount($phpcsFile, $stackPtr); |
| 36 | + if ($length <= $this->maxLength) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + $ignored = $this->ignoredLines(); |
| 41 | + $ignoring = $ignored ? sprintf(' (ignoring: %s)', implode(', ', $ignored)) : ''; |
| 42 | + $tokenTypeName = Names::tokenTypeName($phpcsFile, $stackPtr); |
| 43 | + |
| 44 | + $phpcsFile->addWarning( |
| 45 | + '%s length (%d) exceeds allowed maximum of %d lines%s', |
| 46 | + $stackPtr, |
| 47 | + 'TooLong', |
| 48 | + [ucfirst($tokenTypeName), $length, $this->maxLength, $ignoring], |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + private function structureLinesCount(File $file, int $position): int |
| 53 | + { |
| 54 | + /** @var array<int, array<string, mixed>> $tokens */ |
| 55 | + $tokens = $file->getTokens(); |
| 56 | + $token = $tokens[$position] ?? []; |
| 57 | + |
| 58 | + if ( |
| 59 | + !array_key_exists('scope_opener', $token) |
| 60 | + || !array_key_exists('scope_closer', $token) |
| 61 | + ) { |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + $start = (int) $token['scope_opener']; |
| 66 | + $end = (int) $token['scope_closer']; |
| 67 | + $length = (int) $tokens[$end]['line'] - (int) $tokens[$start]['line']; |
| 68 | + |
| 69 | + if ($length <= $this->maxLength) { |
| 70 | + return $length; |
| 71 | + } |
| 72 | + |
| 73 | + return $length - $this->collectLinesToExclude($start, $end, $tokens); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param int $start |
| 78 | + * @param int $end |
| 79 | + * @param array<int, array<string, mixed>> $tokens |
| 80 | + */ |
| 81 | + private function collectLinesToExclude(int $start, int $end, array $tokens): int |
| 82 | + { |
| 83 | + $docBlocks = []; |
| 84 | + $linesData = []; |
| 85 | + $skipLines = [$tokens[$start + 1]['line'], $tokens[$end]['line']]; |
| 86 | + |
| 87 | + for ($i = $start + 1; $i < $end - 1; $i++) { |
| 88 | + if (in_array($tokens[$i]['line'], $skipLines, true)) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $docBlocks = $this->docBlocksData($tokens, $i, $docBlocks); |
| 93 | + $linesData = $this->ignoredLinesData($tokens[$i], $linesData); |
| 94 | + } |
| 95 | + |
| 96 | + $empty = array_filter(array_column($linesData, 'empty')); |
| 97 | + $onlyComment = array_filter(array_column($linesData, 'only-comment')); |
| 98 | + $toExcludeCount = (int) array_sum($docBlocks); |
| 99 | + |
| 100 | + if ($this->ignoreBlankLines) { |
| 101 | + $toExcludeCount += count($empty); |
| 102 | + } |
| 103 | + if ($this->ignoreComments) { |
| 104 | + $toExcludeCount += count($onlyComment) - count($empty); |
| 105 | + } |
| 106 | + |
| 107 | + return $toExcludeCount; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param array<string, mixed> $token |
| 112 | + * @param array<int, array{empty:bool, only-comment:bool}> $lines |
| 113 | + * @return array<int, array{empty:bool, only-comment:bool}> |
| 114 | + */ |
| 115 | + private function ignoredLinesData(array $token, array $lines): array |
| 116 | + { |
| 117 | + $line = (int) $token['line']; |
| 118 | + if (!array_key_exists($line, $lines)) { |
| 119 | + $lines[$line] = ['empty' => true, 'only-comment' => true]; |
| 120 | + } |
| 121 | + |
| 122 | + if (!in_array($token['code'], [T_COMMENT, T_WHITESPACE], true)) { |
| 123 | + $lines[$line]['only-comment'] = false; |
| 124 | + } |
| 125 | + |
| 126 | + if ($token['code'] !== T_WHITESPACE) { |
| 127 | + $lines[$line]['empty'] = false; |
| 128 | + } |
| 129 | + |
| 130 | + return $lines; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @param array<int, array<string, mixed>> $tokens |
| 135 | + * @param int $position |
| 136 | + * @param list<int> $docBlocks |
| 137 | + * @return list<int> |
| 138 | + */ |
| 139 | + private function docBlocksData(array $tokens, int $position, array $docBlocks): array |
| 140 | + { |
| 141 | + if ( |
| 142 | + !$this->ignoreDocBlocks |
| 143 | + || $tokens[$position]['code'] !== T_DOC_COMMENT_OPEN_TAG |
| 144 | + ) { |
| 145 | + return $docBlocks; |
| 146 | + } |
| 147 | + |
| 148 | + $closer = $tokens[$position]['comment_closer'] ?? null; |
| 149 | + |
| 150 | + $docBlocks[] = is_numeric($closer) |
| 151 | + ? 1 + (int) $tokens[(int) $closer]['line'] - (int) $tokens[$position]['line'] |
| 152 | + : 1; |
| 153 | + |
| 154 | + return $docBlocks; |
| 155 | + } |
| 156 | + |
| 157 | + /** @return list<string> */ |
| 158 | + private function ignoredLines(): array |
| 159 | + { |
| 160 | + $ignored = []; |
| 161 | + $flags = [ |
| 162 | + 'ignoreBlankLines' => 'blank lines', |
| 163 | + 'ignoreComments' => 'inline comments', |
| 164 | + 'ignoreDocBlocks' => 'doc blocks', |
| 165 | + ]; |
| 166 | + |
| 167 | + foreach ($flags as $flag => $type) { |
| 168 | + if (!filter_var($this->{$flag}, FILTER_VALIDATE_BOOLEAN)) { |
| 169 | + continue; |
| 170 | + } |
| 171 | + |
| 172 | + $ignored[] = $type; |
| 173 | + } |
| 174 | + |
| 175 | + return $ignored; |
| 176 | + } |
| 177 | +} |
0 commit comments