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

    Test and fix static function indexation
---
 phpinspect.el           | 36 +++++++++++++++++++++++++++---------
 test/phpinspect-test.el | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 9 deletions(-)

diff --git a/phpinspect.el b/phpinspect.el
index 23b7be484b..8b362cae2d 100644
--- a/phpinspect.el
+++ b/phpinspect.el
@@ -1476,7 +1476,6 @@ resolve types of function argument variables."
 
 (defun phpinspect--make-type-resolver (types &optional token-tree namespace)
   "Little wrapper closure to pass around and resolve types with."
-  (unless namespace (setq namespace ""))
 
   (let* ((inside-class
           (if token-tree (or (phpinspect--find-innermost-incomplete-class 
token-tree)
@@ -1497,8 +1496,10 @@ resolve types of function argument variables."
          type)))))
 
 (defun phpinspect--real-type (types namespace type)
-  "Get the FQN for `type`, using `types` as an alist to retrieve
-said FQN's by class name"
+  "Get the FQN for TYPE, using TYPES and NAMESPACE as context.
+
+TYPES must be an alist with class names as cars and FQNs as cdrs.
+NAMESPACE may be nil, or a string with a namespace FQN."
   (phpinspect--log "Resolving %s from namespace %s" type namespace)
   ;; Absolute FQN
   (cond ((string-match "^\\\\" type)
@@ -1509,12 +1510,14 @@ said FQN's by class name"
          (concat "\\" type))
 
         ;; Relative FQN
-        ((string-match "\\\\" type)
+        ((and namespace (string-match "\\\\" type))
          (concat "\\" namespace "\\" type))
 
         ;; Clas|interface|trait name
         (t (concat "\\" (or (assoc-default type types #'string=)
-                            (concat namespace "\\" type))))))
+                          (if namespace
+                              (concat namespace "\\" type)
+                            type))))))
 
 (defun phpinspect-var-annotation-p (token)
   (phpinspect-type-p token :var-annotation))
@@ -1613,16 +1616,18 @@ said FQN's by class name"
       (dolist (word (cadr class))
         (if (phpinspect-word-p word)
             (cond ((string= (cadr word) "extends")
-                   (phpinspect--log "Extends was true")
+                   (phpinspect--log "Class %s extends other classes" 
class-name)
                    (setq enc-extends t))
                   ((string= (cadr word) "implements")
                    (setq enc-extends nil)
-                   (phpinspect--log "Implements was true")
+                   (phpinspect--log "Class %s implements in interface" 
class-name)
                    (setq enc-implements t))
                   (t
                    (phpinspect--log "Calling Resolver from index-class on %s" 
(cadr word))
-                   (cond (enc-extends (push (funcall type-resolver (cadr 
word)) extends))
-                         (enc-implements (push (funcall type-resolver (cadr 
word)) implements))))))))
+                   (cond (enc-extends
+                          (push (funcall type-resolver (cadr word)) extends))
+                         (enc-implements
+                          (push (funcall type-resolver (cadr word)) 
implements))))))))
 
     (dolist (token (caddr class))
       (cond ((phpinspect-scope-p token)
@@ -1654,7 +1659,20 @@ said FQN's by class name"
                                                                  token
                                                                  
comment-before)
                           methods))))
+            ((phpinspect-static-p token)
+             (cond ((phpinspect-function-p (cadr token))
+                    (push (phpinspect--index-function-from-scope type-resolver
+                                                                 `(:public
+                                                                   ,(cadr 
token))
+                                                                 
comment-before)
+                          static-methods))
 
+                   ((phpinspect-variable-p (cadr token))
+                    (push (phpinspect--index-variable-from-scope type-resolver
+                                                                 `(:public
+                                                                   ,(cadr 
token))
+                                                                 
comment-before)
+                          static-variables))))
             ((phpinspect-const-p token)
              ;; Bare constants are always public
              (push (phpinspect--index-const-from-scope (list :public token))
diff --git a/test/phpinspect-test.el b/test/phpinspect-test.el
index 44c93668b5..ebb21ec0e2 100644
--- a/test/phpinspect-test.el
+++ b/test/phpinspect-test.el
@@ -202,6 +202,41 @@
     (should (string= "\\App\\Controller\\Dupuis\\GastonLagaffe"
                      (funcall type-resolver "Dupuis\\GastonLagaffe")))))
 
+(ert-deftest phpinspect-index-static-methods ()
+  (let* ((class-tokens
+          `(:root
+            (:class
+             (:declaration (:word "class") (:word "Potato"))
+             (:block
+              (:static
+               (:function (:declaration (:word "function")
+                                        (:word "staticMethod")
+                                        (:list (:variable "untyped")
+                                               (:comma)
+                                               (:word "array")
+                                               (:variable "things")))
+                          (:block)))))))
+         (index (phpinspect--index-tokens class-tokens))
+         (expected-index
+          `(phpinspect--root-index
+            (classes
+             ("\\Potato" phpinspect--class
+              (methods)
+              (class-name . "\\Potato")
+              (static-methods . (,(phpinspect--make-function
+                                   :name "staticMethod"
+                                   :scope '(:public)
+                                   :arguments '(("untyped" nil)
+                                                ("things" "\\array"))
+                                   :return-type nil)))
+              (static-variables)
+              (variables)
+              (constants)
+              (extends)
+              (implements)))
+            (functions))))
+    (should (equal expected-index index))))
+
 
 (provide 'phpinspect-test)
 ;;; phpinspect-test.el ends here

Reply via email to