Skip to content

Commit ca1bc43

Browse files
committed
Add phpstan-copy-dumped-type command
1 parent e90ed7c commit ca1bc43

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
All notable changes of the `phpstan.el` are documented in this file using the [Keep a Changelog](https://keepachangelog.com/) principles.
44

5-
<!-- ## Unreleased -->
5+
## Unreleased
6+
7+
### Added
8+
9+
* Add `phpstan-copy-dumped-type` command to copy the nearest dumped type from `PHPStan\dumpType()` or `PHPStan\dumpPhpDocType()` messages.
610

711
## [0.8.2]
812

README.org

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ Insert a ~@phpstan-ignore~ tag to suppress any PHPStan errors on the current lin
105105
By default it inserts the tag on the previous line, but if there is already a tag at the end of the current line or on the previous line, the identifiers will be appended there.
106106

107107
If there is no existing tag and ~C-u~ is pressed before the command, it will be inserted at the end of the line.
108+
*** Command ~phpstan-copy-dumped-type~
109+
Copy the nearest dumped type message from PHPStan's output.
110+
111+
This command looks for messages like ~Dumped type: int|string|null~ reported by ~PHPStan\dumpType()~ or ~PHPStan\dumpPhpDocType()~, and copies the type string to the kill ring.
112+
113+
If there are multiple dumped types in the buffer, it selects the one closest to the current line.
114+
115+
If no dumped type messages are found, the command signals an error.
108116
** API
109117
Most variables defined in this package are buffer local. If you want to set it for multiple projects, use [[https://www.gnu.org/software/emacs/manual/html_node/elisp/Default-Value.html][setq-default]].
110118

flycheck-phpstan.el

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
(errors (phpstan--plist-to-alist (plist-get data :files))))
8484
(unless phpstan-disable-buffer-errors
8585
(phpstan-update-ignorebale-errors-from-json-buffer errors))
86+
(phpstan-update-dumped-types errors)
8687
(flycheck-phpstan--build-errors errors)))
8788

8889
(defun flycheck-phpstan--temp-buffer ()

phpstan.el

+25
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ have unexpected behaviors or performance implications."
169169
(defvar-local phpstan--use-xdebug-option nil)
170170

171171
(defvar-local phpstan--ignorable-errors '())
172+
(defvar-local phpstan--dumped-types '())
172173

173174
;;;###autoload
174175
(progn
@@ -523,6 +524,17 @@ it returns the value of `SOURCE' as it is."
523524
(setq phpstan--ignorable-errors
524525
(mapcar (lambda (v) (cons (car v) (mapcar #'cdr (cdr v)))) (seq-group-by #'car identifiers)))))
525526

527+
(defun phpstan-update-dumped-types (errors)
528+
"Update `phpstan--dumped-types' variable by ERRORS."
529+
(save-match-data
530+
(setq phpstan--dumped-types
531+
(cl-loop for (_ . entry) in errors
532+
append (cl-loop for message in (plist-get entry :messages)
533+
for msg = (plist-get message :message)
534+
if (string-match (eval-when-compile (rx bos "Dumped type: ")) msg)
535+
collect (cons (plist-get message :line)
536+
(substring-no-properties msg (match-end 0))))))))
537+
526538
(defconst phpstan--re-ignore-tag
527539
(eval-when-compile
528540
(rx (* (syntax whitespace)) "//" (* (syntax whitespace))
@@ -590,6 +602,19 @@ POSITION determines where to insert the comment and can be either `this-line' or
590602
(if new-position (if append ", " " ") "// @phpstan-ignore ")
591603
(string-join identifiers ", ")))))))
592604

605+
;;;###autoload
606+
(defun phpstan-copy-dumped-type ()
607+
"Copy a dumped PHPStan type."
608+
(interactive)
609+
(if phpstan--dumped-types
610+
(let ((type (if (eq 1 (length phpstan--dumped-types))
611+
(cdar phpstan--dumped-types)
612+
(let ((linum (line-number-at-pos)))
613+
(cdar (seq-sort-by (lambda (elm) (abs (- linum (car elm)))) #'< phpstan--dumped-types))))))
614+
(kill-new type)
615+
(message "Copied %s" type))
616+
(user-error "No dumped PHPStan types")))
617+
593618
;;;###autoload
594619
(defun phpstan-insert-dumptype (&optional expression prefix-num)
595620
"Insert PHPStan\\dumpType() expression-statement by EXPRESSION and PREFIX-NUM."

0 commit comments

Comments
 (0)