diff --git a/phpinspect-eldoc.el b/phpinspect-eldoc.el index 166acb3..52146f4 100644 --- a/phpinspect-eldoc.el +++ b/phpinspect-eldoc.el @@ -242,8 +242,8 @@ be implemented for return values of `phpinspect-eld-strategy-execute'") (let ((doc-string (concat "$" (truncate-string-to-width (car arg) phpinspect-eldoc-word-width) - (if (cadr arg) " " "") - (phpinspect--display-format-type-name (or (cadr arg) ""))))) + (if (cdr arg) " " "") + (phpinspect--display-format-type-name (or (cdr arg) ""))))) (when (and arg-pos (= arg-count arg-pos)) (setq doc-string (propertize diff --git a/phpinspect-index.el b/phpinspect-index.el index faa28ec..4464fb9 100644 --- a/phpinspect-index.el +++ b/phpinspect-index.el @@ -37,20 +37,35 @@ (cadr scope)) (t nil))) -(defun phpinspect--index-function-arg-list (type-resolver arg-list &optional add-used-types) +(defun phpinspect--index-function-arg-list (type-resolver arg-list &optional add-used-types comment-before) (let ((arg-index) (current-token) + param-annotation (arg-list (cl-copy-list arg-list))) (while (setq current-token (pop arg-list)) (cond ((and (phpinspect-word-p current-token) (phpinspect-variable-p (car arg-list))) - (push `(,(cadr (pop arg-list)) - ,(funcall type-resolver (phpinspect--make-type :name (cadr current-token)))) + (push (cons (cadr (pop arg-list)) + (funcall type-resolver + (phpinspect--make-type :name (cadr current-token)))) arg-index) (when add-used-types (funcall add-used-types (list (cadr current-token))))) + ((and (phpinspect-variable-p (car arg-list)) + comment-before + (setq param-annotation + (phpinspect--find-var-annotation-for-variable + comment-before (cadr (car arg-list)) #'phpinspect-param-annotation-p))) + (push (cons (cadr (car arg-list)) + (funcall type-resolver + (phpinspect--make-type + :name (phpinspect-var-annotation-type param-annotation)))) + arg-index) + + (when add-used-types + (funcall add-used-types + (list (phpinspect-var-annotation-type param-annotation))))) ((phpinspect-variable-p (car arg-list)) - (push `(,(cadr (pop arg-list)) - nil) + (push (cons (cadr (pop arg-list)) nil) arg-index)))) (nreverse arg-index))) @@ -60,7 +75,7 @@ of TYPE, if available." (or (not type) (phpinspect--type= type phpinspect--object-type))) -(defun phpinspect--index-function-declaration (declaration type-resolver add-used-types) +(defun phpinspect--index-function-declaration (declaration type-resolver add-used-types &optional comment-before) (let (current name function-args return-type) (catch 'break (while (setq current (pop declaration)) @@ -71,7 +86,7 @@ of TYPE, if available." ((phpinspect-list-p current) (setq function-args (phpinspect--index-function-arg-list - type-resolver current add-used-types)) + type-resolver current add-used-types comment-before)) (when (setq return-type (seq-find #'phpinspect-word-p declaration)) (setq return-type (funcall type-resolver @@ -127,7 +142,7 @@ function (think \"new\" statements, return types etc.)." (pcase-setq `(,name ,arguments ,type) (phpinspect--index-function-declaration - declaration type-resolver add-used-types)) + declaration type-resolver add-used-types comment-before)) ;; FIXME: Anonymous functions should not be indexed! (or if they are, they ;; should at least not be visible from various UIs unless assigned to a @@ -178,10 +193,10 @@ function (think \"new\" statements, return types etc.)." (define-inline phpinspect-var-annotation-type (annotation) (inline-quote (cadadr ,annotation))) -(defun phpinspect--find-var-annotation-for-variable (annotation-list variable) +(defun phpinspect--find-var-annotation-for-variable (annotation-list variable &optional predicate) (catch 'return (dolist (annotation annotation-list) - (when (and (phpinspect-var-annotation-p annotation) + (when (and (or (phpinspect-var-annotation-p annotation) (and predicate (funcall predicate annotation))) (phpinspect-var-annotation-variable annotation) (string= (phpinspect-var-annotation-variable annotation) variable)) @@ -408,9 +423,8 @@ SCOPE should be a scope token (`phpinspect-scope-p')." (phpinspect--log "Looking for variable type in constructor arguments (%s)" variable) (let ((constructor-parameter-type - (car (alist-get (phpinspect--variable-name variable) - (phpinspect--function-arguments constructor) - nil nil #'string=)))) + (phpinspect--function-argument-type + constructor (phpinspect--variable-name variable)))) (if constructor-parameter-type (setf (phpinspect--variable-type variable) (funcall type-resolver constructor-parameter-type)))))))) @@ -566,6 +580,13 @@ Returns a list of type name strings." (let ((type (cadr token))) (when (not (string-match-p "\\\\" type)) (setq used-types-rear (setcdr used-types-rear (cons type nil)))))) + ((phpinspect-comment-p token) + (setq used-types-rear + (nconc used-types-rear (phpinspect--find-used-types-in-tokens (cdr token))))) + ((and (or (phpinspect-var-annotation-p token) (phpinspect-param-annotation-p token)) + (phpinspect-var-annotation-type token)) + (setq used-types-rear + (setcdr used-types-rear (cons (phpinspect-var-annotation-type token) nil)))) ((and (phpinspect-static-attrib-p token) (phpinspect-word-p previous-token)) (let ((type (cadr previous-token))) diff --git a/phpinspect-serialize.el b/phpinspect-serialize.el index 79ba1e5..740185d 100644 --- a/phpinspect-serialize.el +++ b/phpinspect-serialize.el @@ -47,7 +47,7 @@ :scope (quote ,(phpinspect--function-scope func)) :arguments ,(append '(list) (mapcar (lambda (arg) - `(list ,(car arg) ,(phpinspect--serialize-type (cadr arg)))) + `(cons ,(car arg) ,(phpinspect--serialize-type (cdr arg)))) (phpinspect--function-arguments func))) :return-type ,(when (phpinspect--function-return-type func) (phpinspect--serialize-type diff --git a/phpinspect-token-predicates.el b/phpinspect-token-predicates.el index 4724c39..a17d19e 100644 --- a/phpinspect-token-predicates.el +++ b/phpinspect-token-predicates.el @@ -148,6 +148,9 @@ Type can be any of the token types returned by (defun phpinspect-var-annotation-p (token) (phpinspect-token-type-p token :var-annotation)) +(defun phpinspect-param-annotation-p (token) + (phpinspect-token-type-p token :param-annotation)) + (defun phpinspect-return-annotation-p (token) (phpinspect-token-type-p token :return-annotation)) diff --git a/phpinspect-type.el b/phpinspect-type.el index 5df2478..0127491 100644 --- a/phpinspect-type.el +++ b/phpinspect-type.el @@ -259,6 +259,9 @@ as first element and the type as second element.") "A phpinspect--type object representing the the return type of the function.")) +(defun phpinspect--function-argument-type (fn argument-name) + (alist-get argument-name (phpinspect--function-arguments fn) nil nil #'string=)) + (defun phpinspect--function-anonyous-p (fn) (eq (phpinspect-intern-name "anonymous") (phpinspect--function-name-symbol fn))) diff --git a/test/fixtures/IndexClass1-indexed.eld b/test/fixtures/IndexClass1-indexed.eld index 532c59b..217b10f 100644 --- a/test/fixtures/IndexClass1-indexed.eld +++ b/test/fixtures/IndexClass1-indexed.eld @@ -1 +1 @@ -`(phpinspect--root-index (imports \, (list)) (classes \, (list (cons (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t) `(phpinspect--indexed-class (complete \, t) (class-name \, (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t)) (declaration \, '(:declaration (:word "class") (:word "AuthToken"))) (imports \, (list (cons (phpinspect-intern-name "ORM") (phpinspect--make-type :name "\\Doctrine\\ORM\\Mapping" :collection nil :contains nil :fully-qualified t)))) (methods \, (list (phpinspect--make-function :name "arrayReturn" :token '(:function (:declaration (:word "function") (:word "arrayReturn") (:list) (:word "array")) (:block (:word "return") (:array (:word "new") (:word "\\DateTime") (:list)) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\array" :collection t :contains (phpinspect--make-type :name "\\App\\Entity\\DateTime" :collection nil :contains nil :fully-qualified t) :fully-qualified t)) (phpinspect--make-function :name "getCreationTime" :token '(:function (:declaration (:word "function") (:word "getCreationTime") (:list) (:word "\\DateTime")) (:block (:word "return") (:variable "this") (:object-attrib (:word "creation_time")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "isValid" :token '(:function (:declaration (:word "function") (:word "isValid") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "valid")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "hasStudentRole" :token '(:function (:declaration (:word "function") (:word "hasStudentRole") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "role_student")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getUser" :token '(:function (:declaration (:word "function") (:word "getUser") (:list) (:word "User")) (:block (:word "return") (:variable "this") (:object-attrib (:word "user")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getToken" :token '(:function (:declaration (:word "function") (:word "getToken") (:list) (:word "string")) (:block (:word "return") (:variable "this") (:object-attrib (:word "token")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "__construct" :token '(:function (:declaration (:word "function") (:word "__construct") (:list (:word "string") (:variable "token") (:comma ",") (:word "User") (:variable "user") (:comma ",") (:word "bool") (:variable "valid") (:assignment "=") (:word "false") (:comma ",") (:word "\\DateTime") (:variable "creation_time") (:assignment "=") (:word "null"))) (:block (:variable "this") (:object-attrib (:word "token")) (:assignment "=") (:variable "token") (:terminator ";") (:variable "this") (:object-attrib (:word "user")) (:assignment "=") (:variable "user") (:terminator ";") (:variable "this") (:object-attrib (:word "valid")) (:assignment "=") (:variable "valid") (:terminator ";") (:variable "this") (:object-attrib (:word "creation_time")) (:assignment "=") (:variable "creation_time") (:word "new") (:word "\\DateTime") (:list) (:terminator ";"))) :scope '(:public) :arguments (list (list "token" (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t)) (list "user" (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (list "valid" (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (list "creation_time" (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t))) :return-type (phpinspect--make-type :name "\\null" :collection nil :contains nil :fully-qualified t)))) (static-methods \, (list)) (static-variables \, (list)) (variables \, (list (phpinspect--make-variable :name "creation_time" :type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "valid" :type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "user" :type (phpinspect--make-type :name "\\App\\Entity\\App\\\\Entity\\\\User" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "token" :type (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t) :scope '(:private)))) (constants \, (list)) (extends \, (list)) (implements \, (list)))))) (functions \, (list))) +`(phpinspect--root-index (imports \, (list)) (classes \, (list (cons (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t) `(phpinspect--indexed-class (complete \, t) (class-name \, (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t)) (declaration \, '(:declaration (:word "class") (:word "AuthToken"))) (imports \, (list (cons (phpinspect-intern-name "ORM") (phpinspect--make-type :name "\\Doctrine\\ORM\\Mapping" :collection nil :contains nil :fully-qualified t)))) (methods \, (list (phpinspect--make-function :name "arrayReturn" :token '(:function (:declaration (:word "function") (:word "arrayReturn") (:list) (:word "array")) (:block (:word "return") (:array (:word "new") (:word "\\DateTime") (:list)) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\array" :collection t :contains (phpinspect--make-type :name "\\App\\Entity\\DateTime" :collection nil :contains nil :fully-qualified t) :fully-qualified t)) (phpinspect--make-function :name "getCreationTime" :token '(:function (:declaration (:word "function") (:word "getCreationTime") (:list) (:word "\\DateTime")) (:block (:word "return") (:variable "this") (:object-attrib (:word "creation_time")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "isValid" :token '(:function (:declaration (:word "function") (:word "isValid") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "valid")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "hasStudentRole" :token '(:function (:declaration (:word "function") (:word "hasStudentRole") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "role_student")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getUser" :token '(:function (:declaration (:word "function") (:word "getUser") (:list) (:word "User")) (:block (:word "return") (:variable "this") (:object-attrib (:word "user")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getToken" :token '(:function (:declaration (:word "function") (:word "getToken") (:list) (:word "string")) (:block (:word "return") (:variable "this") (:object-attrib (:word "token")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "__construct" :token '(:function (:declaration (:word "function") (:word "__construct") (:list (:word "string") (:variable "token") (:comma ",") (:word "User") (:variable "user") (:comma ",") (:word "bool") (:variable "valid") (:assignment "=") (:word "false") (:comma ",") (:word "\\DateTime") (:variable "creation_time") (:assignment "=") (:word "null"))) (:block (:variable "this") (:object-attrib (:word "token")) (:assignment "=") (:variable "token") (:terminator ";") (:variable "this") (:object-attrib (:word "user")) (:assignment "=") (:variable "user") (:terminator ";") (:variable "this") (:object-attrib (:word "valid")) (:assignment "=") (:variable "valid") (:terminator ";") (:variable "this") (:object-attrib (:word "creation_time")) (:assignment "=") (:variable "creation_time") (:word "new") (:word "\\DateTime") (:list) (:terminator ";"))) :scope '(:public) :arguments (list (cons "token" (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t)) (cons "user" (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (cons "valid" (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (cons "creation_time" (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t))) :return-type (phpinspect--make-type :name "\\null" :collection nil :contains nil :fully-qualified t)))) (static-methods \, (list)) (static-variables \, (list)) (variables \, (list (phpinspect--make-variable :name "creation_time" :type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "valid" :type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "user" :type (phpinspect--make-type :name "\\App\\Entity\\App\\\\Entity\\\\User" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "token" :type (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t) :scope '(:private)))) (constants \, (list)) (extends \, (list)) (implements \, (list)))))) (functions \, (list))) diff --git a/test/fixtures/IndexClass2-indexed.eld b/test/fixtures/IndexClass2-indexed.eld index e557b5c..86af9de 100644 --- a/test/fixtures/IndexClass2-indexed.eld +++ b/test/fixtures/IndexClass2-indexed.eld @@ -1 +1 @@ -`(phpinspect--root-index (imports \, (list)) (classes \, (list (cons (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t) `(phpinspect--indexed-class (complete \, t) (class-name \, (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t)) (declaration \, '(:declaration (:word "class") (:word "AuthToken"))) (imports \, (list (cons (phpinspect-intern-name "ORM") (phpinspect--make-type :name "\\Doctrine\\ORM\\Mapping" :collection nil :contains nil :fully-qualified t)))) (methods \, (list (phpinspect--make-function :name "getCreationTime" :token '(:function (:declaration (:word "function") (:word "getCreationTime") (:list) (:word "\\DateTime")) (:block (:word "return") (:variable "this") (:object-attrib (:word "creation_time")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "anAddedFunction" :token '(:function (:declaration (:word "function") (:word "anAddedFunction") (:list)) (:block (:word "return") (:variable "this") (:object-attrib (:word "extra")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\null" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "isValid" :token '(:function (:declaration (:word "function") (:word "isValid") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "valid")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "hasStudentRole" :token '(:function (:declaration (:word "function") (:word "hasStudentRole") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "role_student")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getUser" :token '(:function (:declaration (:word "function") (:word "getUser") (:list) (:word "User")) (:block (:word "return") (:variable "this") (:object-attrib (:word "user")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getToken" :token '(:function (:declaration (:word "function") (:word "getToken") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "token")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "__construct" :token '(:function (:declaration (:word "function") (:word "__construct") (:list (:word "string") (:variable "token") (:comma ",") (:word "User") (:variable "user") (:comma ",") (:word "bool") (:variable "valid") (:assignment "=") (:word "false") (:comma ",") (:word "\\DateTime") (:variable "creation_time") (:assignment "=") (:word "null"))) (:block (:variable "this") (:object-attrib (:word "token")) (:assignment "=") (:variable "token") (:terminator ";") (:variable "this") (:object-attrib (:word "user")) (:assignment "=") (:variable "user") (:terminator ";") (:variable "this") (:object-attrib (:word "valid")) (:assignment "=") (:variable "valid") (:terminator ";") (:variable "this") (:object-attrib (:word "creation_time")) (:assignment "=") (:variable "creation_time") (:word "new") (:word "\\DateTime") (:list) (:terminator ";"))) :scope '(:public) :arguments (list (list "token" (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t)) (list "user" (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (list "valid" (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (list "creation_time" (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t))) :return-type (phpinspect--make-type :name "\\null" :collection nil :contains nil :fully-qualified t)))) (static-methods \, (list)) (static-variables \, (list)) (variables \, (list (phpinspect--make-variable :name "creation_time" :type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "valid" :type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "user" :type (phpinspect--make-type :name "\\App\\Entity\\App\\\\Entity\\\\User" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "extra" :type nil :scope '(:private)) (phpinspect--make-variable :name "token" :type (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t) :scope '(:private)))) (constants \, (list)) (extends \, (list)) (implements \, (list)))))) (functions \, (list))) +`(phpinspect--root-index (imports \, (list)) (classes \, (list (cons (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t) `(phpinspect--indexed-class (complete \, t) (class-name \, (phpinspect--make-type :name "\\App\\Entity\\AuthToken" :collection nil :contains nil :fully-qualified t)) (declaration \, '(:declaration (:word "class") (:word "AuthToken"))) (imports \, (list (cons (phpinspect-intern-name "ORM") (phpinspect--make-type :name "\\Doctrine\\ORM\\Mapping" :collection nil :contains nil :fully-qualified t)))) (methods \, (list (phpinspect--make-function :name "getCreationTime" :token '(:function (:declaration (:word "function") (:word "getCreationTime") (:list) (:word "\\DateTime")) (:block (:word "return") (:variable "this") (:object-attrib (:word "creation_time")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "anAddedFunction" :token '(:function (:declaration (:word "function") (:word "anAddedFunction") (:list)) (:block (:word "return") (:variable "this") (:object-attrib (:word "extra")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\null" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "isValid" :token '(:function (:declaration (:word "function") (:word "isValid") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "valid")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "hasStudentRole" :token '(:function (:declaration (:word "function") (:word "hasStudentRole") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "role_student")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getUser" :token '(:function (:declaration (:word "function") (:word "getUser") (:list) (:word "User")) (:block (:word "return") (:variable "this") (:object-attrib (:word "user")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "getToken" :token '(:function (:declaration (:word "function") (:word "getToken") (:list) (:word "bool")) (:block (:word "return") (:variable "this") (:object-attrib (:word "token")) (:terminator ";"))) :scope '(:public) :arguments (list) :return-type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (phpinspect--make-function :name "__construct" :token '(:function (:declaration (:word "function") (:word "__construct") (:list (:word "string") (:variable "token") (:comma ",") (:word "User") (:variable "user") (:comma ",") (:word "bool") (:variable "valid") (:assignment "=") (:word "false") (:comma ",") (:word "\\DateTime") (:variable "creation_time") (:assignment "=") (:word "null"))) (:block (:variable "this") (:object-attrib (:word "token")) (:assignment "=") (:variable "token") (:terminator ";") (:variable "this") (:object-attrib (:word "user")) (:assignment "=") (:variable "user") (:terminator ";") (:variable "this") (:object-attrib (:word "valid")) (:assignment "=") (:variable "valid") (:terminator ";") (:variable "this") (:object-attrib (:word "creation_time")) (:assignment "=") (:variable "creation_time") (:word "new") (:word "\\DateTime") (:list) (:terminator ";"))) :scope '(:public) :arguments (list (cons "token" (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t)) (cons "user" (phpinspect--make-type :name "\\App\\Entity\\User" :collection nil :contains nil :fully-qualified t)) (cons "valid" (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t)) (cons "creation_time" (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t))) :return-type (phpinspect--make-type :name "\\null" :collection nil :contains nil :fully-qualified t)))) (static-methods \, (list)) (static-variables \, (list)) (variables \, (list (phpinspect--make-variable :name "creation_time" :type (phpinspect--make-type :name "\\DateTime" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "valid" :type (phpinspect--make-type :name "\\bool" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "user" :type (phpinspect--make-type :name "\\App\\Entity\\App\\\\Entity\\\\User" :collection nil :contains nil :fully-qualified t) :scope '(:private)) (phpinspect--make-variable :name "extra" :type nil :scope '(:private)) (phpinspect--make-variable :name "token" :type (phpinspect--make-type :name "\\string" :collection nil :contains nil :fully-qualified t) :scope '(:private)))) (constants \, (list)) (extends \, (list)) (implements \, (list)))))) (functions \, (list))) diff --git a/test/test-index.el b/test/test-index.el index 6ddc3d3..2469245 100644 --- a/test/test-index.el +++ b/test/test-index.el @@ -70,8 +70,8 @@ (:word "array") (:variable "things"))) (:block)) - :arguments `(("untyped" nil) - ("things" ,(phpinspect--make-type :name "\\array" + :arguments `(("untyped" . nil) + ("things" . ,(phpinspect--make-type :name "\\array" :collection t :fully-qualified t))) :return-type phpinspect--null-type))) @@ -94,13 +94,15 @@ use UsedTrait; private PropertyType $property; -public function makeThing(): Thing +/** @param ParamAnnotation $par */ +public function makeThing($par): Thing { if ((new Monkey())->tree() === true) { return new ExtendedThing(); } return StaticThing::create(new ThingFactory())->makeThing((((new Potato())->antiPotato(new OtherThing(function (InnerFunctionParam $param) { if ($param instanceof InstanceOffed) { +/** @var VarAnnotation $bing */ $bing = [ 'bong' => [ 'nested' => NestedArray::call(), ], ]; // nothing } @@ -113,7 +115,8 @@ if ($param instanceof InstanceOffed) { (copy-sequence '("Cheese" "Bacon" "Ham" "Bagel" "Monkey" "ExtendedThing" "StaticThing" "Thing" "ThingFactory" "Potato" "OtherThing" - "InnerFunctionParam" "PropertyType" "InstanceOffed" "NestedArray" "UsedTrait")) + "InnerFunctionParam" "PropertyType" "InstanceOffed" + "NestedArray" "UsedTrait" "VarAnnotation" "ParamAnnotation")) #'string<)) (sort used-types (lambda (s1 s2) (string< (phpinspect-name-string s1) (phpinspect-name-string s2)))))))) @@ -163,12 +166,11 @@ if ($param instanceof InstanceOffed) { (should (= 2 (length (phpinspect--function-arguments method)))) (should (phpinspect--type= (phpinspect--make-type :name "\\array" :fully-qualified t) - (car (alist-get - "loose" (phpinspect--function-arguments method) nil nil #'string=)))) + (phpinspect--function-argument-type method "loose"))) + (should (phpinspect--type= (phpinspect--make-type :name "\\bool" :fully-qualified t) - (car (alist-get - "fast" (phpinspect--function-arguments method) nil nil #'string=))))) + (phpinspect--function-argument-type method "fast")))) ((string= (phpinspect--function-name method) "hold") (should (phpinspect--type= @@ -205,12 +207,10 @@ if ($param instanceof InstanceOffed) { (should (= 2 (length (phpinspect--function-arguments method)))) (should (phpinspect--type= (phpinspect--make-type :name "\\array" :fully-qualified t) - (car (alist-get - "loose" (phpinspect--function-arguments method) nil nil #'string=)))) + (phpinspect--function-argument-type method "loose"))) (should (phpinspect--type= (phpinspect--make-type :name "\\bool" :fully-qualified t) - (car (alist-get - "fast" (phpinspect--function-arguments method) nil nil #'string=))))) + (phpinspect--function-argument-type method "fast")))) ((string= (phpinspect--function-name method) "hold") (should (phpinspect--type= @@ -260,7 +260,8 @@ use Example\\Thing; function test_func(): array {} -function example(): Thing {}") +/** @param \\array $bing */ +function example($bing): Thing {}") (tokens (phpinspect-parse-string code)) (index (phpinspect--index-tokens tokens)) functions) @@ -270,6 +271,12 @@ function example(): Thing {}") (should (string= "test_func" (phpinspect--function-name (cadr functions)))) (should (string= "example" (phpinspect--function-name (car functions)))) + (let ((example (car functions))) + (should (= 1 (length (phpinspect--function-arguments example)))) + (should (phpinspect--type= + (phpinspect--make-type :name "\\array") + (phpinspect--function-argument-type example "bing")))) + (should (phpinspect--type= (phpinspect--make-type :name "\\array") (phpinspect--function-return-type (cadr functions)))) (should (phpinspect--type= (phpinspect--make-type :name "\\Example\\Thing")