Add missing param comment types based on literals sent to function#2873
Conversation
📝 WalkthroughWalkthroughA new PHP_CodeSniffer sniff is introduced that analyzes function and method definitions to automatically infer and populate missing Changes
Sequence DiagramsequenceDiagram
participant File as PHP File (Input)
participant Sniff as AddMissingParamTypeFromCalls<br/>Sniff
participant Cache as Per-File Cache
participant Parser as Docblock &<br/>Token Parser
participant Scanner as Call Site<br/>Scanner
participant Fixer as Docblock<br/>Fixer
File->>Sniff: Process T_FUNCTION token
Sniff->>Cache: Check/reset file state
Sniff->>Parser: Extract function definition<br/>& docblock
Parser->>Parser: Locate `@param` tags<br/>Identify missing params
Sniff->>Scanner: Scan file for call sites<br/>of this function
Scanner->>Scanner: Extract literal argument<br/>types from tokens
Scanner->>Sniff: Aggregate types per<br/>parameter index
Sniff->>Sniff: Determine params with<br/>inferred types
Sniff->>Sniff: Report fixable error<br/>for missing `@param`
Sniff->>Fixer: Request docblock update
Fixer->>Fixer: Append `@param` to last<br/>existing `@param` line
Fixer->>File: Update docblock
Estimated Code Review Effort🎯 4 (Complex) | ⏱️ ~75 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (4)
tests/phpunit/entries/test_FrmShowEntryShortcode.php (4)
826-831: Incorrect@paramorder in docblock.The
@paramannotations should be listed in the same order as the method parameters. The method signature istwo_cell_table_row( $field_key, $atts ), but the docblock lists$attsbefore$field_key.📝 Proposed fix
/** - * `@param` array $atts * `@param` string $field_key + * `@param` array $atts * * `@return` string */ protected function two_cell_table_row( $field_key, $atts ) {
867-873: Incorrect@paramorder in docblock.Same issue as
two_cell_table_row- the@paramfor$field_keyshould come before$attsto match the method signatureone_cell_table_row( $field_key, $atts ).📝 Proposed fix
/** - * `@param` array $atts * `@param` string $field_key + * `@param` array $atts * * `@return` string */ protected function one_cell_table_row( $field_key, $atts ) {
887-893: Incorrect@paramorder in docblock.Same issue - the
@paramfor$field_keyshould come before$attsto match the method signature.📝 Proposed fix
/** - * `@param` array $atts * `@param` string $field_key + * `@param` array $atts * * `@return` string */ protected function label_and_value_plain_text_row( $field_key, $atts ) {
910-916: Incorrect@paramorder in docblock.Same issue - the
@paramfor$field_keyshould come before$attsto match the method signature.📝 Proposed fix
/** - * `@param` array $atts * `@param` string $field_key + * `@param` array $atts * * `@return` string */ protected function single_value_plain_text_row( $field_key, $atts ) {
🧹 Nitpick comments (4)
phpcs-sniffs/Formidable/Sniffs/Commenting/AddMissingParamTypeFromCallsSniff.php (4)
106-115: Consider handling interfaces and enums.The class/method detection only checks for
T_CLASSandT_TRAIT. Methods defined in interfaces or enums (PHP 8.1+) won't be recognized as methods, potentially causing incorrect call site matching.♻️ Suggested improvement
- $classPtr = $phpcsFile->findPrevious( array( T_CLASS, T_TRAIT ), $stackPtr - 1 ); + $classPtr = $phpcsFile->findPrevious( array( T_CLASS, T_TRAIT, T_INTERFACE, T_ENUM ), $stackPtr - 1 );
288-288: Remove unused parameter$paramCount.The
$paramCountparameter is declared but never used in the method body. Either implement the intended validation logic or remove the parameter.♻️ Proposed fix
- private function findCallSiteTypes( File $phpcsFile, $functionName, $isMethod, $className, $paramCount ) { + private function findCallSiteTypes( File $phpcsFile, $functionName, $isMethod, $className ) {Also update the call site at line 118:
- $callSiteTypes = $this->findCallSiteTypes( $phpcsFile, $functionName, $isMethod, $className, count( $params ) ); + $callSiteTypes = $this->findCallSiteTypes( $phpcsFile, $functionName, $isMethod, $className );
427-427: Remove unused parameter$content.The
$contentparameter is declared but never used in the method body.♻️ Proposed fix
- private function getLiteralType( $code, $content ) { + private function getLiteralType( $code ) {Also update the call site at line 412:
- $literalType = $this->getLiteralType( $code, $content ); + $literalType = $this->getLiteralType( $code );
508-514: Hardcoded tab indentation may cause formatting inconsistencies.The new
@paramline uses hardcoded tab indentation ("\n\t *@param"). If the codebase uses spaces for indentation, this will produce inconsistent formatting. Consider detecting the existing indentation style from the docblock.
…pes_based_on_literals_sent_to_function Add missing param comment types based on literals sent to function
Summary by CodeRabbit
Release Notes
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.