|
14 | 14 | use PhpCollective\Traits\SignatureTrait;
|
15 | 15 |
|
16 | 16 | /**
|
17 |
| - * Methods always need doc blocks. |
| 17 | + * Methods always need doc blocks if they are using non-typed params/return. |
18 | 18 | * Constructor and destructor may not have one if they do not have arguments.
|
19 | 19 | */
|
20 | 20 | class DocBlockSniff extends AbstractSniff
|
@@ -60,6 +60,10 @@ public function process(File $phpcsFile, $stackPtr): void
|
60 | 60 | return;
|
61 | 61 | }
|
62 | 62 |
|
| 63 | + if ($this->isFullyTyped($phpcsFile, $stackPtr)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
63 | 67 | // We only look for void methods right now
|
64 | 68 | $returnType = $this->detectReturnTypeVoid($phpcsFile, $stackPtr);
|
65 | 69 | if ($returnType === null) {
|
@@ -133,31 +137,6 @@ protected function checkConstructorAndDestructor(File $phpcsFile, int $stackPtr)
|
133 | 137 | $phpcsFile->addError('Missing doc block for method', $stackPtr, 'ConstructDesctructMissingDocBlock');
|
134 | 138 | }
|
135 | 139 |
|
136 |
| - /** |
137 |
| - * @param \PHP_CodeSniffer\Files\File $phpcsFile |
138 |
| - * @param int $docBlockStartIndex |
139 |
| - * @param int $docBlockEndIndex |
140 |
| - * |
141 |
| - * @return int|null |
142 |
| - */ |
143 |
| - protected function findDocBlockReturn(File $phpcsFile, int $docBlockStartIndex, int $docBlockEndIndex): ?int |
144 |
| - { |
145 |
| - $tokens = $phpcsFile->getTokens(); |
146 |
| - |
147 |
| - for ($i = $docBlockStartIndex + 1; $i < $docBlockEndIndex; $i++) { |
148 |
| - if (!$this->isGivenKind(T_DOC_COMMENT_TAG, $tokens[$i])) { |
149 |
| - continue; |
150 |
| - } |
151 |
| - if ($tokens[$i]['content'] !== '@return') { |
152 |
| - continue; |
153 |
| - } |
154 |
| - |
155 |
| - return $i; |
156 |
| - } |
157 |
| - |
158 |
| - return null; |
159 |
| - } |
160 |
| - |
161 | 140 | /**
|
162 | 141 | * For right now we only try to detect void.
|
163 | 142 | *
|
@@ -202,4 +181,36 @@ protected function detectReturnTypeVoid(File $phpcsFile, int $index): ?string
|
202 | 181 |
|
203 | 182 | return $type;
|
204 | 183 | }
|
| 184 | + |
| 185 | + /** |
| 186 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile |
| 187 | + * @param int $stackPtr |
| 188 | + * |
| 189 | + * @return bool |
| 190 | + */ |
| 191 | + protected function isFullyTyped(File $phpcsFile, int $stackPtr): bool |
| 192 | + { |
| 193 | + $tokens = $phpcsFile->getTokens(); |
| 194 | + |
| 195 | + // Get the function's parameter tokens |
| 196 | + $params = $phpcsFile->getMethodParameters($stackPtr); |
| 197 | + // Check all parameters have a type hint |
| 198 | + foreach ($params as $param) { |
| 199 | + if (empty($param['type_hint'])) { |
| 200 | + return false; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // Check for return type |
| 205 | + $hasReturnType = isset($tokens[$stackPtr]['parenthesis_closer']) && |
| 206 | + isset($tokens[$stackPtr]['scope_opener']); |
| 207 | + |
| 208 | + $colonPtr = $phpcsFile->findNext(T_COLON, $tokens[$stackPtr]['parenthesis_closer'], $tokens[$stackPtr]['scope_opener']); |
| 209 | + |
| 210 | + if ($colonPtr === false) { |
| 211 | + return false; // No return type |
| 212 | + } |
| 213 | + |
| 214 | + return true; |
| 215 | + } |
205 | 216 | }
|
0 commit comments