summaryrefslogtreecommitdiff
path: root/phpinspect-imports.el
diff options
context:
space:
mode:
authorHugo Thunnissen <devel@hugot.nl>2023-08-07 09:46:09 +0200
committerHugo Thunnissen <devel@hugot.nl>2023-08-07 09:46:09 +0200
commite35caa7e15d3e79e53353a56dd701075d3e367b4 (patch)
tree8281ed10ad5115d840fef251ead0113540a48f9f /phpinspect-imports.el
parent55413ea9fb99196ddf8cc22df601f4ffda51540a (diff)
Fix a variety of bugs
- phpinspect--index-tokens no longer errors upon unexpected return annotation values - phpinspect-fixt-imports now also fixes imports outside of classes - Functions are no longer included in statements when deriving types
Diffstat (limited to 'phpinspect-imports.el')
-rw-r--r--phpinspect-imports.el49
1 files changed, 30 insertions, 19 deletions
diff --git a/phpinspect-imports.el b/phpinspect-imports.el
index db74413..8aad8ee 100644
--- a/phpinspect-imports.el
+++ b/phpinspect-imports.el
@@ -113,6 +113,28 @@ buffer position to insert the use statement at."
(defalias 'phpinspect-fix-uses-interactive #'phpinspect-fix-imports
"Alias for backwards compatibility")
+(defun phpinspect-namespace-name (namespace)
+ (or (and (phpinspect-namespace-p namespace)
+ (phpinspect-word-p (cadr namespace))
+ (cadadr namespace))
+ ""))
+
+(defun phpinspect-add-use-statements-for-missing-types (types buffer imports project parent-token)
+ (let (namespace)
+ (dolist (type types)
+ (setq namespace (phpinspect-meta-find-parent-matching-token
+ parent-token #'phpinspect-namespace-p)
+ namespace-name (phpinspect-namespace-name namespace))
+ ;; Add use statements for types that aren't imported or already referenced
+ ;; with a fully qualified name.
+ (unless (or (or (alist-get type imports))
+ (gethash (phpinspect-intern-name
+ (concat namespace-name "\\" (symbol-name type)))
+ (phpinspect-autoloader-types
+ (phpinspect-project-autoload project))))
+ (phpinspect-add-use-interactive type buffer project namespace)
+ (phpinspect-buffer-parse buffer 'no-interrupt)))))
+
(defun phpinspect-fix-imports ()
"Find types that are used in the current buffer and make sure
that there are import (\"use\") statements for them."
@@ -126,7 +148,12 @@ that there are import (\"use\") statements for them."
(imports (alist-get 'imports index))
(project (phpinspect--cache-get-project-create
(phpinspect--get-or-create-global-cache)
- (phpinspect-current-project-root))))
+ (phpinspect-current-project-root)))
+ (used-types (alist-get 'used-types index)))
+
+ (phpinspect-add-use-statements-for-missing-types
+ used-types buffer imports project (phpinspect-buffer-root-meta buffer))
+
(dolist (class classes)
(let* ((class-imports (alist-get 'imports class))
(used-types (alist-get 'used-types class))
@@ -143,23 +170,7 @@ that there are import (\"use\") statements for them."
(unless token-meta
(error "Unable to find token for class %s" class-name))
-
-
- (dolist (type used-types)
- (setq namespace (phpinspect-meta-find-parent-matching-token
- token-meta #'phpinspect-namespace-p))
- ;; Add use statements for types that aren't imported.
- (unless (or (or (alist-get type class-imports)
- (alist-get type imports))
- (gethash (phpinspect-intern-name
- (concat (phpinspect-namespace-part-of-typename
- (phpinspect--type-name (alist-get 'class-name class)))
- "\\"
- (symbol-name type)))
- (phpinspect-autoloader-types
- (phpinspect-project-autoload project))))
- (phpinspect-add-use-interactive
- type buffer project namespace)
- (phpinspect-buffer-parse buffer 'no-interrupt))))))))
+ (phpinspect-add-use-statements-for-missing-types
+ used-types buffer (append imports class-imports) project token-meta))))))
(provide 'phpinspect-imports)