From 7bf6fddb99eff7c47541cc18a57bd4ec57c419af Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 23 Jan 2026 11:17:40 -0400 Subject: [PATCH] Add more rules to the new missing types in param comment sniff --- .../Commenting/AddMissingParamTypeSniff.php | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/phpcs-sniffs/Formidable/Sniffs/Commenting/AddMissingParamTypeSniff.php b/phpcs-sniffs/Formidable/Sniffs/Commenting/AddMissingParamTypeSniff.php index f465cde47f..47e6a0dfe4 100644 --- a/phpcs-sniffs/Formidable/Sniffs/Commenting/AddMissingParamTypeSniff.php +++ b/phpcs-sniffs/Formidable/Sniffs/Commenting/AddMissingParamTypeSniff.php @@ -426,6 +426,15 @@ private function typeIncludesType( $existingType, $checkType ) { } } + // If checking for 'object', treat any class name as covering it. + if ( $checkType === 'object' ) { + foreach ( $existingTypes as $existing ) { + if ( $this->isClassName( $existing ) ) { + return true; + } + } + } + foreach ( $checkTypes as $singleType ) { foreach ( $existingTypes as $existing ) { // Normalize types for comparison. @@ -446,6 +455,42 @@ private function typeIncludesType( $existingType, $checkType ) { return false; } + /** + * Check if a type string looks like a class name. + * + * @param string $type The type to check. + * + * @return bool + */ + private function isClassName( $type ) { + $type = trim( $type ); + + // Skip primitive types and known non-class types. + $primitives = array( + 'int', 'integer', 'float', 'double', 'real', 'string', 'bool', 'boolean', + 'array', 'object', 'null', 'mixed', 'void', 'callable', 'iterable', + 'resource', 'true', 'false', 'self', 'static', 'parent', + ); + + $lowerType = strtolower( $type ); + + if ( in_array( $lowerType, $primitives, true ) ) { + return false; + } + + // Skip if it's an array notation. + if ( preg_match( '/\[\]$/', $type ) || preg_match( '/^array\s*[<{]/', $lowerType ) ) { + return false; + } + + // If it starts with uppercase or contains backslash (namespace), it's likely a class. + if ( preg_match( '/^[A-Z]/', $type ) || strpos( $type, '\\' ) !== false ) { + return true; + } + + return false; + } + /** * Normalize a type for comparison. * @@ -476,6 +521,11 @@ private function normalizeType( $type ) { return 'array'; } + // Normalize shorthand array notation (int[], string[], etc.) to just 'array'. + if ( preg_match( '/\[\]$/', $type ) ) { + return 'array'; + } + return $type; }