@@ -190,7 +190,7 @@ public static function parse(string $context,
190190 $ description = '' ;
191191 }
192192 // Only keep lines that don't match the status code pattern in the description
193- $ description = implode ("\n" , array_filter (array_filter (explode ("\n" , $ description ), static fn (string $ line ) => trim ($ line ) !== '' ), static fn (string $ line ) => !preg_match (self ::STATUS_CODE_DESCRIPTION_PATTERN , $ line )));
193+ $ description = Helpers:: cleanDocComment ( implode ("\n" , array_filter (array_filter (explode ("\n" , $ description ), static fn (string $ line ) => trim ($ line ) !== '' ), static fn (string $ line ) => !preg_match (self ::STATUS_CODE_DESCRIPTION_PATTERN , $ line) )));
194194
195195 if ($ paramTag instanceof \PHPStan \PhpDocParser \Ast \PhpDoc \ParamTagValueNode && $ psalmParamTag instanceof \PHPStan \PhpDocParser \Ast \PhpDoc \ParamTagValueNode) {
196196 try {
@@ -226,17 +226,24 @@ public static function parse(string $context,
226226 } elseif ($ paramTag instanceof \PHPStan \PhpDocParser \Ast \PhpDoc \ParamTagValueNode) {
227227 $ type = OpenApiType::resolve ($ context . ': @param: ' . $ methodParameterName , $ definitions , $ paramTag );
228228 } elseif ($ allowMissingDocs ) {
229- $ type = null ;
229+ $ type = OpenApiType:: resolve ( $ context . ' : $ ' . $ methodParameterName . ' : ' . $ methodParameterName , $ definitions , $ methodParameter -> type ) ;
230230 } else {
231231 Logger::error ($ context , "Missing doc parameter for ' " . $ methodParameterName . "' " );
232232 continue ;
233233 }
234234
235- if ($ type !== null ) {
236- $ type ->description = $ description ;
235+ $ type ->description = $ description ;
236+
237+ if ($ methodParameter ->default !== null ) {
238+ try {
239+ $ type ->defaultValue = Helpers::exprToValue ($ context , $ methodParameter ->default );
240+ $ type ->hasDefaultValue = true ;
241+ } catch (UnsupportedExprException $ e ) {
242+ Logger::debug ($ context , $ e );
243+ }
237244 }
238245
239- $ param = new ControllerMethodParameter ($ context , $ definitions , $ methodParameterName , $ methodParameter , $ type );
246+ $ param = new ControllerMethodParameter ($ context , $ definitions , $ methodParameterName , $ type );
240247
241248 if (!$ allowMissingDocs && $ param ->type ->description == '' ) {
242249 Logger::error ($ context . ': @param: ' . $ methodParameterName , 'Missing description ' );
@@ -284,9 +291,38 @@ public static function parse(string $context,
284291 if ($ methodCall ->var instanceof PropertyFetch &&
285292 $ methodCall ->var ->var instanceof Variable &&
286293 $ methodCall ->var ->var ->name === 'this ' &&
287- $ methodCall ->var ->name ->name === 'request ' &&
288- $ methodCall ->name ->name === 'getHeader ' ) {
289- $ headers [] = Helpers::exprToValue ($ context . ': getHeader ' , $ methodCall ->args [0 ]->value );
294+ $ methodCall ->var ->name ->name === 'request ' ) {
295+ if ($ methodCall ->name ->name === 'getHeader ' ) {
296+ $ headers [] = $ methodCall ->args [0 ]->value ->value ;
297+ }
298+ if ($ methodCall ->name ->name === 'getParam ' ) {
299+ $ name = $ methodCall ->args [0 ]->value ->value ;
300+
301+ if (preg_match ('/^[a-zA-Z][a-zA-Z0-9_]*$/ ' , $ name )) {
302+ Logger::error ($ context . ': getParam: ' . $ name , 'Do not use getParam() when a controller method parameter also works. With getParam() it is not possible to add a comment and specify the parameter type, therefore it should be avoided whenever possible. ' );
303+ }
304+
305+ $ defaultValue = null ;
306+ $ hasDefaultValue = false ;
307+ try {
308+ $ defaultValue = count ($ methodCall ->args ) > 1 ? Helpers::exprToValue ($ context . ': getParam: ' . $ name , $ methodCall ->args [1 ]->value ) : null ;
309+ $ hasDefaultValue = true ;
310+ } catch (UnsupportedExprException $ e ) {
311+ Logger::debug ($ context , $ e );
312+ }
313+
314+ $ type = new OpenApiType (
315+ context: $ context ,
316+ // We can not know the type, so need to fallback to object :/
317+ type: 'object ' ,
318+ // IRequest::getParam() has null as a default value, so the parameter always has a default value and allows null.
319+ nullable: true ,
320+ hasDefaultValue: $ hasDefaultValue ,
321+ defaultValue: $ defaultValue ,
322+ );
323+
324+ $ parameters [] = new ControllerMethodParameter ($ context , $ definitions , $ name , $ type );
325+ }
290326 }
291327 }
292328
0 commit comments