@@ -47,7 +47,7 @@ class PhpParser
4747 public function __construct ()
4848 {
4949 $ parserFactory = new ParserFactory ();
50- $ this ->parser = $ parserFactory ->create (ParserFactory:: ONLY_PHP7 );
50+ $ this ->parser = $ parserFactory ->createForNewestSupportedVersion ( );
5151 }
5252
5353 public static function getInstance (): PhpParser
@@ -67,7 +67,7 @@ public function getNodesFromReflectionClass(ReflectionClass $reflectionClass): ?
6767 return $ this ->parser ->parse ($ code );
6868 }
6969
70- public function getNodeFromReflectionType (ReflectionType $ reflection ): Node \ ComplexType | Node \ Identifier | Node \ Name
70+ public function getNodeFromReflectionType (ReflectionType $ reflection ): Node
7171 {
7272 if ($ reflection instanceof ReflectionUnionType) {
7373 $ unionType = [];
@@ -96,6 +96,7 @@ public function getNodeFromReflectionParameter(ReflectionParameter $parameter):
9696 }
9797
9898 if ($ parameter ->hasType ()) {
99+ /* @phpstan-ignore-next-line */
99100 $ result ->type = $ this ->getNodeFromReflectionType ($ parameter ->getType ());
100101 }
101102
@@ -171,13 +172,17 @@ public function getAllMethodsFromStmts(array $stmts, bool $withTrait = false): a
171172 foreach ($ class ->stmts as $ stmt ) {
172173 if ($ stmt instanceof Node \Stmt \TraitUse) {
173174 foreach ($ stmt ->traits as $ trait ) {
174- if (isset ($ uses [$ trait ->getFirst ()])) {
175- $ traitName = $ uses [$ trait ->getFirst ()] . substr ($ trait ->toString (), strlen ($ trait ->getFirst ()));
175+ $ traitString = $ trait ->toString ();
176+ $ traitParts = $ trait ->getParts ();
177+ $ firstPart = $ trait ->getFirst ();
178+
179+ if (isset ($ uses [$ firstPart ])) {
180+ $ traitName = $ uses [$ firstPart ] . substr ($ traitString , strlen ($ firstPart ));
176181 } else {
177- if (count ($ trait -> getParts () ) == 1 ) {
178- $ traitName = $ namespace ->name ->toString () . '\\' . $ trait -> toString () ;
182+ if (count ($ traitParts ) == 1 ) {
183+ $ traitName = $ namespace ->name ->toString () . '\\' . $ traitString ;
179184 } else {
180- $ traitName = $ trait -> toString () ;
185+ $ traitName = $ traitString ;
181186 }
182187 }
183188 $ traitNodes = $ this ->parser ->parse (file_get_contents ((new ReflectionClass ($ traitName ))->getFileName ()));
@@ -192,6 +197,23 @@ public function getAllMethodsFromStmts(array $stmts, bool $withTrait = false): a
192197 return $ methods ;
193198 }
194199
200+ /**
201+ * Convert type name to appropriate Node type.
202+ */
203+ public function getNodeByTypeString (string $ typeName , bool $ nullable = false ): Node
204+ {
205+ if (in_array ($ typeName , PhpParser::TYPES )) {
206+ $ type = new Node \Identifier ($ typeName );
207+ } else {
208+ $ type = new Node \Name ('\\' . $ typeName );
209+ }
210+
211+ if ($ nullable && $ typeName !== 'mixed ' ) {
212+ return new Node \NullableType ($ type );
213+ }
214+ return $ type ;
215+ }
216+
195217 private function getExprFromObject (object $ value )
196218 {
197219 $ ref = new ReflectionClass ($ value );
@@ -207,29 +229,12 @@ private function getExprFromObject(object $value)
207229 );
208230 }
209231
210- private function getTypeWithNullableOrNot (ReflectionType $ reflection ): Node \ ComplexType | Node \ Identifier | Node \ Name
232+ private function getTypeWithNullableOrNot (ReflectionType $ reflection ): Node
211233 {
212234 if (! $ reflection instanceof ReflectionNamedType) {
213235 throw new ReflectionException ('ReflectionType must be ReflectionNamedType. ' );
214236 }
215237
216- $ name = $ reflection ->getName ();
217-
218- if ($ reflection ->allowsNull () && $ name !== 'mixed ' ) {
219- return new Node \NullableType ($ this ->getTypeFromString ($ name ));
220- }
221-
222- if (! in_array ($ name , static ::TYPES )) {
223- return new Node \Name ('\\' . $ name );
224- }
225- return new Node \Identifier ($ name );
226- }
227-
228- private function getTypeFromString (string $ name )
229- {
230- if (! in_array ($ name , static ::TYPES )) {
231- return '\\' . $ name ;
232- }
233- return $ name ;
238+ return $ this ->getNodeByTypeString ($ reflection ->getName (), $ reflection ->allowsNull ());
234239 }
235240}
0 commit comments