Fix namespace resolution bug in phpinspect-fix-imports

Incorrect use of phpinspect-namespace-name caused phpinspect-fix-imports to
incorrectly determine the current namespace. This resulted in imports being
suggested/added for types that already were available in the current
namespace. It did not make the PHP code invalid, but it did cause unnecessary
use statements for types in the same namespace.

This bug has now been fixed.
master
Hugo Thunnissen 2 months ago
parent 3d68374fd0
commit 62bfc7830a

@ -31,6 +31,7 @@
(require 'phpinspect-buffer)
(require 'phpinspect-cache)
(require 'phpinspect-util)
(require 'phpinspect-type)
(defun phpinspect-insert-at-point (point data)
(save-excursion
@ -115,12 +116,15 @@ buffer position to insert the use statement at."
(dolist (type types)
(setq namespace (phpinspect-meta-find-parent-matching-token
parent-token #'phpinspect-namespace-p)
namespace-name (phpinspect-namespace-name namespace))
namespace-name (if namespace
(phpinspect-namespace-name (phpinspect-meta-token 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 "\\" (phpinspect-name-string type)))
(phpinspect--resolve-type-name
nil namespace-name (phpinspect-name-string type)))
(phpinspect-autoloader-types
(phpinspect-project-autoload project))))
(phpinspect-add-use-interactive type buffer project namespace)

@ -358,8 +358,9 @@ mutability of the variable")
"Extract NAMESPACE name as a string.
NAMESPACE should be a namespace token (`phpinspect-namespace-p')."
(or (and (phpinspect-namespace-p namespace)
(phpinspect-word-p (cadr namespace))
(cl-assert (phpinspect-namespace-p namespace))
(or (and (phpinspect-word-p (cadr namespace))
(cadadr namespace))
""))

Loading…
Cancel
Save