Index types used in class property initializers and combine change calls for fix-imports

master
Hugo Thunnissen 1 month ago
parent d7704c9d7c
commit 855529095a

@ -260,10 +260,11 @@ group."
(with-current-buffer (phpinspect-buffer-buffer buffer) (with-current-buffer (phpinspect-buffer-buffer buffer)
(save-excursion (save-excursion
(goto-char start) (goto-char start)
(delete-region start end) (combine-after-change-calls
(dolist (statement statements) (delete-region start end)
(phpinspect-codify-token (cdr statement)) (dolist (statement statements)
(insert-char ?\n)) (phpinspect-codify-token (cdr statement))
(insert-char ?\n)))
(if (looking-at "[[:blank:]\n]+") (if (looking-at "[[:blank:]\n]+")
;; Delete excess trailing whitespace (there's more than 2 between the ;; Delete excess trailing whitespace (there's more than 2 between the
@ -279,6 +280,7 @@ group."
"Find types that are used in the current buffer and make sure "Find types that are used in the current buffer and make sure
that there are import (\"use\") statements for them." that there are import (\"use\") statements for them."
(interactive) (interactive)
(if phpinspect-current-buffer (if phpinspect-current-buffer
(let* ((buffer phpinspect-current-buffer) (let* ((buffer phpinspect-current-buffer)
;; use buffer-reparse to ensure fully structurally correct ;; use buffer-reparse to ensure fully structurally correct
@ -346,6 +348,11 @@ that there are import (\"use\") statements for them."
(phpinspect-add-use-statements-for-missing-types (phpinspect-add-use-statements-for-missing-types
used-types buffer (append imports namespace-imports) project token-meta) used-types buffer (append imports namespace-imports) project token-meta)
(with-current-buffer (get-buffer-create "attempt-before-namespace-removal-code")
(erase-buffer)
(insert (with-current-buffer (phpinspect-buffer-buffer buffer) (buffer-string))))
(phpinspect-remove-unneeded-use-statements (phpinspect-remove-unneeded-use-statements
used-types buffer (append imports namespace-imports) token-meta) used-types buffer (append imports namespace-imports) token-meta)

@ -261,7 +261,9 @@ SCOPE should be a scope token (`phpinspect-scope-p')."
(t (t
(setq variable-name (cadr (seq-find #'phpinspect-variable-p scope))))) (setq variable-name (cadr (seq-find #'phpinspect-variable-p scope)))))
(phpinspect--log "calling resolver from index-variable-from-scope") (when add-used-types
(funcall add-used-types (phpinspect--find-used-types-in-tokens scope)))
(phpinspect--make-variable (phpinspect--make-variable
;; Static class variables are always prefixed with dollar signs when ;; Static class variables are always prefixed with dollar signs when
;; referenced. ;; referenced.
@ -589,6 +591,7 @@ Returns a list of type name strings."
(while tokens (while tokens
(let ((token (pop tokens)) (let ((token (pop tokens))
(previous-token (car previous-tokens))) (previous-token (car previous-tokens)))
(cond ((and (phpinspect-word-p previous-token) (cond ((and (phpinspect-word-p previous-token)
(member (cadr previous-token) `("new" "instanceof")) (member (cadr previous-token) `("new" "instanceof"))
(phpinspect-word-p token)) (phpinspect-word-p token))
@ -633,7 +636,12 @@ Returns a list of type name strings."
(nconc used-types-rear (nconc used-types-rear
(phpinspect--find-used-types-in-tokens (cdr (phpinspect-function-block token)))) (phpinspect--find-used-types-in-tokens (cdr (phpinspect-function-block token))))
used-types-rear (last used-types-rear))) used-types-rear (last used-types-rear)))
((or (phpinspect-list-p token) (phpinspect-block-p token) (phpinspect-array-p token)) ((or (phpinspect-list-p token)
(phpinspect-block-p token)
(phpinspect-array-p token)
(phpinspect-scope-p token)
(phpinspect-static-p token)
(phpinspect-const-p token))
(setq used-types-rear (setq used-types-rear
(nconc used-types-rear (phpinspect--find-used-types-in-tokens (cdr token))) (nconc used-types-rear (phpinspect--find-used-types-in-tokens (cdr token)))
used-types-rear (last used-types-rear)))) used-types-rear (last used-types-rear))))

@ -798,19 +798,19 @@ Returns the consumed text string without face properties."
(phpinspect-defparser scope-public (phpinspect-defparser scope-public
:tree-keyword "public" :tree-keyword "public"
:handlers '(function-keyword static-keyword const-keyword class-variable here-doc :handlers '(function-keyword static-keyword const-keyword class-variable here-doc
string terminator tag comment word) string terminator tag comment assignment-operator array word)
:delimiter-predicate #'phpinspect--scope-terminator-p) :delimiter-predicate #'phpinspect--scope-terminator-p)
(phpinspect-defparser scope-private (phpinspect-defparser scope-private
:tree-keyword "private" :tree-keyword "private"
:handlers '(function-keyword static-keyword const-keyword class-variable here-doc :handlers '(function-keyword static-keyword const-keyword class-variable here-doc
string terminator tag comment word) string terminator tag comment assignment-operator array word)
:delimiter-predicate #'phpinspect--scope-terminator-p) :delimiter-predicate #'phpinspect--scope-terminator-p)
(phpinspect-defparser scope-protected (phpinspect-defparser scope-protected
:tree-keyword "protected" :tree-keyword "protected"
:handlers '(function-keyword static-keyword const-keyword class-variable here-doc :handlers '(function-keyword static-keyword const-keyword class-variable here-doc
string terminator tag comment word) string terminator tag comment assignment-operator array word)
:delimiter-predicate #'phpinspect--scope-terminator-p) :delimiter-predicate #'phpinspect--scope-terminator-p)
(phpinspect-defhandler scope-keyword (start-token max-point) (phpinspect-defhandler scope-keyword (start-token max-point)

@ -311,7 +311,7 @@ use CCC;
(let ((switch nil) (let ((switch nil)
(delta 0)) (delta 0))
(dotimes (i 100) (dotimes (_i 100)
(if switch (if switch
(progn (progn
(setq delta 0) (setq delta 0)

@ -92,6 +92,8 @@
use UsedTrait; use UsedTrait;
private array $bong = [ PropertyArray::class => PropertyArrayItem::class ];
private PropertyType $property; private PropertyType $property;
/** @param ParamAnnotation $par /** @param ParamAnnotation $par
@ -106,7 +108,7 @@ return StaticThing::create(new ThingFactory())->makeThing((((new Potato())->anti
if ($param instanceof InstanceOffed) { if ($param instanceof InstanceOffed) {
/** @var VarAnnotation $bing */ /** @var VarAnnotation $bing */
try { try {
$bing = [ 'bong' => [ 'nested' => (CastedType) NestedArray::call(), ], ]; $bing = [ ArrayKey::class => [ NestedArrayKey::class => (CastedType) NestedArray::call(), 'ba' => ArrayItem::class ], ];
} catch (CaughtException $e) { } catch (CaughtException $e) {
// nothing // nothing
} }
@ -122,7 +124,9 @@ try {
"StaticThing" "Thing" "ThingFactory" "Potato" "OtherThing" "StaticThing" "Thing" "ThingFactory" "Potato" "OtherThing"
"InnerFunctionParam" "PropertyType" "InstanceOffed" "InnerFunctionParam" "PropertyType" "InstanceOffed"
"NestedArray" "UsedTrait" "VarAnnotation" "ParamAnnotation" "NestedArray" "UsedTrait" "VarAnnotation" "ParamAnnotation"
"ThrowAnnotationException" "CaughtException" "CastedType")) "ThrowAnnotationException" "CaughtException" "CastedType"
"NestedArrayKey" "ArrayKey" "ArrayItem" "PropertyArray"
"PropertyArrayItem" "array"))
#'string<)) #'string<))
(sort used-types (lambda (s1 s2) (string< (phpinspect-name-string s1) (phpinspect-name-string s2)))))))) (sort used-types (lambda (s1 s2) (string< (phpinspect-name-string s1) (phpinspect-name-string s2))))))))

Loading…
Cancel
Save