22
33namespace OpenAPIExtractor ;
44
5- use PhpParser \Node \Expr ;
6- use PhpParser \Node \Expr \Array_ ;
7- use PhpParser \Node \Expr \ArrayItem ;
8- use PhpParser \Node \Expr \ConstFetch ;
9- use PhpParser \Node \Expr \UnaryMinus ;
105use PhpParser \Node \Param ;
11- use PhpParser \Node \Scalar \LNumber ;
12- use PhpParser \Node \Scalar \String_ ;
136
147class ControllerMethodParameter {
158 public OpenApiType $ type ;
@@ -21,46 +14,12 @@ public function __construct(string $context, array $definitions, public string $
2114 $ this ->type = OpenApiType::resolve ($ context , $ definitions , $ methodParameter ->type );
2215 }
2316 if ($ methodParameter ->default != null ) {
24- $ this ->type ->hasDefaultValue = true ;
25- $ this ->type ->defaultValue = self ::exprToValue ($ context , $ methodParameter ->default );
26- }
27- }
28-
29- private static function exprToValue (string $ context , Expr $ expr ): mixed {
30- if ($ expr instanceof ConstFetch) {
31- $ value = $ expr ->name ->getLast ();
32- return match ($ value ) {
33- "null " => null ,
34- "true " => true ,
35- "false " => false ,
36- default => Logger::panic ($ context , "Unable to evaluate constant value ' " . $ value . "' " ),
37- };
38- }
39- if ($ expr instanceof String_) {
40- return $ expr ->value ;
41- }
42- if ($ expr instanceof LNumber) {
43- return intval ($ expr ->value );
44- }
45- if ($ expr instanceof UnaryMinus) {
46- return -self ::exprToValue ($ context , $ expr ->expr );
47- }
48- if ($ expr instanceof Array_) {
49- $ values = array_map (fn (ArrayItem $ item ): mixed => self ::exprToValue ($ context , $ item ), $ expr ->items );
50- $ filteredValues = array_filter ($ values , fn (mixed $ value ) => $ value !== null );
51- if (count ($ filteredValues ) != count ($ values )) {
52- return null ;
17+ try {
18+ $ this ->type ->defaultValue = Helpers::exprToValue ($ context , $ methodParameter ->default );
19+ $ this ->type ->hasDefaultValue = true ;
20+ } catch (UnsupportedExprException $ e ) {
21+ Logger::debug ($ context , $ e );
5322 }
54- return $ values ;
55- }
56- if ($ expr instanceof ArrayItem) {
57- return self ::exprToValue ($ context , $ expr ->value );
5823 }
59- if ($ expr instanceof Expr \ClassConstFetch || $ expr instanceof Expr \BinaryOp) {
60- // Not supported
61- return null ;
62- }
63-
64- Logger::panic ($ context , "Unable to evaluate expression ' " . get_class ($ expr ) . "' " );
6524 }
6625}
0 commit comments