Implement parsing and indexation of @throws annotations

master
Hugo Thunnissen 1 month ago
parent 6f1e11c32a
commit 33922a6ff2

@ -138,7 +138,7 @@ function (think \"new\" statements, return types etc.)."
(phpinspect--log "Indexing function")
(let* ((php-func (cadr scope))
(declaration (cadr php-func))
name type arguments)
name type arguments throws used-types)
(pcase-setq `(,name ,arguments ,type)
(phpinspect--index-function-declaration
@ -159,17 +159,26 @@ function (think \"new\" statements, return types etc.)."
(phpinspect--apply-annotation-type return-annotation-type type type-resolver)
(setq type (funcall type-resolver (phpinspect--make-type :name return-annotation-type)))))
(when-let ((throw-annotations (seq-filter #'phpinspect-throws-annotation-p comment-before)))
(dolist (tr throw-annotations)
(when-let ((type (phpinspect-var-annotation-type tr)))
(push (funcall type-resolver (phpinspect--make-type :name type)) throws)
(push type used-types))))
(when add-used-types
(let ((used-types (phpinspect--find-used-types-in-tokens
`(,(seq-find #'phpinspect-block-p php-func)))))
(when type (push (phpinspect--type-bare-name type) used-types))
(funcall add-used-types used-types)))
(setq used-types (nconc used-types
(phpinspect--find-used-types-in-tokens
`(,(seq-find #'phpinspect-block-p php-func)))))
(when type (push (phpinspect--type-bare-name type) used-types))
(funcall add-used-types used-types))
(phpinspect--log "Creating function object")
(phpinspect--make-function
:scope `(,(car scope))
:token php-func
:name (concat (if namespace (concat namespace "\\") "") name)
:throws throws
:return-type (or type phpinspect--null-type)
:arguments arguments)))
@ -563,6 +572,7 @@ NAMESPACE will be assumed the root namespace if not provided"
Covers usage of types:
- with the \"new\" keyword
- as function argument/return types
- Types used in annotations (@var, @throws etc.)
see `phpinspect--index-class' for indexation of types used in
classes (like property typehints).
@ -583,7 +593,9 @@ Returns a list of type name strings."
((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))
((and (or (phpinspect-var-annotation-p token)
(phpinspect-param-annotation-p token)
(phpinspect-throws-annotation-p token))
(phpinspect-var-annotation-type token))
(setq used-types-rear
(setcdr used-types-rear (cons (phpinspect-var-annotation-type token) nil))))

@ -499,6 +499,9 @@ nature like argument lists"
((string= annotation-name "method")
(cons :method-annotation
(phpinspect--parse-annotation-parameters 4)))
((string= annotation-name "throws")
(cons :throws-annotation
(phpinspect--parse-annotation-parameters 1)))
(t
(list :annotation annotation-name))))
(list :annotation nil)))

@ -151,6 +151,9 @@ Type can be any of the token types returned by
(defun phpinspect-param-annotation-p (token)
(phpinspect-token-type-p token :param-annotation))
(defun phpinspect-throws-annotation-p (token)
(phpinspect-token-type-p token :throws-annotation))
(defun phpinspect-return-annotation-p (token)
(phpinspect-token-type-p token :return-annotation))

@ -233,6 +233,10 @@ NAMESPACE may be nil, or a string with a namespace FQN."
:type symbol
:documentation
"A symbol associated with the name of the function")
(throws nil
:type list
:documentation "List of exception types that function throws (according to doc
block).")
(token nil
:type phpinspect-function-p
:documentation

@ -94,7 +94,9 @@ use UsedTrait;
private PropertyType $property;
/** @param ParamAnnotation $par */
/** @param ParamAnnotation $par
@throws ThrowAnnotationException */
*/
public function makeThing($par): Thing
{
if ((new Monkey())->tree() === true) {
@ -116,7 +118,8 @@ if ($param instanceof InstanceOffed) {
'("Cheese" "Bacon" "Ham" "Bagel" "Monkey" "ExtendedThing"
"StaticThing" "Thing" "ThingFactory" "Potato" "OtherThing"
"InnerFunctionParam" "PropertyType" "InstanceOffed"
"NestedArray" "UsedTrait" "VarAnnotation" "ParamAnnotation"))
"NestedArray" "UsedTrait" "VarAnnotation" "ParamAnnotation"
"ThrowAnnotationException"))
#'string<))
(sort used-types (lambda (s1 s2) (string< (phpinspect-name-string s1) (phpinspect-name-string s2))))))))

Loading…
Cancel
Save