Skip to content

Commit 41b1479

Browse files
committed
Dont require docblocks for fully typed methods.
1 parent 56dcef3 commit 41b1479

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

PhpCollective/Sniffs/Commenting/DocBlockSniff.php

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PhpCollective\Traits\SignatureTrait;
1515

1616
/**
17-
* Methods always need doc blocks.
17+
* Methods always need doc blocks if they are using non-typed params/return.
1818
* Constructor and destructor may not have one if they do not have arguments.
1919
*/
2020
class DocBlockSniff extends AbstractSniff
@@ -60,6 +60,10 @@ public function process(File $phpcsFile, $stackPtr): void
6060
return;
6161
}
6262

63+
if ($this->isFullyTyped($phpcsFile, $stackPtr)) {
64+
return;
65+
}
66+
6367
// We only look for void methods right now
6468
$returnType = $this->detectReturnTypeVoid($phpcsFile, $stackPtr);
6569
if ($returnType === null) {
@@ -133,31 +137,6 @@ protected function checkConstructorAndDestructor(File $phpcsFile, int $stackPtr)
133137
$phpcsFile->addError('Missing doc block for method', $stackPtr, 'ConstructDesctructMissingDocBlock');
134138
}
135139

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-
161140
/**
162141
* For right now we only try to detect void.
163142
*
@@ -202,4 +181,36 @@ protected function detectReturnTypeVoid(File $phpcsFile, int $index): ?string
202181

203182
return $type;
204183
}
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+
}
205216
}

0 commit comments

Comments
 (0)