@@ -517,7 +517,7 @@ impl Backend {
517517 . as_ref ( )
518518 . and_then ( |ext| ext. types . first ( ) . map ( |ident| ident. value ( ) . to_string ( ) ) ) ;
519519
520- let ( methods, properties, constants) =
520+ let ( methods, properties, constants, used_traits ) =
521521 Self :: extract_class_like_members ( class. members . iter ( ) , doc_ctx) ;
522522
523523 let start_offset = class. left_brace . start . offset ;
@@ -531,6 +531,7 @@ impl Backend {
531531 start_offset,
532532 end_offset,
533533 parent_class,
534+ used_traits,
534535 } ) ;
535536 }
536537 Statement :: Interface ( iface) => {
@@ -543,7 +544,7 @@ impl Backend {
543544 . as_ref ( )
544545 . and_then ( |ext| ext. types . first ( ) . map ( |ident| ident. value ( ) . to_string ( ) ) ) ;
545546
546- let ( methods, properties, constants) =
547+ let ( methods, properties, constants, used_traits ) =
547548 Self :: extract_class_like_members ( iface. members . iter ( ) , doc_ctx) ;
548549
549550 let start_offset = iface. left_brace . start . offset ;
@@ -557,6 +558,27 @@ impl Backend {
557558 start_offset,
558559 end_offset,
559560 parent_class,
561+ used_traits,
562+ } ) ;
563+ }
564+ Statement :: Trait ( trait_def) => {
565+ let trait_name = trait_def. name . value . to_string ( ) ;
566+
567+ let ( methods, properties, constants, used_traits) =
568+ Self :: extract_class_like_members ( trait_def. members . iter ( ) , doc_ctx) ;
569+
570+ let start_offset = trait_def. left_brace . start . offset ;
571+ let end_offset = trait_def. right_brace . end . offset ;
572+
573+ classes. push ( ClassInfo {
574+ name : trait_name,
575+ methods,
576+ properties,
577+ constants,
578+ start_offset,
579+ end_offset,
580+ parent_class : None ,
581+ used_traits,
560582 } ) ;
561583 }
562584 Statement :: Namespace ( namespace) => {
@@ -571,20 +593,28 @@ impl Backend {
571593 }
572594 }
573595
574- /// Extract methods, properties, and constants from class-like members.
596+ /// Extract methods, properties, constants, and used trait names from
597+ /// class-like members.
575598 ///
576- /// This is shared between `Statement::Class` and `Statement::Interface`
577- /// since both use the same `ClassLikeMember` representation.
599+ /// This is shared between `Statement::Class`, `Statement::Interface`,
600+ /// and `Statement::Trait` since all use the same `ClassLikeMember`
601+ /// representation.
578602 ///
579603 /// When `doc_ctx` is provided, PHPDoc `@return` and `@var` tags are used
580604 /// to refine (or supply) type information for methods and properties.
581605 fn extract_class_like_members < ' a > (
582606 members : impl Iterator < Item = & ' a ClassLikeMember < ' a > > ,
583607 doc_ctx : Option < & DocblockCtx < ' a > > ,
584- ) -> ( Vec < MethodInfo > , Vec < PropertyInfo > , Vec < ConstantInfo > ) {
608+ ) -> (
609+ Vec < MethodInfo > ,
610+ Vec < PropertyInfo > ,
611+ Vec < ConstantInfo > ,
612+ Vec < String > ,
613+ ) {
585614 let mut methods = Vec :: new ( ) ;
586615 let mut properties = Vec :: new ( ) ;
587616 let mut constants = Vec :: new ( ) ;
617+ let mut used_traits = Vec :: new ( ) ;
588618
589619 for member in members {
590620 match member {
@@ -675,11 +705,16 @@ impl Backend {
675705 } ) ;
676706 }
677707 }
708+ ClassLikeMember :: TraitUse ( trait_use) => {
709+ for trait_name_ident in trait_use. trait_names . iter ( ) {
710+ used_traits. push ( trait_name_ident. value ( ) . to_string ( ) ) ;
711+ }
712+ }
678713 _ => { }
679714 }
680715 }
681716
682- ( methods, properties, constants)
717+ ( methods, properties, constants, used_traits )
683718 }
684719
685720 /// Update the ast_map, use_map, and namespace_map for a given file URI
@@ -718,7 +753,7 @@ impl Backend {
718753 Statement :: Use ( use_stmt) => {
719754 Self :: extract_use_items ( & use_stmt. items , & mut use_map) ;
720755 }
721- Statement :: Class ( _) | Statement :: Interface ( _) => {
756+ Statement :: Class ( _) | Statement :: Interface ( _) | Statement :: Trait ( _ ) => {
722757 Self :: extract_classes_from_statements (
723758 std:: iter:: once ( inner) ,
724759 & mut classes,
@@ -741,7 +776,7 @@ impl Backend {
741776 }
742777 }
743778 }
744- Statement :: Class ( _) | Statement :: Interface ( _) => {
779+ Statement :: Class ( _) | Statement :: Interface ( _) | Statement :: Trait ( _ ) => {
745780 Self :: extract_classes_from_statements (
746781 std:: iter:: once ( statement) ,
747782 & mut classes,
@@ -834,6 +869,12 @@ impl Backend {
834869 let resolved = Self :: resolve_name ( parent, use_map, namespace) ;
835870 class. parent_class = Some ( resolved) ;
836871 }
872+ // Resolve trait names to fully-qualified names
873+ class. used_traits = class
874+ . used_traits
875+ . iter ( )
876+ . map ( |t| Self :: resolve_name ( t, use_map, namespace) )
877+ . collect ( ) ;
837878 }
838879 }
839880
0 commit comments