branch: externals/phpinspect
commit f2f1ac9b849cc1bde26f395cae6e4ab06735626c
Author: Hugo Thunnissen <de...@hugot.nl>
Commit: Hugo Thunnissen <de...@hugot.nl>

    Add property types and anonymous function argument types to used-types index
---
 phpinspect-index.el | 47 ++++++++++++++++++++++++++++++++++-------------
 test/test-index.el  |  8 ++++++--
 2 files changed, 40 insertions(+), 15 deletions(-)

diff --git a/phpinspect-index.el b/phpinspect-index.el
index 474e9123ca..417b7f38c9 100644
--- a/phpinspect-index.el
+++ b/phpinspect-index.el
@@ -173,7 +173,7 @@ function (think \"new\" statements, return types etc.)."
     ;; return nil.
     (when (stringp type) type)))
 
-(defun phpinspect--index-variable-from-scope (type-resolver scope 
comment-before &optional static)
+(defun phpinspect--index-variable-from-scope (type-resolver scope 
comment-before &optional static add-used-types)
   "Index the variable inside SCOPE, use doc in COMMENT-BEFORE if missing 
typehint.
 
 Provide STATIC if the variable was defined as static.
@@ -212,7 +212,9 @@ SCOPE should be a scope token (`phpinspect-scope-p')."
        :name (if static (concat "$" variable-name) variable-name)
        :scope `(,(car scope))
        :lifetime (when static '(:static))
-       :type (if type (funcall type-resolver (phpinspect--make-type :name 
type)))))))
+       :type (when type
+               (when add-used-types (funcall add-used-types (list type)))
+               (funcall type-resolver (phpinspect--make-type :name type)))))))
 
 (defun phpinspect-doc-block-p (token)
   (phpinspect-token-type-p token :doc-block))
@@ -292,9 +294,8 @@ SCOPE should be a scope token (`phpinspect-scope-p')."
                     (push (phpinspect--index-const-from-scope token) 
constants))
 
                    ((seq-find #'phpinspect-variable-p token)
-                    (push (phpinspect--index-variable-from-scope type-resolver
-                                                                 token
-                                                                 
comment-before)
+                    (push (phpinspect--index-variable-from-scope
+                           type-resolver token comment-before nil 
add-used-types)
                           variables))
 
                    ((phpinspect-static-p (cadr token))
@@ -307,10 +308,9 @@ SCOPE should be a scope token (`phpinspect-scope-p')."
                                  static-methods))
 
                           ((seq-find #'phpinspect-variable-p (cdadr token))
-                           (push (phpinspect--index-variable-from-scope 
type-resolver
-                                                                        
`(,(car token) ,@(cdadr token))
-                                                                        
comment-before
-                                                                        
'static)
+                           (push (phpinspect--index-variable-from-scope
+                                  type-resolver `(,(car token) ,@(cdadr token))
+                                  comment-before 'static add-used-types)
                                  static-variables))))
                    (t
                     (phpinspect--log "comment-before is: %s" comment-before)
@@ -329,10 +329,9 @@ SCOPE should be a scope token (`phpinspect-scope-p')."
                           static-methods))
 
                    ((seq-find #'phpinspect-variable-p (cdr token))
-                    (push (phpinspect--index-variable-from-scope type-resolver
-                                                                 `(:public 
,@(cdr token))
-                                                                 comment-before
-                                                                 'static)
+                    (push (phpinspect--index-variable-from-scope
+                           type-resolver `(:public ,@(cdr token)) 
comment-before
+                           'static add-used-types)
                           static-variables))))
             ((phpinspect-const-p token)
              ;; Bare constants are always public
@@ -481,6 +480,21 @@ NAMESPACE will be assumed the root namespace if not 
provided"
                    functions))))
     functions))
 
+(defun phpinspect-function-declaration (function-token)
+  (seq-find #'phpinspect-declaration-p function-token))
+
+(defun phpinspect--find-used-types-in-function-token (token)
+  (let* (types
+         (add-types (lambda (add-types)
+                      (setq types (nconc types add-types))))
+         (resolver (phpinspect--make-type-resolver nil)))
+
+    (phpinspect--index-function-declaration
+     (phpinspect-function-declaration token) resolver add-types)
+
+    (nconc types (phpinspect--find-used-types-in-tokens 
(phpinspect-function-block token)))))
+
+
 (defun phpinspect--find-used-types-in-tokens (tokens)
   "Find usage of the \"new\" keyword in TOKENS.
 
@@ -509,6 +523,13 @@ Return value is a list of the types that are \"newed\"."
                          (nconc used-types-rear
                                 (phpinspect--find-used-types-in-tokens (cdr 
list)))
                          used-types-rear (last used-types-rear)))))
+              ((phpinspect-function-p token)
+               (setq used-types-rear
+                     (nconc used-types-rear 
(phpinspect--find-used-types-in-function-token token))
+                     used-types-rear
+                     (nconc used-types-rear
+                            (phpinspect--find-used-types-in-tokens (cdr 
(phpinspect-function-block token))))
+                     used-types-rear (last used-types-rear)))
               ((or (phpinspect-list-p token) (phpinspect-block-p token))
                (setq used-types-rear
                      (nconc used-types-rear 
(phpinspect--find-used-types-in-tokens (cdr token)))
diff --git a/test/test-index.el b/test/test-index.el
index 5f3f4bf4e0..31635b950d 100644
--- a/test/test-index.el
+++ b/test/test-index.el
@@ -88,12 +88,15 @@
   (let* ((result (phpinspect--index-tokens
                   (phpinspect-parse-string
                    "<?php namespace Field; class Potato extends Cheese, Bacon 
implements Ham, Bagel {
+
+private PropertyType $property;
+
 public function makeThing(): Thing
 {
 if ((new Monkey())->tree() === true) {
    return new ExtendedThing();
 }
-return StaticThing::create(new ThingFactory())->makeThing((((new 
Potato())->antiPotato(new OtherThing()))));
+return StaticThing::create(new ThingFactory())->makeThing((((new 
Potato())->antiPotato(new OtherThing(function (InnerFunctionParam $param) 
{})))));
 }")))
          (used-types (alist-get 'used-types (car (alist-get 'classes 
result)))))
     (should (equal
@@ -101,7 +104,8 @@ return StaticThing::create(new 
ThingFactory())->makeThing((((new Potato())->anti
                      (sort
                       (copy-sequence
                        '("Cheese" "Bacon" "Ham" "Bagel" "Monkey" 
"ExtendedThing"
-                         "StaticThing" "Thing" "ThingFactory" "Potato" 
"OtherThing"))
+                         "StaticThing" "Thing" "ThingFactory" "Potato" 
"OtherThing"
+                         "InnerFunctionParam" "PropertyType"))
                       #'string<))
              (sort used-types (lambda (s1 s2) (string< (phpinspect-name-string 
s1) (phpinspect-name-string s2))))))))
 

Reply via email to