44
55namespace  Riverwaysoft \PhpConverter \Ast ;
66
7+ use  PHPStan \PhpDocParser \Ast \PhpDoc \ParamTagValueNode ;
8+ use  PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocChildNode ;
9+ use  PHPStan \PhpDocParser \Ast \PhpDoc \ReturnTagValueNode ;
710use  PHPStan \PhpDocParser \Ast \PhpDoc \PhpDocTagNode ;
11+ use  PHPStan \PhpDocParser \Ast \PhpDoc \TemplateTagValueNode ;
812use  PHPStan \PhpDocParser \Ast \PhpDoc \VarTagValueNode ;
913use  PHPStan \PhpDocParser \Ast \Type \ArrayTypeNode ;
14+ use  PHPStan \PhpDocParser \Ast \Type \GenericTypeNode ;
1015use  PHPStan \PhpDocParser \Ast \Type \IdentifierTypeNode ;
1116use  PHPStan \PhpDocParser \Ast \Type \TypeNode ;
1217use  PHPStan \PhpDocParser \Ast \Type \UnionTypeNode ;
1520use  PHPStan \PhpDocParser \Parser \PhpDocParser ;
1621use  PHPStan \PhpDocParser \Parser \TokenIterator ;
1722use  PHPStan \PhpDocParser \Parser \TypeParser ;
23+ use  Riverwaysoft \PhpConverter \Dto \DtoClassProperty ;
1824use  Riverwaysoft \PhpConverter \Dto \PhpType \PhpListType ;
1925use  Riverwaysoft \PhpConverter \Dto \PhpType \PhpTypeFactory ;
2026use  Riverwaysoft \PhpConverter \Dto \PhpType \PhpTypeInterface ;
2127use  Riverwaysoft \PhpConverter \Dto \PhpType \PhpUnionType ;
28+ use  Riverwaysoft \PhpConverter \Dto \PhpType \PhpUnknownType ;
2229use  function  array_map ;
2330
2431class  PhpDocTypeParser
@@ -35,27 +42,95 @@ public function __construct()
3542        $ this phpDocParser  = new  PhpDocParser ($ typeParser$ constExprParser
3643    }
3744
38-     public  function  parse (string  $ inputPhpTypeInterface null 
45+     /** @return DtoClassProperty[] */ 
46+     public  function  parseMethodParams (string  $ inputarray 
3947    {
40-         $ tokens new   TokenIterator ( $ this lexer -> tokenize ($ input) );
41-         $ result  =  $ this -> phpDocParser -> parse ( $ tokens )-> children ; 
42-         $ varTagNode null ;
48+         $ phpDocNodes $ this commentToPhpDocNodes ($ input
49+         /** @var DtoClassProperty[] $results */ 
50+         $ results [] ;
4351
44-         foreach  ($ result as  $ node
52+         foreach  ($ phpDocNodes as  $ node
4553            if  (!$ nodeinstanceof  PhpDocTagNode) {
4654                continue ;
4755            }
4856
49-             if  ($ nodevalue  instanceof  VarTagValueNode) {
50-                 $ varTagNode$ nodevalue ;
57+             if  (!($ nodevalue  instanceof  ParamTagValueNode)) {
58+                 continue ;
59+             }
60+ 
61+             $ convertedType$ this convertToDto ($ nodevalue ->type );
62+             if  (!$ convertedType
63+                 continue ;
5164            }
65+ 
66+             $ variableName$ this getVariableNameWithoutDollarSign ($ nodevalue );
67+             if  (!$ variableName
68+                 continue ;
69+             }
70+ 
71+             $ resultsnew  DtoClassProperty (
72+                 type: $ convertedType
73+                 name: $ variableName
74+             );
5275        }
5376
54-         if  (!$ varTagNode
77+         return  $ results
78+     }
79+ 
80+     private  function  getVariableNameWithoutDollarSign (ParamTagValueNode $ nodeValuestring |null 
81+     {
82+         $ isParameterNameValidstr_starts_with ($ nodeValueparameterName , '$ ' ) && mb_strlen ($ nodeValueparameterName ) > 1 ;
83+         if  (!$ isParameterNameValid
5584            return  null ;
5685        }
86+         return  ltrim ($ nodeValueparameterName , '$ ' );
87+     }
88+ 
89+     /** @return PhpUnknownType[]  */ 
90+     public  function  parseClassComments (string  $ inputarray 
91+     {
92+         $ phpDocNodes$ this commentToPhpDocNodes ($ input
93+         /** @var PhpUnknownType[] $generics */ 
94+         $ generics
95+ 
96+         foreach  ($ phpDocNodesas  $ node
97+             if  (!$ nodeinstanceof  PhpDocTagNode) {
98+                 continue ;
99+             }
100+ 
101+             if  (!($ nodevalue  instanceof  TemplateTagValueNode)) {
102+                 continue ;
103+             }
104+             $ genericsnew  PhpUnknownType ($ nodevalue ->name );
105+         }
106+ 
107+         return  $ generics
108+     }
57109
58-         return  $ this convertToDto ($ varTagNodetype );
110+     /** @return PhpDocChildNode[] */ 
111+     private  function  commentToPhpDocNodes (string  $ inputarray 
112+     {
113+         $ tokensnew  TokenIterator ($ this lexer ->tokenize ($ input
114+         return  $ this phpDocParser ->parse ($ tokenschildren ;
115+     }
116+ 
117+     public  function  parseVarOrReturn (string  $ inputPhpTypeInterface null 
118+     {
119+         $ phpDocNodes$ this commentToPhpDocNodes ($ input
120+ 
121+         foreach  ($ phpDocNodesas  $ node
122+             if  (!$ nodeinstanceof  PhpDocTagNode) {
123+                 continue ;
124+             }
125+ 
126+             if  (!($ nodevalue  instanceof  VarTagValueNode) && !($ nodevalue  instanceof  ReturnTagValueNode)) {
127+                 continue ;
128+             }
129+ 
130+             return  $ this convertToDto ($ nodevalue ->type );
131+         }
132+ 
133+         return  null ;
59134    }
60135
61136    private  function  convertToDto (TypeNode $ nodePhpTypeInterface null 
@@ -69,6 +144,12 @@ private function convertToDto(TypeNode $node): PhpTypeInterface|null
69144        if  ($ nodeinstanceof  UnionTypeNode) {
70145            return  new  PhpUnionType (array_map (fn  (TypeNode $ child$ this convertToDto ($ child$ nodetypes ));
71146        }
147+         if  ($ nodeinstanceof  GenericTypeNode) {
148+             return  PhpTypeFactory::create ($ nodetype ->name , [], array_map (
149+                 fn  (TypeNode $ child$ this convertToDto ($ child
150+                 $ nodegenericTypes ,
151+             ));
152+         }
72153        return  null ;
73154    }
74155}
0 commit comments